.py) and the interpreter executes the whole file.#) begins a single-line comment. Triple-quoted strings ('''...''' or """...""") are commonly used for multi-line comments or documentation strings (docstrings).a = b = c = 1 a, b, c = 1, 2, "john"
_) and may contain letters, digits and underscores. Python is case-sensitive (Value and value are different).MyClass).my_variable)._name) indicates the identifier is intended for internal use (conventionally private).__name) invoke name mangling for strongly private identifiers.__init__) are special language-defined names.
None, and collection literals (list, tuple, set, dict).
"hello" or 'hello'.[1, "a", 3.5].(1, 2, 3) or 1, 2, 3.{1, 2, 3}.{ "name": "Asha", "age": 17 }.




if (or elif or else) block may itself contain further if statements; these are nested conditionals.for loops).while loop).
A while loop executes its body as long as the condition is true. The condition is evaluated before each iteration.
a = 3 while (a > 0): print(a) a -= 1 # Output: # 3 # 2 # 1
Ctrl+C. Infinite loops can be intentionally useful in some programmes (for example, servers waiting for input).while loop may include an else block. The else block executes when the loop terminates normally (condition becomes false). It does not execute if the loop is exited by breakor an exception is raised.a = 3 while (a > 0): print(a) a -= 1 else: print("Reached 0") # Output: # 3 # 2 # 1 # Reached 0
break occurs inside the loop the elseclause is skipped.a = 3 while (a > 0): print(a) a -= 1 if a == 1: break else: print("Reached 0") # Output: # 3 # 2
a = 3 while a > 0: print(a); a -= 1 # Output: # 3 # 2 # 1
The for loop iterates over items of a sequence (like elements of a list, characters of a string, or values produced by range()). The syntax differs from C/Java style indexed loops; Python uses the in keyword.
for a in range(3): print(a) # Output: # 0 # 1 # 2
To print 1 to 3 you can adjust the expression inside the loop:
for a in range(3): print(a + 1) # Output: # 1 # 2 # 3
list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list(range(2, 7)) # [2, 3, 4, 5, 6] list(range(2, 12, 2)) # [2, 4, 6, 8, 10] list(range(12, 2, -2)) # [12, 10, 8, 6, 4]
Example: nested loops to print a pattern
for i in range(1, 6): for j in range(1, i): print("*", end=" ") print() # Output: # # * # * * # * * * # * * * *


pdb) or IDE debuggers to step through code, set breakpoints and examine state.[1, 2, "apple"].[start:stop] (stop excluded).L.in and not in test presence.+ to join lists.append(), insert(), extend(), sort(), remove(), reverse(), pop().(1, "a", 3.5).len(), max(), min() are useful; tuples are immutable so there is no append or remove.{ "roll": 21, "name": "Ravi" }.len(), clear(), items(), keys(), values(), update().in and not in test for keys, not values.+ to join strings.* to repeat strings.in and not in to test substrings.==, !=, <, >, <=, >= compare lexicographically.ord(ch) - returns the Unicode (ASCII for ASCII-range) code point of character ch.chr(n) - returns the character corresponding to integer code point n.string[n:m], which returns characters from index n up to but not including m. Omitting n or m uses the start or end of the string respectively.| 1. How do I remember all the Python data types and when to use each one for CBSE exams? | ![]() |
| 2. What's the difference between mutable and immutable objects in Python, and why does it matter? | ![]() |
| 3. How do I fix common syntax errors when writing Python functions and loops? | ![]() |
| 4. What's the easiest way to understand list comprehensions and dictionary comprehensions for my Python revision? | ![]() |
| 5. How should I approach object-oriented programming concepts like classes and inheritance when revising Python? | ![]() |