Handbook of python3.5 cultivation 7

Source: Internet
Author: User

Meta-group

Tuples differ from lists in that the list of tuples cannot be modified.

The method of creating tuples is relatively simple to use, separating some values and creating tuples.

For example:

A=1,2,3print (a)

C:\python\python.exe c:/python.py/yuanzu.py

(1, 2, 3)

The most common method is to use () to enclose the values.

For example:

b= (' Hello ', ' World ') print (b)

C:\python\python.exe c:/python.py/yuanzu.py

(' Hello ', ' world ')

Create an empty tuple (that is, no content is included in parentheses)

For example:

C= () print (c)

C:\python\python.exe c:/python.py/yuanzu.py

()

Create a tuple of values

D= (1,) print (d)

C:\python\python.exe c:/python.py/yuanzu.py

(1,)

You must add one to create a tuple of values (comma)


Tuple function

The function of the tuple function is basically the same as the list function, which takes a sequence as an argument and converts it to a tuple.

For example:

Print (Tuple ([' Du ', ' Yu ', ' Heng '))

C:\python\python.exe c:/python.py/yuanzu.py

(' du ', ' Yu ', ' Heng ')

If the parameter is a tuple, the parameters are returned as-is

For example:

Print (Tuple ((' Du ', ' Yu ', ' Heng '))

C:\python\python.exe c:/python.py/yuanzu.py

(' du ', ' Yu ', ' Heng ')


Basic operations for tuples

accessing tuples

You can access the values in the tuple using the subscript index

For example:

Mix = (' Hello ', ' workd ', 2016,2017) print (' Mix[1]is: ', mix[1])

C:\python\python.exe c:/python.py/yuanzu.py

Mix[1]is:workd

num= (1,2,3,4,5,6,7,8,9) print ("Num[1:5]is:", Num[1:5])

C:\python\python.exe c:/python.py/yuanzu.py

Num[1:5]is: (2, 3, 4, 5)


modifying tuples

element values in tuples are allowed to be modified, but they can be combined in groups of tuples

For example:

Name = (' du ', ' Yu ', ' heng ') num = (666,888) print ("Merge result:", Name+num)

C:\python\python.exe c:/python.py/yuanzu.py

The results of the merger were: (' du ', ' Yu ', ' Heng ', 666, 888)


Delete a tuple

element values in tuples are not allowed to be deleted, but you can use the DEL statement to delete an entire tuple

For example:

Name = (' du ', ' Yu ', ' Heng ') del nameprint (' result after deleting the element: ', name)

C:\python\python.exe c:/python.py/yuanzu.py

Traceback (most recent):

File "c:/python.py/yuanzu.py", line 8, <module>

Print (' result after deleting element: ', name)

Nameerror:name ' name ' is not defined

Error: Name is not defined, meaning name no longer exists.


Tuple index, intercept

Because tuples are also a sequence, you can access elements in a tuple at a specified location, or you can intercept an element in an index

For example:

Name = (' Duyuheng ', ' Xuwei ', ' Gaoshuang ') print (name[2]) print (name[-2]) print (name[1:])

C:\python\python.exe c:/python.py/yuanzu.py

Gaoshuang

Xuwei

(' Xuwei ', ' Gaoshuang ')


Tuple built-in functions

The following are the built-in functions for the more common elements in Python


The len (tuple) function is used to calculate the number of tuple elements

For example:

Tup = (' python ', ' shell ', ' Java ', ' C + + ') print (len (tup))

C:\python\python.exe c:/python.py/yuanzu.py

4


The max (tuple) function returns the maximum value of an element in a tuple

For example:

num = (4,2,1,3,5,8) print (' Remove the largest number of tuples in the tuple: ', max (num))

C:\python\python.exe c:/python.py/yuanzu.py

Remove the largest number of tuples: 8


The min (tuple) function returns the minimum value of an element in a tuple

For example:

num = (4,2,1,3,5,8) print (' Take out a small number of tuples: ', min (num))

C:\python\python.exe c:/python.py/yuanzu.py

To remove a tuple in small numbers: 1


The tuple (SEQ) function is used to convert a list to a tuple

For example:

Love = [' I ', ' loving ', ' you ', ' Beauty ', ' Li ', ' ', ' gu ', ' Niang ']mylove=tuple (AI) print (mylove)

C:\python\python.exe c:/python.py/yuanzu.py

(' I ', ' love ', ' You ', ' Beauty ', ' Li ', ' ', ' gu ', ' Niang ')


This article from "Duyuheng" blog, declined reprint!

Handbook of python3.5 cultivation 7

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.