1.if 语句
if condition:
do something
elif other_condition:
do something
2.for 语句
Code:
#if statement example
number = 59
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that')
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that')
# you must have guessed > number to reach here
print('Done')
# This last statement is always executed,
# after the if statement is executed.
#the for loop example
for i in range(1, 10): #[1,9)
print(i)
else:
print('The for loop is over')
a_list = [1, 3, 5, 7, 9]
for i in a_list:
print(i)
a_tuple = (1, 3, 5, 7, 9)
for i in a_tuple:
print(i)
a_dict = {'Tom':'111', 'Jerry':'222', 'Cathy':'333'}
for ele in a_dict:
print(ele)
print(a_dict[ele])
for key, elem in a_dict.items():
print(key, elem)
3.while语句
4.range语句
Code:
#while example
number = 59
guess_flag = False
while guess_flag == False:
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
guess_flag = True
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that, keep guessing')
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that, keep guessing')
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')
#For example
number = 59
num_chances = 3
print("you have only 3 chances to guess")
for i in range(1, num_chances + 1):
print("chance " + str(i))
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
break #跳出for循环
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
# You can do whatever you want in a block ...
else:
print('No, the number is lower than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
# you must have guessed > number to reach here
print('Done')
5.break
6.continue
7.pass
Code:
#break & continue example
number = 59
while True:
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
break
# New block ends here
if guess < number:
# Another block
print('No, the number is higher than that, keep guessing')
continue #直接跳到while进入下一次循环
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that, keep guessing')
continue #可有可无
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')
continue and pass difference
a_list = [0, 1, 2]
print("using continue:")
for i in a_list:
if not i: #not 0是真,not 2是假
continue
#当前循环体后面语句不执行,直接跳到下一次循环
print(i)
print("using pass:")
for i in a_list:
if not i:
pass
#相当于空代码,什么也不做,后面的语句将执行
print(i)