python基础_控制流

x = 5
if x > 6 :
print("x is greater than six")
elif x > 4 and x == 5:
print("{}".format(x))
else:
print("x is not greater than four")

5

y = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','OCt','NOv','Dec']
z = ['Annie','Betty','Claire','Daphne','Ellie','Franchesca','Greta','Holly','Isabel','Jenny']
print("Output #11:")
for month in y:
print('{!s}'.format(month))
for i in range(len(z)):
print("{0!s} {1!s}".format(i,z[i]))
for j in range(len(z)):
if y[j].startswith('J'):
print("{!s}".format(y[j]))

  

Output #11:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
OCt
NOv
Dec
0 Annie
1 Betty
2 Claire
3 Daphne
4 Ellie
5 Franchesca
6 Greta
7 Holly
8 Isabel
9 Jenny
Jan
Jun
Jul

 ##列表生成式

my_data = [[1,2,3],[4,5,6],[7,8,9]]
rows_to_keep = [row for row in my_data if row[2] > 5]
print("Output #11:{}".format(rows_to_keep))

Output #11:[[4, 5, 6], [7, 8, 9]]

集合生成式

my_data = [(1,2,3),(4,5,6),(7,8,9),(7,8,9)]
set_of_tuples1 = {x for x in my_data}
print("{}".format(set_of_tuples1))

{(4, 5, 6), (7, 8, 9), (1, 2, 3)}

#字典生成式

my_dictionary = {'customer1':7,'customer2':9,'custormer3':11}
my_result = {key : value for key,value in my_dictionary.items() if value > 10}
print("{}".format(my_result))

{'custormer3': 11}
原文地址:https://www.cnblogs.com/wei23/p/13160369.html