0% found this document useful (0 votes)
5 views29 pages

Python - Operators & Precedence

The document provides an overview of operators in Python, categorizing them into groups such as arithmetic, assignment, comparison, logical, identity, membership, bitwise, and unary operators. It explains the functionality and examples of each operator type, as well as operator precedence and associativity rules. Additionally, it briefly covers expressions in Python, string operations, and data structures like tuples, lists, and dictionaries.

Uploaded by

Vidhya B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views29 pages

Python - Operators & Precedence

The document provides an overview of operators in Python, categorizing them into groups such as arithmetic, assignment, comparison, logical, identity, membership, bitwise, and unary operators. It explains the functionality and examples of each operator type, as well as operator precedence and associativity rules. Additionally, it briefly covers expressions in Python, string operations, and data structures like tuples, lists, and dictionaries.

Uploaded by

Vidhya B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 29

UNIT I – Part 3 -Operators

02/23/2025 Unit 1 1
Operators
- Operators are used to perform operations on
variables and values. Python divides the operators
in the following groups:
1) Arithmetic operators
2) Assignment operators
3) Comparison operators
4) Logical operators
5) Identity operators
6) Membership operators
7) Bitwise operators
02/23/2025 Unit 1 2
8)Unary operators
Operators
1) Arithmetic Operator: (x=100 y=200)
Operator Meaning Example
+ Add two operands x+y
- Subtract right operand from the left x-y
* Multiply two operands x*y
/ Divide left operand by the right one a/b
% Modulus - remainder of the division of x%y
left operand by the right
// Floor division - division that results into x//y
12//5 = 2
whole number adjusted to the left in the 12.0//5.0=2.0
-19//5=-4
number line -20.0//3=-7.0

** Exponent - left operand raised to the x**y


power of right

02/23/2025 Unit 1 3
Operators (Continued)
2) Assignment Operator or In-place operator:
Operator Meaning Example
= Assigns values from right side operands to left side c = a + b assigns
operand value of a + b into c
+= adds right operand to the left operand and assign the c += a is equivalent
result to left operand to c = c + a
-= subtracts right operand from the left operand and c -= a is equivalent
assign the result to left operand to c = c - a
*= multiplies right operand with the left operand and c *= a is equivalent
assign the result to left operand to c = c * a
/= divides left operand with the right operand and c /= a is equivalent
assign the result to left operand to c = c / a
%= takes modulus using two operands and assign the c %= a is equivalent
result to left operand to c = c % a
**= Performs exponential (power) calculation on c **= a is equivalent
02/23/2025
operators and assign value toUnit
the1 left operand to c = c ** a 4
Operators (Continued)
3) Comparison Operator:
Operator Meaning Example
== if the values of two operands are equal, then the (a == b) is not true.
condition becomes true.
!= if values of two operands are not equal, then (a != b) is true.
condition becomes true.
<> if values of two operands are not equal, then (a <> b) is true.
condition becomes true. similar to !=
> If the value of left operand is greater than the value (a > b) is not true.
of right operand, then condition becomes true.
< If the value of left operand is less than the value of (a < b) is true.
right operand, then condition becomes true.
>= the value of left operand is greater than or equal to (a >= b) is not true.
the value of right operand, then condition becomes
true.
<= the value of left operand is less than or equal to the (a <= b) is true.
02/23/2025 value of right operand, then Unit
condition
1 becomes true. 5
Operators (Continued)
4) Logical Operator:
Operator Meaning Example

and Logical If both the operands are true then condition becomes (a and b) is true.
AND true.
or Logical If any of the two operands are non-zero then condition (a or b) is true.
OR becomes true.

not Logical Used to reverse the logical state of its operand. Not(a and b) is
NOT false.

02/23/2025 Unit 1 6
Operators (Continued)
5) Unary Operators
- Unary operators act as a single operand.
- Python supports Unary minus operator.-
- When an operand is preceded by minus sign, the
unary operator negates its values.
- For example : if a number is positive , it becomes
negative when preceded with a unary minus
operator.
Eg:

02/23/2025 Unit 1 7
Operators (Continued)
6) Bitwise Operator:
- Bitwise operator works on bits and performs bit by bit operation.
- Assume if a = 60; and b = 13; Now in the binary format their
values will be 0011 1100 and 0000 1101

Operator Meaning Example

& Binary Operator copies a bit to the result if it exists in both (a & b) (means 0000
AND operands 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means
0011 1101)
^ Binary It copies the bit if it is set in one operand but not (a ^ b) = 49 (means
XOR both. 0011 0001)

02/23/2025 Unit 1 8
Operators (Continued)
7) Bitwise Operator:
Truth Tables for Bit wise Operator:
A B A&B A|B A ^B
0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

02/23/2025 Unit 1 9
Operators (Continued)
Bitwise Operator…
Operator Meaning Example

~ Binary (~a ) = -61 (means


Ones 1100 0011 in 2's
Complement It is unary and has the effect of 'flipping' bits. complement form
due to a signed
binary number.
<< Binary The left operands value is moved left by the number a << 2 = 240 (means
Left Shift of bits specified by the right operand. 1111 0000)
>> Binary The left operands value is moved right by the a >> 2 = 15 (means
Right Shift number of bits specified by the right operand. 0000 1111)

02/23/2025 Unit 1 10
Operators (Continued)
5) Membership Operator:
- tests for membership in a sequence, such as
strings, lists, or tuples.

Operator Meaning Example

in Evaluates to true if it finds a variable in the x in y, here in


specified sequence and false otherwise. results in a 1 if x
is a member of
sequence y.
not in Evaluates to true if it does not finds a variable x not in y, here not
in the specified sequence and false otherwise. in results in a 1 if
x is not a member
of sequence y.
02/23/2025 Unit 1 11
Operators (Continued)
6) Identity Operators:
- Compares the memory locations of two objects.

Operator Meaning Example

is operator Returns true if operands on both sides of the


a is b returns 1; if
operator point to the same object and false
id(a) same as id(b)
otherwise
is not Returns true if operands or values on both sides a is not b returns
operator of the operator doesnot point to the same object 1; if id(a) not
and false otherwise same as id(b)

02/23/2025 Unit 1 12
Precedence of Arithmetic Operators
- Priority of Operators
- Operator precedence determines which
operator is performed first in an expression
with more than one operators with different
precedence
- Example

02/23/2025 Unit 1 13
Precedence of Arithmetic Operators
Operator Name Priority
** Exponentiation <=, <, >, >= Comparison
Operators
~,+,- Complement, unary pus <> ,==,!= Equality
and minus Operators
* / % // Multiply, Divide, =, %=,/=,//=,-=, Assignment
Modulus +=,*= Operators
floor divison
+- Addition Subtraction is, is not Identity
>> << Right and left bitwise in, not in Membership
shift
& Bitwise AND Not, or , and Logical
^| Bitwise Exclusive OR
& regular OR

02/23/2025 Unit 1 14
Precedence … (Continued)
- If an expression has mixture of parenthesis,
then the expression inside the parenthesis
will be executed first
- Doesn't matter about higher/ lower
precedence operators inside
Example:
a * (b + c)
- In the above expression, (b+c) will be
executed first,
- then the result will be multiplied with ‘a’
02/23/2025 Unit 1 15
Precedence … (Continued)
Associativity Rule:
1) If an Expression has, several multiplication,
Division and Remainder operators, the
operators are applied from Left-to-Right
2) If an expression has, several Addition and
Subtraction operators, the operators are
applied from Left-to-Right
3) An assignment operator has the lowest
priority, so it is applied last
02/23/2025 Unit 1 16
Precedence … (Continued)
Example:
1) Algebra: m = a+b+c+d+e
5
C : m= (a+b+c+d+e)/5

2) Algebra: y=mx+c Unit 1

C : y= m*x+c

3) Algebra: z= pr % q + w/x – y
C : z = p * r % q + w/x - y
02/23/2025
1 2 4 3 5 17
Precedence … (Continued)
4) y = a*x*x + b*x + c
let a=2, b= 3, c= 7 and x= 5
Step Expression Result of the Current Step
1 2* 5 * 5 + 3 * 5 + 7 2 * 5 = 10
(Left most Multiplication)
2 10 * 5 + 3* 5 + 7 10 * 5 = 50
(Left most Multiplication)
3 50 + 3 * 5 + 7 3 * 5 = 15
(most Multiplication)
4 50 + 15 + 7 50 + 15 = 65
(Left most addition)
5 65 + 7 65 + 7 = 72
(left most addition)
6 Y = 72 72 is assigned to y using = operator
02/23/2025 Unit 1 18
Precedence … (Continued)
Examples:

02/23/2025 Unit 1 19
Expressions in Python
- sequence of operands and operators reduced
to a single value
- In python an expression must have at least
one operand (variable or constant) and can
have one or more operators.
- Evaluating an expression get a value.
- Operand is the value on which operator is
applied.

02/23/2025 Unit 1 20
Expressions in Python
- Operators use constants and variables to
form expression.
- Eg: A*B+C-5
(+,*,-) / Operators (A,B,C) /Operands 5 – Constant
When an expression has more than one
operator ,is evaluated using operator
precedence chart.
Illegal Expression (Error will be displayed)
a+,-b, <y++
02/23/2025 Unit 1 21
Operations on Strings - Concatenation

02/23/2025 Unit 1 22
Operations on Strings - Concatenation

02/23/2025 Unit 1 23
Operations on Strings - Concatenation

02/23/2025 Unit 1 24
TUPLES
-similar to list, values in list can be changed but in list cannot be changed. Tuples
contains number of values separated by commas enclosed within parenthesis

02/23/2025 Unit 1 25
Lists
- versatile datatype, contains list of items
separated by commas and enclosed within
square brackets[].
- Similar to arrays
- array contain values of same datatype,
- list contains the values of different datatypes.
- Values in the list are accessed using indexes.

02/23/2025 Unit 1 26
Dictionary
- Stores data in key-value pairs.
- Key values – strings & value any
datatype
- Key value pair represented
using({ }).
- Pair is separated using :
- Accessing can be done by
specifying key in [].
- Used mainly for fast retrieval of
data.
- List & Data are mutable
datatypes; values can be
changed
02/23/2025 Unit 1 27
Type Conversion

02/23/2025 Unit 1 28
Type Conversion
- defines type conversion functions to directly
convert one data type to another data type
Functions Description
int (10,2) converts any data type to integer. ‘Base’ specifies the base in
which string is if data type is string
float() Converts any data type to a floating point number

ord() converts a character to integer.


hex() convert integer to hexadecimal string
oct() converts integer to octal string
tuple() converts to a tuple
set() returns the type after converting to set
list() converts any data type to a list type
02/23/2025 Unit 1 29

You might also like