CBSE Class 11 CS (083) - Operators and Expressions

Operators and Expressions

Operators are symbols that perform specific operations on operands. An expression is a combination of operators and operands that evaluates to a single value. This chapter covers all operator types and expression evaluation in Python.

1. Introduction to Operators

An operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation on one or more operands.

Python provides the following categories of operators:

CategoryOperators
Arithmetic+, -, *, /, //, %, **
Relational (Comparison)==, !=, >, <, >=, <=
Logicaland, or, not
Assignment=, +=, -=, *=, /=, %=, //=, **=
Bitwise&, |, ^, ~, <<, >>
Identityis, is not
Membershipin, not in

2. Arithmetic Operators

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (float)10 / 33.333...
//Floor Division10 // 33
%Modulus (remainder)10 % 31
**Exponentiation10 ** 31000
a, b = 15, 4
print(a + b) # 19
print(a - b) # 11
print(a * b) # 60
print(a / b) # 3.75
print(a // b) # 3 (floor division)
print(a % b) # 3 (remainder)
print(a ** b) # 50625 (15^4)

3. Relational (Comparison) Operators

Relational operators compare two values and return a boolean result (True or False).

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 3False

4. Logical Operators

Logical operators combine conditional statements.

OperatorDescriptionExampleResult
andTrue if both operands are TrueTrue and FalseFalse
orTrue if at least one operand is TrueTrue or FalseTrue
notInverts the boolean valuenot TrueFalse
marks = 85
attendance = 90
if marks >= 80 and attendance >= 75:
print("Eligible for exam")

5. Assignment Operators

The = operator assigns a value to a variable.

x = 10     # Simple assignment
y = x + 5 # Expression assignment
a = b = c = 20 # Chained assignment
p, q, r = 1, 2, 3 # Multiple assignment

6. Augmented Assignment Operators

These operators combine an arithmetic operation with assignment.

OperatorMeaningExampleEquivalent
+=Add and assignx += 5x = x + 5
-=Subtract and assignx -= 3x = x - 3
*=Multiply and assignx *= 2x = x * 2
/=Divide and assignx /= 4x = x / 4
//=Floor divide and assignx //= 2x = x // 2
%=Modulus and assignx %= 3x = x % 3
**=Exponent and assignx **= 2x = x ** 2

7. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

OperatorNameExample (a=5=101, b=3=011)Result
&Bitwise ANDa & b1 (001)
|Bitwise ORa | b7 (111)
^Bitwise XORa ^ b6 (110)
~Bitwise NOT~a-6 (inverts bits)
<<Left shifta << 110 (1010)
>>Right shifta >> 12 (010)

8. Identity Operators

Identity operators check whether two objects are the same object (same memory location).

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a is c) # True (same object)
print(a is b) # False (different objects)
print(a is not b) # True
print(a == b) # True (same value)

9. Membership Operators

Membership operators test whether a value is present in a sequence.

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True

text = "Hello, World!"
print("World" in text) # True

10. Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Higher precedence operators are evaluated first.

Precedence (High to Low)OperatorsDescription
1()Parentheses (highest)
2**Exponentiation
3~, +, -Unary operators
4*, /, //, %Multiplication, Division, Modulus
5+, -Addition, Subtraction
6<<, >>Bitwise shift
7&Bitwise AND
8^Bitwise XOR
9|Bitwise OR
10==, !=, >, <, >=, <=Comparison
11notLogical NOT
12andLogical AND
13orLogical OR (lowest)
result = 5 + 3 * 2 ** 2  # 5 + 3 * 4 = 5 + 12 = 17
result2 = (5 + 3) * 2 ** 2 # 8 * 4 = 32

11. Associativity

When operators have the same precedence, associativity determines the order of evaluation.

  • Most operators have left-to-right associativity.
  • Exponentiation (**) has right-to-left associativity.
# Left to right
print(10 - 5 - 2) # (10 - 5) - 2 = 3
print(10 / 5 * 2) # (10 / 5) * 2 = 4.0

# Right to left (exponentiation)
print(2 ** 3 ** 2) # 2 ** (3 ** 2) = 2 ** 9 = 512

12. Expressions and Evaluation

An expression is a combination of values, variables, operators, and function calls that evaluates to a single value.

# Arithmetic expressions
area = 3.14 * radius ** 2
average = (a + b + c) / 3

# Boolean expressions
is_pass = marks >= 33
can_vote = age >= 18 and citizenship == "Indian"

# Mixed expressions
result = 10 + 20 * 3 - 5 / 2 # 10 + 60 - 2.5 = 67.5

13. Revision Questions and Answers

Very Short Answer Questions

1. Name any four types of operators in Python.
Arithmetic, Relational, Logical, Assignment.

2. What is the difference between = and ==?
= is assignment operator, == is equality comparison operator.

3. What does the // operator do?
Floor division - divides and returns the integer quotient.

4. Which operator is used for exponentiation?
** (double asterisk).

5. What is the result of 10 % 3?
1 (remainder when 10 is divided by 3).

Short Answer Questions

1. Explain operator precedence with an example.
Operator precedence determines evaluation order. Example: 5 + 3 * 2 = 11 because * has higher precedence than +. Parentheses override precedence: (5 + 3) * 2 = 16.

2. Differentiate between is and == operators.
== compares values (equality), while is compares object identity (memory location). Two different objects with the same value are == equal but not is equal.

Long Answer Questions

1. Write a Python program that takes three numbers as input and finds the largest using logical operators.
a = float(input("Enter first number: ")); b = float(input("Enter second: ")); c = float(input("Enter third: ")); if a >= b and a >= c: print("Largest:", a); elif b >= a and b >= c: print("Largest:", b); else: print("Largest:", c)

2. Explain all augmented assignment operators with examples.
Augmented operators combine arithmetic and assignment: += (x += 3 means x = x + 3), -=, *=, /=, //=, %=, **=. They provide a shorter way to modify and assign in one step.