Python Basics - #3 action list

#3 operation list

Traversal list for

Python's for loop syntax structure for a in b: #a is an element in list b. (don't forget the colon)

For loop execution process: first take the first value in b, store it in a, and then execute the code in the for loop; Since there are other values in b, continue to execute for until the values in b are traversed once.

countries=['china','korean','british','american','australian']
for country in countries:
	print(country)

Operation results:

china
korean
british
american
australian

It can also be used with the contents of Chapter 1

print("\n\n\n")
countries=['china','korean','british','american','australian']
for country in countries:
	print(country.title()+" is a country!")

Operation results:

China is a country!
Korean is a country!
British is a country!
American is a country!
Australian is a country!

A for loop can contain multiple statements

Python does not use {} to limit the loop body of the for loop, but determines the inside and outside of the loop through different alignment

For instance

print("\n\n\n")
countries=['china','korean','british','american','australian']
for country in countries:
	print(country.title()+" is a country!")
	print(country.upper()+" is a name of a country!")
print("That's all thank you!"+country)

Operation results:

China is a country!
CHINA is a name of a country!
Korean is a country!
KOREAN is a name of a country!
British is a country!
BRITISH is a name of a country!
American is a country!
AMERICAN is a name of a country!
Australian is a country!
AUSTRALIAN is a name of a country!
That's all thank you!australian

The last sentence is not indented, so it is executed only once, and the last australian, do you know why?

Because the last assignment of country is australian when the for loop is executed, it will be output naturally in the next output.

At the same time, we also found that indentation is very important in Python programs. Indentation errors may not lead to syntax errors, but will lead to program logic errors.

So far, we have learned the syntax rules. Only the loop body statements of the for loop need to be indented. What other statements need to be indented later? Let's wait and see!

Create a list of numbers

range()

for value in range(1,5):
	print(value)

Result:

1
2
3
4

The number 5 does not appear, so it is speculated that the working principle of range() is 1 < = x < 5

`list()

Using list() you can convert the value of range() directly to a list

numbers=list(range(1,5))
print(numbers)

Result:

[1, 2, 3, 4]

The range() function can also specify the step size

print("\n\n\n")
for value in range(1,7,2):
	print(value)

Result:

1
3
5

By expanding your imagination, range can create any number set you need

Square of 1-10

squares=[]
for value in range(1,11):
	square = value**2
	squares.append(square)
print(squares)

Result:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

The above code can also be simplified

squares=[]
for value in range(1,11):
	squares.append(value**2)
print(squares)

List of Statistics

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
min_squares=min(squares)
max_squares=max(squares)
sum_squares=sum(squares)
print(min_squares)
print(max_squares)
print(sum_squares)

Result:

1
100
385

List parsing

Simplify the above code

squares=[value**2 for value in range(1,11)]
print(squares)

Result:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Use part of the list

section

List name [start index, end index + 1]

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares[0:3])

Result:

[1, 4, 9]

If the colon is preceded by the specified start index, it starts from the list header by default;

If the termination index is not specified after the colon, it defaults to the end of the table.

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares[:4])
print(squares[2:])

Result:

[1, 4, 9, 16]
[9, 16, 25, 36, 49, 64, 81, 100]

-2 starts from the penultimate position

print(squares[-2:])

Result:

[81, 100]

Each slice is equivalent to a sublist, which can also be traversed through the for loop

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
for square in squares[3:8]:
	print(square)
print("That's end of the FOR")

Result:

16
25
36
49
64
That's end of the FOR

Copy list

Leave the start and end indexes of the slice blank [:]

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
s_squares=squares[:]
print(s_squares)		

Result:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Tuple (immutable list)

Tuples are distinguished from lists by parentheses

dimensions=(200,50,40,10)
print(dimensions[0])
print(dimensions[-1])

Result:

200
10

An error will be reported if you try to modify the value of the tuple

Traversal tuple

for dimension in dimensions:
	print(dimension)

Result:

200
50
40
10

Modify tuple variable

Tuple purpose, either unchanged or completely changed

The element of the tuple cannot be modified, but the variable storing the tuple can be reassigned.

dimensions=(200,50,40,10)
for dimension in dimensions:
	print(dimension)

print("\n")
dimensions=(40,2,86,3)
for dimension in dimensions:
	print(dimension)

Result:

200
50
40
10

40
2
86
3

Keywords: Python

Added by Sulman on Wed, 19 Jan 2022 19:38:41 +0200