Introduction to Python
Presented by the Ethiopian Artificial Intelligence Institute
Looping Statements
❖ Looping statements, also known as iteration or repetition
statements, are used in programming to repeatedly execute a
block of code.
❖ They are essential for performing tasks such as iterating over
elements in a list, or executing a set of instructions a specific
number of times.
Types of Looping Statements in Python
Python primarily offers two types of looping statements:-
❖ for loop: Used for iterating over a sequence (like a list, tuple,
string, range, or dictionary) or other iterable objects. It executes a
block of code for each item in the sequence.
❖ while loop: Used for repeatedly executing a block of code as long
as a certain condition remains true. It continues to loop until the
condition becomes false.
for loop
❖ The for loop is ideal when you know, or can determine, the
number of iterations beforehand, or when you want to
process each item in a collection.
Syntax:
for loop
❖ item: A variable that takes on the value of each element in
the iterable during each iteration.
❖ iterable: Any Python object that can be iterated over (e.g.,
list, tuple, string, range, dictionary, set). An iterator is an
object that contains a countable number of values
Questions
❖ Write a program that takes the natural numbers from 1 to 20,
separates them into two lists — one containing all the even numbers
and the other containing all the odd numbers — and then prints
both lists clearly.
Questions
❖ Write a Python program that prompts the user to enter a single
positive integer. This number will represent the upper limit of a
numeric range starting from 1. Your task is to calculate the sum of
all odd numbers between 1 and the number provided by the user
(inclusive).
while loop
❖ The while loop is used when you don't know the exact number of
iterations in advance, but you have a condition that must be met
for the loop to continue.
Syntax:
while loop
❖ condition: A Boolean expression that is evaluated at the beginning
of each iteration. If True, the loop body executes. If False, the loop
terminates.
while loop
Important Considerations for while loops:
❖ Initialization: Ensure that the variable(s) used in the condition are
initialized before the loop starts.
❖ Condition Update: Crucially, ensure that something inside the loop body
changes the condition at some point, otherwise, you'll create an infinite loop.
Question
❖ Write a program using a while loop that:
Continuously asks the user to enter a number.
The loop should stop only when the user enters a negative number.
At the end, display the total number of positive numbers the user
entered (excluding the negative number that ends the loop).
Jump Statements
❖ Jump statements in programming are used to change the flow of control within a
program. They allow the programmer to transfer program control to different parts
of the code based on certain conditions or requirements.
❖ The jump statement include the following:-
break Statement.
continue Statement
break Statement
❖ break Statement :- The break statement is primarily used to exit
from loops prematurely. When encountered inside a loop, it
terminates the loop's execution and transfers control to the
statement immediately following the loop.
Example
continue Statement
❖ continue Statement :- The continue statement is used to skip the
current iteration of a loop and proceed to the next iteration.
Example
Question
numbers = [3, 4, 7, 10, 13, 18, 21, 24, 27, 30]
Task:
Write a Python program that:
Skips all the even numbers.
Adds each odd number into two separate lists:
The first list should contain all odd numbers less than 20.
The second list should contain all odd numbers 20 or greater .