0% found this document useful (0 votes)
6 views15 pages

Python Operators Explained

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, membership, identity, and bitwise operators. It explains their functions with examples and discusses operator precedence and evaluation order. Additionally, it emphasizes the importance of understanding these operators for effective problem-solving and programming in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Python Operators Explained

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, membership, identity, and bitwise operators. It explains their functions with examples and discusses operator precedence and evaluation order. Additionally, it emphasizes the importance of understanding these operators for effective problem-solving and programming in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

23ITT202 – Problem Solving and Python Programming

(Module 1 - Session 4)

Module 1
TOPIC : Operators
[Link]
AP/IT

23ITT202-Problem Solving and Python Programming 1


Python Operators
The operator is a symbol that performs a specific operation between two operands.
In the example below, we use the + operator to add together two values:
Example:
print(10 + 5)

23ITT202-Problem Solving and Python Programming 2


Types of Operators:

23ITT202-Problem Solving and Python Programming 3


Arithmetic operators:
Arithmetic operators are used to perform basic mathematical operations.
Assume variable a = 10 and variable b =20, then

Operator Name Example


+ Addition a + b = 30
- Subtraction a – b = -10
* Multiplication a * b = 200
/ Division b/a=2
% Modulus b%a=0
** Exponent a**b =10**20
// Floor Division 9//2 = 4

23ITT202-Problem Solving and Python Programming 4


Comparison (Relational) Operators:

• Comparison operators are used to compare values.

• It either returns True or False according to the condition. Assume, a=5 and b=10

Operator Name Example


== Equal (a == b) is not true.
!= Not equal (a != b) is true.
> Greater than (a > b) is not true.
< Less than (a < b) is true.
>= Greater than or equal to (a >= b) is not true.
<= Less than or equal to (a <= b) is true.

23ITT202-Problem Solving and Python Programming 5


Assignment Operators:
-Assignment operators are used in Python to assign values to variables.
Operator Example Same As
= a = 10 a = 10
+= a += 30 a = a + 30
-= a -= 15 a = a - 15
*= a *= 10 a = a * 10
/= a /= 5 a=a/5
%= a %= 5 a=a%5
**= a **= 4 a = a ** 4
//= a //= 5 a = a // 5
&= a &= 5 a=a&5
|= a |= 5 a=a|5
^= a ^= 5 a=a^5
>>= a >>= 5
23ITT202-Problem a =Programming
Solving and Python a >> 5 6
Logical Operators
• Python logical operators are used to combine two or more conditions and check
the final result.

23ITT202-Problem Solving and Python Programming 7


Example Output

a = True
b = False
print('a and b is',a and b) a and b is False
print('a or b is',a or b) a or b is True
print('not a is',not a) not a is False

23ITT202-Problem Solving and Python Programming 8


Membership Operators
• Python's membership operators test for membership in a sequence, such as
strings, lists, or tuples.
• There are two membership operators as explained below

Operator Description Example


Returns True if it finds a
in variable in the specified a in b
sequence, false otherwise.
returns True if it does not
finds a variable in the
not in a not in b
specified sequence and
false otherwise.

23ITT202-Problem Solving and Python Programming 9


Let, x=[5,3,6,4,1]. To check particular item in list or not, in and not in operators are used

Example:
x=[5,3,6,4,1]
print(5 in x)
print(5 not in x)
OUTPUT
True
false

23ITT202-Problem Solving and Python Programming 10


Identity Operators:
They are used to check if two values (or variables) are located on the same part of
the memory.
x=5 Output
y=5
False
x2 = 'Hello'
y2 = 'Hello' True
print(x is not y)
print(x2 is y2)

23ITT202-Problem Solving and Python Programming 11


Bitwise Operators
• Bitwise operator works on bits and performs bit by bit operation.
• These operators are used to compare binary numbers.

Operator Name Example


& Bitwise AND a&b
| Bitwise OR a|b
^ Bitwise XOR a^b
~ Bitwise NOT ~a
<< Zero fill left shift a << 3
>> Signed right shift a >> 3

23ITT202-Problem Solving and Python Programming 12


OPERATOR PRECEDENCE:
• When an expression contains more than one operator, the order of evaluation depends on the order of
operations.
• The following table lists all operators from highest precedence to lowest.
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'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators

is is not Identity operators


in not in Membership operators
not or and Logical operators

23ITT202-Problem Solving and Python Programming 13


• For mathematical operators, Python follows mathematical convention.

• The acronym PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) is a


useful way to remember the rules

❖ Parentheses have the highest precedence and can be used to force an expression to evaluate in the order
you want.

❖ Since expressions in parentheses are evaluated first, 2 * (3-1)is 4, and (1+1)**(5-2) is 8.

a=9-12/3+3*2-1 a=2,
a=? b=12,
a=9-4+3*2-1 c=1
a=9-4+6-1 d=a<b>c-1
a=5+6-1 d=2<12>1-1
a=11-1 d=2<12>0
a=10 d=1>0
d=1

23ITT202-Problem Solving and Python Programming 14


Thankyou

23ITT202-Problem Solving and Python Programming 15

You might also like