列表 List
album=['BlackStar','DB',25,True]
album.append('newsong')
print(album[0],album[-1])
bool(0) #False
bool([]) #False
bool('') #False
bool(False) #False
bool(None) #False
a_thing= None
1 < 3and 2 < 5 #True
1 < 3and 2 > 5 #False
1 < 3or 2 > 5 #True
1 > 3or 2 > 5 #False
条件控制
ifcondition:
do something
else:
do something
password_list=['*#*#','12345']
def account_login():
password=input('Password:\n')
password_correct=password==password_list[-1]
password_reset=password==password_list[0]
if password_correct:
print('Loginsuccess!')
elif password_reset:
new_password=input('Entrya new password:\n')
password_list.append(new_password)
print("Yourpassword has been changed successfully!")
account_login()
else:
print("Incorrectpassword or invalid input!")
account_login()
account_login()
循环 Loop
for循环
for itemin iterable:
do something
songslist = ['HolyDiver', 'Thunderstruck', 'Rebel Rebel']
for song in songslist:
if song == 'Holy Diver': print(song,' -Dio')
elif song == 'Thunderstruck': print(song,' -AC/DC')
elif song == 'Rebel Rebel': print(song,' -David Bowie')
乘法表
for i in range(1,10):
for j in range(1,10):
print('{0}* {1} = {2}'.format(i,j,i*j), end='\t')
print('\n')
while循环
whilecondition:
do something
while1<3:
print('1 is less than 3')# Ctrl+C 停止运行
count = 0
whileTrue:
print('Repeat this line!')
count=count+1
if count == 5:
break