![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python基础语法
学习笔记
kay_beauty
我已经不是从前的我
展开
-
15. 列表的基本操作
extend和append的区别list.append(object) 向列表中添加一个对象objectlist.extend(sequence) 把一个序列seq的内容添加到列表中music_media = ['compact disc', '8-track tape', 'long playing record']new_media = ['DVD Audio disc', 'Super Audio CD']music_media.append(new_media)print music.原创 2021-05-13 02:28:58 · 280 阅读 · 0 评论 -
14. list列表
创建列表alist=[1,2,3,4,3,“stre”]print(alist)list()可以将任何可迭代的数据转化为列表a=list(“abcdedf”)print(a)range()创建整数列表a=list(range(3,15,2))print(a)b=list(range(15,3,-1))print(b)c=list(range(15,-30,-2))print©列表生成式a=[x for x in range(1,5)]print(a)b=[x2..原创 2021-05-11 02:02:25 · 161 阅读 · 0 评论 -
13. 数据类型
一、数据类型基本数据类型:int,bool,str高级数据类型:tuple,list,set,dict序列:str,tuple,list不可变类型:int,bool,str,tuple,set可变类型:list,dict# eval()用来计算在字符串中的有效python表达式,并返回一个对象print(eval("5+10"))a=10b=30print(eval("a+b"))...原创 2021-05-11 01:58:05 · 101 阅读 · 0 评论 -
12. 切片操作
切片应用于序列:字符串,列表,元祖pystr="python"a=pystr[:]print(a)a=pystr[2:]print(a)print("python"[:5]) #左闭右开print("python"[2:5:2])print("python"[-2:])print([1,2,3,4,5,6,7,8,9][-7:-3])print([1,2,3,4,5,6,7,8,9][::-1])print([1,2,3,4,5,6,7,8,9][::-2])...原创 2021-05-11 01:54:00 · 64 阅读 · 0 评论 -
11. break和continue
1.continue用于跳出本次循环,执行下次循环;在循环嵌套中,只针对最近一次循环。#单次循环:#打印1-9之间的奇数:for i in range(10): if i%2==0: continue print(i)#打印3遍1-9之间的奇数:for i in range(3): for i in range(1,10): if i%2==0: continue print(i)2.break用于退出循环体;在嵌套循环中,只针对最近一次循环。#剩余两个名字不打印原创 2021-05-07 01:27:27 · 59 阅读 · 0 评论 -
10.嵌套循环(分别用while循环和for循环输出九九乘法表)
方法一while循环:i=1while i <10: j=1 while j<=i: print("%d*%d=%d"%(i,j,i*j),end=" ") j+=1 print()i+=1方法二for循环:for i in range(10): for j in range(1,i+1): print("%d*%d=%d"%(i,j,i*j),end=" ") print()原创 2021-05-07 00:34:03 · 1392 阅读 · 0 评论 -
9. for循环
定义:for循环用于遍历一个集合,每次循环,会从集合中取得一个元素,并执行一次代码块,直到集合中所有的元素都获取,for循环才结束。for循环的语法格式如下:for 变量 in 可迭代对象:循环体语句方法一:names=["kay","alice","stacey","jack"]for name in names: print(name)方法二:for i in range(len(names)): print(names[i])...原创 2021-04-27 01:47:56 · 148 阅读 · 0 评论 -
8. while循环
#打印1-9#方法一count=0while count<=9: print(count) count+=1#打印1-9#方法二result=" "count=0while count<=9: result+=str(count)+" " count+=1print(result)#while,elseresult=""count=0while count<=9: result+=str(count)+" " count+=原创 2021-04-27 01:12:02 · 52 阅读 · 0 评论 -
7.多分支和三元条件运算符
多分支score=98if score>=90: print("优秀")elif score>=80: print("良好")elif score>=70: print("中等")elif score >=60: print("一般")else: print("差")三元条件运算符(又名三目运算符)表达式: A=Y if X else Z 如果X为真, 那么就执行A=Y, 如果X为假,那么就执行A=Z...原创 2021-04-27 00:49:13 · 98 阅读 · 0 评论 -
6.jupyter note的打开方式
在cmd终端进入文件位置的快捷方式:在要创建文件的位置导航栏输入cmd即可打开该位置终端;打开jupyter notebook的方式:在cmd窗口中,找到要创建文件的位置,输入jupyter notebook即可打开;原创 2021-04-26 00:50:15 · 209 阅读 · 0 评论 -
5. python是强类型的动态脚本语言
强类型:不允许不同类型相加,例如:5+‘hello’会报错。动态:不使用显示数据类型声明,且确定一个变量的类型是在给它赋值的时候。脚本语言:一般是解释性语言,运行代码只需要一个解释器,不需要编译器。...原创 2021-04-25 23:47:35 · 96 阅读 · 0 评论