Full-stack python development-Day5 tuples, dictionaries, python-day5

Source: Internet
Author: User

Full-stack python development-Day5 tuples, dictionaries, python-day5
Full-stack python development-Day5 tuples and dictionariesI. PrefaceFirst of all, no matter what data type we learn, we will start learning with the following questions:#1: basic usage 1. Purpose 2. Definition method 3. Common Operations + Built-in methods #2. Summary of this type 1. Save one value or multiple values, only one value can be saved and multiple values can be saved. What types of values can be saved? 2. ordered or unordered. 3. variable or variable !!! Variable: The value changes, and the id remains unchanged. Variable = non-hash !!! Immutable: The value changes, and the id changes. Immutable = hash II. tuples # function: store multiple values. For the Comparison List, tuples are immutable (which can be used as keys of the dictionary ), # mainly used for read definition: Compared with the list type, only [] is changed to () age = (, 44, 55) essentially age = tuple (, 55 )) # preferred operations: #1. Take the index value (Forward fetch + reverse fetch): only the value of # is the same as that of string and list, I will not elaborate on it. #2. Slice (skip the tail and step size)

1 t = (,) 2 print (t []) 3 4 # The result is (2, 3, 4)
#3. Length
1 t = (,) 2 print (len (t) 3 4 # The result is 6.
#4. member operations in and not in
1 t = (,) 2 print (1) in t) 3 4 # The result is true # not in.
#5. Loop
1 t = (,) 2 3 # No index loop 4 for I in t: 5 print (I) 6 7 # Two cycles depending on indexes 8 # for Loop 9 for I in range (len (t) 10 print (I) 11 12 # while loop 13 n = 014 while Ture: 15 print (t [I]) 16 n + = 1 17

 

3. dictionary # function: store multiple values for key-value access. The value is fast and each value has a corresponding key # definition: the key of multiple elements separated by commas in {} must be of an unchangeable type, and the value can be of any type. info = {'name': 'egon', 'age': 18, 'Sex ': 'male'} # Nature info = dict ({....}) or info = dict (name = 'egon', age = 18, sex = 'male') Or info = dict (['name', 'egon'], ('age', 18)]) or {}. fromkeys ('name', 'age', 'sex'), None) # preferred operations: #1. key-Based Access value: available
1 dic = {'name': 'duoduo'} 2 dic ['age'] = 103 print (dic) # The value is {'name': 'duoduo ', 'age': 10} 4 5 dic ['name'] = 'Qian '6 print (dic) # The value is {'name': 'Qian ', 'age ': 10} changeable
#2. Length len
1 dic = {'name': 'duoduo', 'age': 18} 2 print (len (dic) 3 4 # result 2
#3. member operations in and not in (judge whether the key exists)
1 dic = {'name': 'egon', 'age': 18} 2 print ('name' in dic) 3 4 # The result is true.
#4. Delete
1 dic = {'name': 'duoduo', 'age': 18} 2 res = dic. pop ('name') # The parameter 3 print (res) added after the pop parameter is different from the list # The value is 'duoduo' 4 print (dic) # value: {'age': 18} 5 6 # del dic ['name'] 7 # print (dic) omnipotent Delete 8 9 10 # res = dic. pop ('Qian ', None) # The deleted object is no longer in the original dictionary 11 # print (res) # No error will be reported if None is added to the end. The returned value is None.
#5. Key keys (), value values (), key-value pair items (), and loop
# Key keys () dic = {'name': 'duoduo', 'age': 18} print (dic. keys () # The value is dict_keys (['name', 'age']) for key in dic. keys (): # dictionary. keys () This method is omitted without writing directly dic. You can also print (key) # The result is name age # value values () dic = {'name': 'duoduo', 'age ': 18} print (dic. values () # value: dict_values (['duo', 18]) for values in dic. values (): print (values) # result: duoduo 18 # key-Value Pair itemsdic = {'name': 'duoduo', 'age': 18} print (dic. items () # The value is dict_items ([('name', 'egon'), ('age', 18)]) for k, v in dic. items (): # k, v = ('name', 'duoduo') print (k, v)
# Knowledge:
1 dic = {'name': 'duoduo', 'age': 18} 2 print (dic. get ('namexxxx') # No, output None3 print (dic ['namexxxxxxxxxxxxx']) 4 # This will report an error # Summary: The returned value is found, and None is returned if not found.

 

Related Article

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.