Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction — In Python, data structures are used to store important data information in a project. The Python language has a variety of built-in data structures, such as lists, tuples, dictionaries, and collections. In this lesson we will look at one of the most important data structures in Python: tuples.

In Python, we can think of tuples as a special kind of list. The only difference between a list and a tuple is that the data elements in a tuple cannot be changed. . When we want to create an immutable set of data, we usually put the data into a tuple

1. Tuple creation && access

(1) Tuple creation:

In Python, the basic form of creating a tuple is to enclose data elements in parentheses “()”, separated by commas “, “. As follows:

tuple1 = ('xiaoming'.'xiaohong'.18.21)
tuple2 = (1.2.3.4.5)

You can create empty tuples.
tuple3 = ()

# Note - If you create a tuple that contains only one element, don't forget to put a comma after the element. Let it be identified as a tuple:
tuple4 = (22.)Copy the code

(2) Visit:

Tuples are similar to strings and lists in that indexes start at 0 and can be cut and combined. As follows:

tuple1 = ('xiaoming'.'xiaohong'.18.21)
tuple2 = (1.2.3.4.5)

Display the value of the element with index 1 in the tuple
print("tuple1[1]:", tuple1[0])

Display the values of the elements indexed from 1 to 3 in the tuple
print("tuple2[1:3]:", tuple2[1:3])
Copy the code

2. Tuple modification && deletion

(1) Modification of tuples:

Although tuples are said to be immutable at the beginning, there is one supported operation that concatenates tuples:

tuple1 = ('xiaoming'.'xiaohong'.18.21)
tuple2 = (1.2.3.4.5)

tuple_new = tuple1 + tuple2
print(tuple_new)
Copy the code

(1) Tuple deletion:

Although tuples are immutable, the entire tuple can be deleted with the DEL statement. As follows:

tuple1 = ('xiaoming'.'xiaohong'.18.21)

print(tuple1)		# Print tuple1 normally

del tuple1

print(tuple1)		# Tuple1 was deleted from the list.
Copy the code

🔆 In The End!

Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!

If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!