554/how-can-i-use-python-s-range-function
You can use a range function whenever you want to run a piece of code more than once. You can use "for" loop. Look at the below example:
fruits = ['Banana', 'Apple', 'Grapes'] for index in range(len(fruits)): print (fruits[index])
Notice here, we have specified the range, that means we know the number of times the code block will be executed.
Output:
Banana Apple Grapes
The range function is mostly used in for-loop.
Ex:
for i in range(0,5): print(i)
The output will be:
0 1 2 3 4
Note: When you use the range() function, the 2nd parameter is excluded in the range.
range(x) returns a list of numbers from 0 to x - 1.
>>> range(1) [0] >>> range(2) [0, 1] >>> range(3) [0, 1, 2] >>> range(4) [0, 1, 2, 3]
for i in range(x): executes the body (which is print i in your first example) once for each element in the list returned by range(). i is used inside the body to refer to the “current” item of the list. In that case, i refers to an integer, but it could be of any type, depending on the objet on which you loop.
>>> range(1,10) [1, 2, 3, 4, 5, 6, 7, 8, 9]
# You can use range() wherever you would use a list. a = range(1, 10) for i in a: print i for a in range(21,-1,-2): print a #output>> 21 19 17 15 13 11 9 7 5 3 1
# We can use any size of step (here 2) >>> range(0,20,2) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> range(20,0,-2) [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
# The sequence will start at 0 by default. #If we only give one number for a range this replaces the end of range value. >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# If we give floats these will first be reduced to integers. >>> range(-3.5,9.8) [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
print(pow(3,4)) #this will return the exponentiation of 3 ...READ MORE
If there is list =[1,2,4,6,5] then use ...READ MORE
You can use sleep as below. import time print(" ...READ MORE
I'm wondering whether you meant "recursive". Here ...READ MORE
For sake of simplicity, maybe you should ...READ MORE
suppose you have a string with a ...READ MORE
You can also use the random library's ...READ MORE
Syntax : list. count(value) Code: colors = ['red', 'green', ...READ MORE
Enumerate() method adds a counter to an ...READ MORE
yes, you can use "os.rename" for that. ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.