current position:Home>Initial Python tuple

Initial Python tuple

2022-01-30 15:32:01 Solitary cold

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

introduction —— stay Python in , Save important data information in the project through data structure .Python The language has built-in multiple data structures , For example, a list of , Tuples , Dictionaries and collections, etc . In this class, let's talk about Python A big data structure that is of great importance in —— Tuples .

stay Python in , We can think of tuples as a special kind of list . The only difference between it and the list is : Data elements in tuples cannot be changed 【 This one doesn't change —— Not only can you not change the data items , And you can't add or delete data items !】. When we need to create a set of immutable data , Usually put this data into tuples ~

1. Of a tuple establish && visit

(1) Tuple creation :

stay Python in , The basic form of creating tuples is in parentheses “()” Enclose data elements , Use commas between the elements “,” separate . as follows :

tuple1 = ('xiaoming', 'xiaohong', 18, 21)
tuple2 = (1, 2, 3, 4, 5)

#  and —— Yes, you can create empty tuples !
tuple3 = ()

#  Little attention —— If the tuple you create contains only one element , And don't forget to put a comma after the element . Let it be recognized as a tuple :
tuple4 = (22, )
 Copy code 

(2) visit :

Tuples are similar to strings and lists , The indexes are all from 0 Start , And can be intercepted and combined . as follows :

tuple1 = ('xiaoming', 'xiaohong', 18, 21)
tuple2 = (1, 2, 3, 4, 5)

#  The index in the display tuple is 1 The value of the element 
print("tuple1[1]:", tuple1[0])

#  Displays the index from the tuple 1 To 3 The value of the element 
print("tuple2[1:3]:", tuple2[1:3])
 Copy code 

 Insert picture description here

2. Of a tuple modify && Delete

(1) Modification of tuples :

Although it is said at the beginning that tuples are immutable , But it still has a supported operation —— Join and combine tuples :

tuple1 = ('xiaoming', 'xiaohong', 18, 21)
tuple2 = (1, 2, 3, 4, 5)

tuple_new = tuple1 + tuple2
 Copy code 

 Insert picture description here

(1) Deletion of tuples :

Although tuples are immutable , But it can go through del Statement to delete the entire tuple . as follows :

tuple1 = ('xiaoming', 'xiaohong', 18, 21)

print(tuple1)		#  Normal print tuple1

del tuple1

print(tuple1)		#  Because it deleted tuple1, So printing again will report an error !
 Copy code 

 Insert picture description here

In The End!

 Please add a picture description

From now on , Hold the line , Make a little progress every day , Near future , You will thank you for your efforts !

The blogger will continue to update the basic crawler column and the actual crawler column , My friends who have read this article carefully , You can like it and comment on it . And can pay attention to this blogger , Read more about reptiles in the days to come !

If there are mistakes or inappropriate words, please point out in the comments section , thank you !
If you reprint this article, please contact me for my consent , And indicate the source and the name of the blogger , thank you !

copyright notice
author[Solitary cold],Please bring the original link to reprint, thank you.

Random recommended