Python中的控制流(if & for语句)

1. if 语句
if condition:
     do something
elif other_condition:

     do something

例:

#if statement example
number = 59
guess = int(input('Enter an integer : '))
# print("guess is: "+str(guess))

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')
else:
    print('No, the number is lower than that')
    # you must have guessed > number to reach here
print('Done')
# This last statement is always


#the for loop example
for i in range(1, 10):     #range是一个关键字,表示从第一个数字到第二个数字的所有值的一个列表,但是他只包含第一个数字并不包含最后一个数字
    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(): #通过“.items”打印出所有的值
    print(key, elem)


猜你喜欢

转载自blog.csdn.net/qlulibin/article/details/79723314
今日推荐