How to understand a list of numbers in python

The number list is similar to other lists, but there are some functions that can make the operation of the number list more efficient. Let's create a list of 10 numbers and see what can be done.

# Print out the first ten numbers.
numbers =[1,2,3,4,5,6,7,8,9,10]for number in numbers:print(number)

range() function

The normal list creation method can create 10 numbers, but if you want to create a large number of numbers, this method is not suitable. The range() function helps us generate a large number of numbers. As follows:

# print the first ten number
for number inrange(1,11):print(number)

The parameters of the range() function include the start number and the end number. The resulting list of numbers contains the start number but not the end number. At the same time, you can also add a step parameter to tell the range() function how far the interval is. As follows:

# Print the first ten odd numbers.for number inrange(1,21,2):print(number)

If you want to convert the numbers obtained by the range() function to a list, you can use the list() function to convert. As follows:

# create a list of the first ten numbers.
numbers =list(range(1,11))print(numbers)

This method is quite powerful. Now we can create a list of the first million numbers, just as easy as creating a list of the first 10 numbers. As follows:

# Store the first million numbers in a list
numbers =list(range(1,1000001))
# Show the length of the list
print("The list 'numbers' has "+str(len(numbers))+" numbers in it.")
# Show the last ten numbers.print("\nThe last ten numbers in the list are:")for number in numbers[-10:]:print(number)

min(), max() and sum() functions

As the title indicates, you can apply these three functions to a list of numbers. The min() function finds the minimum value in the list, the max() function finds the maximum value, and the sum() function calculates the sum of all numbers in the list. As follows:

ages =[23,16,14,28,19,11,38]
youngest =min(ages)
oldest =max(ages)
total_years =sum(ages)print("Our youngest reader is "+str(youngest)+" years old.")print("Our oldest reader is "+str(oldest)+" years old.")print("Together, we have "+str(total_years)+" years worth of life experience.")

Knowledge point supplement:

range() function

In python, you can use the range() function to generate a series of numbers

for w inrange(1,11):print(w)

Output:

12345678910

# Note: here it ends at 10, excluding 11

So far, this article on how to understand the number list in python is introduced. For more detailed explanations of the number list in python, please search ZaLou.Cn

Recommended Posts

How to understand a list of numbers in python
How to understand the introduction of packages in Python
How to filter numbers in python
How to understand variables in Python
How to find the area of a circle in python
How to understand global variables in Python
How to sort a dictionary in python
How to write a confession program in python
How to understand python objects
Example of how to automatically download pictures in python
How to create a Python virtual environment in Ubuntu 14.04
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
How to verify successful installation of python
How to make a globe with Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to view installed modules in python
How to open python in different systems
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to program based on interfaces in Python
How to install python in ubuntu server environment
An article to understand the yield in Python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to perform continuous multiplication calculation in python
How to create a CentOS virtual machine in VMware
How does python call the key of a dictionary
How to create a CentOS virtual machine in VMware
How to save IE as an attachment in python
Example of how to modify ip address in Ubuntu20.04
Python implements FTP to upload files in a loop
How to comment python code
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
How to learn python quickly
How to uninstall python plugin
How to use python tuples
Python | An article to understand Python list, tuple and string operations
A large inventory of commonly used third-party libraries in Python
Do you still know how to draw cakes in python? ? ?
The usage of wheel in python