Python Learning---Day 1

Source: Internet
Author: User

List and tuple

First, List

list, which refers to lists, an ordered set of elements that can be added and removed at any time. Such as:

>>>namelist=[' Michael ', ' Bob ', ' Tom ']

NameList is a list, and the index of the list is counted starting from 0, such as taking NameList first element:

>>>NAMELIST[0]

The Len () function can get the number of elements in the list, or you can use Len ()-1 to get the index value of the last element of the list, such as:

>>>len (NameList)

3

>>>len (NameList)-1

2

The first element of the countdown:

>>>NAMELIST[-1]

' Tom '

By analogy, you can get the penultimate, the third ... An element.

List is mutable and there are 3 ways to add elements to the list:

@append method is to append a new element to the end of the list. Can only be added one by one.

>>>namelist.append (' Jerry ')

>>>namelist

[' Michael ', ' Bob ', ' Tom ', ' Jerry ']

@extend method is to append multiple values from another list at the end of the list.

>>>namelist.extend ([' Katty ', ' Peter ', ' Tomas ')

>>>namelist

[' Michael ', ' Bob ', ' Tom ', ' Jerry ', ' Katty ', ' Peter ', ' Tomas ']

@insert method allows you to insert an element anywhere in the list.

>>>namelist.insert (1, ' Walker ')

>>>namelist

[' Michael ', ' Walker ', ' Bob ', ' Tom ', ' Jerry ', ' Katty ', ' Peter ', ' Tomas ']

The method of deleting an element in the list is pop,remove.

@pop method is simple, this method returns the value of the deleted element, which is to be remembered. In addition, no element index is added after the pop, which means that the last element is deleted directly, if the specified element needs to be deleted, the index value of the deleted element must be filled in ().

>>>namelist.pop ()

' Tomas '

>>>namelist.pop (1)

' Walker '

@remove method is to remove an element from the list.

>>>namelist.remove (' Katty ')

>>>namelist

[' Michael ', ' Bob ', ' Tom ', ' Jerry ', ' Peter ']

The element substitution for the list:

>>>namelist[1]= ' Sarah '

>>>namelist

[' Michael ', ' Sarah ', ' Tom ', ' Jerry ', ' Peter ']


Second, tuple

Tuple, referring to a tuple, similar to list, the method of obtaining an element is consistent with list, the biggest difference is that once a tuple is initialized, it cannot be changed, such as:

>>>namelist= (' Michael ', ' Bob ', ' Tom ')

Because the elements in the tuple can not be changed, so add, delete, replace operation can not be used, if you want to use, you need to replace the tuple to list, add, delete, change the operation is completed and then replace the list back to the tuple. Such as:

>>> a_tuple= (0,1,2,4,5,6,7,8,9)

>>> list_=list (a_tuple)

>>> List_.insert (3,3)

>>> A_tuple=tuple (List_)

>>> A_tuple

(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

The elements in a tuple can contain lists, so that the elements in the list can be changed.

In addition, only one element of the tuple definition is a comma must be added to eliminate the ambiguity of the mathematical formula parentheses, such as:

>>>t= (1,)

>>>t

(1,)







Python Learning---Day 1

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.