0% found this document useful (0 votes)
16 views33 pages

3 Loops

Loops allow repeating code segments to process multiple elements. There are two main types of loops in Python: for loops, which iterate over a sequence of items like a list, and while loops, which repeat as long as a condition is true. For loops use the range function to iterate a set number of times. While loops check a conditional expression and repeat the block until it evaluates to False. Loops are useful for printing characters in a string, calculating factorials, and repeating user input until a stop condition is reached. Care must be taken to ensure loops terminate to avoid infinite repetition.

Uploaded by

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

3 Loops

Loops allow repeating code segments to process multiple elements. There are two main types of loops in Python: for loops, which iterate over a sequence of items like a list, and while loops, which repeat as long as a condition is true. For loops use the range function to iterate a set number of times. While loops check a conditional expression and repeat the block until it evaluates to False. Loops are useful for printing characters in a string, calculating factorials, and repeating user input until a stop condition is reached. Care must be taken to ensure loops terminate to avoid infinite repetition.

Uploaded by

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

Loops

Olatz Perez de Viñaspre Garralda


Ander Soraluze Irureta
Help

Python’s documentation:
https://docs.python.org/3/

GOOGLE in general!!!
Do not repeat yourself

Do not copy/paste the same piece of


code once and again

foggodavid
Let’s say, we want to
print the name
character by character
You can access it with it’s position

● Index between [ ]

Strings
● First character
○ print(name[0])
● Last character
You can access a specific ○ print(name[-1])
character
Be careful! Indices start at 0
3.01. Exercise: Print first and
last character
Ask for a word and print it’s first
and last characters
Let’s try it
03-01-stringCharacter.py
To not repeat the code...
And to avoid the previous problem,
we can make use of loops Python
There are two kinds of
loops: for and while
Previous example with loops

name = “Olatz” name = “Olatz”

for character in name: for i in range(len(name)):


print(character) print(name[i])
Loops with a fixed number
of repetitions
Often, it is known the
number of loops

Suppose we have to write the same


message 100 times

foggodavid
for i in range(3):
print(i)

Range function Output:


It is useful to get a list of numbers 0
1
2
Range function: begin/end

Two parameters Be careful!!!!

for i in range(5,8): It will never reach the last number!


print(i)
If the first value is not set, it will be 0
Output by default

5 range(0,10) == range(10)
6
7
Range function: steps

Suppose we want to print the even Three parameters


numbers between 0 and 10…
for i in range(0,10,2):
The third parameter indicates the print(i)
incrementation
Output
If it is not set, it will be 1 by default
0
range(5,10) == range(5,10,1) 2
range(10) == range(0,10,1) 4
6
8
Let’s try it
03-02-range.py
In the same way, backwards...

If the value of the third parameter is Third negative


negative, it will decrease the counting
for i in range(3,0,-1):
Be careful! print(i)

First parameter, start Output


Second parameter, end
3
2
1
Let’s try it
03-03-rangeBackwards.py
3.02. Exercise: Cheerleader
Create a program that will sing as
a Cheerleader
Given a word, it will print it
character by character.
Example
Loops for calculations
Loops for Factorial of a number:

calculations result = 1
for i in range(1, number+1):
result = result * i
In the same way, we can also do
print(result)
math in loops
Let’s try it
03-04-factorial.py
Unknown number of loops
Or, conditional loops
It is also useful for a fixed number of
Conditional loops loops

(while) i=0
while i < 3:
In general, we will use them in two print(i)
scenarios: i=i+1
● While a condition is reached
● Unknown number of loops
Design of loops

1. Initialization: before starting the loop, give the initial


value to the variables to be used
2. End: when should the loop stop? Think carefully about
the condition to end the loop
3. Actions: what does the loop have to do? What specific
actions should it take?
4. Update: how to update the variable(s) to meet the ending
requirement?
as with for loops, we can do
the update as we wish
One by one, two by two, three by three or even in a
more complicated way (with the use of conditionals, for
example)
Let’s try it
03-05-while.py
Be careful with infinite
loops!

Always make sure that your condition


will be true in same point!!!

i=3
while i < 3:
print(i)

It will never end!!!


Loops depending text = input(“Enter a text:”)
on strings while text != “out”:
print(“Your text:”,text)
Let’s say, we want to print text = input(“Enter a text:”)
whatever the user inputs until
he/she writes “out”
Let’s try it!
03-06-whileOut.py
While empty text is entered...

text = input("Enter a text:") Remember:


while text:
print("Your text:",text) Python will consider False:
text = input("Enter a text:")
● In numbers: 0 edo 0.0
● In strings: “”
● In booleans: False
● In lists: []
Let’s try it!
03-07-whileOutEmpty.py
Want more?
http://www.parentesis.com

You might also like