Day 4
Day 4
Day 4
Iteration
● Most useful and powerful structure
● Allows the repetition of instructions or statements in the loop body
Iteration
Parts of the iteration structure
Types
● while loop
● for loop
○ In
○ Range
When to use the while loop and the for loop
while condition:
statement1
statement2
The statements inside the while loop are executed as long as the condition
remains true
Iteration (While Loop)
Sample while loop
a= 1
while a < 6:
print(a)
a += 1
Day 4 Act 1
1. Write a program that will loop the message 20 times. Use while loop
only.
names = ["Mike","Ana","Jun"]
for name in names:
print(name)
Iteration (For Loop)
Sample for For loop using in keyword Sample for For loop using range
keyword
names =
["Mike","Ana","Jun"] for a in range(10,20):
for name in names: print(a)
print(name)
fruits = ['banana', 'apple',
'mango']
for index in range(len(fruits)):
print('Current fruit :',
fruits[index])
Break Statements
● force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body
of the loop
● The loop is terminated and program control resumes at the
next statement following the loop
Continue Statements
In loops, a continue statement cause control to be transferred
directly to the conditional expression that controls the loop.
Pass Statements
The pass statement is a null operation; nothing happens when
it executes. The pass is also useful in places where your code
will eventually go, but has not been written yet
Day 4 Act 2
1. Write a program that adds two numbers.
2. The program will ask to enter first and second number
3. The program will display “The sum of n1 and n2 is nTotal”
4. The program will ask if the user wants to try again. The user will input
Y/y if Yes and N/n if No
5. If Yes, refer to step 2.
6. If No, the program will display “Thank you!”.
7. Save your file as lastname_firstname_day4_act2.py
8. Submit your file in google classroom
List
The list is a most versatile data type available in Python which can be written
as a list of comma-separated values (items) between square brackets.
numbers= [1, 2, 3, 4, 5 ];
food = ["cake", "burger", "fries"]
print(numbers[index number])
print(numbers[0]) #1
print(food[1]) #burger
Methods to control list and its objects
Changing value in a list
male = ['John','Mike']
print(male[0])
male[0] = "Frank"
print(male[0])
Output
John
Frank
Adding object to a list
Using append() Using insert()
Output Output
[1, 2, 3, 4, 5, 99] ['John', 'Jake', 'Mike']
Removing object to a list
Using remove() Using pop()
Output Output
['John', 'Mike'] ['John', 'Jake']
Arranging object in a list
Using sort() Using reverse()
numList = [20, 2, 33, 42, 25] numList = [20, 2, 33, 42, 25]
numList.sort() numList.reverse()
print(numList) print(numList)
numList.sort(reverse=True)
Output
Output [25, 42, 33, 2, 20]
[2, 20, 25, 33, 42]
Counting object Checking object index
Using count() Using index()
numList = [25, 2, 33, 42, 25] numList = [11, 2, 33, 42, 25]
counter = numList.count(25) indexPosition = numList.index(42)
print(counter) print(indexPosition)
Output
2 Output
3
Combining lists
Using count()
male = ['Bien','John']
female = ['Jayde','Ana']
male.extend(female)
print(male)
print(male[3])
Output
['Bien', 'John', 'Jayde', 'Ana']
Day 4 Act 3
1. Write a word bank program
2. The program will ask to enter a word
3. The program will store the word in a list
4. The program will ask if the user wants to try again. The user will input
Y/y if Yes and N/n if No
5. If Yes, refer to step 2.
6. If No, Display the total number of words and all the words that user
entered.
7. Save your file as lastname_firstname_day4_act3.py
8. Submit your file in google classroom