The List and tuple of the basic knowledge of programming can be lazy to a certain extent~


Previously, we learned several basic data types: string, integer and floating point,

Now we learn two types of tuple and tuple.

1, List

1. What is a List

List is a data type built into Python. It is an ordered collection, in which the elements can be added and deleted at any time

So why List?

Let's use an example to illustrate.

Now a team wants to go out and sign up first. If we use the knowledge we have learned before, we use a string variable to put them
All recorded.

But it's too troublesome and not beautiful.

In programming, we must learn to be lazy and avoid "repetitive work". If there are 100 members, you can copy and paste them in time
I'm tired of writing you.

At this point, you can use the list.

In this way, one line of code can store more than N names.

2. How to create a List

From the above example, we can see that the format of the list is like this.
In fact, a list is the data enclosed in brackets [], and each data in it is called an element. Use commas between each element

Moreover, the data elements of the list are not necessarily the same data type.

For example:

list1=['Two point water','twowter','liangdianshui',123]

There are string types and integer types.

We try to print it out and see what the print result is.

3. How to access the values in the List

Like the example at the beginning, sometimes we don't need to print out the names of all the staff. Sometimes we need to know the third registration
Who are you? Who are the top two applicants?

So how do you get it out of the list?

In other words, how to access the values in the list?

At this time, we can access the values in the list through the subscript index of the list. Similarly, you can intercept characters in the form of square brackets.

For example:

name = ['bit of water', 'Two point water', 'Three point water', 'Four water', 'Five water']

# Access the list print(name[2]) through the index 
# Intercept the data in the list in the form of square brackets 

It can be seen that we need to know who is the third person in the name list? Just use name[2].

Here you ask, why is it 2, not 3?

This is because in the programming world, we all start from 0, not from 1 in our living habits.

So you need to know who the third one is?

That's name[2].

From the example, we also printed the result of name[0:2].

From the printing results, only the contents of the first and second elements are printed.

There may be questions here?

Why not print the first three? Doesn't that mean 2 is the third?

That's because it's a left closed right open interval.

So name[0:2] means to start from 0 and get to 2, but not including 2.

Still that sentence, in order to better understand, you can try more and play more programming.

So you can try the following ways:
According to the output results and the knowledge mentioned above, it is easy to understand some usage.

## 4,How to update List((list) ##

As an example at the beginning, we recorded the name of the applicant in code. There may be new people joining or finding a new one
I began to write the wrong name and want to modify it.

What should we do at this time?

At this time, you can modify or update the data items of the list through the index, or you can use the append() method to add the list items.

name = ['bit of water', 'Two point water', 'Three point water', 'Four water', 'Five water'] 

# Modify or update the data items of the list through the index 
name[1]='2 Water' 

# Use the append() method to add a list item 
name.append('Six water') 

5. How to delete elements in a List

In that case, someone will quit halfway.

Then we need to remove his name from the list.

In this case, the del statement is used to delete the elements of the list
Look at the output. There is no data of four points of water in the list. Prove that it has been deleted successfully.

6. List operator

The operators of list pairs + and * are similar to strings+ Number is used for combined list and * number is used for repeated list.

7. List function & method

8. Examples

Finally, an example is given to get familiar with the operation of List


#-----------------------Use of list---------------------------------- 

# 1. For a product, you need to list the users of the product. At this time, you can use a list to represent it 
user=['liangdianshui','twowater','Two point water'] 
print('1.Product users') 

# 2. If you need to count the number of users, this time 
len() Function can get list Number of elements in len(user) 
print('\n2.Count how many users there are') 

# 3. At this time, what if you need to know the specific user? You can use the index to access the elements at every position in the list, and the index starts from 0 
print('\n3.View specific users') 

# 4. When a new user comes suddenly, we need to add a user at the end of the original list 
user.append('Yin Yin') 
print('\n4.Add new user at the end') 

# 5. Another user is added, but this user is a VIP student and needs to be placed first. You can insert it to the specified location through the insert method 
# Note: when inserting data, pay attention to whether it is out of bounds. The index cannot exceed len(user)-1 
user.insert(0,'VIP user') 
print('\n5.Add user at specified location') 

# 6. It is suddenly found that "Yinyin" is a "VIP user", so "Yinyin" needs to be deleted; pop() deletes the element at the end of the list
print('\n6.Delete end user') 

# 7. After a period of time, the user "liangdianshui" did not play the product and deleted the account 
# Therefore, you need to delete the element at the specified position, and use the pop(i) method, where i is the index position 
print('\n7.Delete the specified location list element') 

# 8. The user "two water" wants to change his nickname 
user[2]='Three point water' 
print('\n8.Replace one element with another') 

# 9. It doesn't seem good enough to just save the user's nickname. It's best to put the account in it 
# The account here is of integer type, which is different from the string type of nickname, but the data type of elements in the list can be different 
# And the list element can also be another list 
newUser=[['VIP user',11111],['twowater',22222],['Three point water',33333]] 
print('\n9.Different element types list data') 

To learn more knowledge or answer questions, source code and tutorials, please click

2, Tuple (tuple)

1. What is a tuple

In the previous section, we just talked about a List with sequence table. Now we talk about another List with sequence table called tuple.

Tuple is very similar to List, but tuple cannot be modified once initialized. That is, tuples are immutable, so
What does immutability mean?

The immutability of tuple means that when you create a tuple, it cannot be changed, that is, it does not have append(),
insert() is a method, but it also has a method to obtain an index value, but it cannot assign a value.

So why tuple?

That's because tuple s are immutable, so the code is safer.

Therefore, it is suggested to use tuple instead of list.

2. How to create tuple s

Tuple creation is very simple. You only need to add elements in parentheses and separate them with commas.

tuple1=('Two point water','twowter','liangdianshui',123,456) 
tuple2='Two point water','twowter','liangdianshui',123,456

If you do not add a comma, you will not create a tuple, but the number 123.

This is because parentheses () can represent both tuple s and parentheses in mathematical formulas, which leads to ambiguity.

So if you don't add a comma when there is only one element, the computer can't recognize whether you want to perform integer or decimal operation or representation

Therefore, Python stipulates that in this case, if you calculate according to the parentheses, the calculation result will naturally be 123, and if you want to represent the value of tuples
When, you need to add a comma.

See the output values of tuple4 and tuple5 in the figure below

3. How to access tuple s

The index of tuple index also starts from 0. Tuple can use index index to access the value in tuple.
4. Modify tuple

When you see this subtitle, some people may wonder, didn't it take a long paragraph to say that tuple is immutable?

Here's how to modify tuple again.

That's because the element values in tuples are not allowed to be modified, but we can connect and combine tuples and modify other lists
Value, which affects the value of the tuple.

See the following example for details:

As you can see, tuple1 has four elements, the last element is a List, and there are two elements in the List.

When we change the two elements 124 and 456 in the List to 789 and 100, it is calculated from the output value of tuple1
Look, it does seem to have changed.

But it is not the tuple element that changes, but the list element.

The list pointed to by tuple at the beginning has not been changed to another list. Therefore, the so-called "invariance" of tuple means that each element of tuple points to
Never change. Note that the fourth element in tupe1 still points to the original List, which has not changed. We only modify the List
The elements inside.

5. Delete tuple

Element values in tuple tuples are not allowed to be deleted, but we can use del statement to delete the whole tuple

6. tuple operator

Like strings, tuples can be operated with + and * signs. This means that they can combine and copy, and they will
Generate a new tuple.

8. Examples

Finally, just like the list, let's take an example. You can also try more to play all kinds of tuples.

name1 = ('bit of water', 'Two point water', 'Three point water', 'Four water', 'Five water') 
name2 = ('1 Water', '2 Water', '3 Water', '4 Water', '5 Water') 
list1 = [1, 2, 3, 4, 5] 
# Calculate the number of elements 
# Join, add two tuples 
print(name1 + name2) 
# Duplicate tuple 
print(name1 * 2) 
# Whether the element exists (whether the tuple name1 contains a little water) 
print('bit of water' in name1) 
# Maximum value of element 
# Minimum value of element 
# Convert list to tuple 

The output results are as follows:

Keywords: Python data structure list

Added by on Mon, 14 Feb 2022 17:12:19 +0200