Intro To Python - Day 3
Intro To Python - Day 3
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Agenda
● Conditional statements:
○ If statement
○ If-else statement
○ If elif else statement
○ Nested if-else
○ Loops
○ For Loop
○ While Loop
○ List Comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Let’s explore conditional statements
● If the constraint is ‘True’, the statements in the body are executed, and if it is ‘False’,
the statements in body are skipped
● Conditional statements:
○ If statement
○ If-else statement
○ If elif else statement
○ Nested if-else
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If Statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The if...else Statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If-else statement
● The statement(s) under ‘else:’ are executed when the condition in ‘if ’ is not
satisfies, ie., the boolean output is ‘False’
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If-else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If-else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If elif else Statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If elif else statement
● Elif statement is used to control multiple conditions only if the given if condition false
● It's similar to an if-else statement and the only exception is that in else we are not
checking the condition but in elif, we check the condition
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If elif else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
If elif else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested if and if...else Statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested If-else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested If-else statement
● Note that the ‘if’ statements are within the body of another if statement
● If the first ‘if’ condition is satisfied then the program executes the
commands within that ‘if’
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested If-else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested If-else statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested If statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Loops
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Python Iterables
• Iterable is a Python object that is capable of returning its elements (or members)
one by one
• Sequence type Python objects like lists, tupes, dictionaries, strings and sets are
iterable type of objects
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Iteration through the Iterables
Iteration is a general term for taking each item from a sequence one after another
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
For Loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The for loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The for loop code example
No
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read each element of a string
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
While Loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The while loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The while loop code example
Is the N0
conditio
n met?
Yes
Print the element,
increment the value,
pass control to while Increment x by 1 and pass the control to the
statement while statement
End loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Print each element of a list using while loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Break, Continue
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Exiting a loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Exit a loop with break statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Exit a while loop with break statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Exit a for loop with break statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Skip part of the loop with continue statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Skip part of the loop with continue statement using
while loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Skip part of the loop with continue statement using for
loop
The for...loop iterates through
the string character by
character
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
More on Loops
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read each element of a tuple
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read through a list of list
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Loop through a range
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Computation using for…loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read through a dictionary object
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read through the values in a dictionary to create a list
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Replace values in a dictionary with computed values
A dictionary object
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The for loop with conditional statements
For loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested for loop
1st indentation
Nested loop 2nd indentation
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested for loop
Outer loop
Inner loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested for loop with list of lists
Outer loop
Inner loop
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Example: Calculating sum of numbers
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
While loop with else statement
The else part is executed if the condition in the while loop evaluates to False
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Exit a while loop with continue statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
List Comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
What is a list comprehension?
● Using list comprehensions reduces the number of code lines making it it look more
elegant
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
What is a list comprehension?
It consists of [ ] containing:
● an input sequence
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
List comprehension vs loops
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
For loop using list comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read the first letter of each word using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Read the first three letter of each word using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Iteration over more than one iterable in a list using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
List comprehension using conditional logic
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
List comprehension using conditional logic with custom
function
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Extract all the digits from the string using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Extract all the numbers from the string using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Extract all the vowels from the string using list
comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Nested condition within a list comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Find strings in a list that are longer than 9 character
using list comprehension
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
A list comprehension helps reduce three lines of code
into a single line of code.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Thank You
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.