Basics of Python (2) Tuples, more suitable for novices! ! !


Preface

Tuple is another important sequence structure in Python. It has many similarities with list . But tuples are immutable sequences. Learning tuples can be combined with the learning experience of lists, which is simple and easy to use! ! !


1. The creation and deletion of Python tuples

1. Tuple creation

Like a list, when creating a tuple, you can directly use "=" to assign a tuple to a variable:

tuplename=(element 1,element 2,element 3,…,element n) #Assignment operator creates tuple

Create an empty tuple:

emptytuple=( )

Create a numeric tuple:

tuple(data)

If you don’t understand, please study with the list: please click on me

2. Deletion of tuples

from the tuplename

2. Access and modification of tuples

1. Access tuple elements

In Python, if you want to output the contents of the tuple is very simple, just use the print() function directly
Insert picture description here

operation result:
Insert picture description here

2. Modify tuple elements

Because a tuple is an immutable sequence, its individual elements cannot be modified, but the tuple can be reassigned:

Insert picture description here

Running result:
Insert picture description here
you can also connect and combine tuples:
Insert picture description here
running result:
Insert picture description here
tuples can actually traverse its elements, the method is similar to the list.

to sum up

After learning the list, do you feel that tuples are really simple? I hope this article can help friends who see them learn tuples <( ̄︶ ̄)↗[GO!]
Later, we will introduce sequence slices, lists, and tuples Derivatives etc.

Guess you like

Origin blog.csdn.net/My_daily_life/article/details/108817440