01
Unit 1
Features, Identifiers & Keywords
Python is an interpreted, high-level, dynamically typed, object-oriented language created by Guido van Rossum (1991). Key features: simple syntax, portable, extensible, free/open-source, large standard library.
Identifier Rules
| Rule | Valid | Invalid |
|---|---|---|
| Letters, digits, underscore only | my_var, x1 | my-var, x@1 |
| Cannot start with digit | _count, name | 1count, 2name |
| Case sensitive | Age ≠ age | — |
| Cannot be keyword | myif | if, for, while |
Python Keywords (35)
FalseNoneTrueandasassertasyncawaitbreakclasscontinuedefdelelifelseexceptfinallyforfromglobalifimportinislambdanotorpassraisereturntrywhilewithyield
02
Unit 1
Variables, Data Types & Type Conversions
| Type | Example | Notes |
|---|---|---|
int | x = 10 | Unlimited precision integers |
float | x = 3.14 | 64-bit IEEE 754 |
complex | x = 3+4j | Real + imaginary part |
str | x = "hello" | Immutable sequence of chars |
bool | x = True | Subclass of int; True=1, False=0 |
list | x = [1,2,3] | Mutable ordered sequence |
tuple | x = (1,2,3) | Immutable ordered sequence |
dict | x = {'a':1} | Key-value pairs |
set | x = {1,2,3} | Unordered, no duplicates |
Type Conversion Functions
int("42") # → 42 float(10) # → 10.0 str(3.14) # → "3.14" bool(0) # → False (0, "", [], None → False; everything else → True) list("abc") # → ['a', 'b', 'c'] tuple([1,2]) # → (1, 2)
03
Unit 1–2
Operators, Precedence & Associativity
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + - * / // % ** | 17//3=5, 17%3=2, 2**3=8 |
| Comparison | == != < > <= >= | 5 != 3 → True |
| Logical | and or not | True and False → False |
| Assignment | = += -= *= /= //= %= | x += 5 |
| Bitwise | & | ^ ~ << >> | 5 & 3 = 1 |
| Identity | is, is not | x is None |
| Membership | in, not in | 3 in [1,2,3] |
Precedence (high→low):
Associativity: Left-to-right except
** → ~ + - (unary) → * / // % → + - → << >> → & → ^ → | → comparison → not → and → orAssociativity: Left-to-right except
** (right-to-left)
04
Unit 3
Input, Output, Import & Math Functions
# input() always returns string name = input("Enter name: ") age = int(input("Enter age: ")) # print() with sep and end print("Hello", name, sep=", ", end="!\n") print(f"Age: {age}") # f-string (preferred) print("%.2f" % 3.14159) # → 3.14 # range(start, stop, step) list(range(1, 10, 2)) # → [1, 3, 5, 7, 9]
math module
import math math.sqrt(16) # 4.0 math.ceil(4.2) # 5 math.floor(4.9) # 4 math.pow(2,8) # 256.0 math.log(100,10)# 2.0 math.factorial(5) # 120 math.pi # 3.14159... math.e # 2.71828...
05
Unit 4
Decision-Making Structures
# if / elif / else if x > 0: print("positive") elif x == 0: print("zero") else: print("negative") # Ternary (conditional expression) result = "even" if x % 2 == 0 else "odd" # Nested if if a > 0: if b > 0: print("both positive")
Python has no switch/case (pre-3.10). Use
if/elif chains or a dict mapping. Python 3.10+ has match/case.06
Unit 5
Looping Structures
# while loop i = 1 while i <= 5: print(i) i += 1 # for loop with range for i in range(1, 6): print(i) # for loop over iterable for ch in "hello": print(ch) # else on loop — runs if loop NOT broken for i in range(5): print(i) else: print("done")
Nested loops: Time complexity multiplies. O(n²) for two nested loops each running n times.
07
Unit 6
Control Statements
| Statement | Effect | Works In |
|---|---|---|
break | Exit loop immediately | for, while |
continue | Skip rest of current iteration | for, while |
pass | Do nothing (placeholder) | Anywhere |
# break — find first even for i in range(10): if i % 2 == 0: print(i); break # continue — skip odd for i in range(5): if i % 2 != 0: continue print(i) # 0 2 4 # pass — empty function body def todo(): pass
FC
Quick Review
Flashcards — Module I
Tap to flip