Python3 tuple

Python's tuples are similar to lists, except that the elements of the tuple cannot be modified.

Tuples use parentheses and lists use square brackets.

Tuple creation is simple, just add elements to the parentheses and separate them with commas.


Let's utilization a capacity to yield "Hi World!":

>>>def hello() : print("Hello World!") >>> hello() Hello World! >>>

Function call

Define a capacity: Give the capacity a name, indicate the parameters contained in the capacity, and the code square structure.

After the fundamental structure of this capacity is finished, you can execute it from another capacity call, or legitimately from the Python order brief.

The following model calls the printme() function:

Instance (Python 3.0+)

#!/usr/receptacle/python3 #definition function def printme( str ): # Print any approaching strings print (str) return # calling function printme("I need to call a client characterized function!") printme("call a similar capacity again")

Instance (Python 3.0+)

#!/usr/bin/python3 tup1 = (12, 34.56); tup2 = ('abc', 'xyz' ) # The following modifications to tuple element operations are illegal. # tup1[0] = 100 # Create a new tuple tup3 = tup1 + tup2; print (tup3)

The output of the above example:

(12, 34.56, 'abc',  'xyz')

Delete tuple

The element values in the tuple are not allowed to be deleted, but we can use the del statement to delete the entire tuple, as in the following example:

Instance(Python 3.0+)

#!/usr/bin/python3 tup = ('Google', 'Welookups', 1997, 2000) print (tup) del tup; print ("Deleted tuple tup : ") print (tup)

After the above instance tuple is deleted, the output variable will have exception information, the output is as follows:

Deleted tuple tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name 'tup' is not defined

Tuple operator

As with strings, tuples can be operated with + and * signs. This means that they can be combined and copied, and a new tuple is generated after the operation.

Python ExpressionResults Description
len((1, 2, 3))3 Calculate the number of elements
(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Connection< /td>
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!')Copy
3 in (1, 2, 3)TrueElements exist
for x in (1, 2, 3): print (x,)1 2 3iteration

tuple index, interception

Because the tuple is also a sequence, we can access the elements of the specified position in the tuple, or we can intercept an element in the index as follows:


L = ('Google', 'Javacodegeeks', 'Welookups')
Python ExpressionResults Description
L[2]'Welookups'Read the third element
L[-2]'Javacodegeeks'Reverse reading; reading the penultimate element
L[1:]('Javacodegeeks', 'Welookups') Intercept the elements, all elements from the second start.

Run the example as follows:

>>> L = ('Google', 'Javacodegeeks', 'Welookups')
>>> L[2]
>>> L[-2]
>>> L[1:]
('Javacodegeeks', 'Welookups')

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.