1. Item
1 my_dict ={'a': 1,'b': 2}2 Print my_dict.items ()34 [('a', 1), (' b', 2)]
2, Keys,values
1 my_dict ={ " a : 1," b ": 2} 2 print My_dict.keys () 3 print My_dict.values () 4 5 [ " a ", " b 6 [1, 2]
3. Advanced List
1New_list = [x forXinchRange (1,6)]2 #= = [1, 2, 3, 4, 5]3Doubles = [x*2 forXinchRange (1,6)]4 #= = [2, 4, 6, 8, ten]5Doubles_by_3 = [x*2 forXinchRange (1,6)if(x*2)%3 = =0]6 #= [6]7Even_squares = [x**2 forXinchRange (1,11)ifx%2 = =0]8 #= = [4 , +, +, +--]
1 " ! Xexgxaxsxsxexmx xtxexrxcxexsx xexhxtx xmxax XI"2 message = garbled[::-2]#逆序 3 Print message 4 # I am The secret message!
4. Lambda
1languages = ["HTML","JavaScript","Python","Ruby"]2 PrintFilterLambdaX:x = ='Python', Languages)3 #[' Python ']
Equivalent to
1languages = ["HTML","JavaScript","Python","Ruby"]2 defBy_py (languages):3Lst_new = []4 forIinchlanguages:5 ifi = ='Python':6 lst_new.append (i)7 returnlst_new8 PrintBy_py (Languages)
1 my_list = range (2)Print filter (lambda x:x% 3 = = 0, my_list)
3#[0, 3, 6, 9, and up]
Equivalent to
1 my_list = range (2)def by_three (x):3 lst_new = [] 4for in x:5 if i%3 = = 0: 6 lst_new.append (i)7 return lst_new 8 print by_three (my_list)
1 for in range (1,11)]2print filter (lambda x:30<=x<=70,squares)
Equivalent to
1Squares = [x**2 forXinchRange (1,11)]2 defby_sq (squares):3Lst_new = []4 forIinchSquares:5 if30<=i<=70:6 lst_new.append (i)7 returnlst_new8 PrintBY_SQ (squares)
Python Learning Notes (3)--dict&tuple&somthing Advance