Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Range Function in Python | Loops in Python
Introduction to Python (dev copy)

bookRange Function in Python

If you're looking to loop through indices rather than the elements themselves, you'll want to get familiar with the range() function. range() produces a series of numbers and returns a range object. This function can accept 1, 2, or 3 positive number arguments.

When you provide just one argument, n, it returns all integers from 0 up to, but not including, n. For example, range(5) yields the numbers 0 through 4.

123
# Range with one argument for i in range(5): print(i, end = ' ')
copy

If you give the function two arguments, n and m, it returns all integers starting from n and going up to, but not including, m.

So, range(5, 10) will produce the numbers 5 through 9.

123
# Range with two arguments for i in range(5, 10): print(i, end = ' ')
copy

When you provide three arguments, n, m, and s, it returns integers starting from n and ending before m, but incrementing by s.

For instance, range(10, 30, 5) will give you the numbers 10, 15, 20, 25.

123
# Range with three arguments for i in range(10, 30, 5): print(i, end = ' ')
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 5

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Ställ mig frågor om detta ämne

Sammanfatta detta kapitel

Visa verkliga exempel

Awesome!

Completion rate improved to 1.64

bookRange Function in Python

Svep för att visa menyn

If you're looking to loop through indices rather than the elements themselves, you'll want to get familiar with the range() function. range() produces a series of numbers and returns a range object. This function can accept 1, 2, or 3 positive number arguments.

When you provide just one argument, n, it returns all integers from 0 up to, but not including, n. For example, range(5) yields the numbers 0 through 4.

123
# Range with one argument for i in range(5): print(i, end = ' ')
copy

If you give the function two arguments, n and m, it returns all integers starting from n and going up to, but not including, m.

So, range(5, 10) will produce the numbers 5 through 9.

123
# Range with two arguments for i in range(5, 10): print(i, end = ' ')
copy

When you provide three arguments, n, m, and s, it returns integers starting from n and ending before m, but incrementing by s.

For instance, range(10, 30, 5) will give you the numbers 10, 15, 20, 25.

123
# Range with three arguments for i in range(10, 30, 5): print(i, end = ' ')
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 5
some-alt