Syllabus topics: Introduction to Python, features of Python, programming modes (interactive and script), identifiers, naming conventions, keywords, built-in data types, literals (integer, float, complex, boolean, string), variables, operators, expressions, assignment statements, input/output statements, Python syntax (lines, comments, indentation), operators and operands, classification of operators (arithmetic, relational, logical, bitwise, assignment, augmented assignment, identity), expressions and precedence rules.
Python was created by Guido van Rossum and released in 1991. The name comes from Monty Python's Flying Circus, not the snake.
Features — a standard five-mark question:
malloc/free| C | Python | |
|---|---|---|
| Type system | Static — declare every type | Dynamic — inferred from the value |
| Execution | Compiled | Interpreted |
| Memory | Manual (malloc/free) |
Automatic (garbage collected) |
| Block structure | Braces {} |
Indentation |
| Statement end | Semicolon required | Newline |
| Speed | Fast | Slower |
| Lines to do the same job | More | Fewer |
Interactive mode — the >>> prompt. Type a statement, see the result
immediately. Ideal for experimenting; nothing is saved.
>>> 2 + 3
5
>>> print("Hello")
Hello
Script mode — write a .py file and run it with python3 program.py.
Reusable, and how real programs are written.
One difference that catches people out: in interactive mode an expression's
value is displayed automatically; in script mode you must print() it.
Rules (enforced by the interpreter):
total, Total and TOTAL are three namesConventions (PEP 8 — not enforced, but expected):
| Kind | Convention | Example |
|---|---|---|
| Variable, function | lower_snake_case |
total_marks, find_average() |
| Constant | UPPER_SNAKE_CASE |
MAX_SIZE, PI |
| Class | PascalCase |
Student, BankAccount |
| Private (by convention) | leading underscore | _internal |
| Name-mangled | two leading underscores | __private |
| Dunder (special) | double underscore both ends | __init__, __str__ |
Python 3 has 35 keywords:
False None True and as assert async await
break class continue def del elif else except
finally for from global if import in is
lambda nonlocal not or pass raise return try
while with yield
Check at any time with import keyword; print(keyword.kwlist).
| Category | Types |
|---|---|
| Numeric | int, float, complex |
| Boolean | bool |
| Sequence | str, list, tuple, range |
| Set | set, frozenset |
| Mapping | dict |
| Binary | bytes, bytearray, memoryview |
| None | NoneType |
count = 42 # int -- unlimited size in Python 3
price = 99.75 # float
z = 3 + 4j # complex -- z.real is 3.0, z.imag is 4.0
flag = True # bool
name = "Ananya" # str
nothing = None # NoneType
Integers have no size limit in Python 3. 2 ** 1000 is computed exactly —
no overflow, unlike C. This is a genuinely notable difference and a good exam
point.
bool is a subclass of int: True == 1 and False == 0 are both true,
and True + True is 2. Odd, but occasionally useful:
sum(x > 50 for x in marks) counts how many exceed 50.
0b1010 # binary -> 10
0o17 # octal -> 15
0xFF # hexadecimal -> 255
1_000_000 # underscores for readability -> 1000000
type(42) # <class 'int'>
isinstance(42, int) # True -- preferred, because it honours inheritance
Python variables are references to objects, not boxes holding values.
x = 10 # the name x now refers to an int object with value 10
x = "hello" # x now refers to a str object -- perfectly legal
No declaration and no fixed type. This is dynamic typing.
a, b, c = 1, 2, 3 # unpacking
x = y = z = 0 # chained -- all three refer to the same object
a, b = b, a # swap, with no temporary variable
That last one is worth remembering: Python swaps in one line where C needs three.
| Immutable (cannot be changed in place) | Mutable (can be) |
|---|---|
int, float, complex, bool |
list |
str |
dict |
tuple |
set |
frozenset |
bytearray |
s = "hello"
s[0] = "H" # TypeError -- strings are immutable
s = "H" + s[1:] # build a new string instead
lst = [1, 2, 3]
lst[0] = 99 # fine -- lists are mutable
This distinction governs function argument behaviour, dictionary keys, default arguments and copying. It is examined constantly. Learn it now.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
True division | 7 / 3 |
2.3333 |
// |
Floor division | 7 // 3 |
2 |
% |
Modulus | 7 % 3 |
1 |
** |
Exponent | 7 ** 3 |
343 |
/ always returns a float, even when it divides evenly: 6 / 3 is 2.0,
not 2. This differs from C, where 7/3 is 2. In Python you must write
7 // 3 for that.
Floor division rounds towards negative infinity, not towards zero:
-7 // 2 is -4, not -3. Similarly -7 % 2 is 1, because Python's
modulus takes the sign of the divisor. C would give -1.
==, !=, <, >, <=, >= — all return True or False.
Chained comparison is a Python feature C lacks:
if 0 < x < 100: # reads naturally, and means 0 < x and x < 100
and, or, not — words, not &&, ||, !.
They short-circuit: and stops at the first falsy operand, or at the
first truthy one. So if x != 0 and 10 / x > 2 is safe — the division is never
attempted when x is 0.
They also return an operand, not necessarily a boolean:
"a" and "b" # 'b' -- both truthy, so the last is returned
"" or "default" # 'default'
Falsy values: False, None, 0, 0.0, "", [], (), {},
set(). Everything else is truthy.
& AND · | OR · ^ XOR · ~ NOT · << left shift · >> right shift
12 & 5 # 1100 & 0101 = 0100 = 4
12 | 5 # 1101 = 13
12 ^ 5 # 1001 = 9
12 << 1 # 24 -- shifting left by 1 doubles
12 >> 1 # 6 -- shifting right by 1 halves
= += -= *= /= //= %= **= &= |= ^= >>= <<=
x += 5 is shorthand for x = x + 5 — though for mutable objects the two are
not identical (+= on a list modifies in place; = x + builds a new one).
| Operator | Question it answers | |
|---|---|---|
| Equality | == |
Do these have the same value? |
| Identity | is |
Are these the same object in memory? |
a = [1, 2, 3]
b = [1, 2, 3]
c = a
a == b # True -- same contents
a is b # False -- two separate list objects
a is c # True -- c is another name for the same object
Use is only with None, True and False. if x is None is correct
style; if x is 5 may work by accident (small integers are cached) and fail
for larger values.
in and not in:
"a" in "cat" # True
3 in [1, 2, 3] # True
"key" in {"key": 1} # True -- checks KEYS, not values
**
~ +x -x (unary)
* / // %
+ -
<< >>
&
^
|
== != > >= < <= is is not in not in
not
and
or
Two consequences worth remembering: ** binds tighter than unary minus, so
-2 ** 2 is -4; and ** is right associative, so 2 ** 3 ** 2 is
2 ** 9 = 512, not 64.
name = input("Enter your name: ") # ALWAYS returns a string
age = int(input("Enter your age: ")) # convert explicitly
cgpa = float(input("Enter your CGPA: "))
input() always returns a string. Forgetting to convert is the single most
common beginner error: input() + 1 raises TypeError, and
if input() == 5 is never true because "5" != 5.
name, marks = "Ananya", 87.5
print(f"{name} scored {marks}") # f-string -- modern, preferred
print("{} scored {}".format(name, marks)) # .format()
print("%s scored %.2f" % (name, marks)) # %-formatting -- C style
print(f"{marks:.2f}") # 87.50 -- two decimals
print(f"{name:>10}") # right-aligned in 10 columns
print(f"{name:<10}") # left-aligned
print(f"{name:^10}") # centred
print() parameters:
print("a", "b", sep="-") # a-b -- separator between items
print("a", end="") # no newline at the end
This is Python's most distinctive feature. Where C uses {}, Python uses
indentation, and it is not optional:
if x > 0:
print("positive") # 4 spaces -- part of the if block
print("still inside")
print("always runs") # unindented -- outside the block
Use 4 spaces per level. Never mix tabs and spaces — Python 3 rejects it
with a TabError. Configure your editor to insert spaces when Tab is pressed.
# a single-line comment
"""
A triple-quoted string. Used as a docstring when it is the first statement
in a module, function or class; otherwise it is just an unused string that
happens to serve as a multi-line comment.
"""
def area(r):
"""Return the area of a circle of radius r.""" # a real docstring
return 3.14159 * r ** 2
print(area.__doc__) # docstrings are retrievable at runtime
One statement per line, no semicolon needed. To break a long line:
total = (first_value +
second_value +
third_value) # implicit continuation inside brackets
total = first_value + \
second_value # explicit continuation -- avoid if you can
Brackets are preferred; a stray space after a backslash breaks it silently.
Two marks
/ and //?== and is.input() return, and why does it matter?Five marks
2 ** 3 ** 2.Ten marks
COMMON ERRORS
int() around input()/ to return an integer&&, ||, ! instead of and, or, notis to compare values instead of ==: after if, for, while, def, class-2 ** 2 is 4 (it is -4)