[Python] Explains how to use the range function with a concrete example

[Python] Explains how to use the range function with a concrete example

How to use the range function, which is often used sensuously.

If you know it, the width will expand and it will be convenient.


**table of contents**
  1. [What you can do with the range function](# 1-What you can do with the range function)
  2. [Basic syntax of range function](# 2-Basic syntax of range function)
  3. [What is range type? ](What is # 3-range type)
  4. [How to check the contents of the execution result of the range function](# 4-Example of how to check the contents of the execution result of the range function)
  5. ① [Make it a list](Make it a #list)
  6. ② [Specify the sequence number](#Specify the sequence number)
  7. ③ [Retrieve with for statement](Extract with #for statement)
  8. [Use Minus](# 5-Use Minus)
  9. [If there is no element in the range](# 6-If there is no element in the range)
  10. [Error Occurrence Case](# 7-Error Occurrence Case)

## 1. What you can do with the range function

■ 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" ..


## 2. Basic syntax of range function

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).


### Classify by number of elements The following three descriptions can be used. ① `range (y)`: 1 element ② `range (x, y)`: 2 elements ③ `range (x, y, z)`: 3 elements
> ** ■ python official description ** [(URL)](https://docs.python.org/ja/3/library/functions.html#func-range) range(stop) range(start, stop[, step])

-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.


## 3. What is range type? The output result of the range function. You can check the instructions.

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)

** ▼ Range type output result when initial value and change amount are also specified **

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)

## 4. How to check the contents of the execution result of the range function (example) I don't know what kind of numerical value is stored in range type. How to check the contents quickly.

① Make a list ② Specify the sequence number ③ Take out with a for statement

① Make a list

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

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.


### ③ Take out with a for statement You can print the contents one by one by using the for statement and the print method.

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

5. Use minus

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]

### 6. If there are no elements in the range The data is just empty. No error occurs.

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
# []

7. Error occurrence case

Decimal point (float)

Error: Decimal point (float)


range(1.25)

#output
# TypeError: 'float' object cannot be interpreted as an integer

String (str)

Error: string (str)


range("AAA")

#output
# TypeError: 'str' object cannot be interpreted as an integer

[Return to top](Explains how to use the #pythonrange function with a concrete example)

Recommended Posts

[Python] Explains how to use the range function with a concrete example
[Python] Explains how to use the format function with an example
[Introduction to Python] How to iterate with the range function?
[Introduction to Python] How to write a character string with the format function
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
Python: How to use async with
How to use FTP with Python
How to use python zip function
How to use the __call__ method in a Python class
[Introduction to Python] How to get data with the listdir function
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
[python] How to use __command__, function explanation
[September 2020 version] Explains the procedure to use Gmail API with Python
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
[Introduction to Python] How to use the in operator in a for statement?
How to send a request to the DMM (FANZA) API with python
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
How to use the C library in Python
How to read a CSV file with Python 2/3
A simple example of how to use ArgumentParser
Specify the Python executable to use with virtualenv
[Introduction to Python] How to sort the contents of a list efficiently with list sort
The easiest way to use OpenCV with python
[Python] How to use the enumerate function (extract the index number and element)
[Algorithm x Python] How to use the list
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to create a submenu with the [Blender] plugin
How to use the Raspberry Pi relay module Python
[Python] How to specify the download location with youtube-dl
How to use python interactive mode with git bash
[Python] How to use the graph creation library Altair
Specify MinGW as the compiler to use with Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the model learned in Lobe in Python
[Python] How to rewrite the table style with python-pptx [python-pptx]
[Python] How to create a 2D histogram with Matplotlib
[Python] How to call a c function from python (ctypes)
[Python] How to draw a scatter plot with Matplotlib
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to call a function
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to identify the element with the smallest number of characters in a Python list?