Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Unit 2 – Python Basics  ·  Lesson 13

Operators, Expressions & Precedence

Operators and operands

An operator is a symbol that performs an operation; the values it works on are operands. In marks + 5, + is the operator and marks and 5 are operands. An operator with one operand (like -x or not x) is unary; with two it is binary.

➕
Arithmetic+ - * / // % **
⚖️
Relational== != > < >= <=
📝
Assignment= += -= *= /= //= %= **=
🔀
Logicaland, or, not
🆔
Identityis, is not
📦
Membershipin, not in
Families of Python operators

Arithmetic operators

OperatorMeaning17 ? 5
+Addition22
-Subtraction12
*Multiplication85
/Division (always float)3.4
//Floor division (quotient)3
%Modulus (remainder)2
**Exponent (power)17**2 = 289
Python
a, b = 17, 5
print(a + b, a - b, a * b)
print(a / b, a // b, a % b, a ** 2)
print(-17 // 5, -17 % 5)        # floor division rounds DOWN
22 12 85 3.4 3 2 289 -4 3
💡 Uses of % and //

n % 2 == 0 checks even numbers; n % 10 gives the last digit; n // 10 removes the last digit. + also joins strings ('Py' + 'thon') and * repeats them ('-' * 20).

Relational (comparison) operators

They compare two values and always give True or False.

Python
a, b = 17, 5
print(a > b, a < b, a == 17, a != b, a >= 17, b <= 4)
print('apple' < 'banana', 'Zoo' < 'apple')   # compared by Unicode codes
True False True True True False True True

Strings are compared letter by letter using their Unicode codes; capital letters (65–90) come before small letters (97–122), so 'Zoo' < 'apple' is True.

Assignment operators

A compound assignment like x += 5 is a short form of x = x + 5.

Python
x = 10
x += 5;  print(x)
x -= 3;  print(x)
x *= 2;  print(x)
x //= 5; print(x)
x **= 3; print(x)
x %= 10; print(x)
15 12 24 4 64 4

Logical operators

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
Truth table of and, or, not
Python
age, has_id = 20, False
print(age >= 18 and has_id)    # both must be True
print(age >= 18 or has_id)     # any one True is enough
print(not has_id)
print(0 and 5, 3 and 5, 0 or 'default', '' or 'guest')
False True True 0 5 default guest

Short-circuit: and stops at the first false value; or stops at the first true value, and they return that value itself. That is why '' or 'guest' gives 'guest' — handy for default values.

Identity and membership operators

Python
L = [1, 2, 3]
M = L
N = [1, 2, 3]
print(M is L, N is L)        # identity: same object?
print(N == L, N is not L)    # equality: same value?
print('a' in 'class', 5 in [1, 2, 3], 'z' not in 'python')
True False True True True False True

== (equality)

  • Are the values the same?
  • N == L → True
  • Use for comparing data

is (identity)

  • Is it the same object?
  • N is L → False (two separate lists)
  • Use mainly for x is None
Do not confuse == with is

Bitwise operators

They work on the binary digits of integers: & AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shift.

Python
print(12 & 10, 12 | 10, 12 ^ 10)   # 1100 and 1010
8 14 6

12 = 1100 and 10 = 1010 → AND 1000 = 8, OR 1110 = 14, XOR 0110 = 6.

Expressions and precedence

An expression is a combination of values, variables and operators that Python evaluates to a single value, e.g. (a + b) / 2. When an expression has many operators, precedence decides which is done first, and associativity decides the order among operators of equal precedence (mostly left to right; ** is right to left).

( )parentheses – highest**exponent (right to left)+x -x ~xunary* / // %multiplicative+ -additive== != > < >= <= is incomparisonsnotandorlowest
Operator precedence (top first)
Python
print(2 + 3 * 4)        # * before +
print((2 + 3) * 4)      # brackets first
print(2 ** 3 ** 2)      # ** is right to left: 2 ** 9
print(10 - 4 - 3)       # left to right
print(100 / 10 * 2)     # left to right
print(5 + 4 > 8 and not 3 > 4)
14 20 512 3 20.0 True
🧮 Solved: evaluate 12 + 3 * 2 ** 2 // 4 - 1

2 ** 2 = 4 → 3 * 4 = 12 → 12 // 4 = 3 → 12 + 3 = 15 → 15 − 1 = 14.

Summary & practice

  • / gives float, // floor quotient, % remainder, ** power.
  • Comparisons give True/False; logical operators short-circuit.
  • is checks identity, == checks value, in checks membership.
  • Precedence: () → ** → unary → * / // % → + - → comparisons → not → and → or.
✅ Practice

Find the output: (a) print(15 % 4 + 2 ** 3) (b) print(-9 // 2) (c) print(not 5 > 3 or 2 == 2) (d) print(10 / 4 * 2)

Answers: (a) 11 (b) -5 (c) True (d) 5.0

Frequently Asked Questions

What is the difference between / and // in Python?
/ is true division and always gives a float (7/2 = 3.5). // is floor division and gives the largest whole number not greater than the result (7//2 = 3, -7//2 = -4).
What is the difference between == and is?
== checks whether two values are equal. is checks whether two names refer to the very same object in memory.
Which operator has the highest precedence?
Brackets () are evaluated first, then exponent **, then unary + and -, then * / // %, then + -, then comparisons, then not, and, or.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicInput, Output, Type Conversion & Errors Your progress: 13 / 29 (45%) · Lesson 14 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free