0% found this document useful (0 votes)
2 views13 pages

Operators in Python

The document provides an overview of operators in Python, categorizing them into seven types: Arithmetic, Comparison, Assignment, Logical, Bitwise, Membership, and Identity. It explains the function of each operator type, along with examples and expected outputs for various expressions. Additionally, it discusses operator precedence and the distinction between value equality and memory identity.
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)
2 views13 pages

Operators in Python

The document provides an overview of operators in Python, categorizing them into seven types: Arithmetic, Comparison, Assignment, Logical, Bitwise, Membership, and Identity. It explains the function of each operator type, along with examples and expected outputs for various expressions. Additionally, it discusses operator precedence and the distinction between value equality and memory identity.
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

Operators in Python

Operands & Expressions

Operators: Special symbols or keywords used to perform specific


computation on data.

Operands: The actual data values or variables on which the operation is


performed.

Expression: A valid combination of operators and operands that evaluates


to a result.

4 5 # Result: 9

4, 5
+
7 Types of Operators

1. Arithmetic 2. Comparison 3. Assignment 4. Logical


Perform basic and advanced math Relational evaluation of two Assign or perform in-place Combine multiple conditional
calculations like addition, floor values returning Boolean True or mathematical updating of conditions using Boolean algebra
division, and powers. False. variable values. (and, or, not).

5. Bitwise 6. Membership 7. Identity


Manipulate individual binary bits of integer Test if a sequence contains a specified element Compare whether two variables share the same
operands directly. (in, not in). memory location (is, is not).
Arithmetic Operators

Operator Name Description Example Result

+ Addition Adds two values together 10 + 20 30

- Subtraction Subtracts right value from left value 10 - 20 -10

* Multiplication Multiplies two numbers 10 * 20 200

/ Division Divides values (always returns float) 20 / 10 2.0

% Modulus Returns division remainder 20 % 10 0

** Exponentiation Raises left value to power of right 10 ** 2 100

// Floor Division Truncates result to nearest lower integer 9 // 2 4

Negative Floor Division Note:


Arithmetic Operators Assignment
•What will be the output of the following?
a = 15
b=4
print(a + b, a - b)

•Find the output:


print(17 // 5)
print(17 % 5)

•What will be the output?


print(2 ** 4)
print(25 / 5)

•Find the output:


print(-11 // 3)
print(-11 % 3)

•Write Python expressions using arithmetic operators to calculate:


•Square of 12
•Remainder when 35 is divided by 6
•Floor division of 35 by 6
Relational & Assignment

Relational Operators Assignment Operators

Op Meaning Example Result Op Equivalent Expression Operation

== Equal to 7 == 23 False = c=a+b Direct Assignment

!= Not equal to 7 != 23 True += c=c+a Add & Assign

> Greater than 7 > 23 False -= c=c-a Subtract & Assign

< Less than 7 < 23 True *= c=c*a Multiply & Assign

>= Greater or equal 7 >= 98 False /= c=c/a Divide & Assign

<= Less or equal 8 <= 9 True %= c=c%a Modulus & Assign


Relational & Assignment Operator Assignment
•Find the output: •Find the final value of x:
a = 25 x = 20
b = 30 x += 10
print(a > b) x -= 5
print(a < b)
•Find the output: •Find the output:
x = 15 x=8
print(x == 15) x *= 3
print(x != 20) print(x)
•What will be the output?
a = 50 •What will be the value of x?
b = 50 x = 25
print(a >= b) x //= 4
print(a <= b)
•Write suitable comparison expressions to check whether: •Rewrite the following using a simple assignment operator:
•x is greater than 100 x += 15
•x is equal to 50
•x is not equal to 75 •Find the final value of a:
•Predict the output: a=5
a = 10 a **= 2
b = 20 a %= 3
print(a + 5 > b)
print(a * 2 == b)
Logical & Bitwise

Logical Operators
Bitwise Operators
Used to evaluate and combine boolean conditions:
Perform operations on integer binary representations:
and : Returns True if both conditions are True.
& (AND), | (OR), ^ (XOR), ~ (NOT)
or : Returns True if at least one condition is True.
<< (Left Shift), >> (Right Shift)
not : Reverses the boolean logical result.

# Binary: 101 & 011 = 001


# Output: 1
and # Output: True
Logical & Bitwise Operator Assignment

•Find the output:


x=8
print(x > 5 and x < 10) •Find the output:
print(5 & 3)
•Find the output:
a = 15 •Find the output:
print(a < 10 or a > 12) print(5 | 3)

•What will be the output? •Find the output:


x=7 print(5 ^ 3)
print(not(x > 5))
•What will be the output?
•Evaluate the following: print(5 << 1)
(10 > 5) and (20 < 30) print(5 >> 1)

•Write a logical expression to check whether marks are greater


than or equal to 40 and less than or equal to 100.
Membership & Identity
Membership Operators: Check presence inside sequence objects like
strings, lists, or tuples.
• in • not in

Identity Operators: Determine if variables share the exact same


memory object.
• is • is not

in # Returns: True

is # Returns: True
Membership & Identity Operator Assignment
•Find the output:
•Find the output: a = [1, 2, 3]
text = "PYTHON" b=a
print("P" in text) print(a is b)
•Find the output:
•Find the output: a = [1, 2, 3]
text = "COMPUTER" b = [1, 2, 3]
print("X" not in text) print(a == b)
print(a is b)
•What will be the output? •What is the difference between == and is?
nums = [10, 20, 30, 40] •Find the output:
print(30 in nums) x = [10, 20]
y=x
•Find the output: print(x is not y)
nums = [5, 10, 15] •Consider:
print(20 not in nums) a = 100
b=a
•Write Python expressions to check whether: What will be the result of:
•"A" is present in "APPLE" a=100
•25 is present in [10, 20, 30] b=a
print(a is b)
print(a == b)
Value vs Memory Identity

Comparing Equality vs Identity


== (Equality): Compares the actual values stored within two variables.

== vs is is (Identity): Compares whether both variables point to the exact same memory address
(`id()`).

Crucial Distinction

# True (Same values)


# False (Different list objects in memory!)
Operator Precedence

Priority Operators Description

1 (Highest) () Parentheses (Grouped Expressions)

2 ** Exponentiation

3 +x, -x, ~x Unary Positive, Unary Negative, Bitwise NOT

4 *, /, //, % Multiplication, Division, Floor Division, Modulus

5 +, - Addition, Subtraction

6 <<, >> Bitwise Shift Operators

7 &, ^, | Bitwise AND, XOR, OR

8 (Lowest) ==, !=, is, in, and, or Comparisons, Membership, Identity, Logical Ops

Precedence Example: 18

You might also like