0% found this document useful (0 votes)
28 views43 pages

Chapter Two and Three

The document discusses various control structures in Python programming including conditional statements like if, if-else and elif statements, loops like for and while loops, and other statements like break, continue and pass. It provides syntax and examples for each control structure.

Uploaded by

alemu7230
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)
28 views43 pages

Chapter Two and Three

The document discusses various control structures in Python programming including conditional statements like if, if-else and elif statements, loops like for and while loops, and other statements like break, continue and pass. It provides syntax and examples for each control structure.

Uploaded by

alemu7230
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/ 43

Introduction to

Python
Programming

Bahir Dar University


Bahir Dar Institute of
Technology

Faculty of Computing
Chapter Two
Control Structures
What is Control Structure
Control flow is the order that instructions are executed in a
program.
A control statement is a statement that determines the control flow
of a set of instructions.
A control structure is a set of instructions and the control
statements controlling their execution.
Control structures allows us to change the ordering of how the
statements in our programs are executed.
What is Control Structure
The basic Control Structures in programming languages are:
Sequential: executes each statement one after another
Conditionals (or Selection): which are used to execute one or more
statements if a condition is met.
Loops (or Iteration): which purpose is to repeat a statement a certain
number of times or while a condition is fulfilled.
Conditional Statements
Decision making is the most important aspect of almost all the
programming languages. As the name implies, decision making
allows us to run a particular block of code for a particular decision.
Here, the decisions are made on the validity of the particular
conditions.
Condition checking is the backbone of decision making.
In python, decision making is performed by the following
statements.
if statements
if else statements
Nested if statements
if Statement
The if statement is used to test a
particular condition and if the
condition is true, it executes a block
of code known as if-block.
The condition of if statement can be
any valid logical expression which can
be either evaluated to true or false.
Syntax:
if expression:
statement
if Statement
Example 1
if Statement
Example 2
If-else Statement
The if-else statement provides an else block
combined with the if statement which is
executed in the false case of the condition.
If the condition is true, then the if-block is
executed. Otherwise, the else-block is
executed.
Syntax:
if condition:
#block of statements
else:
#block of statements (else-block)
If-else Statement
Example 1: Program to check whether a person is eligible to vote or
not.
If-else Statement
Example 2: Program to check whether a number is even or not.
elif Statements
The elif statement enables us to check
multiple conditions and execute the
specific block of statements depending
upon the true condition among them.
We can have any number of elif
statements in our program depending
upon our need.
The elif statement works like an if-else-if
ladder statement in C++. It must be
succeeded by an if statement.
elif Statements Example

Syntax
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
elif Statement

Output
Nested if Statement
Syntax:
There may be a situation if expression1:
when you want to check for statement(s)
another condition after a if expression2:
condition resolves to true. statement(s)
In such a situation, you can elif expression3:
use the nested if construct. statement(s)
In a nested if construct, you else
statement(s)
can have an if...elif...else
elif expression4:
construct inside another
statement(s)
if...elif...else construct. else:
statement(s)
Nested if Statement
Example: Program that prints the largest among the three numbers

Output
Switch Statement
A switch statement is a multiway branch statement that compares
the value of a variable to the values specified in case statements.
Unlike any other programing language like C, C++, and Java Python
language doesn’t have a switch statement.
Chapter Three
Looping
Loops
The flow of the programs written in any programming language is
sequential by default. Sometimes we may need to alter the flow of
the program. The execution of a specific code may need to be
repeated several numbers of times.
For this purpose, The programming languages provide various types
of loops which are capable of repeating some specific code several
numbers of times.
Consider the following diagram to understand the working of a loop
statement.
Loops
Loops
Advantages of loop
It provides code re-usability.
Using loops, we do not need to write the same code again and again.
Using loops, we can traverse over the elements of data structures (array or
linked lists).
There are the following loop statements in Python.
for loop
while loop
Nested loop
for Loop
The for loop in Python is used to
iterate the statements or a part of the
program several times.
It is frequently used to traverse the
data structures like list, tuple, or
dictionary.
Syntax
for iterating_var in sequence:
statement(s)
for Loop Output

Example: Iterating string using for loop


for Loop
Output
Example: Iterating list using for loop
for Loop
The range() function
The range() function is used to generate the sequence of the
numbers.
Syntax: range(start,stop,step size)
 The start represents the beginning of the iteration.
 The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
 The step size is used to skip the specific numbers from the iteration. It is
optional to use. By default, the step size is 1. It is optional.
for Loop
Example: Program to print “Hello World” 10 times
for Loop
Example: Program to print even number using step size in range().

Output
Nested for Loop
Python allows us to nest any number of for loops inside a for loop.
The inner loop is executed n number of times for every iteration of
the outer loop.
Syntax
for iterating_var1 in sequence: #outer loop
for iterating_var2 in sequence: #inner loop
#block of statements
#Other statements
Nested for Loop
Python allows us to nest any number of for loops inside a for loop.
The inner loop is executed n number of times for every iteration of
the outer loop.
Syntax
for iterating_var1 in sequence: #outer loop
for iterating_var2 in sequence: #inner loop
#block of statements
#Other statements
Nested for Loop
Exercise 1: Write a python program that displays the following pattern
Nested for Loop
Using else statement with for loop
Unlike other languages like C, C++, or Java, Python allows us to use the else
statement with the for loop which can be executed only when all the iterations
are exhausted.
Here, we must notice that if the loop contains any of the break statement then
the else statement will not be executed.

Output
Using else statement with for loop

In the above example, the loop is broken due to the break statement;
therefore, the else statement will not be executed.
The statement present immediate next to else block will be executed.
while loop
The Python while loop allows a part of the code
to be executed until the given condition returns
false. It is also known as a pre-tested loop.
It can be viewed as a repeating if statement.
When we don't know the number of iterations
then the while loop is most effective to use.
Syntax:
while expression:
statements
while loop
Example-1: Program to print 1 to 10 using while loop
while loop
Infinite while loop
If the condition is given in the while loop never becomes false, then
the while loop will never terminate, and it turns into the infinite
while loop.
Any non-zero value in the while loop indicates an always-
true condition, whereas zero indicates the always-false condition.
break Statement
The break is a keyword in python which is used to bring the program
control out of the loop. The break statement breaks the loops one by
one, i.e., in the case of nested loops, it breaks the inner loop first
and then proceeds to outer loops.
In other words, we can say that break is used to abort the current
execution of the program and the control goes to the next line after
the loop.
The break is commonly used in the cases where we need to break
the loop for a given condition.
break Statement
Example:
break Statement
Example:
continue Statement
In Python, the continue keyword return control of the iteration to
the beginning of the Python for loop or Python while loop.
All remaining lines in the prevailing iteration of the loop are skipped
by the continue keyword, which returns execution to the beginning
of the next iteration of the loop.
Both Python while and Python for loops can leverage the continue
statements.
continue Statement
Example:
pass Statement
In Python, the pass keyword is used to execute nothing; it means,
when we don't want to execute code, the pass can be used to
execute empty.
It is the same as the name refers to. It just makes the control to pass
by without executing any code. If we want to bypass any code pass
statement can be used.
It is beneficial when a statement is required syntactically, but we
want we don't want to execute or execute it later.
The difference between the comments and pass is that, comments
are entirely ignored by the Python interpreter, where the pass
statement is not ignored.
pass Statement
Suppose we have a loop, and we do not want to execute right this
moment, but we will execute in the future. Here we can use the
pass.
What is the output of the following program

You might also like