इस chapter में हम Python में operators, expressions और statements के बारे में सीखेंगे। इसमें Arithmetic, Relational, Logical, Bitwise Operators, Conditional Statements और Loop Control Statements जैसे break, continue, pass, else और assert शामिल हैं।
Python में Assignment Statement का प्रयोग किसी variable को value देने के लिए किया जाता है। यह statement memory में data store करने का तरीका बताता है।
variable_name = value
यहाँ = एक assignment operator है जो right side की value को left side variable में store करता है।
x = 10
y = 20
z = x + y
print("Sum =", z)
Output:
Sum = 30
a, b, c = 5, 10, 15
print(a, b, c)
Output:
5 10 15
x = y = z = 100
print(x, y, z)
Output:
100 100 100
x = 10
x += 5 # same as x = x + 5
print("New value of x =", x)
Output:
New value of x = 15
Expression Python का एक बहुत ही महत्वपूर्ण हिस्सा है।
Expression किसी value या result को produce करने वाला logical combination होता है
जिसमें operands (values or variables) और operators शामिल होते हैं।
जैसे गणित में हम लिखते हैं 2 + 3 × 4 —
Python में यही concept code के रूप में लागू होता है।
x = 10
y = 5
z = x + y * 2
print("Result =", z)
Output:
Result = 20
यहाँ Python पहले multiplication (y * 2) करेगा, फिर addition (x + ...)।
यानी Operator Precedence rule follow होता है।
Arithmetic expressions में +, -, *, /, %, //, ** जैसे operators का प्रयोग होता है। ये mathematical calculations करने के लिए उपयोगी हैं।
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
Output:**) का मतलब power होता है (15⁴)।
Relational expressions दो values की तुलना करते हैं और True या False result return करते हैं।
Operators: >, <, ==, !=, >=, <=
x = 10
y = 20
print("x == y:", x == y)
print("x != y:", x != y)
print("x > y:", x > y)
print("x < y:", x < y)
print("x >= y:", x >= y)
print("x <= y:", x <= y)
Output:
x == y: False
x != y: True
x > y: False
x < y: True
x >= y: False
x <= y: True
Logical expressions का उपयोग multiple conditions को combine करने के लिए किया जाता है। Operators हैं: and, or, not.
a = 10
b = 20
c = 5
print(a > b and b > c) # False
print(a < b or b < c) # True
print(not(a < b)) # False
Bitwise operators binary digits (0 और 1) पर काम करते हैं। Operators: &, |, ^, ~, <<, >>
x = 5 # 0101
y = 3 # 0011
print("AND:", x & y)
print("OR:", x | y)
print("XOR:", x ^ y)
print("NOT x:", ~x)
print("Left Shift:", x << 1)
print("Right Shift:", x >> 1)
Output:
Assignment expression value को variable में store करने के लिए प्रयोग होता है।
Example: x = 10, x += 2, (n := len(list)) (walrus operator)।
x = 10
x += 5
print("x =", x)
# Walrus Operator (Python 3.8+)
if (n := len("Hello")) > 3:
print("Length is:", n)
जब operators को strings पर use किया जाता है तो उन्हें string expressions कहते हैं।
Example: concatenation (+), repetition (*).
name = "Python"
greet = "Hello " + name
print(greet)
line = "Hi! " * 3
print(line)
Output:
Hello Python
Hi! Hi! Hi!
Compound expressions में एक से अधिक operators और sub-expressions combine होते हैं।
Example: (a + b) * (c - d) / e
a, b, c, d, e = 10, 5, 8, 3, 2
result = (a + b) * (c - d) / e
print("Result =", result)
Output:
Result = 37.5
| Level | Operators | Description |
|---|---|---|
| 1 | ** | Exponentiation |
| 2 | ~, +, - | Unary plus, minus, bitwise NOT |
| 3 | *, /, //, % | Multiplication, division |
| 4 | +, - | Addition, subtraction |
| 5 | <<, >> | Bitwise shifts |
| 6 | & | Bitwise AND |
| 7 | ^, | | XOR, OR |
| 8 | <, <=, >, >=, ==, != | Comparisons |
| 9 | not, and, or | Logical operations |
| 10 | =, +=, -=, *=, /= | Assignments |
Arithmetic Operators का प्रयोग गणितीय (mathematical) operations करने के लिए किया जाता है जैसे – जोड़ (addition), घटाना (subtraction), गुणा (multiplication), भाग (division) आदि। Python में arithmetic operators integer, float, और complex data types पर काम करते हैं।
| Operator | Description | Example | Output |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 6 * 3 | 18 |
| / | Division | 10 / 4 | 2.5 |
| // | Floor Division | 10 // 4 | 2 |
| % | Modulus (Remainder) | 10 % 4 | 2 |
| ** | Exponentiation (Power) | 3 ** 4 | 81 |
a = 10
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
Output:/) हमेशा float result देता है।
- Floor Division (//) integer result देता है।
- Modulus remainder देता है।
- Exponentiation (**) power का कार्य करता है।
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
sum = x + y
diff = x - y
prod = x * y
quot = x / y
rem = x % y
print("Sum =", sum)
print("Difference =", diff)
print("Product =", prod)
print("Quotient =", quot)
print("Remainder =", rem)
Explanation:
यह example user input लेता है और सभी arithmetic results दिखाता है।
int() या float() में convert करें
क्योंकि input() default रूप से string देता है।
a, b, c = 10, 5, 2
result = (a + b) * c ** 2 / 5
print("Result =", result)
Output:
Result = 60.0
**, फिर * /, और अंत में + execute होते हैं।
हमेशा parentheses का प्रयोग करके expression को readable बनाएं।
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
avg = (a + b + c) / 3
print("Average =", avg)
Flow:
Start → Input 3 numbers → Add → Divide by 3 → Output average → Stop
Output:
Average = (depends on input values)
p = float(input("Enter Principal: "))
r = float(input("Enter Rate of Interest: "))
t = float(input("Enter Time (years): "))
si = (p * r * t) / 100
print("Simple Interest =", si)
Output:
Simple Interest = (p×r×t)/100
a = 5
b = 7
a = a + b
b = a - b
a = a - b
print("After Swapping: a =", a, ", b =", b)
Output:
After Swapping: a = 7 , b = 5
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit =", fahrenheit)
Explanation:
Formula used → F = (C × 9/5) + 32
यही logic arithmetic expressions से directly compute किया गया है।
| Priority | Operator | Meaning |
|---|---|---|
| 1 | ** | Exponentiation |
| 2 | *, /, //, % | Multiplication / Division / Modulus |
| 3 | +, - | Addition / Subtraction |
x = 5 + 3 * 2 ** 2
print(x)
Output:
17
Explanation:
Order: 2 ** 2 → 4, then 3 * 4 → 12, then 5 + 12 → 17
Relational (या Comparison) Operators का उपयोग दो values की तुलना (comparison) करने के लिए किया जाता है।
इन operators का result हमेशा True या False होता है।
ये decision making (जैसे if statements) में बहुत उपयोगी होते हैं।
| Operator | Description | Example | Output |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 7 > 4 | True |
| < | Less than | 3 < 7 | True |
| >= | Greater than or equal to | 7 >= 7 | True |
| <= | Less than or equal to | 4 <= 5 | True |
a = 10
b = 10
print(a == b)
Output: True
Explanation: Both numbers are same, so condition is True.
a = 15
b = 20
print(a != b)
Output: True
Explanation: 15 and 20 are different, so result is True.
print(8 > 5)
Output: True
print(12 < 9)
Output: False
print(7 >= 7)
Output: True
Explanation: Greater OR equal condition satisfied.
print(2 <= 10)
Output: True
print("apple" < "banana")
Output: True
Explanation: Strings are compared alphabetically (lexicographically).
print("A" < "a")
Output: True
Explanation: ASCII value of 'A' (65) is smaller than 'a' (97).
print(True > False)
Output: True
Explanation: Internally, True = 1 and False = 0.
print([1, 2, 3] < [1, 3, 0])
Output: True
Explanation: Python compares element-by-element like dictionary order.
marks = int(input("Enter marks: "))
if marks >= 40:
print("Pass")
else:
print("Fail")
Output:
If marks = 55 → Pass
If marks = 30 → Fail
a = 12
b = 8
if a > b:
print("A is greater")
else:
print("B is greater")
Output: A is greater
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible for voting.")
else:
print("Not eligible.")
Output: (depends on input)
n = int(input("Enter number: "))
if 10 <= n <= 50:
print("Within range")
else:
print("Out of range")
Output:
Input = 25 → Within range
Input = 70 → Out of range
a = "HELLO"
b = "hello"
print(a.lower() == b.lower())
Output: True
Explanation: Both converted to lowercase before comparison.
a < b < c
a, b, c = 5, 10, 15
print(a < b < c)
Output: True
if, while, and filtering conditions.Logical Operators का प्रयोग conditions को जोड़ने या उनके result को manipulate करने के लिए किया जाता है। Python में 3 मुख्य logical operators होते हैं: and, or, और not। ये expressions का boolean (True/False) result देते हैं और decision making में अत्यंत उपयोगी होते हैं।
| Operator | Description | Example | Result |
|---|---|---|---|
| and | Returns True if both conditions are True | (5>3) and (8>5) | True |
| or | Returns True if at least one condition is True | (5>10) or (8>5) | True |
| not | Reverses the result (True→False, False→True) | not(5>3) | False |
a = 10
b = 5
print(a > b and b < 10)
Output: True
Explanation: दोनों conditions True हैं, इसलिए result True।
x = 8
print(x < 5 or x == 8)
Output: True
Explanation: पहली condition False है लेकिन दूसरी True है, इसलिए output True।
a = 10
print(not(a > 5))
Output: False
Explanation: (a > 5) True है, not इसे उलटकर False बना देता है।
n = 15
print(n > 10 and n < 20)
Output: True
marks = 60
attendance = 80
if marks >= 40 and attendance >= 75:
print("Pass")
else:
print("Fail")
Output: Pass
n = 0
if n > 0 or n == 0:
print("Positive or Zero")
else:
print("Negative")
Output: Positive or Zero
n = 7
print(not (n % 2 == 0))
Output: True
Explanation: 7 odd है, तो (n%2==0) False है, और not उसे True बना देता है।
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Invalid credentials")
Output: Login Successful
n = 10
if n % 3 == 0 or n % 5 == 0:
print("Divisible by 3 or 5")
else:
print("Not divisible")
Output: Divisible by 3 or 5
age = 17
if age >= 13 and age <= 19:
print("Teenager")
else:
print("Not a Teenager")
Output: Teenager
a = "Hello"
b = "HELLO"
print(a.lower() == b.lower() and len(a) == len(b))
Output: True
x, y, z = 5, 10, 15
print(x < y and y < z or z == 15)
Output: True
Explanation: and का result True है और or condition भी True, इसलिए final True।
a = 10
b = 5
print(not(a < b and a != b))
Output: True
n = 120
if not(50 <= n <= 100):
print("Out of range")
else:
print("Within range")
Output: Out of range
a, b, c = True, False, True
print(a and not b or c)
Output: True
Explanation: not b = True, तो (a and True) = True, फिर or c → True.
x = True or False and not False
print(x)
Output: True
Explanation: not पहले चलता है, फिर and, फिर or।
Bitwise Operators का प्रयोग integers के binary bits पर operation करने के लिए किया जाता है। ये operators direct bits (0s और 1s) पर काम करते हैं, इसलिए ये बहुत fast और efficient होते हैं। Bitwise operators का उपयोग low-level programming, encryption, और device control में किया जाता है।
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
| & | AND | Sets bit to 1 if both bits are 1 | 5 & 3 | 1 |
| | | OR | Sets bit to 1 if at least one bit is 1 | 5 | 3 | 7 |
| ^ | XOR | Sets bit to 1 if bits are different | 5 ^ 3 | 6 |
| ~ | NOT | Inverts all bits | ~5 | -6 |
| << | Left Shift | Shift bits to left, adds 0s on right | 5 << 1 | 10 |
| >> | Right Shift | Shift bits to right, drops bits on right | 5 >> 1 | 2 |
Decimal : 5 → Binary : 0101
Decimal : 3 → Binary : 0011
a = 5 # 0101
b = 3 # 0011
print(a & b)
Output: 1
Explanation: 0101 & 0011 = 0001 → 1
print(5 | 3)
Output: 7
Explanation: 0101 | 0011 = 0111 → 7
print(5 ^ 3)
Output: 6
Explanation: 0101 ^ 0011 = 0110 → 6
print(~5)
Output: -6
Explanation: ~n = -(n+1) → ~5 = -6
print(5 << 1)
Output: 10
Explanation: 0101 → 1010 → 10
print(5 >> 1)
Output: 2
Explanation: 0101 → 0010 → 2
a = 12 # 1100
b = 25 # 11001
print(a & b)
Output: 8
x, y = 7, 3
print((x & y) | (x ^ y))
Output: 7
n = 13 # 1101
mask = 1<<2 # 0100
print(n & ~mask)
Output: 9
Explanation: Third bit cleared → 1001 → 9
n = 9 # 1001
mask = 1<<1 # 0010
print(n | mask)
Output: 11
n = 10 # 1010
mask = 1<<1 # 0010
print(n ^ mask)
Output: 8
Explanation: Second bit flipped → 1000 → 8
print(4 << 3)
Output: 32
Explanation: 4 × 2³ = 32
print(20 >> 2)
Output: 5
n = 14
if n & 1 == 0:
print("Even")
else:
print("Odd")
Output: Even
a, b = 5, 7
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b)
Output: 7 5
| Priority | Operator | Meaning |
|---|---|---|
| 1 | ~ | Bitwise NOT |
| 2 | <<, >> | Shift operators |
| 3 | & | AND |
| 4 | ^ | XOR |
| 5 | | | OR |
Conditional Statements का प्रयोग किसी condition के आधार पर decision लेने के लिए किया जाता है। Python में यह control flow को manage करने का सबसे महत्वपूर्ण हिस्सा है। ये statements किसी condition के True या False होने पर अलग-अलग code block execute करते हैं।
if condition:
statement(s)
elif another_condition:
statement(s)
else:
statement(s)
num = 10
if num > 0:
print("Positive number")
Output: Positive number
num = -5
if num >= 0:
print("Positive")
else:
print("Negative")
Output: Negative
n = 7
if n % 2 == 0:
print("Even")
else:
print("Odd")
Output: Odd
a, b = 10, 20
if a > b:
print("A is greater")
else:
print("B is greater")
Output: B is greater
a, b, c = 30, 20, 10
if a > b and a > c:
print("A is largest")
elif b > c:
print("B is largest")
else:
print("C is largest")
Output: A is largest
marks = 45
if marks >= 40:
print("Pass")
else:
print("Fail")
Output: Pass
marks = 85
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Output: Grade A
age = 17
if age >= 18:
print("Eligible for voting")
else:
print("Not eligible")
Output: Not eligible
year = 2024
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap Year")
else:
print("Not Leap Year")
else:
print("Leap Year")
else:
print("Not Leap Year")
Output: Leap Year
n = 35
if 10 <= n <= 50:
print("In range")
else:
print("Out of range")
Output: In range
ch = 'e'
if ch.lower() in ['a', 'e', 'i', 'o', 'u']:
print("Vowel")
else:
print("Consonant")
Output: Vowel
a = 5
b = 10
print("A is greater") if a > b else print("B is greater")
Output: B is greater
num = 15
if num % 3 == 0 and num % 5 == 0:
print("Multiple of 3 and 5")
else:
print("Not multiple of both")
Output: Multiple of 3 and 5
ch = '7'
if ch.isdigit():
print("Digit")
elif ch.isalpha():
print("Alphabet")
else:
print("Special Character")
Output: Digit
user = "admin"
pwd = "1234"
if user == "admin" and pwd == "1234":
print("Access Granted")
else:
print("Access Denied")
Output: Access Granted
Looping statements का प्रयोग तब किया जाता है जब किसी code block को बार-बार दोहराना (repeat) हो। Python में मुख्य दो प्रकार के loops होते हैं — for loop और while loop। इसके अलावा loops को nested (एक loop के अंदर दूसरा loop) भी बनाया जा सकता है।
# for loop
for variable in sequence:
statement(s)
# while loop
while condition:
statement(s)
for i in range(1, 6):
print(i)
Output:
i = 1
while i <= 5:
print(i)
i += 1
Output:
for i in range(2, 11, 2):
print(i)
Output: 2 4 6 8 10
sum = 0
for i in range(1, 11):
sum += i
print("Sum =", sum)
Output: Sum = 55
for i in range(1, 11):
print("5 x", i, "=", 5*i)
Output:
5×1=5 ... 5×10=50
s = "Python"
for ch in reversed(s):
print(ch, end="")
Output: nohtyP
for ch in "HELLO":
print(ch)
Output:
n = 5
fact = 1
while n > 0:
fact *= n
n -= 1
print("Factorial =", fact)
Output: Factorial = 120
i = 1
while i <= 10:
print(i)
i += 1
Output: 1 to 10
n = int(input("Enter number: "))
for i in range(1, 11):
print(n, "x", i, "=", n*i)
Output: (depends on input)
num = 1234
sum = 0
while num > 0:
digit = num % 10
sum += digit
num //= 10
print("Sum =", sum)
Output: Sum = 10
for i in range(1, 6):
print("*" * i)
Output:
*
**
***
****
*****
for i in range(5, 0, -1):
print("*" * i)
Output:
*****
****
***
**
*
for i in range(1, 4):
for j in range(1, 6):
print(i, "x", j, "=", i*j)
print("-----------")
Output: tables of 1, 2, 3
for i in range(1, 51):
if i % 3 == 0 and i % 5 == 0:
print(i)
Output: 15, 30, 45
for i in range(1, 11):
if i == 6:
break
print(i)
Output: 1 2 3 4 5
for i in range(1, 10):
if i % 2 == 0:
continue
print(i)
Output: 1 3 5 7 9
s = 0
for i in range(1, 6):
s += i*i
print("Sum of squares =", s)
Output: 55
# WARNING: Infinite Loop
while True:
print("Hello")
break # prevents actual infinity
Output: Hello
a, b = 0, 1
for i in range(10):
print(a, end=" ")
a, b = b, a + b
Output: 0 1 1 2 3 5 8 13 21 34
Python में Loop Control Statements का प्रयोग loop के normal flow को बदलने के लिए किया जाता है। इनका उपयोग loop को रोकने, किसी iteration को skip करने या future code के लिए placeholder के रूप में किया जाता है।
| Statement | Description |
|---|---|
| break | Stops the loop immediately |
| continue | Skips the current iteration and moves to next |
| pass | Null statement, does nothing (used as placeholder) |
for i in range(1, 11):
if i == 5:
break
print(i)
Output: 1 2 3 4
Explanation: जब i = 5 होता है, loop तुरंत रुक जाता है।
nums = [12, 45, -8, 33, 90]
for n in nums:
if n < 0:
print("First negative =", n)
break
Output: First negative = -8
count = 1
while count <= 5:
print(count)
if count == 3:
break
count += 1
Output: 1 2 3
nums = [10, 20, 30, 40]
target = 30
for n in nums:
if n == target:
print("Found", n)
break
Output: Found 30
for i in range(1, 10):
if i % 2 == 0:
continue
print(i)
Output: 1 3 5 7 9
for i in range(1, 8):
if i == 5:
continue
print(i)
Output: 1 2 3 4 6 7
nums = [12, -7, 8, 0, -2, 15]
for n in nums:
if n <= 0:
continue
print(n)
Output: 12 8 15
for ch in "PYTHON":
if ch in "AEIOU":
continue
print(ch, end="")
Output: PYTHN
marks = [75, 20, 55, 38, 90]
for m in marks:
if m < 40:
continue
print("Passed with", m)
Output:
for i in range(5):
pass # future logic here
print("Loop executed successfully.")
Output: Loop executed successfully.
num = 5
if num > 0:
pass
else:
print("Negative number")
Output: (no output)
def future_task():
pass
print("Function created successfully.")
Output: Function created successfully.
for i in range(1, 11):
if i == 7:
break
print(i)
Output: 1 2 3 4 5 6
for i in range(1, 21):
if i == 15:
break
if i % 2 == 0:
continue
print(i)
Output: 1 3 5 7 9 11 13
for i in range(1, 6):
if i == 3:
pass
else:
print("Processing:", i)
Output:| Statement | Action | Used For |
|---|---|---|
| break | Exits loop | Stop loop immediately |
| continue | Skips iteration | Ignore certain conditions |
| pass | No operation | Placeholder / future code |
जब एक loop के अंदर दूसरा loop होता है, उसे Nested Loop कहा जाता है। Nested loops का प्रयोग patterns, matrices, tables और complex data traversal के लिए किया जाता है। हर outer loop के अंदर inner loop कई बार execute होता है।
for i in range(outer_limit):
for j in range(inner_limit):
# inner loop statements
for i in range(1, 6):
for j in range(1, i + 1):
print("*", end="")
print()
Output:
for i in range(5, 0, -1):
for j in range(i):
print("*", end="")
print()
Output:
for i in range(1, 6):
print(" " * (5 - i) + "*" * i)
Output:
for i in range(1, 6):
print(" " * (5 - i) + "*" * (2*i - 1))
Output:
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2*i - 1))
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2*i - 1))
Output:
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end="")
print()
Output:
for i in range(1, 6):
for j in range(1, i + 1):
print(i, end="")
print()
Output:
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
Output:
for i in range(1, 6):
print(" " * (5 - i), end="")
for j in range(1, i + 1):
print(j, end="")
for j in range(i - 1, 0, -1):
print(j, end="")
print()
Output:
for i in range(65, 70): # ASCII A=65
for j in range(65, i + 1):
print(chr(j), end="")
print()
Output:
for i in range(1, 6):
print(" " * (5 - i), end="")
for j in range(65, 65 + i):
print(chr(j), end="")
print()
Output:
for i in range(69, 64, -1):
for j in range(65, i + 1):
print(chr(j), end="")
print()
Output:
n = 5
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()
Output:
n = 5
for i in range(n):
for j in range(n):
if i == j or i + j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()
Output:
for i in range(1, 6):
for j in range(1, i + 1):
print((i + j) % 2, end="")
print()
Output:ये FAQs Python के Nested Loops और Pattern Programs पर आधारित हैं — जो आजकल O Level, College Exams और Interviews में सबसे ज्यादा पूछे जाते हैं।
जब एक loop के अंदर दूसरा loop हो, तो उसे Nested Loop कहते हैं। इसका उपयोग तब किया जाता है जब हमें दो-स्तरीय iteration (जैसे rows और columns) की आवश्यकता होती है।
जब हमें किसी 2D structure (जैसे matrix या grid) या pattern printing करनी हो, जहाँ rows और columns दोनों को अलग-अलग control करना हो।
यदि outer loop n बार और inner loop भी n बार चलता है, तो उसकी time complexity O(n²) होगी।
Outer loop rows को नियंत्रित करता है और Inner loop columns को। हर outer iteration में inner loop पूरा चलता है और pattern बनता है।
Inner loop में break लगाने से inner loop तुरंत रुक जाता है जबकि continue current iteration को छोड़कर अगली iteration पर चला जाता है।
for i in range(outer_limit):
for j in range(inner_limit):
print(i, j)
बहुत अधिक nesting readability को घटा सकती है, performance धीमी हो सकती है और variable scope में confusion आ सकता है।
क्योंकि हम rows और columns को अलग-अलग नियंत्रित कर सकते हैं — जिससे logic आसान और structured हो जाता है।
for i in range(3):
j = 0
while j < 3:
print(i, j)
j += 1
क्योंकि list comprehension कोड को छोटा और तेज बनाती है — यह nested loops के समान काम को एक लाइन में कर सकती है।