Python | Day 4 operation list

catalogue

1, Traversal list

2, Avoid indent errors

1. Forget indent

2. Forget to indent additional lines of code

3. Unnecessary indentation

4. Unnecessary indentation after loop

5. Missing colon

III. create a digital list

1. Use the function range()

3. Perform simple statistical calculations on the list of numbers

4. List parsing

IV. use part of the list

1. Slice

2. Traversal slice

3. Copy list

New list = original list [:]

5, Tuple

1. Define tuples

2. Traversal

3. Modify tuple variable

1, Traversal list

Use the for loop to traverse the list.

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print(fruit)

In this way, you can traverse the list:Of course, there can also be such operations:

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)

Perform other operations in the for loop

Or, like this:

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)
    print("What about you?\n")

2, Avoid indent errors

Python judges the relationship between the code line and the previous code line according to the indentation.

1. Forget indent

fruits=['apple','banana','orange','grape']
for fruit in fruits:
print('I have a '+fruit)        #Forget indent here

An error message will be generated:

Correct writing:

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)

2. Forget to indent additional lines of code

For example, I want this result:

However, my code is like this:

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)
print("What about you!\n")

The result of running the code is as follows:

This is caused by ignoring indenting additional lines of code. The correct writing method is:

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)
    print("What about you!\n")

3. Unnecessary indentation

Unnecessary indentation is also not allowed in Python.

print('Hello,Python!')
    print('Hello,world!')    #Indent error here

An error will be reported:

4. Unnecessary indentation after loop

The indentation should be removed from the next line of code at the end of the loop, otherwise the function of the code is still in the loop.

fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)
    print("What about you!")

print('*************************************')
fruits=['apple','banana','orange','grape']
for fruit in fruits:
    print('I have a '+fruit)
print("What about you!\n")

Obviously, as you wish:

5. Missing colon

The colon at the end of the for statement tells Python that the next line is the first line of the loop.

fruits=['apple','banana','orange','grape']
for fruit in fruits    #Colon missing here
    print('I have a '+fruit)
    print("What about you!")

Sure enough, I made a mistake

3, Create a list of numbers

1. Use the function range()

        

for i in range(1,4):
    print(i)

Operation:

Obviously, there are only 1, 2, 3 and no 4 in the output results. What's going on? The original function range() starts with a specified parameter, but stops after reaching the specified second parameter.

2. Use the range() function to create a list of numbers

The range() function can pass in three parameters: start, end and step.

numbers=list(range(1,11))    #1-10
print(numbers)

numbers=list(range(1,11,2))    #Odd number of 1-10
print(numbers)

#Square of 1-10
new_numbers=[]
numbers=list(range(1,11))
for number in numbers:
    new_number=number**2
    new_numbers.append(new_number)

print(new_numbers)

Operation:

3. Perform simple statistical calculations on the list of numbers

numbers=list(range(1,11))
print(numbers)    #Print
print(min(numbers))    #Print minimum
print(max(numbers))    #Print maximum
print(sum(numbers))    #Printing and

Operation:

4. List parsing

List name = [the expression is used to provide a value to the expression for the loop]

Look at a list:

numbers=[number**3 for number in range(1,6)]
print(numbers)

This code realizes the cubic calculation of 1, 2, 3, 4 and 5, stores the results in the list numbers, and finally prints numbers:

IV. use part of the list

1. Slice

List name [start index: end index: step]

If the start index is not specified, the slice list starts from the beginning; If the end index is not specified, the slice ends at the end of the list; If no step is specified, the default value is 1; Negative indexes can be used for indexes.

numbers=list(range(1,11))
print(numbers)
print(numbers[2:4]) #Index position 2-4, excluding 4
print(numbers[:5])  #Index position starts at - 5, excluding 5
print(numbers[5:])  #Index position 5 - end
print(numbers[-4:-1])   #The index position is from the penultimate to the penultimate, excluding the last
print(numbers[3:10:3])  #Index 3, 6, 9

Operation:

2. Traversal slice

You can traverse slices using the for loop.

numbers=list(range(1,11))
#print(numbers[3:10:3])  #Index 3, 6, 9
for i in numbers[3:10:3]:
    print(i)

Operation:

3. Copy list

New list = original list [:]

numbers=list(range(1,11))
print(numbers)
new_numbers=numbers[:]
print(new_numbers)

Copy succeeded:

5, Tuple

1. Define tuple

Tuples want lists very much, but tuples are identified by (), while lists are identified by [].

Lists can be modified, but tuples cannot.

numbers=(1,2,3)
print(numbers)

Operation:

Attempt to modify tuple:

numbers=(1,2,3)
numbers[0]=10
print(numbers)

An error is reported:

2. Traversal

This can be achieved using the for loop.

numbers=(1,2,3)
for i in numbers:
    print(i)

Operation:

3. Modify tuple variable

Although the element of the tuple cannot be modified, the variable storing the tuple can be assigned a value.  

numbers=(1,2,3)
print(numbers)
numbers=(4,5,6)
print(numbers)
numbers=(1,2,3,4,5,6)
print(numbers)

Operation:

If there are mistakes in the article, please don't hesitate to give advice!

Keywords: Python Programming list

Added by LukeO on Sat, 22 Jan 2022 20:55:29 +0200