current position:Home>Python notes (VII): definition and use of tuples

Python notes (VII): definition and use of tuples

2022-01-30 10:41:22 A bowl week

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Hello everyone , I am a A bowl week , One doesn't want to be drunk ( Internal volume ) The front end of the . If you are lucky enough to get your favor , I'm very lucky ~

Write it at the front

stay Python Metagroup is a data type very similar to a list , The difference is that the elements in the list can be modified , and Elements in tuples cannot be modified .

Defining and using tuples

Definition

Defining elements usually uses parentheses () Literal grammar , The sample code is as follows :

tuple1 = ("Hello", " A bowl week ", " Hello ")

print(type(tuple1))  #  View tuple types   <class 'tuple'>
print(tuple1)  # ('Hello', ' A bowl week ', ' Hello ')
 Copy code 

Use

Tuples support the same operators and use the same methods as lists , The sample code is as follows :

tuple1 = ("Hello", " A bowl week ", " Nuggets ", " developer ", "strive")
tuple2 = (10, 20, 60, 34)

#  Splicing 
tuple3 = tuple1 + tuple2
print(tuple3)  # ('Hello', ' A bowl week ', ' Nuggets ', ' developer ', 'strive', 10, 20, 60, 34)
print(tuple2 * 2)  # (10, 20, 60, 34, 10, 20, 60, 34)

#  Index and slice 
print(tuple1[1], tuple1[-4])  #  A bowl week   A bowl week 
print(tuple3[:2])  # ('Hello', ' A bowl week ')
print(tuple3[::2])  # ('Hello', ' Nuggets ', 'strive', 20, 34)

#  Traversal operation 
for ch in tuple1:
    print(ch)
'''
Hello
 A bowl week 
 Nuggets 
 developer 
strive
'''

#  Members of the operation 
print(" A bowl week " in tuple1)  # True
print(" Nuggets " not in tuple1)  # False
 Copy code 

An empty () Represents an empty tuple , There are two elements in a tuple , Call it a binary , Five are called quintuples , But if it's ('hello') It's not a tuple , This bracket becomes a parenthesis for changing priority ; If you want to turn it into parentheses , Just add a comma ,, Otherwise, it doesn't represent a tuple . The sample code is as follows :

a = ()
print(type(a))    # <class 'tuple'>
b = (' A bowl week ')
print(type(b))    # <class 'str'>
c = (100)
print(type(c))    # <class 'int'>
d = (' A bowl week ', )
print(type(d))    # <class 'tuple'>
e = (100, )
print(type(e))    # <class 'tuple'>
 Copy code 

Tuple application scenario

Packing and unpacking

When assigning multiple comma separated values to a variable , Multiple values are packaged into a tuple type ; When assigning a tuple to multiple variables , Tuples will be unpacked into multiple values and then assigned to the corresponding variables , The sample code is as follows :

t = "Hello", " A bowl week ", " Nuggets ", " developer ", "strive"
print(type(t))  # <class 'tuple'>
print(t)  # ('Hello', ' A bowl week ', ' Nuggets ', ' developer ', 'strive')

x, y, z, i, j = t
print(x, y, z, i, j)  # Hello  A bowl week   Nuggets   developer  strive
 Copy code 

If when unpacking , The number of elements is inconsistent with the number of variables , It causes an error , Here's the code

t= ("Hello", " A bowl week ", " Nuggets ", " developer ", "strive")

# x, y, z = t # ValueError: too many values to unpack

x, y, z, i, j, k, l = t # ValueError: not enough values to unpack
 Copy code 

ValueError: too many values to unpack Insufficient unpacking value

ValueError: not enough values to unpack Too many unpacked values

The way to solve the error that variables are less than the number of tuples is to use * wildcard , With this wildcard, a variable can receive multiple values , Change a into a list , This wildcard can only appear once , The sample code is as follows :

t = ("Hello", " A bowl week ", " Nuggets ", " developer ", "strive")

x, y, *z = t
print(x, y, z)  # Hello  A bowl week  [' Nuggets ', ' developer ', 'strive']

x, *y, z = t
print(x, y, z)  # Hello [' A bowl week ', ' Nuggets ', ' developer '] strive

*x, y, z = t
print(x, y, z)  # ['Hello', ' A bowl week ', ' Nuggets ']  developer  strive
 Copy code 

Exchange the values of two variables

stay Python in , Exchange two variables a and b You only need to use the code shown below

a, b = b, a
 Copy code 

Empathy , If you want to put three variables abc The value of the swap , namely b Assign to a,c Assign to b,a Assign to c, You can do the same .

a, b, c = b, c, a
 Copy code 

Let the function return multiple values

If return There are two values in the statement , These two values will be assembled into a binary and then returned . So calling the defined function will get this binary , You can also assign two values in a binary to two variables through unpacking syntax .

copyright notice
author[A bowl week],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301041202436.html

Random recommended