Python Programming
Python Programming tests whether you can read a short program and predict its exact output, and whether you can write small scripts using conditionals, loops, functions, strings and lists. Questions often show a snippet and ask for the output or ask you to correct one faulty line.
Core rules
- Numbers, strings and tuples are immutable; lists, sets and dictionaries are mutable.
- Indentation defines blocks; a colon opens the block and a consistent 4-space indent replaces braces.
- / gives float division, // floor division, % remainder, ** exponent.
- range(start, stop, step) excludes stop; range(1, 5) yields 1, 2, 3, 4.
- Slicing s[a:b] includes a and excludes b; negative indices count from the end; s[::-1] reverses.
- Default arguments are evaluated once at definition, so never use a mutable default such as [].
- Lists are passed by reference, so a function can modify the caller's list.
Worked example
s = "RBSE2024" print(s[2:6], s[-4:], len(s))
- s[2:6] takes indices 2, 3, 4, 5 → "SE20".
- s[-4:] takes the last four characters → "2024".
- len(s) counts all characters → 8. Output: SE20 2024 8.
Traps
- Confusing print with return; a function with no return statement gives None.
- Expecting s.upper() to change s; string methods return a new string.
- Off-by-one in range, and forgetting that range(5) starts at 0.
- Integer division surprises: 7/2 is 3.5, 7//2 is 3, and -7//2 is -4.
- Mixing tabs and spaces, which raises IndentationError.