DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

range in Python

Buy Me a Coffee

*Memos:

range() can create a sequence of numbers as shown below:
*Memos:

  • The 1st argument is start(Optional-Default:0-Type:int).
  • The 2nd argument is stop(Required-Type:int).
  • The 3rd argument is step(Optional-Default:1-Type:int).
  • start=, stop= and step= cannot be used.
print(range(4))
print(range(0, 4))
print(range(0, 4, 1))
# range(0, 4)

print(range(4).start, range(4).stop, range(4).step)
# 0 4 1

print(list(range(4)))
# [0, 1, 2, 3]

print(range(4)[0], range(4)[1], range(4)[2], range(4)[3])
# 0 1 2 3

print(list(range(-5, 12, 3)))
# [-5, -2, 1, 4, 7, 10]

print(list(range(12, -5, -3)))
# [12, 9, 6, 3, 0, -3]

for i in range(4):
for i in range(0, 4):
for i in range(0, 4, 1):
  print(i)
# 0
# 1
# 2
# 3

for i in range(-5, 12, 3):
    print(i)
# -5
# -2
# 1
# 4
# 7
# 10

for i in range(12, -5, -3):
    print(i)
# 12
# 9
# 6
# 3
# 0
# -3
Enter fullscreen mode Exit fullscreen mode
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]

for i in range(4):
for i in range(0, 4):
for i in range(0, 4, 1):
    print(fruits[i])
# Apple
# Orange
# Banana
# kiwi

for i in range(1, 6, 2):
    print(fruits[i])
# Orange
# Kiwi
# Mango

for i in range(5, 0, -2):
    print(fruits[i])
# Mango
# Kiwi
# Orange
Enter fullscreen mode Exit fullscreen mode
print(list(zip(range(4), range(-5, 12, 3), range(12, -5, -3))))
# [(0, -5, 12), (1, -2, 9), (2, 1, 6), (3, 4, 3)]

print(list(zip(range(4), range(-5, 12, 3), range(12, -5, -3)))[0])
# (0, -5, 12)

i, j, k = list(zip(range(4), range(-5, 12, 3), range(12, -5, -3)))[0]
print(i, j, k)
# 0 -5 12

for i, j, k in zip(range(4), range(-5, 12, 3), range(12, -5, -3)):
    print(i, j, k)
# 0 -5 12
# 1 -2 9
# 2 1 6
# 3 4 3
Enter fullscreen mode Exit fullscreen mode
enum = enumerate

print(list(enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7)))
# [(7, (0, -5, 12)), (8, (1, -2, 9)), (9, (2, 1, 6)), (10, (3, 4, 3))]

print(list(enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7))[0])
# (7, (0, -5, 12))

i, jkl = \
  list(enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7))[0]
print(i, jkl)
# 7 (0, -5, 12)

i, (j, k, l) = \
  list(enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7))[0]
print(i, j, k, l)
# 7 0 -5 12

for i, jkl in enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7):
    print(i, jkl)
# 7 (0, -5, 12)
# 8 (1, -2, 9)
# 9 (2, 1, 6)
# 10 (3, 4, 3)

for i, (j, k, l) \
  in enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7):
for i, [j, k, l] \
  in enum(zip(range(4), range(-5, 12, 3), range(12, -5, -3)), 7):
    print(i, j, k, l)
# 7 0 -5 12
# 8 1 -2 9
# 9 2 1 6
# 10 3 4 3
Enter fullscreen mode Exit fullscreen mode

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay