Python-Day2 Python basics advanced, python-day2python

Source: Internet
Author: User

Python-Day2 Python basics advanced, python-day2python
I. Data Types

Python3 has six standard data types:

  • Number)
  • String (String)
  • List)
  • Tuple (tuples)
  • Sets)
  • Dictionary)

Python3 supportInt (integer), float (float), bool (Boolean), complex (plural). In Python 3, there is only one Integer type int, which is a Long integer without the Long in python2. Boolean value: true or false, 1 or 0.

Ii. List 1. List Creation
>>> list = [1,2,3,4,5]>>> print(list)[1, 2, 3, 4, 5]
2. List slice
List = [1, 2, 3, 5] >>> print (list) [1, 2, 3, 4, 5] >>> list [] # obtain numbers between subscript 1 and subscript 4, including subscript 1, excluding subscript 4 [2, 3, 4] >>> list [1:-1] # obtain the values from subscript 1 to-1, excluding-1 [2, 3, 4] >>> list [0: 3] [1, 2, 3] >>> list [: 3] # If it is retrieved from the beginning, 0 can be ignored, the same effect as the sentence [1, 2, 3] >>> list [3:] # If you want to get the last one, you must not write-1, you can only write this [4, 5] >>> list [:-1] # in this way,-1 will not be included in [1, 2, 3, 4] >>> list [0 :: 2] # the next 2 is representative. Every other element takes a [1, 3, 5] >>> list [: 2] # with the same effect as the previous sentence [1, 3, 5]
3. List appending
>>> List. append (6) >>> list [1, 2, 3, 4, 5, 6] # append to the end by default. To view the next
4. List insertion
>>> List [1, 2, 3, 4, 5, 6] >>> list. insert (1, "insert to front 2") # insert to front of subscript by default. subscript of element 2 is 1 >>> list [1, 'insert to front 2 ', 2, 3, 4, 5, 6] >>> list. insert (3, "insert after 2") # The subscript of 2 is changed to 2, and the suffix of insert 2 is inserted before subscript 3 >>> list [1, 'insert to 2 ', 2,' insert to 2 ', 3, 4, 5, 6]
5. Modify the list
>>> List [1, 'insert to 2 ', 2,' insert to 2 ', 3, 4, 5, 6] >>> list [1] = "tag1" >>>> list [3] = "tag3" >>> list [1, 'tag1 ', 2, 'lower mark 3', 3, 4, 5, 6]
6. delete a list
>>> List [1, 'bidding 1', 2, 'bidding 3', 3, 4, 5, 6] >>> del list [1] # delete an element whose subscript is 1 >>> list [1, 2, 'bottom s3', 3, 4, 5, 6] >>> list. remove ("bottom mark 3") # delete an element named bottom mark 3 >>> list [1, 2, 3, 4, 5, 6] >>> list. pop (5) # Delete the element 6> list [1, 2, 3, 4, 5]> list. pop () # Delete the last element by default 5 >>> list [1, 2, 3, 4]
7. List Extension
>>> list[1, 2, 3, 4]>>> list + [5,6,7,8,9][1, 2, 3, 4, 5, 6, 7, 8, 9]>>> list[1, 2, 3, 4]>>> list=list + [5,6,7,8,9]>>> list[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> list2=[10]>>> list.extend(list2)>>> list[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8. List copy (light copy and deep copy)
>>> list[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> list2=list.copy()>>> list2[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> List [[0, 1], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list2 = list. copy () >>> list2 [[0, 1], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list [0] [1] = "test" >>> list [[0, 'test'], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list2 [[0, 'test'], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # When the list changes, list2 also changes. copy can only be the first layer. For deep copy, the copy module must be >>> import copy >>> list3 = copy. deepcopy (list) # Deep copy >>> list3 [[0, 0], '1', 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list [0] [0] = 666 >>> list [[666, 0], '1', 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list3 [[0, 0], '1', 2, 3, 4, 5, 6, 7, 8, 9, 10] # No changes have been made after the list is modified.
9. List statistics
>>> List [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list. count (1) # count the number of elements in 1: 2
10. Flip and sort
>>> List [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list. reverse () # list element flip >>> list [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1] >>> list. sort () # sort by ASCII table> list [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
11. Obtain subscript
>>> List [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list. index (1) # subscript 0 of element 1
3. tuples

In fact, a group is similar to a list. It also stores a group of numbers. It cannot be modified once it is created, so it is also called a read-only list. It has only two methods, one is index and the other is count.

>>> list = (0,1)>>> list.index(0)0>>> list.count(0)1
4. Dictionary

A dictionary is a key-value data type. It is used to query the details of a corresponding page through strokes and letters.

Dictionary features:

  • Dict is unordered
  • The key must be unique.
1. Create a dictionary
>>> Dict = {'key1': 'value1', 'key2': 'value2'} # braces are used in the dictionary, and the list is in brackets, the tuples are Parentheses >>> dict {'key1': 'value1', 'key2': 'value2 '}
2. dictionary Addition
>>> dict['key3']='value3'>>> dict{'key3': 'value3', 'key1': 'value1', 'key2': 'value2'}
3. dictionary Modification
>>> dict['key3']='value33333'>>> dict{'key3': 'value33333', 'key1': 'value1', 'key2': 'value2'}
4. Delete the dictionary (three positions)
>>> Dict. pop ("key3") # first pose 'value33333'> dict {'key1': 'value1', 'key2': 'value2'} >>> dict. popitem () # randomly delete a pose ('key1', 'value1') >>> dict {'key2': 'value2' >>> del dict ('key2 ') # Note: The symbol is incorrect. Unlike the above SyntaxError: can't delete function call >>> del dict ['key2'] # delete key2 >>> dict {}
5. dictionary search
Dict = {'key1': 'value1', 'key2': 'value2'} >>> 'key1' in dict # standard usage True >>> dict. get ("key1") # obtain 'value1' >>> dict. get ('key10') # If this key is not found >>> dict ['key1'] # obtain 'value1 '>>> dict ['key10'] # If this key is not found, error Traceback (most recent call last): File "<pyshell #125>", line 1, in <module> dict ['key10'] KeyError: 'key10'
1 City_list = {2 "Beijing": {3 "Haidian": ['shangdi ', 'qinghai'], 4 "Changping": ['huilongguan', 'shahe '], 5}, 6 "Hebei": {7 "Baoding": ['yixian ', 'dingzhou'], 8 "Shijiazhuang": ['xinhua district', 'qiaoxi district '], 9}, 10 "Liaoning": {11 "Shenyang": ['huanggu district ', 'tiexi district'], 12 "Dalian": ['wafangdian ', 'pulandian'], 13}, 14} 15 one = True16 two = True17 while one: 18 print ("Welcome to multi-level menu. Now the first-level menu is displayed:") 19 for I in City_list.keys (): 20 print ("", I) 21 first_input = input ("enter the name of the provincial menu to open the municipal menu, press q to exit, and press B to return to the main menu :") 22 if first_input = "q": 23 one = False24 break25 elif first_input = 'B': 26 break27 else: 28 for k in (City_list [first_input]. keys (): 29 print ("", k) 30 while two: 31 second_input = input ("enter the name of the Municipal menu to open the county menu, press q to exit, return to the main menu by B: ") 32 if second_input =" q ": 33 one = two = False34 break35 elif second_input =" B ": 36 break37 else: 38 for l in (City_list [first_input] [second_input]): 39 print ("", l) 40 third_input = input ("after execution, press q to exit, press B to return to the previous layer. Any key is returned to the main menu: ") 41 if third_input =" q ": 42 exit () 43 elif third_input =" B ": 44 continue45 else: 46 for n in City_list.keys (): 47 print ("", n) 48 breakMulti-level menu Program

 

Multilevel menu Flowchart

 

 

The following are common examples:

Life is more than just what you think is your life. Life is suitable for the distance, how far you can go; not far, not a penny. ------- "Gao Xiaosong's 184-day prison life Transcript: There are poems and distant places in life"

 

 

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.