Python
Conditionals and Loops
Session III
Norms
Take Notes! Do It With Me! Participate!
Agenda
1. Conditionals
2. If and Else statements
3. Loops
4. For and While loops
5. Exception handling
What are Conditions?
Conditions!
● Conditions in Python or Conditional
statements are used to perform decision
making challenges.
● Python supports the usual logical
conditions from mathematics.
Conditions!
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
These conditions can be used in
several operations, most commonly in
"if and else statements" & "loops"
Conditions!
Operations compare some values or operands, producing a boolean
based on some conditions.
Conditions!
Conditions!
Activity!
Apply >= and <= for given values and run the
code.
● X = 8, Y = 9
● X = 9856, Y = 3579
● X = 8.68, Y =26.58
How are If and Else statements used,
with the help of conditions?
If statements!
● An if statement is used execute
some code if certain condition
evaluates to be True.
● An "if statement" is written by using
the if keyword.
If statements!
Elif statement!
● The elif keyword used when we
have multiple conditions and want
to check them one by one.
● The elif keyword is pythons way of
saying "if the previous conditions
were not true, then try this
condition".
Elif statement!
● An if else statement is used execute
some code if certain condition
evaluates to be True and some other
code if statement evaluates to false.
Elif statement!
Else statement!
● The else keyword catches anything
which isn't caught by the preceding
conditions.
Else statement!
Indentations
● Python relies on indentation (whitespace
at the beginning of a line) to define scope
in the code.
● Other programming languages often use
curly-brackets for this purpose.
● Python uses indentation to indicate a
block of code.
Indentations
● Indentation improves the readability of
the code.
● If there is an incorrect indentation, this
will result in an error, and the python
interpreter will just return an error
function.
Activity!
Apply if statement and elif statement for given
values.
● X = 8, Y = 9
● X = 9856, Y = 3579
● X = 8.68, Y =26.58
What are Looping statements?
Looping statements!
● During programming there are times,
when you have to perform certain tasks
for a number of times, e.g. printing a
name 100 times.
● You can copy and paste some code
multiple time to do that or you can
instruct you application to do it a number
of times using loops.
Looping statements!
● Looping is the process in which we have
a some code that gets executed
repeatedly until a particular condition is
satisfied.
● Python has two primitive loop
commands.
For loop!
● The for statement is used to loop over a
group or collection of data (that is either
a list, a tuple, a dictionary, a set, or a
string).
● With the for loop we can execute a set of
statements, once for each item in a list,
tuple, set etc.
For loop!
Create a for loop.
For loop!
The break statement.
For loop!
The continue statement
For loop!
The Range function.
For loop!
Else in for loop.
For loop!
Nested Loop: A nested loop is a loop inside a
loop.
Activity!
● Create a loop including names of your
family members.
● Create a loop through the letters in your
name.
While loop!
● The while statement simply loops until a
condition is evaluates to False.
While loop!
Create a while loop.
While loop!
The break statement.
While loop!
The continue statement.
While loop!
The else statement.
Activity!
Create a while loop including the below
conditions.
m = 7; m == 4; m > 4
How to handle exceptions?
Try & Except!
● When an error occurs, or exception as we
call it, Python will normally stop and
generate an error message.
● These exceptions can be handled using
the try and except statements.
Try & Except!
● The try block lets you test a block of
code for errors.
● The except block lets you handle the
error.
Try & Except!
Try & Except!
You can define as many exception blocks as you want, e.g. if you want
to execute a special block of code for a special kind of error.
Try & Except!
Raise an exception.
● As a Python developer you can choose to throw an exception if a
condition occurs.
● To throw (or raise) an exception, use the raise keyword.
Try & Except!
Can you give a real life example
where Try and Except method can be used?
What we learnt today!
● Conditionals
● If & else statements
● Loops
● For and while loops
● Try and except
Time for Feedback!
1. Click on the feedback form link.
2. Enter your personal details.
3. Rate the session based on
a. Overall Experience
b. Trainer
c. Content
4. Then click on Submit…!
Doubts?
Key Takeaways
What is your one key takeaway from the session?