Python list operation tour

One: I know how to create a list, and then understand the operation of the list elements. Next, let ’s understand traversing the entire list. For example, on a website, you may need to display each title in the article list; for lists that contain numbers, you may To perform the same statistical operation on each element, you can use the for loop in python.

    magicians.py
     1 magicians = ['alice', 'david', 'carolina']
	 2 for magician in magicians:
	 3         print(magician)

Look at the result:

    [email protected]:~/python$ python magicians.py 
	alice
	david
	carolina

You can include as many lines of code as you want in the for loop, but in for xxx in xxx: each line of code behind must be indented. Only the indented code is part of the loop body, and there is the first loop Don't forget to write a colon after the statement. Forgetting that the colon will prompt an error message when compiling and running.

Two:
Create a list of numbers. In data visualization, almost all the collections are composed of numbers (such as temperature, distance, number of people, longitude and latitude, etc.).
Lists are ideal for storing collections of numbers, and python provides many tools that can help you efficiently deal with lists of numbers, and understand how to use these tools effectively, even if the list contains millions of elements, the written code can run well.
1. Use the range () function to easily generate a series of numbers: for example:

 	numbers.py
   1 for value in range(1,5):
   2         print(value)

result:

[email protected]:~/python$ python number.py 
1
2
3
4

2. Use range () to create a list of numbers
To create a list of numbers, you can use the function list () to directly convert the result of range () to a list, using range () as the parameter of list (), and the output will be a list of numbers

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

The results of it:

[email protected]:~/python$ python number.py 
[1, 2, 3, 4, 5]

When using the function range (), you can also specify the step size, for example, the following code prints out even numbers within 1 ~ 10:

  1 even_numbers = list(range(2,11,2))
  2 print(even_numbers)

The results of it:

[email protected]:~/python$ python even_numbers.py 
[2, 4, 6, 8, 10]

In Python, two asterisks (**) represent the power operation, for example:

  1 squares = []  #create empty list
  2 for value in range(1,11):
  3         square = value**2    #calculate value
  4         squares.append(square)  
  5 print(squares)

The results of it:

[email protected]:~/python$ python squares.py 
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3. Perform simple statistical operations on number lists.
There are several python functions dedicated to dealing with number lists. For example, you can easily find the maximum, minimum, and sum of number lists.

	   7 digits = [1,2,3,4,5,6,7,8,9,0]
	   8 
       9 print(min(digits))
      10 
      11 print(max(digits))
      12 
      13  print(sum(digits))

Look at the result:

	[email protected]:~/python$ python squares.py 
	0
	9
	45

Simple operations are available with functions

Published 53 original articles · praised 16 · visits 2213

Guess you like

Origin blog.csdn.net/m0_37757533/article/details/105029162