1 for number in range(1,5): 2 print number 3 4 print range(4) 5 # [0, 1, 2, 3] 6 7 8 xx=[x*x for x in range(4)] 9 print xx10 #[0, 1, 4, 9]11 12 xx=[x*x for x in range(4) if x % 3==0]13 print xx14 #[0, 9]15 16 xx=[(x,y) for x in range(3) for y in range(3)]17 print xx18 #[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]19 20 21 22 xx=[[x,y] for x in range(3) for y in range(3)]23 print xx24 #[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]25 26 27 28 girls=['alice','jerry','jessica']29 boys=['king','kim','thomas']30 all=[a+"-"+b for a in girls for b in boys]31 print all32 33 raw_input("pressto close")
1 names=['a','b','c'] 2 ages=[1,2,3] 3 for k in range(len(names)): 4 print names[k] +'=' +str(ages[k])+ 'years old' 5 6 7 union =zip(names,ages) 8 9 print union10 ##[('a', 1), ('b', 2), ('c', 3)]11 for name,age in union:12 print name+":"+str(age)