In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python how to use the range () function, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Range () function for value in range (1mai 5): print (value)
This code is a number in the print range [1 ~ 5]. The running result is as follows:
1234
If you use range () and the output is not as expected, you can try to add or subtract 1 from the specified value.
Create a list
When you create a list of numbers, you can directly convert the result of the range () function to a list using list ():
Number = list (range (1Magol 6)) print (number)
The results are as follows:
[1, 2, 3, 4, 5]
When using the range () function, you can also specify the step size, such as printing an even number from 1 to 10:
Even_numbers = list (range (2mai 11pm 2)) print (even_numbers)
The results are as follows:
[2, 4, 6, 8, 10]
Using range (), you can create almost any set of numbers you need, such as the square of an integer from 1 to 10. (* * represents multiplicative operation)
Squares= [] for value in range (1mai 11): square = value**2 squares.append (square) print (squares)
First we create an empty list, and then we use the range () function to iterate over integers from 1 to 10. In the loop, we calculate the square of each integer, then store it in the variable square, and then add the value to the list squares. After the loop ends, we print the list squares:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
To make the code more concise, we can avoid using the temporary variable square:
Squares= [] for value in range (1mai 11): squares.append (value**2) print (squares)
The results are as follows:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] perform statistics on the list of numbers
There are several python functions that specialize in dealing with the list of numbers, and you can easily find the maximum, minimum, and sum of the list numbers:
List=list (range (1c11))
The list list is a list of numbers from 1 to 10.
Find the maximum value:
> > max (list) 10
Find the minimum value
> min (list) 1
Summation
> sum (list) 55 list parser
In the above example, we generate a squares list with 3 or 4 lines of code, while using list parsing, we only need one line of code to complete the whole operation, which is more concise.
Squares = [values**2 for value in range (1mai 11)] print (squares)
The running results are as follows:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.