# for循环>>>for car in cars:...print(car.title()+" is a good car!")...
Audi is a good car!
Bmw is a good car!
Subaru is a good car!
Toyota is a good car!
--->>>for value inrange(1,5):#使用range创建数值列表 创建的是左闭又开...print(value)...1234>>> numbers=list(range(1,6))>>> numbers
[1,2,3,4,5]>>> even_nums=list(range(2,10,2))#range函数可以 接受第三个参数:指定步长>>> even_nums
[2,4,6,8]--->>> squares=[]>>>for i inrange(1,6):... squares.append(i*i)...>>> squares
[1,4,9,16,25]---#简单的统计计算>>>min(squares)#最小值1>>>max(squares)#最大值25>>>sum(squares)#求和55>>> squares=[value**2for value inrange(1,10)]#列表解析 更快速的方式>>> squares
[1,4,9,16,25,36,49,64,81]----#切片--->>> squares[0:3][1,4,9]>>> squares[3:][16,25,36,49,64,81]>>> squares[:4][1,4,9,16]>>> squares[-3:]#倒数三个元素[49,64,81]>>> squares_copy=squares[:]#复制列表 深拷贝>>> squares_copy
[1,4,9,16,25,36,49,64,81]>>> temp=squares #直接复制是浅拷贝>>> temp.append(0)>>> squares
[1,4,9,16,25,36,49,64,81,0]--->>> s=tuple(squares)#列表转化为元组>>> s
(1,4,9,16,25,36,49,64,81,0)>>>len(s)#获取长度10>>> s+s #元组拼接(1,4,9,16,25,36,49,64,81,0,1,4,9,16,25,36,49,64,81,0)>>> hi=("hi",)>>> hi*4#类似于字符串乘法('hi','hi','hi','hi')>>>5in s #判断存在与否False>>>for x in s:...print(x)...1491625364964810>>> s[2:5]#元组也支持切片操作(9,16,25)>>>min(s)0>>>max(s)81>>>sum(s)285
4.if语句
age=18
age==18
Out[2]:True
age<=20
Out[3]:True
age>=21
Out[4]:False
age>10and age <20
Out[5]:True
age >20or age<10
Out[6]:False
names=["Jim","Jhon","Smith"]
user="Andrew"if user notin names:print(user.title()+" ,you are not in the list")
Andrew ,you are notin the listif age<4:print("you are a kid")elif age<18:print("you are a teenager")else:print("you can enter")
you can enter
5.字典
Jim={"age":18,"gender":"male"}
Jim["age"]
Out[14]:18
Jim["gender"]
Out[15]:'male'print(Jim["age"])18---添加元素
box={'color':"green",'height':20}
box['x_pos']=0
box['y_pos']=21
box
Out[20]:{'color':'green','height':20,'x_pos':0,'y_pos':21}
box['color']="red"#修改值
box
Out[22]:{'color':'red','height':20,'x_pos':0,'y_pos':21}del box['height']#删除键值对
box
Out[24]:{'color':'red','x_pos':0,'y_pos':21}for key,value in box.items():#遍历字典print("\nKey: "+key)print("Value: "+str(value))> Key: color Value: red
>> Key: x_pos Value:0>> Key: y_pos Value:21for name in box.keys():#遍历键print(name.title())> Color X_Pos Y_Pos
for name insorted(box.keys()):#顺序遍历print(name.title())> Color Weight X_Pos Y_Pos
box['height']=10
box
Out[38]:{'color':'red','x_pos':0,'y_pos':21,'weight':10,'height':10}for name in box.values():#遍历值print(str(name).title())
Red
0211010#值时有可能重复的,所以如果不想重复打印可以去重for name inset(box.values()):print(str(name).title())
Red
01021
6.嵌套
Stus #列表嵌套字典
Out[50]:[{'color':'pink','height':179},{'color':'green','height':144},{'color':'yellow','height':151}]for stu in Stus:print(stu){'color':'pink','height':179}{'color':'green','height':144}{'color':'yellow','height':151}
Jim={}#字典嵌套 列表
Jim["gender"]="Male"
Jim["address"]="ShenZhen"
Jim["class"]=["history","Mathematic","Computer","Politics"]
Jim
Out[56]:{'gender':'Male','address':'ShenZhen','class':['history','Mathematic','Computer','Politics']}---#字典嵌套字典
user
Out[58]:{'aeinstein':{'first':'albert','last':'einstein','location':'princeton'},'mcurie':{'first':'marie','last':'curie','location':'paris'}}for username, userinfo in user.items():print("\nUsername: "+username)
fullname=userinfo['first']+" "+userinfo['last']
location=userinfo['location']print("\tFull name: "+fullname.title())print("\tLocation: "+location.title())
Username: aeinstein
Full name: Albert Einstein
Location: Princeton
Username: mcurie
Full name: Marie Curie
Location: Paris