Python tuple types, dictionary types, and common operations

Source: Internet
Author: User

One, tuple type1. Use

Multiple values are recorded, and when multiple values are not changed, it is more appropriate to use tuples at this time, and Python's tuples are similar to lists, except that the elements of tuples cannot be modified.

2. How to define

Separate multiple values of any type with commas within ()
T= (1,6, (5, '), {}) #t =tuple ((1, 6, (5, '), {}))
T1=tuple (' Hello ')

3. Common operation + built-in method
Priority operations:
1, by index value (positive fetch + reverse fetch): can only take
t= (1, 6, (5, '), [1,5,3])
t[0]= ' 1 '

2, slicing (Gu Tou regardless of tail, step)
T= (0,2,3,45,6,9)
Print (T[0])
Print (T[0:5:2])

3. Length
Print (len (t))


4, member operations in and not in

5. Circulation
T= (0,2,3,45,6,9)
For item in T:
Print (item)

What you need to know
t= (0,2,3, ' a ', 6,9,2,2,2)
  
Print (T.count (2))
# index
# Print (T.index (2,2,8))
4. Summary of this type

1. Save Multiple values

2. Orderly
3. Non-volatile

First, the dictionary type1. Use

  Record multiple values, each of which corresponds to a key used to describe the role of value

2. How to define

Separate multiple key:value with commas within {}, where value can be any type, and key must be an immutable type, usually a str type
Dic={0: ' A ', 1: ' B ', 2: ' C '}
dic={: ' A ', 1: ' B ', 2.5: ' C '} #dic =dict ({): ' A ', 1: ' B ', 2.5: ' C '})

3. Common operation + built -in method

1, by key access value: can be saved desirable
dic={' name ': ' Lyf ', ' Age ': 18}
Print (dic[' name '])
dic[' sex ']= ' male '
Print (DIC)

2, the number of length Len:key
dic={' name ': ' Lyf ', ' Age ': 18}

3, member operations in and not in: The dictionary member operation is determined by the key value

4. Delete
dic={' name ': ' Lyf ', ' Age ': 18}
General
Del dic[' name ']
Print (DIC)
The deleted key does not exist then an error
Self-Bringing method
Sre=dic.pop (' age ') #返回value值
Print (Sre,dic)
The deleted key does not exist then an error
Src=dic.popitem ()
Print (SRC) #返回键值对, guaranteed to exist in the tuple
Print (DIC)

5, key keys (), value values (), key value to items ()
dic={' name ': ' Lyf ', ' Age ': 18}
Print (Dic.keys ())
Print (Dic.values ())
Print (Dic.items ())
Python2
Print (List (Dic.keys ()))
Print (List (dic.values ()))
Print (List (Dic.items ()))

6. Circulation
dic={' name ': ' Lyf ', ' age ':, ' sex ': ' Male '}
For K in DiC:
Print (k)
For V in Dic.values ():
Print (v)
For k,v in Dic.items ():
Print (K,V)

7. Get () method
dic={' name ': ' Lyf ', ' age ':, ' sex ': ' Male '}
Src=dic.get (' sex ')
Print (SRC)

4. Summary of this type

1 Save Multiple values
2 disorder
3 variable

Python tuple types, dictionary types, and common operations

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.