Week 3 Slides
Week 3 Slides
Amirsina Eskandarifar
Review last session
Condition
If
Elif
Else
Input
String functions
Example
Write a program to ask the user to insert a number, and then print
how many 0s their number has.
Now write a program to ask 3 times the same question, and every
time print number of 0s (what about 100 times?)
print(x**i)
i = i+1
Loop: while
While is a kind of loop that continues as long as the condition is true
Let’s see how while loop looks like in Python
while (condition):
executable code 1
executable code 2
.
.
.
executable code out of loop
Be careful! The while loop may go forever, unless you change the factors in the
condition
Be careful! The indentation and tabulation is important to Python to understand
what block of code is part of the loop, what is not.
Loop: for
for is another kind of loop in python that its iterator iterates over
a sequence of data in each iteration:
for (i in [1,2,3,4,5]):
executable code 1 (like print(i))
executable code 2
.
.
.
executable code out of loop
for (i in [1,2,3,4,5]):
for (i in range(?,?)):
Loop: practice
Write a program that:
1- Ask the user for multiple numbers until they insert 000, and
reserve all inserted values
2- Then, find and print the summation and average of inserted
numbers
3- Then, find and print the min & max
Loop: practice 2
Write a program that:
1- Ask the user for both a sentence and a letter
2- Then check if the sentence has that letter
3- Also count number of appearance of that letter
1 9 2 8 5 4 3
https://www.youtube.com/watch?v=JU767SDMDvA
https://www.youtube.com/watch?v=g-PGLbMth_g
https://www.youtube.com/watch?v=xli_FI7CuzA