0% found this document useful (0 votes)
9 views22 pages

Chapter 2 - Operators and Control Flow Statments

Uploaded by

prasaddigole81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Chapter 2 - Operators and Control Flow Statments

Uploaded by

prasaddigole81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

OPERATORS AND

CONTROL FLOW Mr. Rupnar P. M.

STATEMENTS
TYPES OF OPERATOR
Python language supports the following types of
operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
ARITHMETIC OPERATORS
Operator Description Example

Adds values on either side of


+ Addition a + b = 30
the operator.
Subtracts right hand operand
- Subtraction a – b = -10
from left hand operand.
Multiplies values on either
* Multiplication a * b = 200
side of the operator
Divides left hand operand by
/ Division b/a=2
right hand operand
Divides left hand operand by right
% Modulus hand operand and returns b%a=0
remainder
** Performs exponential (power) a**b =10 to
Exponent calculation on operators the power 20

Floor Division - The division of


operands where the result is the
9//2 = 4 and
quotient in which the digits after
9.0//2.0 = 4.0,
the decimal point are removed. But
// -11//3 = -4,
if one of the operands is negative,
-11.0//3 = -
the result is floored, i.e., rounded
4.0
away from zero (towards negative
infinity) −
• Comparison (Relational) Operators
• Assume a =2 and b=7
Operat
Description Example
or
If the values of two operands are equal, (a == b) is not
==
then the condition becomes true. true.
If values of two operands are not equal,
!= (a != b) is true.
then condition becomes true.
If the value of left operand is greater
(a > b) is not
> than the value of right operand, then
true.
condition becomes true.
If the value of left operand is less than
< the value of right operand, then (a < b) is true.
condition becomes true.
Comparison (Relational) Operators

If the value of left operand is


greater than or equal to the (a >= b) is not
>=
value of right operand, then true.
condition becomes true.
If the value of left operand is
less than or equal to the
<= (a <= b)
value of right operand, then
condition becomes true.
PYTHON ASSIGNMENT
OPERATORS
Operator Description Example
Assigns values from right
c = a + b assigns value of a
= side operands to left side
+ b into c
operand
It adds right operand to
+= Add the left operand and c += a is equivalent to c = c
AND assign the result to left +a
operand
It subtracts right operand
-=
from the left operand and c -= a is equivalent to c = c -
Subtract
assign the result to left a
AND
operand
PYTHON ASSIGNMENT
OPERATORS
It divides left operand with
the right operand and c /= a is equivalent
/= Divide AND
assign the result to left to c = c / a
operand
It takes modulus using two
%= Modulus c %= a is equivalent
operands and assign the
AND to c = c % a
result to left operand
Performs exponential
**= Exponent (power) calculation on c **= a is equivalent
AND operators and assign value to c = c ** a
to the left operand
It performs floor division
//= Floor c //= a is equivalent
on operators and assign
PYTHON BITWISE
OPERATORS
Operator Description Example
Operator copies a bit to the
& Binary (a & b) (means 0000
result if it exists in both
AND 1100)
operands
It copies a bit if it exists in (a | b) = 61 (means
| Binary OR
either operand. 0011 1101)
^ Binary It copies the bit if it is set in (a ^ b) = 49 (means
XOR one operand but not both. 0011 0001)
PYTHON BITWISE
OPERATORS
(~a ) = -61 (means
~ Binary
1100 0011 in 2's
Ones It is unary and has the effect
complement form
Complemen of 'flipping' bits.
due to a signed
t
binary number.
The left operands value is
<< Binary moved left by the number of a << 2 = 240
Left Shift bits specified by the right (means 1111 0000)
operand.
The left operands value is
>> Binary moved right by the number of a >> 2 = 15 (means
Right Shift bits specified by the right 0000 1111)
PYTHON LOGICAL
OPERATORS
Operator Description Example
and Logical If both the operands are true
(a and b) is true.
AND then condition becomes true.

If any of the two operands


or Logical
are non-zero then condition (a or b) is true.
OR
becomes true.
not Logical Used to reverse the logical Not(a and b) is
NOT state of its operand. false.
PYTHON MEMBERSHIP
OPERATORS
Oper
Description Example
ator
Evaluates to true if it
x in y, here in results in a 1
finds a variable in the
in if x is a member of
specified sequence and
sequence y.
false otherwise.
Evaluates to true if it
x not in y, here not in
does not finds a variable
not in results in a 1 if x is not a
in the specified sequence
member of sequence y.
and false otherwise.
PYTHON IDENTITY OPERATORS
Operator Description Example
Evaluates to true if the
variables on either side of x is y, here is results
is the operator point to the in 1 if id(x) equals
same object and false id(y).
otherwise.
Evaluates to false if the
x is not y, here is
variables on either side of
not results in 1 if
is not the operator point to the
id(x) is not equal to
same object and true
id(y).
otherwise.
Operator Description
** Exponentiation (raise to the power)
Complement, unary plus and minus
~+- (method names for the last two are +@
and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'td>
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *=
Assignment operators
**=
is is not Identity operators
CONTROL FLOW
STATEMENTS
Sr.No. Statement & Description
if statements An if statement consists of a
1 boolean expression followed by one or more
statements.
if...else statements An if statement can be
2 followed by an optional else statement, which
executes when the boolean expression is FALSE.
nested if statements You can use one if or else if
3 statement inside another if or else if
statement(s).
IF
IF ELSE
IF…..ELIF……ELSE
LOOPING STATEMENTS
Sr.No
Loop Type & Description
.
while loop Repeats a statement or group of
1 statements while a given condition is TRUE. It
tests the condition before executing the loop body.
for loop Executes a sequence of statements
2 multiple times and abbreviates the code that
manages the loop variable.
nested loops You can use one or more loop inside
3
any another while, for or do..while loop.
PYTHON WHILE LOOP
STATEMENTS The syntax of a while loop in
Python programming language is −
while expression: statement(s)
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.
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.
LOOP CONTROL STATEMENTS
Sr.
No Control Statement & Description
.
break statement Terminates the loop statement and
1 transfers execution to the statement immediately
following the loop.
continue statement Causes the loop to skip the
2 remainder of its body and immediately retest its
condition prior to reiterating.
pass statement The pass statement in Python is used
3 when a statement is required but you do not want any
command or code to execute.
Else statement The else clause executes after the loop

You might also like