SoFunction
Updated on 2024-10-28

Python3 common function range usage details

0X01 Function Description:

The python range() function creates a list of integers, typically used in a for loop.

0X02 function syntax:

range(start,stop[,step])

  • start: The count starts at start. The default is 0. For example, range(5) is equivalent to range(0, 5).
  • stop: count up to but not including stop. e.g. range(0, 5) is [0, 1, 2, 3, 4] without 5
  • step: step size, default is 1. e.g. range(0, 5) is equivalent to range(0, 5, 1)

Examples:

# Set a sequence of integers starting at 0 and going up to 10
range1 = range(10)
for range1 in range1:
  print("listingsrange(10)Element Output:",range1)
print("---------------------------------------------")
# Setting a sequence of 0 to 30 with a step size of 5
range2 = range(0,30,5)
for range2 in range2:
  print("listingsrange(0,30,5)Element Output:",range2)
print("---------------------------------------------")
#Ranges are used in conjunction with for loops.
new_str = "I am a genius"
for a in range(len(new_str)):
  print("Letters in a string:",new_str[a])

Run results:

C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\ C:/Users/aaron/.PyCharmCE2019.3/config/scratches/
listingsrange(10)Element Output: 0
listingsrange(10)Element Output: 1
listingsrange(10)Element Output: 2
listingsrange(10)Element Output: 3
listingsrange(10)Element Output: 4
listingsrange(10)Element Output: 5
listingsrange(10)Element Output: 6
listingsrange(10)Element Output: 7
listingsrange(10)Element Output: 8
listingsrange(10)Element Output: 9
---------------------------------------------
listingsrange(0,30,5)Element Output: 0
listingsrange(0,30,5)Element Output: 5
listingsrange(0,30,5)Element Output: 10
listingsrange(0,30,5)Element Output: 15
listingsrange(0,30,5)Element Output: 20
listingsrange(0,30,5)Element Output: 25
---------------------------------------------
A letter in a string: I
A letter in a string: 
A letter in a string: a
A letter in a string: m
A letter in a string: 
A letter in a string: a
A letter in a string: 
A letter in a string: g
A letter in a string: e
A letter in a string: n
A letter in a string: i
A letter in a string: u
A letter in a string: s

Process finished with exit code 0

summarize

The above is a small introduction to the Python3 common function range() usage, I hope to help you!