算術(shù)運(yùn)算
print(10 / 3)
print(10 // 3)
print(10 ** 2)
賦值運(yùn)算
增量賦值
age=18
age+=1#age=age + 1
print(age)
age=18
age/=3 #age=age/3
print(type(age))
age**=2 #age=age**2
交叉賦值
x=10
y=20
temp=x
x=y
y=temp
x,y=y,x
print(x,y)
鏈?zhǔn)劫x值
x=10
y=x
z=y
x=y=z=10
print(id(x))
print(id(y))
print(id(z))
解壓賦值
l=[1.2,2.2,3.3,4.4,5.5]
a=l[0]
b=l[1]
c=l[2]
d=l[3]
e=l[4]
a,b,c,d,e=l
a,b,c,d,e,f=l
a,b,c,d=l
print(a,b,c,d,e)
l=[1.2,2.2,3.3,4.4,5.5]
a,b,*_=l
print(a,b)
a,*_,b=l
print(a,b)
*_,a,b=l
print(a,b)
流程控制之if
語法1:
if 條件:
代碼1
代碼2
代碼3
...
age_of_bk=30
print('start.....')
inp_age=input('>>>: ') #inp_age='18'
inp_age=int(inp_age)
if inp_age == age_of_bk:
print('猜對了')
print('end.....')
語法2:
if 條件:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
'''
age=38
gender='male'
is_beautiful=True
if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
print('開始表白心铃。狠角。。褒傅。')
else:
print('阿姨好')
'''
語法3:
if 條件1:
代碼1
代碼2
代碼3
...
elif 條件2:
代碼1
代碼2
代碼3
...
elif 條件3:
代碼1
代碼2
代碼3
...
elif 條件4:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
'''
score=input('your score>>: ')
score=int(score)
if score >=90:
print('優(yōu)秀')
elif score >=80:
print('良好')
elif score >=70:
print('普通')
else:
print('很差')
語法4:
if 條件1:
if 條件2:
代碼1
代碼2
代碼3
...
代碼2
代碼3
'''
age=18
gender='female'
is_beautiful=True
is_successful=True
if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
print('開始表白。虐秦。胶征。尔艇。')
if is_successful:
print('在一起')
else:
print('我逗你玩呢。喜庞。诀浪。')
else:
print('阿姨好')
while條件循環(huán)
I: 基本語法
while 條件:
代碼1
代碼2
代碼3
...
# 示范
name_of_bk='egon'
pwd_of_bk='123'
tag=True
while tag:
inp_name=input('your name>>: ')
inp_pwd=input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
tag=False
else:
print('username or password error')
print('other code...')
II: while+break:break代表結(jié)束本層循環(huán)
name_of_bk='egon'
pwd_of_bk='123'
while True:
inp_name=input('your name>>: ')
inp_pwd=input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
break
else:
print('username or password error')
print('other code......')
III: while + continue: continue代表結(jié)束本次循環(huán),直接進(jìn)入下一次
# 示范
count=1
while count < 6:
if count == 3:
count+=1
continue
print(count)
count+=1
# 輸錯三次退出
name_of_bk='egon'
pwd_of_bk='123'
count=0
while True:
if count == 3:
print('輸錯的次數(shù)過多延都。雷猪。。')
break
inp_name=input('your name>>: ')
inp_pwd=input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
break
else:
print('username or password error')
count+=1 #count=3 輸錯3次
IV:while + else
count=0
while True:
if count == 10:
break
print(count)
count+=1
else:
print("else的子代塊只有在while循環(huán)沒有被break打斷的情況下才會執(zhí)行")
count=0
while count <= 10:
print(count)
count+=1
else:
print("else的子代塊只有在while循環(huán)沒有被break打斷的情況下才會執(zhí)行")
name_of_bk='egon'
pwd_of_bk='123'
count=0
tag=True
while tag:
if count == 3:
print('輸錯的次數(shù)過多晰房。求摇。。')
break
inp_name=input('your name>>: ')
inp_pwd=input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
while tag:
print("""
0 退出
1 購物
2 支付
3 查看購物
""")
cmd=input('>>>: ')
if cmd == '0':
tag=False
continue
if cmd == '1':
print('購物殊者。与境。。猖吴。摔刁。。海蔽。')
elif cmd == '2':
print('支付共屈。。党窜。拗引。。')
elif cmd == '3':
print('查看購物車')
else:
print('輸入錯誤的指令')
else:
print('username or password error')
count+=1 #count=3 輸錯3次