Operator

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

Course Name :

Python Programming

By- Prof. Renuka Dewangan

Zeal Group of Management Institutes


Operators

• Operators in general are used to perform


operations on values and variables. These are
standard symbols used for the purpose of
logical and arithmetic operations.
• OPERATORS: These are the special symbols.
Eg- + , * , /, etc.
• OPERAND: It is the value on which the
operator is applied.

Zeal Group of Management Institutes


Types of Operators in Python

• 1. Arithmetic Operators
• 2. Comparison
• 3. Logical
• 4. Bitwise
• 5. Assignment
• 6. Identity Operators and Membership
Operators

Zeal Group of Management Institutes


Arithmetic Operators in Python

Operator Description Syntax


+ Addition: adds two operands x+y

– Subtraction: subtracts two operands x–y

* Multiplication: multiplies two x*y


operands

/ Division (float): divides the first x/y


operand by the second

// Division (floor): divides the first x // y


operand by the second

Modulus: returns the remainder when


% the first operand is divided by the x%y
second

** Power: Returns first raised to power x ** y


second

Zeal Group of Management Institutes


Example of Arithmetic Operators in Python

• Division Operators
• Division Operators allow you to divide two numbers and
return a quotient, i.e., the first number or number at the left
is divided by the second number or number at the right and
returns the quotient.
• There are two types of division operators:
• Float division
• Floor division

Zeal Group of Management Institutes


• Float division
• The quotient returned by this operator is
always a float number, no matter if two
numbers are integers. For example:
• # python program to demonstrate the use of "/"
• print(5/5)
• print(10/2)
• print(-10/2)
• print(20.0/2)
Zeal Group of Management Institutes
Zeal Group of Management Institutes
Integer division( Floor division)

• The quotient returned by this operator is dependent on the


argument being passed. If any of the numbers is float, it
returns output in float. It is also known as Floor division
because, if any number is negative, then the output will be
floored. For example:
• # python program to demonstrate the use of "//"
• print(10//3)
• print (-5//2)
• print (5.0//2)
• print (-5.0//2)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Precedence of Arithmetic Operators in Python

• The precedence of Arithmetic Operators in python is as follows:


• P – Parentheses
• E – Exponentiation
• M – Multiplication (Multiplication and division have the same precedence)
• D – Division
• A – Addition (Addition and subtraction have the same precedence)
• S – Subtraction
• The modulus operator helps us extract the last digit/s of a
number. For example:
• x % 10 -> yields the last digit
• x % 100 -> yield last two digits

Zeal Group of Management Institutes


# Examples of Arithmetic Operator
• a=9
• b=4

• # Addition of numbers
• add = a + b

• # Subtraction of numbers
• sub = a - b

• # Multiplication of number
• mul = a * b

• # Modulo of both number


• mod = a % b

• # Power
• p = a ** b

Zeal Group of Management Institutes


• # print results
• print(add)
• print(sub)
• print(mul)
• print(mod)
• print(p)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Comparison Operators in Python
• In Python Comparison of Relational Operators compares the values. It
either returns True or False according to the condition.
Operator Description Syntax

> Greater than: True if the left operand x>y


is greater than the right

< Less than: True if the left operand is x<y


less than the right

== Equal to: True if both operands are x == y


equal

!= Not equal to – True if operands are x != y


not equal

Greater than or equal to True if the


>= left operand is greater than or equal x >= y
to the right

Less than or equal to True if the left


<= operand is less than or equal to the x <= y
right

= is an assignment operator and == comparison


operator.
Zeal Group of Management Institutes
Precedence of Comparison Operators in Python

• In python, the comparison operators have lower precedence than the arithmetic
operators. All the operators within comparison operators have same precedence
order.
• Example of Comparison Operators in Python

Zeal Group of Management Institutes


• # Examples of Relational Operators
• a = 13
• b = 33

• # a > b is False
• print(a > b)

• # a < b is True
• print(a < b)

Zeal Group of Management Institutes


• # a == b is False
• print(a == b)

• # a != b is True
• print(a != b)

• # a >= b is False
• print(a >= b)

• # a <= b is True
• print(a <= b)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Logical Operators in Python
• Python LogicalOperators perform Logical AND, Logical OR,
and Logical NOT operations. It is used to combine conditional
statements.
Operator Description Syntax

and Logical AND: True if both the operands x and y


are true

or Logical OR: True if either of the x or y


operands is true

not Logical NOT: True if the operand is not x


false

Precedence of Logical Operators in Python


The precedence of Logical Operators in python is as follows:
1.Logical not
2.logical and
3.logical or

Zeal Group of Management Institutes


Example of Logical Operators in Python
• # Examples of Logical Operator
• a = True
• b = False

• # Print a and b is False


• print(a and b)

• # Print a or b is True
• print(a or b)

• # Print not a is False


• print(not a)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Bitwise Operators in Python
• Python Bitwise Operator act on bits and perform bit-by-bit operations.
These are used to operate on binary numbers.

Operator Description Syntax


& Bitwise AND x&y

| Bitwise OR x|y

~ Bitwise NOT ~x

^ Bitwise XOR x^y

>> Bitwise right shift x>>

<< Bitwise left shift x<<

Zeal Group of Management Institutes


Precedence of Bitwise Operators in Python

• The precedence of Bitwise Operators in


python is as follows:
• Bitwise NOT
• Bitwise Shift
• Bitwise AND
• Bitwise XOR
• Bitwise OR

Zeal Group of Management Institutes


Bitwise Operators in Python

• Here is an example showing how Bitwise Operators in Python work:


• # Examples of Bitwise operators
• a = 10
• b=4

• # Print bitwise AND operation


• print(a & b)

• # Print bitwise OR operation


• print(a | b)

• # Print bitwise NOT operation


• print(~a)

• # print bitwise XOR operation


• print(a ^ b)

• # print bitwise right shift operation


• print(a >> 2)

• # print bitwise left shift operation


• print(a << 2)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Assignment Operators in Python
• Python Assignment Operators are used to assign values to the variables.

Operator Description Syntax

= Assign the value of the right side of the expression to the left side operand x=y+z

Add AND: Add right-side operand with left-side operand and then assign to left
+= a+=b a=a+b
operand

Subtract AND: Subtract right operand from left operand and then assign to left
-= a-=b a=a-b
operand

Multiply AND: Multiply right operand with left operand and then assign to left
*= a*=b a=a*b
operand

/= Divide AND: Divide left operand with right operand and then assign to left operand a/=b a=a/b

Modulus AND: Takes modulus using left and right operands and assign the result
%= a%=b a=a%b
to left operand

Divide(floor) AND: Divide left operand with right operand and then assign the
//= a//=b a=a//b
value(floor) to left operand

Exponent AND: Calculate exponent(raise power) value using operands and assign
**= a**=b a=a**b
value to left operand

&= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b

|= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b

^= Performs Bitwise xOR on operands and assign value to left operand a^=b a=a^b

>>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b

<<= Performs Bitwise left shift on operands and assign value to left operand a <<= b a= a << b

Zeal Group of Management Institutes


Assignment Operators in Python
• # Examples of Assignment Operators
• a = 10

• # Assign value
• b=a
• print(b)

• # Add and assign value


• b += a
• print(b)

• # Subtract and assign value


• b -= a
• print(b)

• # multiply and assign


• b *= a
• print(b)

• # bitwise lishift operator


• b <<= a
• print(b)

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Identity Operators in Python
• In Python, is and is not are the Identity Operator both are used to check if
two values are located on the same part of the memory. Two variables
that are equal do not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical

• Example Identity Operators in Python


• a = 10
• b = 20
• c=a
Output
print(a is not b) True
True
• print(a is c)

Zeal Group of Management Institutes


Membership Operators in Python
• In Python, in and not in are the membership
operators that are used to test whether a
value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence

Zeal Group of Management Institutes


Examples of Membership Operators in Python

• # Python program to illustrate


• # not 'in' operator
• x = 24
• y = 20
• list = [10, 20, 30, 40, 50]

• if (x not in list):
• print("x is NOT present in given list")
• else:
• print("x is present in given list")

• if (y in list):
• print("y is present in given list")
• else:
• print("y is NOT present in given list")

Zeal Group of Management Institutes


Zeal Group of Management Institutes
Ternary Operator in Python
• In Python, Ternary operators also known as conditional expressions are operators
that evaluate something based on a condition being true or false.
It simply allows testing a condition in a single line replacing the multiline if-else
making the code compact.

Syntax : [on_true] if [expression] else [on_false]


Examples of Ternary Operator in Python
# Program to demonstrate conditional operator
a, b = 10, 20

# Copy value of a in min if a < b else copy b


min = a if a < b else b
print(min)
Output:
10

Zeal Group of Management Institutes


Precedence and Associativity of Operators in Python
• In Python, Operator precedence and associativity determine the priorities
of the operator.
• Operator Precedence in Python
• This is used in an expression with more than one operator with different
precedence to determine which operation to perform first.
• # Examples of Operator Precedence

• # Precedence of '+' & '*'


• expr = 10 + 20 * 30
• print(expr)

• # Precedence of 'or' & 'and'


• name = "Alex"
• age = 0

• if name == "Alex" or name == "John" and age >= 2:


• print("Hello! Welcome.")
• else:
• print("Good Bye!!")

Zeal Group of Management Institutes


Output
610 Hello! Welcome.

Zeal Group of Management Institutes


Operator Associativity in Python
• If an expression contains two or more operators with the same precedence then
Operator Associativity is used to determine. It can either be Left to Right or from
Right to Left.
• The following code shows how Operator Associativity in Python works:
• # Examples of Operator Associativity
# Left-right associativity
• # 100 / 10 * 10 is calculated as
• # (100 / 10) * 10 and not
• # as 100 / (10 * 10)
• print(100 / 10 * 10)

• # Left-right associativity
• # 5 - 2 + 3 is calculated as
• # (5 - 2) + 3 and not
• # as 5 - (2 + 3)
• print(5 - 2 + 3)

• # left-right associativity
• print(5 - (2 + 3))

• # right-left associativity
• # 2 ** 3 ** 2 is calculated as
• # 2 ** (3 ** 2) and not
• # as (2 ** 3) ** 2
• print(2 ** 3 ** 2)

Zeal Group of Management Institutes


Zeal Group of Management Institutes

You might also like