Python Learning--tuples, dictionaries, collections

Source: Internet
Author: User

Meta-group

Function: To store multiple values, the tuple is immutable (can be used as a dictionary key) for the list to read,

Order of

Definition: Age=tuple (11,22,33,44, ' Lgh ') of any type of value separated by commas within ()

1. Priority operations:

  

value by index age= (11,22,33)Print(age[1]) Slice age= (11,22,33)Print(Age[0:2]) Length age= (11,22,33,44)Print(Len (age)) member Operation age= (11,22,33,44,55,66,77)if33inchAge :Print(True)if50 not inchAge :Print(False) Loop age= (11,22,33,44,55,66) forIinchAge :Print(i)

Dictionary

Function: Record multiple values, each corresponding to the key used to describe the role of value

How to define: separate multiple key:value with commas within {}, where value can be any type, key must be immutable, and usually str type

1. Prioritized operations and built-in methods

  

1, press key to access the value: can save the preferred dic={'name':'Egon'}Print(dic['name']) dic['name']='Egon'Print(DIC)Print(DIC) dic[' Age']=18Print(DIC) L=['a','b']l[0]='A'l[2]='C'2, Length lendic={'name':'Egon',' Age': 18,'name':'EGON','name':'XXXX'}Print(DIC)Print(Len (DIC))3. Member operations in and notinch: The dictionary member operation is judged by the Keydic={'name':'Egon',' Age': 18,}Print(18inchdic)Print(' Age' inchdic)4, remove dic={'name':'Egon',' Age': 18,} genericdeldic['name']Print(DIC)deldic['XXX']##key不存在则报错Res=dic.pop (' Age')#deletes the value corresponding to key and returns the valuePrint(DIC)Print(RES)#Dic.pop ('XXX')#error If key does not existRes=Dic.popitem ()Print(DIC)Print(RES)5, key keys (), value values (), key value pairs items ()6, cyclic dic={'name':'Egon',' Age':' -','Sex':'male'} forKinchDic.keys ():Print(K,dic[k]) forKinchDIC:Print(K,dic[k]) forVinchdic.values ():Print(v) forKvinchDic.items ():Print(k,v) dic={'name':'Egon',' Age':' -','Sex':'male'}v=dic.get ('name')#this way, even if key does not exist, it will not error. return NonePrint(v)

Collection

  Purpose: Relational operation, de-weight

How to define: separate elements with commas within {}

Note: 1. There are no duplicate elements in the collection

2. The elements of the collection must be immutable types

1. Common operation and built-in methods:

  

stus_linux={'Alex','Egon','Zhangquan Egg','li Tie Egg','Oldboy'}stus_python={'li er ya','wxx','Liudehua','Alex','Egon'enroll both Linux and Python students: intersectionPrint(Stus_linux &Stus_python)Print(Stus_linux.intersection (Stus_python)) All students: the collectionPrint(Stus_linux |Stus_python)Print(Stus_linux.union (Stus_python)) only registered Linux, not registered python: Difference setPrint(Stus_linux-Stus_python)Print(Stus_linux.difference (Stus_python))Print(Stus_python-stus_linux)Print(Stus_python.difference (stus_linux)) The name of the student who has not enrolled in two courses: cross-complement setPrint(stus_linux ^Stus_python)Print(Stus_linux.symmetric_difference (Stus_python))

2. Prioritize operations

   

length Len member operation in and notinchView S1={1,'a','b','C','D'} forIteminchS1:Print(item) add S1={'a','b','C'}#S1.add () # Add one value at a timeS1.add ((4)Print(S1) s1.update ({3,4,5})#add multiple values at oncePrint(S1) Delete S1.discard ()## When the deleted element does not exist, it will not be an errorS1.discard (4)Print(S1) s1.remove ()#when the deleted element does not exist, an errorS1.remove (4) S1.pop ()#take a random element.res=S1.pop ()Print(S1)Print(RES)

Python Learning--tuples, dictionaries, collections

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.