Monday, 6 April 2026

April Python Bootcamp Day 3


 

 

Introduction

In Python, operators are fundamental building blocks used to perform operations on variables and values. Whether you're building a calculator, writing conditions, or designing logic — operators are everywhere.

Key Insight:
Operators are the backbone of decision-making and computation in programming.


 What is an Operator?

An operator is a symbol that performs an operation on operands (values or variables).

a = 10
b = 5
print(a + b) # Output: 15
  • + → Operator
  • a, b → Operands

 Why Operators Matter

Operators are used in:

  •  Calculations → price + tax
  •  Conditions → age > 18
  •  Logic → is_logged_in and is_verified
  •  Control flow → loops, decisions

 Types of Operators in Python


 1. Arithmetic Operators

Used for mathematical operations.

print(10 + 5) # 15
print(10 - 5) # 5
print(10 * 5) # 50
print(10 / 5) # 2.0
print(10 // 3) # 3
print(10 % 3) # 1
print(2 ** 3) # 8

Notes:

  • / → always returns float
  • // → floor division
  • % → useful for even/odd checks

 2. Assignment Operators

Used to assign and update values.

x = 10
x += 5 # 15
x -= 2 # 13
x *= 2 # 26
x /= 2 # 13.0

Notes:

  • Short-hand operations improve readability
  • Common in loops and updates

 3. Comparison Operators

Used to compare values → returns Boolean

print(10 == 10) # True
print(10 != 5) # True
print(10 > 5) # True
print(10 < 5) # False

Notes:

  • Always returns True or False
  • Used in conditions (if, loops)

 4. Logical Operators

Combine multiple conditions

print(True and False) # False
print(True or False) # True
print(not True) # False

Notes:

  • and → all conditions must be True
  • or → at least one True
  • not → reverses result

 5. Identity Operators

Check if two variables refer to the same object

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

print(a is b) # True
print(a is c) # False
print(a == c) # True

Notes:

  • is → checks memory location
  • == → checks value

 6. Membership Operators

Check if a value exists in a sequence

print("a" in "apple") # True
print(10 in [1, 2, 3]) # False
print(1 in (1, 2, 3)) # True

Notes:

  • Works with strings, lists, tuples, sets

 Operator Precedence

Precedence determines which operator executes first.

print(2 + 3 * 4) # 14

 Multiplication happens before addition


 Associativity

Associativity determines evaluation order when precedence is same.


 Left to Right

print(10 - 5 - 2) # 3

 Right to Left

print(2 ** 3 ** 2) # 512

 Precedence + Associativity Table

PriorityOperatorsAssociativity
1()Left → Right
2**Right → Left
3+x, -x, notRight → Left
4* / // %Left → Right
5+ -Left → Right
6ComparisonsLeft → Right
7andLeft → Right
8orLeft → Right
9AssignmentRight → Left

 Important Notes (Exam / Interview Level)

  • True = 1, False = 0
  • and returns first falsy value
  • or returns first truthy value
  • is==
  • Exponent is right-associative
  • Division always returns float

 Practice Questions (NEW – Not Repeated)


 Q1

print(8 + 2 * 5 - 3)

 Q2

print(20 // 3 + 2 ** 2)

 Q3

print(5 > 3 and 8 < 5 or 10 > 2)

 Q4

print(not (4 == 4 and 2 > 3))

 Q5

x = 10
x -= 3 * 2
print(x)

 Q6

print(6 + 3 * 2 ** 2)

 Q7

print("p" in "python" and 5 not in [1,2,3])

 Q8

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

print(a is c)

 Q9

print(0 or 5 and 10)

 Q10 (Advanced)

print(2 ** 2 ** 3 + 1)

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (237) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (3) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (21) data management (15) Data Science (339) Data Strucures (16) Deep Learning (144) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (277) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1283) Python Coding Challenge (1120) Python Mistakes (50) Python Quiz (463) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)