Tuple, list, dict, and pythontuple in Python

Source: Internet
Author: User

Tuple, list, dict, and pythontuple in Python

------------------------------- Updating --------------------------------------

  • Tuple ):

Tuples are commonly represented by parentheses (). The elements are identified by commas.

1 # define a tuple 2 3 # tuple = 'A', 4 5 tuple = ('A', 'B', 'C', 'D', 'E ', 'F', 'G') 6 7 # In general, after defining a tuples, you cannot add or modify the elements of the tuples, however, you can add elements that modify the tuples to the tuples. 8 9 print tuple [] 10 11 tuple = tuple [: 2] + ('H') + temp [2:] 12 13 print (tuple) 14 15 # Use A for loop to traverse tuples 16 17 for each in tuple: 18 19 print each20 21 # use range () function and for loop get element serial number 22 23 for index in range (len (tuple): 24 25 print tuple [index]
  • List ):

List commonly used square brackets, that is, [];

To create a list, you only need to enclose different data items separated by commas (,) in square brackets.

For example:

1 list1 = ['a','b','c',1,3,5]2 list2 = [1,2,3,4,5,6]3 list3 = ["abc","bcd","cde"]

Traversal list: (len (each): indicates the length of each iteration variable, and each: indicates the variable of each iteration)

1 list1 = ['a','b','c',1,3,5]2 for each in list13     print(each,len(each))

Common functions in the list:

Cmp (list1, list2): Compares two list elements.

 

Len (list): returns the number of list elements.

 

Max (list): returns the maximum value of the list element.

 

Min (list): returns the minimum value of the list element.

 

List (tuple): Convert tuples to a list

 

The following nine methods are commonly used in the list:

List. append (obj): Add a new object to the end of the list.

  

List. count (obj): count the number of times an element appears in the list.

 

List. extend (list): Add another sequence containing multiple values at the end of the list, which can be used to expand the list.

  

List. insert (index, obj): insert an object before the index element in the list.

 

List. pop (obj = list [-1]): by default, one element in the list is removed (the last element by default) and the value of this element is returned.

  

List. remove (obj): removes a value from the list.

 

List. reverse (): sorts the elements in the list in reverse order.

 

List. sort (function (): sorts the list.

 

  • Dictionary (dict)

The dictionary contains data from curly braces {}. The curly braces contain keys and values. A pair of keys and values is an item, keys and values are separated by colons. items and items are separated by commas. An empty dictionary is a dictionary that does not contain any items, it can also be understood as an empty dictionary, which means that no content is contained in the curly braces and is directly represented by curly braces.

Create a dictionary:

Dict = {'name': 'john', 'age': 20, 'sex': male}

Note: The Key is an unchangeable data type.

Access dictionary:

Because the dictionary is unordered, you cannot access the dictionary by indexing. You can access the dictionary by using the variable name [key name.

Dictionary add item:

Variable name: [new key name] = value corresponding to the new key

 

Value of the dictionary modification item:

Variable name: [key name to be modified] = New Value

 

Delete a dictionary item or value:

Del method: Delete the value corresponding to the key. del variable name [key name];

Delete the dictionary and del variable name.

 

Clear method: clear the dictionary content.

Variable name. clear ()

 

Pop method: Delete the value corresponding to the key, but it will output the corresponding value and then delete it.

 

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.