07/26/2025
Operators in
Python
B.Hemalatha
Assistant Professor,
Department of Computer Science
Engineering,
Velammal Engineering College.
VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University, Chennai
Accredited by NAAC & NBA
1
2.
07/26/2025
Recap
Introduction toPython
Features of Python
Why Python
Python Setup and Fundamentals
Values and Variables
Keywords and Data Types
2
3.
07/26/2025 3
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
1. Introduction to Python
• Python - general-purpose language.
• High level programming language.
• It was created by Guido Van Rossum in 1991.
• Further developed by the Python Software
Foundation.
• It is used to develop web applications, data
science, creating software prototypes and
etc.
4.
07/26/2025 4
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
2. Features of Python Interactive: Write a program directly on the Python
prompt
Interact directly with the interpreter and get the
immediate results.
Extensible Feature
Integrate Python with other languages such as
C, C++, and Java.
Python has a large and extensive library
wide set of functions for rapid application
development.
Dynamically Typed: Means the type for a value is
decided at runtime.
Not necessary to specify the type of data when
declaring it
GUI Programming Support
provides many graphical user interface (GUI)
libraries PyQT5, Tkinter
5.
07/26/2025 5
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
6.
07/26/2025 6
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
7.
07/26/2025 7
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
4. Python Setup and Fundamentals
8.
07/26/2025 8
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
4. Python using IDLE
Download the Python 3 Installer at python.org.
9.
07/26/2025 9
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
4. Python- IDLE
Interactive mode Script mode
10.
07/26/2025 10
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
4. Python Using Jupyter Notebook
Installing Jupyter Notebook using Anaconda Navigator
11.
07/26/2025 11
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
5. Values and Variables
• Variables are containers for storing data values.
Syntax:
Variable_name = Value (value belongs to any
datatype)
Example
x = 5
y = ”John”
print(x)
print(y)
12.
07/26/2025 12
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
5. Values and Variables - examples
1. Age = 99
2. Name = ‘Rajesh’ or “Rajesh”
3. Pi_Value = 3.14
4. Greeting = ‘Good Morning’
5. Rs=200
Note: = is called as Assignment Operator
13.
07/26/2025 13
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
5. Values and Variables
Python allows you to assign values to multiple variables in one line:
Example :
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
14.
07/26/2025 14
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
6. Data Types
A data type defines the type of data and allows languages to organize
different kinds of data
Eg.123 is an integer data while “hello” is a String data type
15.
07/26/2025 15
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
6. Data Types
16.
07/26/2025 16
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
6. Data Types
The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed
Immutable data types Mutable data types
1.Numbers
2. String
3. Tuple
4. Bool
5. List
6. Dictionaries
7. Set
Examples in Jupyter Notebook
07/26/2025 18
Velammal EngineeringCollege
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
6. Keywords
Keywords are the reserved words in Python.
Cannot use a keyword as a variable name, function name or
any other identifier.
Keywords are case sensitive.
Poll 1 - Recap
19.
OPERATORS
Why
Manipulate
individual items
andreturn
Result
Control program
flow
Benefits
Understanding
and usage of
Operators
helps for
writing efficient code
What are Operators?
Symbols that perform an operation on
one or more operands.
20.
OPERATORS
Operator Types
1. ArithmeticOperators
2. Comparison (Relational)
Operators
3. Assignment Operators
4. Logical Operators
5. Unary Operator
6. Bitwise Operators
7. Membership Operators
8. Identity Operators
Mathematical operators
Compare values
Right operand assigned to the
left operand
Combine conditional
statements
perform operations on a
single operand
bit by bit operation
test whether a value or a
variable is found in a
sequence
Compare object based on
memory location
+ - * / // %
== != < > >= <=
= += -= *= /= //= %=
and or not xor
-
& | ~ ^ >> <<
in not in
is is not
21.
Arithmetic Operators
Basic Operators
+
-
*
/
%
//
**
Examples
x= 5
print(x+3)
8
What ?
Perform arithmetic operations between two
numeric operands
Add
Subtract
Multiply
Divide
Modulus
Floor-division
Exponentiation
x = 12
print(x-2)
10
x = 21
print(x/2)
10.5
x = 21
print(x//4)
5
x = 2
print(x**3)
8
Using Operators withStrings
Python supports concatenating strings using the
addition operator:
Python Script
OUTPUT
Welcome to Python Bridge Course
s1=“Welcome to "
s2=“Python Bridge Course"
concat= s1+s2
print(concat)
23
24.
Using Operators withStrings
Python also supports multiplying strings to form a
string with a repeating sequence:
Python Script
OUTPUT
repeat repeat repeat
s = "repeat " * 3
print(s)
24
25.
Using Operators withLists
Lists can be joined with the addition operators:
Python Script
OUTPUT
[1, 3, 5, 7, 2, 4, 6, 8]
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
25
26.
Using Operators withLists
Python supports forming new lists with a repeating
sequence using the multiplication operator:
Python Statement
OUTPUT
[1, 2, 3, 1, 2, 3, 1, 2, 3]
print([1,2,3] * 3)
26
27.
Assignment Operators
Basic Operators
=
+=
- =
* =
/ =
% =
// =
** =
Examples
x = 5
x + = 3
#x=x+3
print(x)
8
Use Cases
Short hand
expressions
Assigning values
to variables
Modifying
variable values
Benefits
Improved readability
Reduced repetition.
Assign
Add and Assign
Subtract and Assign
Multiply and Assign
Divide and Assign
Modulus and Assign
Floor-divide and Assign
Exponentiation and Assign
x = 12
x - = 2
print(x)
10
x = 21
x / = 4
print(x)
5.25
x = 21
x // = 4
print(x)
5
x = 2
x ** = 3
print(x)
8
Logical operators
Basic Operators
and
or
not
Examples
x,y= 10,10
print(x>y and x==y)
False
print(x>y or x==y)
True
print(x>=y and x==y)
True
Use Cases
Combine multiple
conditions
Benefits
control the
flow of program execution
based on multiple
conditions.
Evaluate to make
decisions
Logical AND :
True if both
operands are
True
Logical OR : True if
at least one operand
is True
Logical NOT :
Reverses the boolean
state of the operand
x,y=10,20
print(not(x>y))
True
29
Poll 2
30.
30
Membership Operators
What
• Testwhether a value is present
in a sequence, such as a list or a
string
Operators
• in
• not in
Returns
• Boolean value True if value
present in the sequence.
• Boolean value True if value
present in the sequence.
x={"python","java","c
language","php","vb"}
print("java" in x)
print(“php” not in x)
Output
True
Fa;se
07/26/2025
PROGRAM OUTPUT
x,y,z=15,15,10
print(x isy)
print(x is z)
print(y is z)
print(x is not y)
print(x is not z)
x,y,z=[1,2,3,4,5], [1,2,3,4,5], [6,7,8,9]
print(x is y)
print(x is z)
print(y is z)
print(x is not y)
print(x is not z)
True
False
False
False
True
False
False
False
True
True
32
33.
07/26/2025
UNARY OPERATOR
❑The unaryoperators require only one operand; they perform
various operations, negating an expression, or inverting the
value of a boolean.
❑unary - (minus) operator yields the negation of its numeric
argument
❑The unary ~ (invert) operator yields the bitwise inversion of its
integer argument. The bitwise inversion of x is defined as -
(x+1). It only applies to integral numbers.
33
34.
07/26/2025
BITWISE OPERATORS
❑The bitwiseoperators perform bit by bit operation on the values of the two
operands.
Eg:
if a = 7
b = 6
then,
binary (a) = 0111
binary (b) = 0011
hence, a & b = 0011
a | b = 0111
a ^ b = 0100
~ a = 1000
34
41
Precedence and Associativity
Determines the order in which they
are evaluated.
The precedence of operators
determines which operation is
performed first in an expression.
The associativity of operators
determines the order in which
operations are performed when
multiple operators of the same
precedence are present.
42.
07/26/2025
PROGRAMS TO ILLUSTRATEPRECEDENCE
AND ASSOCIATIVITY
p= 20
q=10
r=15
s=5
t=0
t=(p+q)*r / s
print (“Value of t”,t)
t=(p+q)* (r / s)
print (“Value of t”,t)
print ((2**4)**2)
OUTPUT
90
90
256
42
07/26/2025
ILLUSTRATIVE PROGRAMS -SIMPLE
ARITHMETIC PROGRAMS
1. Write a program to find average of two numbers in python
2. Write a program to convert temperature from Fahrenheit to Celsius
4. Write a program to find area of circle
5. Write a program to find the distance of two points
6. Write a Python program to accept two numbers, compute the sum, product and print the
result.
7. Write a Python program to accept two numbers, find the greatest and print the result.
8. Write a Python program to exchange the value of two variables
9. Write a Python program to perform addition, subtraction, division, multiplication on two
floating point numbers.
44
45.
07/26/2025 45
Go tocollege
Learn
Leave from college
Any
pocket
money
left?
Buy an ice-cream
Return home
YES
NO
46.
07/26/2025 46
Go tocollege
Learn
Leave from college
Any
pocket
money
left?
Buy an ice-cream
Return home
YES
NO
47.
07/26/2025 47
Get readyfor shopping
Raining
heavily
?
wear raincoat
Go to shop
YES
NO
Take umbrella
Is it
raining
?
YES
NO
Wear a cap
48.
07/26/2025 48
Get readyfor shopping
Rainy
day?
Wear sweatshirt
Wear Coat
YES
NO
Wear raincoat
Sunny
day?
snowy
day?
windy
day?
NO
NO
Wear Cap
Wear t-shirt Go to shop
YES
YES
YES
NO