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
Arithmetic
Comparison/Relational
Assignment
Basic Operators 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.
If
Control Flow
If….Else
Nested If
What are control flow statements in Python?
A program’s control flow is the order in which the program’s code executes.
The control flow of a Python program is regulated by conditional statements, loops, and function calls.
2 for loop
Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
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.
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, val 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.
Flowchart of for Loop
exhausted.
Python Nested Loops
Python programming language allows to use one loop inside another loop. Following
section shows few examples to illustrate the concept.
Syntax
The syntax for a nested while loop statement in Python programming language is as
follows −
A final note on loop nesting is that you can put any type of loop inside of any other type
of loop. For example a for loop can be inside a while loop or vice versa.
LOOP CONTROL STATEMENTS
Python Break and Continue Statement
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
Thank You