Python Learning Notes (3)--dict&tuple&somthing Advance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.