Operators
Operators
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic operators
Arithmetic operators: Used for performing
arithmetic operations.
Jan 6, 2025 5
For example ,if x= 7,y=2
addition: x+y=9
subtraction:x-y=5
Multiplication:x*y=14
Division: x/y=3.5
Floor division(Integer division): x // y=3 (rounds off the
answer to the nearest whole number)
Jan 6, 2025 6
Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then −
Jan 6, 2025 16
Logical operators
If A and B are two relational expressions, say A = (Num1>2000),
B= (Num2>100), the result of combining A and B using logical operator is
based on the result of A and B as shown below:
Jan 6, 2025 17
Logical Operators
Operator Description Example
a = 0011 1100
b = 0000 1101
-----------------
a<<2 # 240 = 1111 0000
a >> 2 # 15 =0000 1111
Bitwise Operators
Bitwise or: a | b
Bitwise exclusive or: a ^ b # Don't confuse this with
exponentiation
Bitwise and: a & b
Shift a left or right by b bits: a << b, a >> b
Bitwise Operators
Operator Description
& Bitwise and Operator copies a bit to the result if it
exists in both operands
Operator Description
<< Binary Left The left operands value is moved left by the
Shift number of bits specified by the right operand.
>> Binary Right The left operands value is moved right by the
Shift number of bits specified by the right operand.
Membership Operators
Two variables that are equal does not imply that they are
identical.
Identity Operators
Jan 6, 2025 28
The operator precedence in Python is listed in the following
table. It is in descending order (upper group has higher
precedence than the lower ones).
Jan 6, 2025 29
Jan 6, 2025 30
Precedence of operators
What do you think is the output of 5+4*9%(3+1)/6-1?
It is done based on the precedence of the operator. Precedence
Jan 6, 2025 32
Precedence of operators
Jan 6, 2025 33
Comments in the program
Comments in Python are the lines in the code that are
ignored by the interpreter during the execution of the
program.
Comments enhance the readability of the code and help
the programmers to understand the code very carefully.
There are three types of comments in Python –
Single line Comments
Multiline Comments
Docstring Comments
Jan 6, 2025 34
Single-Line Comments
Jan 6, 2025 35
Single-Line Comments
Example :
#Defining a variable to store number.
n = 50 #Store 50 as value into variable n.
In the above example program, the first line starts with the hash
symbol, so the entire line is considered a comment.
In the second line of code, "N = 50" is a statement, and after the
statement, the comment begins with the # symbol.
From the # symbol to the end of this line, the line will be treated
as a comment.
Jan 6, 2025 36
Multiline Comments
Python does not provide the option for multiline comments.
However, there are different ways through which we can write
multiline comments.
Jan 6, 2025 37
Multiline Comments
Jan 6, 2025 38
Multiline Comments
Jan 6, 2025 39
Coding standards are the set of guidelines that can be
used to enhance the readability and clarity of the
program and make it easy to debug and maintain the
program.
Jan 6, 2025 40
Basic Syntax
Jan 6, 2025 41
QUESTIONS
1. Explain operators in detail with examples.
2. Evaluate the expression x**y**z given x=2,y=3,z=2 .
3. Explain precedence and write precedence table.
4. What you mean by associativity?
5. What do "comments" in programming refer to? How can this
be implemented in Python?
6. Describe Arithmetic operators, Assignment operators,
Comparison operators, Logical operators, and Bitwise
operators in detail with examples
Jan 6, 2025