0% found this document useful (0 votes)
7 views16 pages

Day 4 - While and For Loops

The document provides an introduction to Python programming, focusing on iterative programming concepts such as loops (for and while loops) and the random library. It includes examples of using loops for tasks like password validation and calculating factorials, as well as generating random numbers. Additionally, it outlines the use of keywords 'break' and 'continue', and presents tutor examples for practical application.

Uploaded by

ddstephanoo
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)
7 views16 pages

Day 4 - While and For Loops

The document provides an introduction to Python programming, focusing on iterative programming concepts such as loops (for and while loops) and the random library. It includes examples of using loops for tasks like password validation and calculating factorials, as well as generating random numbers. Additionally, it outlines the use of keywords 'break' and 'continue', and presents tutor examples for practical application.

Uploaded by

ddstephanoo
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/ 16

INTRODUCTION TO PYTHON By: Kobina Adu (Ogyam Ye Berima)

Assisted by: Tabitha O. Appiah

PROGRAMMING BOOTCAMP 2.0 Powered by: exe software inc


Email: [email protected]
DAY 3 – ITERATIVE PROGRAMMING
QUICK TIPS – RANDOM
The random library is an important library in python. It is used to generate random numbers for
various purposes. However, the random library is not accessible directly and we need to import it first
before using it.
Example:
>> import random
>> num = random.random() # this generates a random float between 0 and 1.
>> num1 = random.choice( [1, 2, 3, 4] ) # this picks up a random number from the list, str, range, etc.
>> num2 = random.choices(range(1,50), k=3) # picks up 3 random numbers from the range.
>> num3 = random.randint(30, 78) # picks up a random integer between a = 30 and b = 78.
ITERATIVE PROGRAMMING
➢Imagine you are developing an App and you want to create a sign in page where the user would enter
his/her password.
➢If he/she enters the wrong password then you want the computer to say “error, password incorrect. Try
again.”
➢Once the user enters the right password the computer stops asking for a password.
➢Moreover, you also want the computer to block the user if he or she fails to input the right password.
➢You would realize that the above conditions requires 2 things:
1. We keep on asking for password until the correct password is provided
2. We allow the user to make only 3 attempts.
In both conditions, an action or set of actions had to be repeated which brings us to the topic of loops (for
loop and while loop).
FOR LOOP AND WHILE LOOP
✓When to use a for loop:
A for loop is needed when you need to run a command or process for a specific
number of times.
Example: 2 × 2 × 2 × 2 × 2 = 25 (This means multiply 2 by itself for 5 times)

✓When to use a while loop:


✓A while loop is needed when you want to run a command or process until a certain
condition is met.
✓Example: 2𝑛 = 8 (This means multiply 2 by itself until you get 8; n = 3)
FLOW CHAT
WHILE LOOP
SYNTAX num < 8
while </condition>:
</action code>
Example:
True False
count = 0

loop
num = 1
while num < 8:
num = num *2 num = num * 2 print(count)
count = count + 1
print(count)
Output:
3 count = count +1 End
FOR LOOP for num in
End of loop
SYNTAX FLOW CHAT [2,4,6] when the list
for </condition>:
• num starts with 2 is exhausted
</action code>
Example:
for num in [2, 4, 6]:
print( num*10 )
Output:
LOOPING
20 # for the 1st loop num = 2. num*10 = 20
40 # for the 2nd loop num = 4. num*10 = 40 num takes up a
60 # for the 3rd loop num = 6. num*10 = 60 next value prints out num
* 10
4 or 6
NB: num is a variable whose value changes per loop.
For loops are relevant when you want o repeat an
action or code for a specific number of times.
KEYWORD – BREAK & CONTINUE
➢The “break” key word is Code example
used to stop or terminate an
entire looping sequence. words = [ “an”, 67, “mouse”, “head”, “the”, 90, 0.1 “apple” ]
➢The “continue” key word is
for word in words:
used to terminate a current
loop so that the next loop
if (word is int) or (word is float):
could begin
continue

if len( word ) == 3:
EXAMPLE: print(word)
Find the first occurrence of a break
3 letter word in the given list
of words. >> ‘the’
VIDEO ON ITERATIVE PROGRAMMING – LOOPS

The video will be made available on YouTube and our official Telegram group.
NESTED LOOPS Code example:
lst = [ [2, 4], [78, 10, 34], [0.9, 2, 34, 50]]

Just as we learnt in our previous count = 0


class, a nested loop could be a for
loop in for loop, while loop in for sub_lst in lst:
while loop, for loop in while loop for num in sub_lst:
or a while loop in a for loop. if num is 2:
You should be careful with the count += 1
indentation. Remember that the
more you nest the more you
indent. print( count )
Example:
Count all the 2s in the nested list.
TUTOR EXAMPE 1
TITLE: FACTORIAL OF A NUMBER
Write a computer program to calculate the factorial of a given number.
5! = 5 × 4 × 3 × 2 × 1 = 120
Example:
>> Enter a number: 3
6
>> Enter a number: 7
5040
NB: 0! = 1
TUTOR EXAMPLE 2
TITLE: FINDING FACTORS OF A GIVEN NUMBER
Write a computer program to help students generate the factors of any given number
and comment if the number is a prime number or not.
Example:
>> Enter a number of interest: 9
Factors: 1 3 9
Not a prime number
TUTOR EXAMPLE 3
Write a computer program that would generate the nearest prime number given an
input from the user.
Explanations:
For instance, let assume that a user enters the value 10 into our program.
6 7 8 9 10 11 12 13
Both 7 and 11 are the prime numbers around 10 however 11 is the nearest.

Similarly, let assume that a user enters the value 23 into our program.
18 19 20 21 22 23 24 25 26 27 26 29 30
Both 19 and 29 are the prime numbers around 23 however 19 is the nearest.
SOLUTION TO TUTOR EXAMPLE 1, 2 &3 FOR DAY 4

The video will be made available on YouTube and our official Telegram group.
WATCH OUT FOR PROJECT 4
Project will be posted in our official Telegram group.
Only qualified participants will have access to the project.
THANK YOU
YO U H AV E C O M P L E T E D
D AY 4
OF OUR 2023 BOOTCAMP 3.0

You might also like