Unit+3+ +Conditional+Statements
Unit+3+ +Conditional+Statements
[email protected]
J732KOTCSB
Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: Conditional Statements
Overview:
Most times, even the simplest questions we ask have two answers- yes or no, this or that. In
situations like these, conditional statements come in. when there are multiple solutions to
problems or picking out the most appropriate solution for a bunch of options, etc.
conditional statements are used in every domain to get one optimal solution.
Outcomes:
1. What are conditional statements?
2. Examples of statements.
3. If statements:
- Meaning of If-Else.
- Syntax of If-else.
- Meaning of If-else if/elif statements.
- Syntax of else if statements.
[email protected]
J732KOTCSB - Meaning of nested If-else statements.
- Syntax of nested else-if statements.
- pass statement
4. Loops:
- While loop.
- Syntax of while loop.
- range() function
- For loops.
- Syntax of for loops.
- break statement
- continue statement
- nested while loops
- nested for loops
- difference between for loop and while loop.
If-else:
When there are questions (maybe one, two or more) having one solution, and the rest
having another solution, then an if-else statement comes in handy. A block of code is
executed only when the condition is true, otherwise the second block contained inside the
‘else: ‘ is executed. If the condition is false, the interpreter does not enter the specified
block of codes.
For example, if a child is told that he would get a chocolate if he does his homework. Here
the condition is homework, if it is done, then the boy gets a chocolate, otherwise he
doesn’t. Similarly, in the world of coding, if-else statements help to execute programs where
multiple either-or or this-or-that that type of problems exist.
Here are a few more examples of If-else statements in programs:
[email protected]
J732KOTCSB 1. A person must get grade ‘Pass’ if they score above 45 in a 100 marks test. If they
score below it, they must get a ‘Fail’.
2. If a person’s salary is more than Rs. 200000, then display ‘Pay Tax’, if it is less than
that, display ‘Not liable to Pay tax’.
3. For whether a number is positive or negative. If the integer is more than zero, then it
is positive, if it less than zero, then it is negative.
if (condition):
code of statements if the given condition is true
else:
code of statements if the given condition is false.
Example 1
[email protected]
J732KOTCSB
Example 1
Note that the code will run even without the else part of the else if function. You can also
compare two conditions in the same condition statement using logical and identical
[email protected]
J732KOTCSBoperators.
elif (condition2):
if (condition 2.1):
Set of statements 3.
else:
Set of statements 4.
else:
if (condition 3.1):
Set of statements 5.
In the example, when the first condition is true, the statements inside it are executed.
[email protected]
Sequentially, the if code inside it is checked. Incase the if within the bigger if is true, the
J732KOTCSB
conditions inside the former are executed, otherwise the else block is executed. The
interpreter enters any block of code if and only if the condition on which its execution is
dependent is true.
Pass Statement:
Pass statement can be used when you don’t have statements inside a set of codes. A pass
statement does not have any impact on the program. The interpreter just continues and
goes to the next statement when it reads a pass statement.
pass is a keyword in Python.
The example shows how pass is used. The program shows no display, meaning it runs
without encountering errors but there are no statements inside the if statements, only an if,
therefore, the output is empty. It is used to execute nothing.
While loop:
In a while loop, the set of statements keeps on repeating till the condition is true. As soon as
the condition is false, the loop ends and the set of codes written after the loop is executed.
It is used in cases when the number of iterations is unknown.
For a while loop, you first initialise a variable (conventionally i=0). Then the while condition
is written. Inside the loop, the set of statements that are to be printed in every iteration are
written. One must note that it is important to increment or decrement the initialised
variable otherwise it will become an infinite loop.
Increment means increasing the value of the variable, decrement means decreasing the
value of a variable.
Example 1
The above example says that i is initialised with value 0, and n with value 10. The while
condition is then checked. According to the example, if i <= n, which in this case is true, it
enters the loop. It prints Hello for the first time, and goes to the next line. i+=1 means that i
[email protected]
J732KOTCSBis incremented. Now the value of i changes from 0 to 1. Again, the while loop is checked. If
the condition i <= n is satisfied, then the statements inside the loop are executed again.
Since it is true, hello is printed the second time and i is incremented again. This continues
until the value of i become 11. In that case, the condition i <= n will not be satisfied because
11 is not less than 10. Thus the loop will end there.
range() function:
before doing for loops, one must know what the range() function do. The range() function is
an in-built python function. In a range(n,m), the code will run from the nth element to the
(m-1)th element.
We can also increment the variables by different integers in a range by specifying it in the
range. For example, the statement i in range(1,20,2) will run from 1 to 20 and will increment
by 2.
i =0
for i in sequence/datastructue :
Block of statements.
….
Statements.
Parts of the code:
1. i = 0 : it is called the iterator variable. It is a variable initialised before the for loop.
2. for i in seq/data_structure : in is a keyword in python. The statement reads as ‘for
loop with iteration variable i in the sequence specified’. Here i begins from the
initialised element position in the sequence and then the interpreter enters the
block of codes within the loop, the iteration variable increments after every loop
until the condition specified is false.
3. Set of codes: as long as the iterative variables satisfy the range/sequence mentioned,
[email protected]
J732KOTCSB
the set of codes inside the for loop is executed.
Example 1
In the above example, i is initialised as 0. Then the next line containing the for loop is
executed. i starts from the sequence mentioned and executes the block of codes within the
for loop. After the loop is over, i increments and repeats executing the code inside the loop
break statement:
The break statement is used when the condition of the loop is specified but one wants to
terminate the loop in between.
Syntax:
for i in range/sequence_Name/list_Name:
set of codes
break
statements.
According to the above mentioned syntax, the for loop runs per usual but when it
encounters a break statement then it exits the loop without completing all its iterations and
starts executing the statements mentioned after the loop. Break is a keyword to break the
flow of statements inside a loop.
[email protected]
J732KOTCSBExample 1
In the above example, the iterative variable is i, the loop begins from 1 and has to be
repeated until 20. When it enters the code, the ith iteration number is printed. Then the if
block is checked. If the ith number is divisible by 7 then the interpreter enter that block,
otherwise it enters the else block. Note that the keyword continue is used here to continue
the flow of the loop without passing any other statements. This loop will continue until 7,
Nested loops:
A loop within a loop is called a nested loop.
Example of a nested while loop:
[email protected]
J732KOTCSB
In the above example, I is initialised at 1. According to the first while loop, 1<=30 is true,
hence the interpreter enters the loop. The second while loop is checked. Since j <= 15, the
block of statements inside the loop are executed. i+2 and j are printed and then both the
variables are incremented. These types of loops- one within another, are called nested
loops.
Speed For loop is faster than while loop. While loop is slower than for loop.