先看看布尔值
:
bool('')
False
2 and 4
4
2 or 4
2
断言(assert)
num=-1
assert num > 0,'num should be positive!'
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-4-91bd8de0b8b3> in <module>()
1 num=-1
----> 2 assert num > 0,'num should be positive!'
AssertionError: num should be positive!
num=5
assert num > 0,'num should be positive!'
if 条件
year=int(input('请输入年份:'))
if year%4==0:
if year%400==0:
print('闰年')
elif year%100==0:
print('平年')
else:
print('闰年')
else:
print('平年')
请输入年份:1992
闰年
year=int(input('请输入年份:'))
if (year%4==0 and year%100 !=0) or year%400==0:
print('闰年')
else:
print('平年')
请输入年份:2333
平年
while 循环
x=1
while x<=100:
print(x)
x+=27
1
28
55
82
# 拉兹猜想
num=int(input('请输入初始值:'))
while num!=1:
if num%2==0:
num/=2
else:
num=num*3+1
print(num)
请输入初始值:34
17.0
52.0
26.0
13.0
40.0
20.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0
for 循环
nums=[1,2,3,4,5]
for i in nums:
print(i)
1
2
3
4
5
list(range(1,10,6))
[1, 7]
# 阶乘
x=1
for i in range(1,11):
x*=i
print('10!=',x)
10!= 3628800
b=[[9,7,3,6,5],[10,2,4,6,7],[0,5,3,2,9],[7,3,5,6,1]]
s=0
for i in range(len(b)):
for j in range(len(b[i])):
s+=b[i][j]
print(s)
100
for x in range(1,10):
for y in range(1,x+1):
print(end='|')
print('%d*%d=%2d'%(x,y,x*y),end='|')
print()
|1*1= 1|
|2*1= 2||2*2= 4|
|3*1= 3||3*2= 6||3*3= 9|
|4*1= 4||4*2= 8||4*3=12||4*4=16|
|5*1= 5||5*2=10||5*3=15||5*4=20||5*5=25|
|6*1= 6||6*2=12||6*3=18||6*4=24||6*5=30||6*6=36|
|7*1= 7||7*2=14||7*3=21||7*4=28||7*5=35||7*6=42||7*7=49|
|8*1= 8||8*2=16||8*3=24||8*4=32||8*5=40||8*6=48||8*7=56||8*8=64|
|9*1= 9||9*2=18||9*3=27||9*4=36||9*5=45||9*6=54||9*7=63||9*8=72||9*9=81|
d={'k1':1,'k2':2,'k3':3}
for i in d:
print(i,':',d[i])
k1 : 1
k2 : 2
k3 : 3
d={'k1':1,'k2':2,'k3':3}
for key,value in d.items():
print(key,':',value)
k1 : 1
k2 : 2
k3 : 3
迭代工具
1. reversed
list(reversed(range(1,10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1]
list(reversed('Hello,world'+'sd'))
['d', 's', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'H']
2. zip
products=['apple','banana','orange']
prices=[50,40,25]
for i in range(len(products)):
print(products[i]+':',prices[i])
apple: 50
banana: 40
orange: 25
list(zip(products,prices))
[('apple', 50), ('banana', 40), ('orange', 25)]
for product,price in zip(products,prices):
print(product+':',price)
apple: 50
banana: 40
orange: 25
a1=[1,3,5,7,9]
a2=[2,4,6,8]
list(zip(a1,a2))
[(1, 2), (3, 4), (5, 6), (7, 8)]
3. 编号迭代
l=['a','b','c']
for i,j in enumerate(l):
print(i,':',j)
0 : a
1 : b
2 : c
循环控制语句
break & continue
- break结束当前循环,然后跳到循环后的下一条语句。
- continue提前结束当前这次循环,且继续进行下一次循环。
a,b=0,1
while True:
a,b=b,a+b
if b>1000:
break
print(a)
987
# (数值之和小于100的行)的奇树数值之和
m=[[12,13,20,9,30,7],[11,22,33,21,44],[30,32,25,66,1],[12,34,56,7]]
result=0
for l in m:
tmp=0
for n in l:
tmp+=n
if tmp>=100:
break
if tmp>=100:
continue
for n in l:
if n % 2==1:
result+=n
print(result)
29
else子句
flag
变量可用来指示某一个特定事件是否已经发生,或某个特定状态是否存在。
若需要在循环之后判断该条件是否符合,则需要额外的标识来记录。
示例:
l=[2,4,8,0,10,12]
flag=False
for n in l:
if n%2==1:
flag=True
break
if not flag:
print('All num is even')
与下面的else子句
等价
l=[2,4,8,0,10,12]
for n in l:
if n%2==1:
flag=True
break
else:
print('All num is even')
All num is even
列表推导式
利用其他集合类对象(列表,元组,集合,字典,...)来创建新的列表的方法:
示例:
[2*x for x in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[2*x for x in range(10) if x%3==0]
[0, 6, 12, 18]
[(x,y) for x in range(2) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
字典推导式
a = ('a','b','c','df','gh')
b = ['sdd',1,2,3,4,5]
d = {a[i]:b[i] for i in range(len(a))}
d
{'a': 'sdd', 'b': 1, 'c': 2, 'df': 3, 'gh': 4}
探寻有趣之事!