0% found this document useful (0 votes)
36 views3 pages

Loops

While loops repeat a block of code as long as a condition is true. The document provides examples of using while loops to print statements 10 times, print all numbers from 1 to 10, add all numbers from 1 to 500, and print statements indefinitely. For loops can also be used to repeat a block of code a specified number of times. For loops use the range function to iterate over a sequence of numbers or other elements. The document provides examples of using for loops to print statements 10 times, print all numbers from 1 to 10, add all numbers from 1 to 500, and print each character in a string or list.

Uploaded by

Apqrs SK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Loops

While loops repeat a block of code as long as a condition is true. The document provides examples of using while loops to print statements 10 times, print all numbers from 1 to 10, add all numbers from 1 to 500, and print statements indefinitely. For loops can also be used to repeat a block of code a specified number of times. For loops use the range function to iterate over a sequence of numbers or other elements. The document provides examples of using for loops to print statements 10 times, print all numbers from 1 to 10, add all numbers from 1 to 500, and print each character in a string or list.

Uploaded by

Apqrs SK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

While Loops:

While loops are loops that keep doing something until a condition is met.

We will print the statement Hello World! ten times.


Code:
i = 0 #Set the counter
while i < 10: #Here we make the counter less than 10 (it can be any number you want)
i += 1 # We increment the counter up by 1.
print("Hello World!") #Print the statement.

#Now the while loop will print this ten times

Result:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Now we will print all the numbers from 1 to 10:

Code:
i = 1 #Initialize the counter
while i < 11: #Set the limit
print(i) #Print the number
i += 1 #Increment the counter

Result:
1
2
3
4
5
6
7
8
9
10

Now we will add all the numbers from 1 to 500:

Code:
i=1
total = 0
while i < 501:
total += 0
i += 1

print("The total is", total)

Result:
The total is 125250

Now we will print a statement forever:

Code:
while True:
print("Hello World!")

Result:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
.
.
.

For Loops:
Everything we have done so far can also be done with a for-loop. For loops are also sometimes a
lot more powerful than while loops.

Before we can use a for loop you have to understand the range function.
range(initial, end, stepSize = 1)
So initial is where you start from, end-1 is where you end (We dont count the end number. It is
end - 1.) The stepSize is what you go up by. It is usually 1 and you dont have to set it.

range(1, 11,) will give you: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

range(1, 11, 2) will give you: 1, 3, 5, 7, 9

range(10, 100, 10) will give you: 10, 20, 30, 40, 50, 60, 70, 80, 90

We will print the statement Hello World ten times:


Code:
for i in range(0, 10):
print("Hello World!")

Result:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Now we will print all the numbers from 1 to 10:


Code:
for i in range(1, 11):
print(i)

Result:
1
2
3
4
5
6
7
8
9
10

Now we will add all the numbers from 1 to 500:


Code:
total = 0
for i in range(1, 501):
total += i
print('The total is: ', total)

Result:
The total is: 125250.

We can also print each element of a list or a string:


Code:
s = "The universe says Hi!"
for i in s:
print(i)

For a list we have:


Code:
li = ["H", "E", "L", "L", "O", "W", "O", "R", "L", "D"]
for i in li:
print(li)

You might also like