How to use the range function, which is often used sensuously.
If you know it, the width will expand and it will be convenient.
■ Numerical values in the specified range can be calculated as one set.
Example: Returns 9 elements "0,1,2,3,4,5,6,7,8" from 3 information "initial value: 0, end of range: 9, change amount: 1" ..
range(x, y, z)
--Only ** 3 ** elements are used --"X": Initial value. Optional. Default 0 --"Y": End of range. ** Cannot be omitted **. -"Z": Amount of change. Optional. Default 1 --The end of the range is less than ** . --The specified number is not included. ――It means to end when the value is reached. --Elements are ** integers ** --You can also use minus --Float cannot be used --Elements can be ** "integers" and "variables" ** --At least one element ** --Specify only the end of the range. (In the above, the variable "z") - Output is range type ** --You can't see the contents just by executing the range function ** --If there is no data in the specified range, the output will be empty (no error will occur).
-Stop: An integer (or variable) that specifies the end of the range. -Start: An integer (or variable) that specifies the initial value. -Step: An integer (or variable) that specifies the amount of change.
If the amount of change is not specified, two numerical values, "initial value" and "end of range", are returned.
If the amount of change is also specified, three numerical values of "initial value", "end of range", and "amount of change" are returned.
** ▼ range type output result when only the end value is specified **
Specify only the end value (int)
range(10)
#Output result
# range(0, 10)
Specify only the end value (variable)
a = 10
range(a)
#Output result
# range(0, 10)
Also specify the initial value (int)
range(5,99)
#Output result
# range(5, 99)
Also specify the initial value and the amount of change (int)
range(5,99,11)
#Output result
# range(5,99,11)
Specify the initial value and the amount of change (variable)
a = 5
b = 99
c = 11
range(a,b,c)
#Output result
# range(5,99,11)
① Make a list ② Specify the sequence number ③ Take out with a for statement
list type (Example 1)
list(range(10))
#output
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list type (Example 2)
a = range(10)
list(a)
#output
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list type (Example 3)
a = 5
b = 99-10
c = 3*4
list(range(a,b,c))
#output
# [5, 17, 29, 41, 53, 65, 77]
Specify the sequence number
range(3,9)[4]
#output
# 7
The output of range (3,9)
is [3, 4, 5, 6, 7, 8]
The fourth (counting from 0) is 7.
Extract with a for statement (Example 1: One element)
for a in range(5):
print(a)
#output
0
1
2
3
4
Extract with a for statement (Example 2: Two elements)
for b in range(4,8):
print(b)
#output
4
5
6
7
Extract with a for statement (Example 3: 3 elements)
for c in range(9,30,7):
print(c)
#output
9
16
23
Extract with a for statement (Example 4: Variable)
A =range(9,30,7)
for a in A:
print(a)
#output
9
16
23
You can also use minus for the initial value, range, and amount of change. (Decrease by the specified number)
Initial value minus (Example 1)
list(range(-3, 2))
#output
# [-3, -2, -1, 0, 1]
Initial value / closing price minus (Example 2)
list(range(-12, -6))
#output
# [-12, -11, -10, -9, -8, -7]
"Initial value <End value" (when the amount of change is positive. Default "+1")
If there is no value in the range, the contents will be empty (** not an error **)
Change amount minus (Example 3)
a =range(5,1,-1)
list(a)
#output
# [5, 4, 3, 2]
Change amount minus-Specified by variable (Example 4)
a = 10
b = -4 * 4
c = -6
A = range(a,b,c)
list(A)
#output
# [10, 4, -2, -8, -14]
Positive range (Example 1)
list(range(10,5))
#output
# []
Negative range (Example 2)
list(range(-5, -10))
#output
# []
Change amount minus (Example 2)
list(range(-5, 10, -2))
#output
# []
Error: Decimal point (float)
range(1.25)
#output
# TypeError: 'float' object cannot be interpreted as an integer
Error: string (str)
range("AAA")
#output
# TypeError: 'str' object cannot be interpreted as an integer
Recommended Posts