0% found this document useful (0 votes)
3 views12 pages

Unit+3+ +Conditional+Statements

The document provides supplementary learning material for a Python for Data Science course, focusing on conditional statements and loops. It covers various types of conditional statements, including if-else, if-elif, and nested if-else statements, along with their syntax and examples. Additionally, it explains loops, including while and for loops, their syntax, and the use of break and continue statements.

Uploaded by

saad.sheriff20
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)
3 views12 pages

Unit+3+ +Conditional+Statements

The document provides supplementary learning material for a Python for Data Science course, focusing on conditional statements and loops. It covers various types of conditional statements, including if-else, if-elif, and nested if-else statements, along with their syntax and examples. Additionally, it explains loops, including while and for loops, their syntax, and the use of break and continue statements.

Uploaded by

saad.sheriff20
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/ 12

Supplementary Learning Material

[email protected]
J732KOTCSB

Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: Conditional Statements

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Unit 3
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.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Conditionals Statements:
As the name suggests conditional statements are those which are dependent on certain
conditions. Meaning if a person had to choose between two or more options, conditions
would play a role in determining the output of the program. They are used to control the
flow of the program based on certain conditions. To see if the condition is true or not,
comparison operators are used. Keywords in conditional statements are: if, else, and elif.
Indentation in python is important when conditional statements are used.

 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.

Syntax of an If -else statement:

if (condition):
code of statements if the given condition is true
else:
code of statements if the given condition is false.

Example 1

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Example 2

[email protected]
J732KOTCSB

 If-elif conditional statements:


The above given syntax is easy to implement in cases where we have two outcomes, but
what if there are more than two? For instance, if we have to divide the grading system of a
subject, say, into more than two parts. Those who score between 80-100 get “Grade A”,
those who score between 60-80 get “Grade B”, those scoring between 40-60 get “Grade C”
and the rest get “Not passed”. For such questions we use something called if-else if
statements. In python, we call it if-elif statements.
Syntax of if-else if statements:
if (condition1):
Statements if condition 1 is true.
elif (condition2):
Statements if condition 2 is true.
elif (condition3):
Statements if condition 3 is true.

elif (condition N):

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Statements if condition N is true.
else:
Statements if all the above conditions are false.

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.

 Nested if-else statements.


A nested if statement is an if statement within an if statement.
Syntax of a nested if statement:
if (condition1):
if (condition 1.1):
Set of statements 1.
else:
Set of statements 2.

elif (condition2):
if (condition 2.1):
Set of statements 3.
else:
Set of statements 4.
else:
if (condition 3.1):
Set of statements 5.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
else:
Set of statements 6.
Example 1

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.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Loops:
When you have a condition that needs to be executed multiple times, typing it everytime
will be a task. Let’s say you want to print numbers from 1 to 100, typing all numbers will
take a lot of human time. But the program is fast, and it doesn’t take as long as humans do.
This is where the concept of using loops comes into the picture. A loop is dependent on a
variable that is initialised before and runs the code the number of times specified. Every
time the loop runs it is called an iteration. For n iterations, the loop will run n times and the
codes within the loop will also run n times, making it possible for us to run multiple
statements again and again. All of it in a giffy!
[email protected]
J732KOTCSB

 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.

Syntax of a while loop:


i=0 #you can use any variable instead of i
while(Condition1):
#code

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
i+=1

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.

Other examples where while loop can be used:


1. To keep printing numbers from 1 to 100.
2. To print prime numbers between 1 and 500.
3. To print multiples for 18 between 200 and 500
4. Printing factorials of numbers in descending order, etc.

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.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
 For loop:
Like any other conditional statement, the for loop is used to keep repeating a block of
statements multiple times. The number of iterations in the for loop is known, that is the
advantage of using for loops over other conditional statements.
You usually use a sequence or range or a data structure like a list, or tuple.
Syntax of a for loop:

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

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
until the iterative variable exceeds the last index number of range/sequence mentioned. For
the given loop, the execution will be done 10 times beginning from 0 to 9. The loop runs
until the (n-1)th number, which in this case is 9.

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,

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
after which when it satisfies the if-statement, it enters the block. First it prints “code ends
here” then it reads break. The break statement ends the loop. the interpreter exits the loop
even if the iterative variable is within the sequence/range limit. It will print the codes that
are written after the loop.
Thus, break terminates the loop.

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.

Example of a nested for loop:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
In the above example, the i and j variables are the iterative variables. They are initialised at
0. When the first for loop condition is satisfied, the interpreter enters the loop and then the
codes within the loop are executed. When the second loop condition is satisfied, then the
codes within the loop are executed. when the condition for j is no longer satisfied, the loop
terminates and then the outer loop runs until the specified condition is no longer satisfied.
Loops within loops are called nested loops.
[email protected]
J732KOTCSBDifference between While and For loop:
Factor While Loop For loop
Syntax i=0 #or any iterative variable i =0
while(Condition1): for i in sequence/data structure:
Block of statements.
#code ….
i+=1 Statements.

Format Variable initialisation, condition Variable initialisation, condition


checking and iteration statements checking are written at the
are written at the beginning. beginning.

Condition The iterations continue infinite If the condition is not mentioned it


times if it is not mentioned. will return an error. The condition is
essential.

Iterations The number of iterations is The number of iterations is


known. unknown.

Speed For loop is faster than while loop. While loop is slower than for loop.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.

You might also like