30 lines
812 B
Python
30 lines
812 B
Python
# Basic example of a while loop
|
|
|
|
# While loops "do something" as long as the condition
|
|
# defined at the start of the loop is True
|
|
|
|
print("Here are the numbers from 1 to 10...")
|
|
print()
|
|
|
|
number = 1
|
|
while number <= 10:
|
|
print(f"Number: {number}")
|
|
number = number + 1
|
|
|
|
# The range(start, end) function returns
|
|
# numbers starting with "start" and ending
|
|
# one number before "end".
|
|
# range(0, 10) will return numbers from 0 to 9
|
|
|
|
# This is a "for" loop. For loops are used to
|
|
# iterate over lists or other collections of data.
|
|
# To iterate means to walk through a set of data
|
|
# one element at a time. A for loop lets you walk
|
|
# through a collection of data and do something
|
|
# with each element of that collection.
|
|
print("Numbers:", end='')
|
|
for num in range(0, 10):
|
|
print(f" {num}", end='')
|
|
print()
|
|
|
|
print() |