0% found this document useful (0 votes)
26 views

Control Statement

Uploaded by

mwaengenim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Control Statement

Uploaded by

mwaengenim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

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.

Indentation is the most used part of the python language since


it declares the block of code. All the statements of one block
are intended at the same level indentation. We will see how
the actual indentation takes place in decision making and
other stuff in python.
The If Statement
5

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.

●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

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.

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

Python's for loop is designed to repeatedly execute a code block while


iterating through a list, tuple, dictionary, or other iterable objects of Python.
The process of traversing a sequence is known as iteration.

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

This program lets the user enter the interest


rate, number of years, and loan amount, and
computes monthly payment and total
payment.

44
Summary
• Type • Integer Division

• Reserved words • Conversion between types

• Variables (mnemonic) • User input

• Operators • Comments (#)

• Operator precedence

You might also like