Unit 2 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

UNIT 2: PYTHON OPERATORS AND CONTROL FLOW STATEMENTS

BASIC OPERATORS:
o Arithmetic operators

+(addition) - (subtraction) *(multiplication)

/(divide) %(reminder) //(floor division) exponent (**)

o Comparison operators

== != <= >= > <

o Assignment Operators

= += -= *= %= **= //=

o Logical Operators

and or not

o Bitwise Operators

& (binary and) | (binary or) ^ (binary xor)

<< (left shift) >> (right shift) ~ (negation)

o Membership Operators

in not in

o Identity Operators

is is not

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
1. Arithmetic operators

Operator Description

+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30

- (Subtraction) It is used to subtract the second operand from the first operand. If the first
operand is less than the second operand, the value result negative. For
example, if a = 20, b = 10 => a - b = 10

/ (divide) It returns the quotient after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a/b = 2

* (Multiplication) It is used to multiply one operand with the other. For example, if a = 20, b =
10 => a * b = 200

% (reminder) It returns the reminder after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a%b = 0

** (Exponent) It is an exponent operator represented as it calculates the first operand


power to second operand.

// (Floor It gives the floor value of the quotient produced by dividing the two
division) operands.

2. Comparison/Relational operators

Operator Description

== If the value of two operands is equal, then the condition becomes true.

!= If the value of two operands is not equal then the condition becomes true.

<= If the first operand is less than or equal to the second operand, then the condition
becomes true.

>= If the first operand is greater than or equal to the second operand, then the condition
becomes true.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

> If the first operand is greater than the second operand, then the condition becomes
true.

< If the first operand is less than the second operand, then the condition becomes true.

3. Assignment operator

Operator Description

= It assigns the the value of the right expression to the left operand.

+= It increases the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 10, b = 20 => a+
= b will be equal to a = a+ b and therefore, a = 30.

-= It decreases the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 20, b = 10 => a- =
b will be equal to a = a- b and therefore, a = 10.

*= It multiplies the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 10, b = 20 => a*
= b will be equal to a = a* b and therefore, a = 200.

%= It divides the value of the left operand by the value of the right operand and assign
the reminder back to left operand. For example, if a = 20, b = 10 => a % = b will be
equal to a = a % b and therefore, a = 0.

**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16
to a.

//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3
= 1 to a.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
4. Logical operators

Operator Description

and If both the expression are true, then the condition will be true. If a and b are the
two expressions, a → true, b → true => a and b → true.

or If one of the expressions is true, then the condition will be true. If a and b are
the two expressions, a → true, b → false => a or b → true.

not If an expression a is true then not (a) will be false and vice versa.

5. Bitwise operators

The bitwise operators perform bit by bit operation on the values of the two operands.

For example,

if a = 7;
b = 6;
then, binary (a) = 0111
binary (b) = 0011

hence, a & b = 0011


a | b = 0111
a ^ b = 0100
~ a = 1000

Operator Description

& (binary If both the bits at the same place in two operands are 1, then 1 is copied to
and) the result. Otherwise, 0 is copied.

| (binary or) The resulting bit will be 0 if both the bits are zero otherwise the resulting
bit will be 1.

^ (binary The resulting bit will be 1 if both the bits are different otherwise the
xor) resulting bit will be 0.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and vice versa.

<< (left shift) The left operand value is moved left by the number of bits present in the
right operand.

>> (right The left operand is moved right by the number of bits present in the right
shift) operand.

6. Membership operators

Python membership operators are used to check the membership of value inside a Python
data structure. If the value is present in the data structure, then the resulting value is true
otherwise it returns false.

Operator Description

in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).

not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).

7. Identity Operators

Operator Description

is It is evaluated to be true if the reference present at both sides point to the same
object.

is not It is evaluated to be true if the reference present at both side do not point to the
same object.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
8. Python Operator Precedence

The precedence of the operators is important to find out since it enables us to know
which operator should be evaluated first. The precedence table of the operators in
python is given below.

Operator Description

** The exponent operator is given priority over all the others used in the
expression.

~+- The negation, unary plus and minus.

* / % // The multiplication, divide, modules, reminder, and floor division.

+- Binary plus and minus

>> << Left shift and right shift

& Binary and.

^| Binary xor and or

<= < > >= Comparison operators (less then, less then equal to, greater then, greater
then equal to).

<> == != Equality operators.

= %= /= //= -= Assignment operators


+=
*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
2.2 control flow:

2.3conditional statements (if,if…..else,nested if)

The if statement

The if statement is used to test a particular condition and if the condition is true, it executes
a block of code known as if-block. The condition of if statement can be any valid logical
expression which can be either evaluated to true or false.

The syntax of the if-statement is given below.

if expression:
statement

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example

Example 2 : Program to print the largest of the three numbers.

The if-else statement

The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

The syntax of the if-else statement is given below.

if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person is eligible to vote or not.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example 2: Program to check whether a number is even or not.

The elif statement

The elif statement enables us to check multiple conditions and execute the specific block of
statements depending upon the true condition among them.

However, using elif is optional.

The syntax of the elif statement is given below.

if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example 1

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example 2

2.4looping in python (while loop,for loop,nested loops)

There are the following advantages of loops in Python.

1. It provides code re-usability.


2. Using loops, we do not need to write the same code again and again.
3. Using loops, we can traverse over the elements of data structures (array or linked
lists).

There are the following loop statements in Python.

Loop Description
Statement

for loop The for loop is used in the case where we need to execute some part of the code
until the given condition is satisfied. The for loop is also called as a per-tested
loop. It is better to use for loop if the number of iteration is known in advance.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

while loop The while loop is to be used in the scenario where we don't know the number of
iterations in advance. The block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is also called a pre-tested
loop.

do-while The do-while loop continues until a given condition satisfies. It is also called post
loop tested loop. It is used when it is necessary to execute the loop at least once
(mostly menu driven programs).

Python for loop

The for loop in Python is used to iterate the statements or a part of the program several
times. It is frequently used to traverse the data structures like list, tuple, or dictionary.

The syntax of for loop in python is given below.

for iterating_var in sequence:


statement(s)

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
example : printing the table of the given number

Nested for loop in python

Python allows us to nest any number of for loops inside a for loop.

The inner loop is executed n number of times for every iteration of the outer loop.

The syntax of the nested for loop in python is given below.

for iterating_var1 in sequence:


for iterating_var2 in sequence:
#block of statements
#Other statements

Example 1
n = int(input("Enter the number of rows you want to print?"))
i,j=0,0
for i in range(0,n):
print()
for j in range(0,i+1):
print("*",end="")

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Output:

Enter the number of rows you want to print?5


*
**
***
****

Using else statement with for loop

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example 2

Python while loop


while expression:
statements

Example 1

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example 2:

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Using else with Python while loop

The else block is executed when the condition given in the while statement becomes false.

if the while loop is broken using break statement, then the else block will not be executed
and the statement present after else block will be executed.

Consider the following example.

2.5loop manipulation using continue, pass, break, else


Python continue Statement

The continue statement in python is used to bring the program control to the beginning of
the loop.

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
The continue statement skips the remaining lines of code inside the loop and start with the
next iteration.

It is mainly used for a particular condition inside the loop so that we can skip some specific
code for a particular condition.

The syntax of Python continue statement is given below.

1. #loop statements
2. continue;
3. #the code to be skipped

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Example 2

Python Pass

In Python, pass keyword is used to execute nothing;

It means, when we don't want to execute code, the pass can be used to execute empty.

It is same as the name refers to.

Python Pass Syntax

pass

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Python break statement

The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops.

break is used to abort the current execution of the program and the control goes to the next
line after the loop.

The break is commonly used in the cases where we need to break the loop for a given
condition.

The syntax of the break is given below.

#loop statements
break;

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

M M POLYTECHNIC ,THERGAON
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS

Python else statement


The else block is executed when the condition given in the loop statement becomes false.

M M POLYTECHNIC ,THERGAON

You might also like