Chapter 02 Python
Chapter 02 Python
Write a simple python program for the given arithmetic Write a Python Program using decision making structure for
expressions. two-way branching to solve the given problem.
1 2 3 4
Use Different Types of operators for writing the arithmetic Write a Python Program using decision
expressions. making structure for multi-way branching
to solve the given problem
Course Outcomes
Basic Operators
• Arithmetic
• Comparison/Relational
• Assignment
• Logical
• Bitwise
• Membership
• Identity
• Python Operator Precedence
What are Operators in
Python?
• For example:
• >>> 2+3
• 5
not in returns True if the specified value is not found in the sequence.
Returns False otherwise.
• The control flow of a Python program is regulated by conditional statements, loops, and function calls.
If
Control Flow
If….Else
Nested If
1. Sequential
3 nested loops
You can use one or more loop inside any another while, for or do..while
loop.
What is while loop in
Python?
• A while loop statement in Python
programming language repeatedly
executes a target statement as long as
a given condition is true.
• A while loop in python iterates till its
condition becomes False. In other
words, it executes the statements
under itself while the condition it
takes is True.
Syntax
The syntax of a while loop in Python programming language is −
When the condition becomes false, program control passes to the line
immediately following the loop.
In Python, all the statements indented by the same number of character spaces
after a programming construct are considered to be part of a single block of code.
Python uses indentation as its method of grouping statements.
Using else Statement with While Loop
Python supports to have an else statement associated with a loop statement.
If the else statement is used with a while loop, the else statement is executed when the condition becomes
false.
While loop with else Single Statement
• Same as with for loops, while loops can ➢ Similar to the if statement syntax, if your while
also have an optional else block. clause consists only of a single statement, it
• The else part is executed if the condition in may be placed on the same line as the while
the while loop evaluates to False. header.
• The while loop can be terminated with a
break statement. In such cases, the else part
is ignored. Hence, a while loop's else part
runs if no break occurs and the condition is
false.
• Using else Statement with While Loop
• Python supports to have an else statement
associated with a loop statement.
• If the else statement is used with
a while loop, the else statement is executed
when the condition becomes false.
CHAPTER 02
What is for loop in Python?
• The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called traversal.
• Syntax of for Loop:
• Here, var is the variable that takes the value of the item inside the sequence on each
iteration.
• Loop continues until we reach the last item in the sequence. The body of for loop is separated
from the rest of the code using indentation.
• The first word of the statement starts with the keyword
“for” which signifies the beginning of the for loop.
• Then we have the iterator variable which iterates over
the sequence and can be used within the loop to
perform various functions
• The next is the “in” keyword in Python which tells the
iterator variable to loop for elements within the
sequence Explanation
• And finally, we have the sequence variable which can
either be a list, a tuple, or any other kind of iterator.
• The statements part of the loop is where you can play
around with the iterator variable and perform various
function
Flowchart of for Loop
exhausted.
Else in for Loop
• When the program control reaches the continue statement, it skips the
statements after ‘continue’.
• It then shifts to the next item in the sequence and executes the block of
code for it.
• You can use it with both for and while loops.
• Syntax of Continue
continue
• Flowchart of continue • The working of the continue statement in for and
while loop is shown below.
pass statement
• In Python, we use the pass statement to implement stubs.
• When we need a particular loop, class, or function in our program,
but don’t know what goes in it, we place the pass statement in it.
• It is a null statement. The interpreter does not ignore it, but it
performs a no-operation (NOP).
• Syntax of pass
pass
Example
Thank You