0% found this document useful (0 votes)
126 views24 pages

Day 4

The document discusses Python iteration and lists. It covers while and for loops, their structure and uses. Common loop applications like accumulating totals and validating user input are described. List methods for adding, removing, sorting, and counting elements are also explained. Several coding exercises are provided to practice these concepts.

Uploaded by

Arolf Patacsil
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)
126 views24 pages

Day 4

The document discusses Python iteration and lists. It covers while and for loops, their structure and uses. Common loop applications like accumulating totals and validating user input are described. List methods for adding, removing, sorting, and counting elements are also explained. Several coding exercises are provided to practice these concepts.

Uploaded by

Arolf Patacsil
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/ 24

TOT - Python Programming Essentials

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

● Loop body instruction(s) or statements which are repeated


● Loop-exit condition the condition to be tested before each repetition

Types
● while loop
● for loop
○ In
○ Range
When to use the while loop and the for loop

● Loops that are dependent on a sentinel value (or indicator)


are better coded using a while loop
● The for loop is generally used for traversing and
manipulating arrays
● When the number of times that the loop will be executed is
known, the for loop provides a convenient shorthand.
Common Loop Applications
Using a loop to accumulate totals
● An accumulator is a variable that “sums up” or
accumulates values
● It is similar to a counter whose values change each time
the loop is entered. Counters, however, add a fixed value
while accumulators accumulate undetermined values.
Common Loop Applications
-Using a loop to validate user entry
● Data entered by a user usually needs validation. It needs
to be checked for errors. Incorrect data can lead to
unwanted results and could end a program abnormally
● Usual checks for data are:
○ If it is the correct data type
○ For numeric data, if it is within an acceptable range of
values
Iteration
Format: while 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.

Python while loop number 1


Python while loop number 2
...
Python while loop number 20

2. Save your file as lastname_firstname_day4_act1.py


3. Submit your file in google classroom
Iteration (For Loop)
Sample for For loop

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()

numList = [1, 2, 3, 4, 5] male = ['John','Mike']


numList.append(99) male.insert(1,"Jake")
print(numList) print(male)

Output Output
[1, 2, 3, 4, 5, 99] ['John', 'Jake', 'Mike']
Removing object to a list
Using remove() Using pop()

male = ['John','Mike','Jake'] male = ['John','Mike','Jake']


male.remove("Jake") male.pop(1)
print(male) print(male)

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

You might also like