Control Statement
Control Statement
Lesson 2
Python If-else 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.
2
…
4 Indentation in Python
For the ease of programming and to achieve simplicity, python
doesn't allow the use of parentheses for the block level code.
In Python, indentation is used to declare a block. If two
statements are at the same indentation level, then they are the
part of the same block.
Generally, four spaces are given to indent the statements
which are a typical amount of indentation in python.
●6
The syntax of the if-statement is
given below.
7
Example
8
Example 2 : Program to print the largest of the three numbers.
9
The if-else statement
10
If else
11
The syntax of the if-else statement is given below.
12
Example 1 : Program to check whether a person is eligible to vote or not.
13
Checkpoint: What is the output of this code?
x,y = 10,20
x,y = y,x
print(x,y)
14
Example 2: Program to check whether a number is even or not.
15
The elif statement
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. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be
succeeded by an if statement.
The syntax of the elif statement is given below.
16
17
18
Syntax
19
Example 2
20
Python Loops
The following loops are available in Python to fulfil the looping needs. Python
offers 3 choices for running the loops. The basic functionality of all the
techniques is the same, although the syntax and the amount of time required
for checking the condition differ.
We can run a single statement or set of statements repeatedly using a loop
command.
The following sorts of loops are available in the Python programming language.
21
Types of loop
22
Loop Control Statements
Statements used to control loops and change the course of iteration are called
control statements. All the objects produced within the local scope of the loop
are deleted when execution is completed.
Python provides the following control statements. We will discuss them later
in detail.
Let us quickly go over the definitions of these loop control statements.
23
..
24
The for Loop
In this case, the variable value is used to hold the value of every item present in
the sequence before the iteration begins until this particular iteration is
completed.
Loop iterates until the final item of the sequence are reached.
25
26
Code
27
Using else Statement with for Loop
As already said, a for loop executes the code block until the sequence element
is reached. The statement is written right after the for loop is executed after
the execution of the for loop is complete.
Only if the execution is complete does the else statement comes into play. It
won't be executed if we exit the loop or if an error is thrown.
Here is a code to better understand if-else statements.
28
…
29
30
31
The range() Function
With the help of the range() function, we may produce a series of numbers.
range(10) will produce values between 0 and 9. (10 numbers).
We can give specific start, stop, and step size values in the manner range(start,
stop, step size). If the step size is not specified, it defaults to 1.
Since it doesn't create every value it "contains" after we construct it, the range
object can be characterized as being "slow." It does provide in, len, and
__getitem__ actions, but it is not an iterator.
The example that follows will make this clear.
32
code
33
To iterate through a sequence of items, we can apply the range() method in
for loops. We can use indexing to iterate through the given sequence by
combining it with an iterable's len() function. Here's an illustration.
34
code
35
while
While loops are used in Python to iterate until a specified condition is met.
However, the statement in the program that follows the while loop is
executed once the condition changes to false.
36
code
37
Using else Statement with while Loops
38
Single statement while Block
39
Break Statement
It stops the execution of the loop when the break statement is reached.
40
41
42
43
Problem:
Computing Loan Payments
44
Summary
• Type • Integer Division
• Operator precedence