Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Range Function in Python | Loops in Python
Introduction to Python
course content

Kursusindhold

Introduction to Python

Introduction to Python

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Range Function in Python

To iterate over indices instead of elements, the range() function is essential. It generates a sequence of numbers and returns a range object. This function accepts one, two, or three numeric arguments. With a single argument n, range(n) produces integers from 0 up to, but not including, n.

For example, range(5) generates the numbers 0 to 4.

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

When range() is given two arguments, n and m, it generates integers starting from n up to, but not including, m.

For example, range(5, 10) produces the numbers 5 through 9.

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

When range() receives three arguments n, m, and s it generates numbers starting from n, up to but not including m, incrementing by s each time.

For example, range(10, 30, 5) produces the sequence 10, 15, 20, 25.

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

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 5
Vi beklager, at noget gik galt. Hvad skete der?
some-alt