current position:Home>Python foundation 2.4 -- tuple

Python foundation 2.4 -- tuple

2022-02-02 09:50:03 Liu Astro Boy

Catalog

1、 Creating a tuple

2、 Combination of tuples ( Use “ +” Connect )

3、 Look for the element ( Use subscripts to find )

4、 Count the number of an object in a tuple   count()

5、 Count the number of elements in tuple     len()

6、 Tuples and lists are converted to each other     Tuple conversion list list()    List to tuple tuple()


    Let's briefly introduce tuples , Tuples are similar to lists , But it's an immutable sequence , Immutability here includes that tuple objects cannot be incremented 、 Transform element position 、 Modifying elements 、 Remove elements , Tuples can store collections of various data types , A list that can be simply understood as non tabulable is called a tuple , Tuples in parentheses (  ()  ) Express , Between the elements (,) separate

1、 Creating a tuple

Think about a problem , stay () I think there is only one element in 1 , At this time, he is int type , But what should I do if I want him to become a tuple ?

Atom = (1)
print(type(Atom))   # Output is <class 'int'>
Atom1 = (1,)
print(type(Atom1))  # Output is  <class 'tuple'>
print(Atom1)       # Output is (1,)

The output is :<class 'int'>
                     <class 'tuple'>

Look at the code above , If we want to assign an element to a tuple variable , Add , Tuple is a special case that distinguishes tuple from other structural data types .

2、 Combination of tuples ( Use “ +” Connect )

Atom2 = (1,2,3,"test")    
Atom3 = ("Atom",Atom2)    #Atom3 For now :'Atom', (1, 2, 3, 'test')
print(Atom2+Atom3)     # Combine two tuples :(1, 2, 3, 'test', 'Atom', (1, 2, 3, 'test'))

The output is :

(1, 2, 3, 'test', 'Atom', (1, 2, 3, 'test'))

3、 Look for the element ( Use subscripts to find )

The way to find elements is the same as in the list , Can be found by taking the subscript , There's no more explanation here .

Atom4 = (1,2,3,"Atom")
print(Atom4[1])     # Output is :2

The output is :   2

4、 Count the number of an object in a tuple   count()

In statistical tuple , Elements 3 The number of

Atom5 = (1,2,3,4,3,3,2)
print(Atom5.count(3))   # There are several... In the statistical tuple 3, The output is 3

The output is :3

5、 Count the number of elements in tuple     len()

Atom6 = (1,2,3,"Atom",2,1)
print(len(Atom6))   # There is... In the tuple 6 Elements , The return result is  6

The output is :6

6、 Tuples and lists are converted to each other     Tuple conversion list list()    List to tuple tuple()

Atom7 = (1,2,3,"Atom")
Atom8 = list(Atom7)    # The tuple Atom7 Change to list Atom8
print(Atom8)           # The output is :[1, 2, 3, 'Atom']
print(type(Atom8))     # The output is :<class 'list'>
Atom9 = [1,2,3,"Atom"]
Atom10 = tuple(Atom9)  # The list Atom9 Convert to tuple Atom10
print(Atom10)          # The output is :(1, 2, 3, 'Atom')
print(type(Atom10))    # The output is :<class 'tuple'>

The output is :

[1, 2, 3, 'Atom']
<class 'list'>
(1, 2, 3, 'Atom')
<class 'tuple'>


   The official account is two-dimensional code. , The content is sent out synchronously , We can focus on learning together

copyright notice
author[Liu Astro Boy],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020950015566.html

Random recommended