0% found this document useful (0 votes)
1 views

Chapter 02 Python

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, along with their functionalities and examples. It also discusses control flow statements, including sequential, selection, and repetition structures, detailing conditional statements like if, if-else, and loops such as while and for. Additionally, it covers loop control statements like break and continue, as well as the use of the pass statement.

Uploaded by

shivamteli07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Chapter 02 Python

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, along with their functionalities and examples. It also discusses control flow statements, including sequential, selection, and repetition structures, detailing conditional statements like if, if-else, and loops such as while and for. Additionally, it covers loop control statements like break and continue, as well as the use of the pass statement.

Uploaded by

shivamteli07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

CHAPTER 02

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?

• Operators are special symbols in Python that carry out


arithmetic or logical computation.

• The value that the operator operates on is called the


operand.

• For example:

• >>> 2+3

• 5

• Here, + is the operator that performs addition. 2 and 3


are the operands and 5 is the output of the operation.
Arithmetic
Operators
• Arithmetic operators are used
to perform mathematical
operations like addition,
subtraction, multiplication, etc.

• Assume variable a holds 10


and variable b holds 20, then −
Python Comparison Operators
• These operators compare the values
on either sides of them and decide
the relation among them. They are
also called Relational operators.

• Comparison operators are used to


compare values. It returns either
True or False according to the
condition.

• Assume variable a holds 10 and


variable b holds 20, then −
Assignment operators are used in Python to
assign values to variables.

Python Assignment a = 5 is a simple assignment operator that


Operators assigns the value 5 on the right to the variable a
on the left.

There are various compound operators in


Python like a += 5 that adds to the variable and
later assigns the same. It is equivalent to a = a +
5.
Python Bitwise Operators
• Bitwise operators act on operands as if they
were strings of binary digits. They operate bit
by bit, hence the name.
• For example, 2 is 10 in binary and 7 is 111.
• Following table lists out the bitwise operators
supported by Python language with an example
each in those, we use the above two variables
(a and b) as operands −
• a = 0011 1100
• b = 0000 1101
• -----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Example (Bitwise Left Shift)
Let’s take a number 14.
Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8 bit)
14 = (00001110) 2
Then 14 << 1 will shift the binary sequence 1 position to the left side.
Example(Bitwise Right Shift)
Let’s take a number 14.
Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8 bit)
14 = (00001110) 2
Then 14 >> 1 will shift the binary sequence by 1 position to the right side.
Python Logical Operators
Logical operators are the and, or, not operators.
Logical Operator Truth Table:
A membership operator is used to identify membership in any sequence
(lists, strings, tuples).

in and not in are membership operators.

in returns True if the specified value is found in the sequence. Returns


False otherwise.

not in returns True if the specified value is not found in the sequence.
Returns False otherwise.

Python Membership Operators


Python Identity Operators

• Python language offers some special


types of operators like the identity
operator or the membership operator.

• is and is not are the identity operators in


Python.

• They are used to check if two values (or


variables) are located on the same part of
the memory.

• Two variables that are equal does not


imply that they are identical.
Python Operators Precedence
• Operator precedence affects how an expression is evaluated. Operators in
the same box evaluate left to right
Thank You
• WHAT IS CONTROL FLOW?
Agenda • IMPORTANCE OF CONTROL FLOW
• TYPES OF FLOW CONTROL IN PYTHON?
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.

• Python has three types of control structures:

• Sequential - default mode


• Selection - used for decisions and branching
• Repetition - used for looping, i.e., repeating a piece of code multiple times.
IMPORTANCE OF CONTROL FLOW
TYPES OF FLOW CONTROL IN
PYTHON
Conditional Statements:

If
Control Flow
If….Else

Nested If
1. Sequential

• Sequential statements are a set of


statements whose execution process
happens in a sequence.

• The problem with sequential statements is


that if the logic has broken in any one of
the lines, then the complete source code
execution will break.
2. Selection/Decision Control
statements
• In Python, the selection statements are also known as Decision control statements or branching
statements.
• The selection statement allows a program to test several conditions and execute instructions based on
which condition is true.
• Some Decision Control Statements are:
• Simple if
• if-else
• nested if
• if-elif-else
Simple if
• If statements are control flow statements that help us to run a particular code, but only when a certain
condition is met or satisfied. A simple if only has one condition to check.
if-else
• The if-else statement evaluates the condition and will execute the body of if if the test condition is True, but
if the condition is False, then the body of else is executed.
Nested if
Nested if statements are an if statement inside another if statement.
if-elif-else
• The if-elif-else statement is used to conditionally execute a statement or a
block of statements.
AGENDA
Python - Loops
• A loop statement allows us Python programming language provides following types of loops to

to execute a statement or handle looping requirements.

group of statements multiple Sr.No. Loop Type & Description

times. 1 while loop


Repeats a statement or group of statements while a given condition is
• The following diagram TRUE. It tests the condition before executing the loop body.

illustrates a loop statement − 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 −

Here, statement(s) may be a single statement or a block of statements.


The condition may be any expression, and true is any non-zero value. The loop
iterates while the condition is true.

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

➢ If a sequence contains an expression list, it is evaluated first.

➢ Then, the first item in the sequence is assigned to the

iterating variable iterating_var.

➢ Next, the statements block is executed.

➢ Each item in the list is assigned to iterating_var, and the

statement(s) block is executed until the entire sequence is

exhausted.
Else in for Loop

Python enables an else clause at the end of a


for loop.
 The else part is executed if the loop
terminates naturally
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 −
Nested for Loops

• A loop inside another loop


is called a nested loop.

• The inner loop will be


performed once for each
iteration of the outer loop.
NESTED WHILE LOOP
LOOP CONTROL STATEMENTS
Python Break and Continue Statement
• Python Break Statement:
• It is sometimes desirable to skip some statements
inside the loop or terminate the loop immediately
without checking the test expression.

• In such cases we can use break statements in


Python.

• The break statement allows you to exit a loop from


any point within its body, bypassing its
normal termination expression.
Syntax
• Syntax of break • The working of break statement in for
loop and while loop is shown below.
break
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
Example
Thank You

You might also like