#if 可以嵌套 if elif else
a = 10
b = 20
if a > b:
print("a > b")
elif b > a:
print("b > a")
elif a == b:
print("a==b")
else:
print("what the fuck!")
name = "arron"
oh = "shit"
if name == "arron":
print("my name is arron")
if oh == "shit":
print("oh shit")
else:
print("no,my name is kobe")
#可以结合 or and
if a == b or a==1:
print("oh ye!")
elif a != b and a == 10 and b == 20:
print("oh no")
b > a
my name is arron
oh shit
oh no
In [9]:
#while
count = 0
while count < 3:
count += 1
print("oh ye")
name = input("please input name :")
while name != "quit":
name = input("please input name :")
print("game over!")
oh ye
oh ye
oh ye
please input name :d
please input name :quit
game over!
In [39]:
#for
for i in range(5):
print(i)
#嵌套
for i in range(1,3):
for j in range(1,3):
print(i,j)
l1 = [1,2,3]
for i in l1:
print(i)
dic1 = {'Tom':1,'Kobe':2,'Arron':3}
for i in dic1:
print(i)
for i in dic1.values():
print(i)
for i in dic1.keys():
print(i)
0
1
2
3
4
1 1
1 2
2 1
2 2
1
2
3
Kobe
Tom
Arron
2
1
3
Kobe
Tom
Arron
In [37]:
#an = a1 + (n-1)*d
a1 = float(input("please input a num:"))
n = int(input("please input n:"))
d = float(input("please input d:"))
sum = 0
for i in range(n):
an = a1+i*d
sum = sum +an
print(sum)
please input a num:2
please input n:2
please input d:2
6.0
In [43]:
两个列表生成字典
#两个列表生成字典
l1 = ['a','b,','c']
l2 = [1,2,3]
dir1 = {}
for i in range(len(l1)):
dir1[l1[i]] = l2[i]
print(dir1)
{'b,': 2, 'c': 3, 'a': 1}
In [46]:
for i,j in zip(l1,l2):
dic[i] = j
"""
zip 语法:zip([iterable, ...])
参数说明:
iterabl -- 一个或多个迭代器;
返回值
返回元组列表。"""
dic = {}
for i,j in zip(l1,l2):
dic[i] = j
print(dic)
{'b,': 2, 'c': 3, 'a': 1}
In [14]:
# 循环控制 break pass continue
#break 结束整个循环
a = 0
while a < 10:
a += 1
if(a == 5):
break
else:
print(a)
#continue 每次5都没有打印出来,跳出了本次的循环
for i in range(2):
for j in range(10):
if j == 5:
continue
print(j)
#pass 占位符 保持代码完整性
1
2
3
4
0
1
2
3
4
6
7
8
9
0
1
2
3
4
6
7
8
9