About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python The basic operation of tuples .
Tuples (tuple): Like a list , The difference is that elements of tuples cannot be modified , Tuples contain elements in parentheses , The list contains elements in square brackets .
Just add the element in parentheses , And use commas to separate elements , Parentheses can also be omitted , Use commas to separate elements directly . (1) If you want to define an empty tuple , Can be represented by parentheses that do not contain content . (2)Python Only 1 Tuples of elements , You must add a comma after the element “,”, for example .
x = ('a', 'b', 1, 2, 3) # Creating a tuple x
print(x)
y = 'a', 'b', 'c', 'd' # Creating a tuple y The parentheses are omitted
print(y)
x = () # Create an empty tuple x
print(x)
x = (1,) # Create a tuple with only one element x
print(x)
give the result as follows .
Like a list , You can use a subscript index to access values in tuples .
x = ('a', 1, 3.14) # Creating a tuple x
print(x[0]) # Output tuple index is 0 The elements of
print(x[1]) # Output tuple index is 1 The elements of
print(x[2]) # Output tuple index is 2 The elements of
give the result as follows .
Element values in tuples are not allowed to be modified , But we can join tuples .
x = (1, 2, 3) # Creating a tuple x
y = ('a', 'b') # Creating a tuple y
z = x + y # Set tuple x and y Assign to after connection combination z
print(z)
give the result as follows .
Same as list , You can also use for Loop to traverse all elements in a tuple .
x = (1, 2, 3, 4, 5) # Creating a tuple x
for n in x: # loop
print(n, ' ', end="") # Output elements
give the result as follows .
Python The tuple built-in functions provided are len()、max()、min() and tuple(). tuple() The function takes a sequence as an argument , And convert it to tuples , If the parameter itself is a tuple , The parameter is returned as is .
a = tuple([1, 2, 3]) # Convert list to tuple
b = tuple('abc') # Convert a string to a tuple
c = tuple((1, 2, 3)) # Parameters are tuples
print(a)
print(b)
print(c)
give the result as follows .
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
The above is about Python The basic operation of tuples , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .
Catalog python Count the two d