Several data types in Python list, Tuple,dict,set use demo

Source: Internet
Author: User
Tags python list

or directly on the code, looking at the code to run, see the function introduction

#Coding=utf-8#1 usage of list-listStudents = [A] a= 5Classmates= [students*2,[1,1,1,1],[1,1,1,1]]#The multiplication of the list is the linear enlargement of the arrayClassmates.append ('Test')#Add to ListClassmates.extend (students)#Iteration Extension ListPrint(Classmates.index ('Test', 2))#List Returns the first index of the developing character unknownClassmates.insert (1, 22)#List InsertClassmates.pop (2)#deletion of list itemsCLASSMATES[1] ='New Item'         #Assignment of a list itemPrint(classmates)Print(Classmates[0][1])#Multidimensional Array invocation#2 The use of tuple-tuples, the values of tuples are immutableIdcode = (1000,1001,1002)#defining Multi-element tuplesName = ('Tom',)#define a single element tupleId_name = ([1000,'Tom'], (1001,'John'), 1002)#defines a multidimensional tuple, containing tuples and lists, where list points to a value that can be changed, a tuple's non -id_name[0][1]=1002Print(Id_name)#3dict-data dictionary, list is not a key, but can be used as valueSalary = {'Max': 1001,'Rachel': 20001,"Jim111": 14000}Print(salary['Max']) salary['Max'] = 2000Print(salary['Max'])Print(Salary.pop ('Jim','No Jim'))Print(Salary.get ('Rachel',-1))Print(Salary) value= []salary['Max'] =valuePrint(Salary)#4set-only key, no Value,set,dict key does not allow mutable objects, such as list, can only be string or integer these invariant objectsSet1 = Set ([All]) Set2= Set ([2, 3,'a']) Set1.add (4) Set1.remove (2) s=set (['SDF', 3])Print(Set1)Print(set1&set2,set1|Set2)Print(Set2)#Several functions sort replacedeff (x):return-Ord (x) A1= ['C','b','a']A2='SDF'b=a2.replace ('s','a') A1.sort (Key=f, reverse=False)Print("A is:"A"\na2 is:", A2,"\NB is", b)

Several data types in Python list, Tuple,dict,set use demo

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.