BCC302 · YEAR 2 · SEM 3
Python Programming — Last-Night Cheat Sheet
Programming answers earn marks for exact syntax plus a traced output. Keep each unit's constructs in its own section.
1. Introduction to Python
Recall anchor. Name → object → operator → indented block. A Python statement is a binding, not a declaration of fixed type.
Scored points.
- Explain a variable as a name bound to an object and note that reassignment can change its type.
- List the basic operators: arithmetic, comparison, assignment, logical and membership.
- State that numeric data types include int for whole numbers and float for decimal values.
- Show how a colon introduces a block and how indentation, not braces, defines the block.
- Use type conversion functions when combining int and float values.
- Distinguish an expression from a statement and show both in a short program.
- Write a small program that reads input, stores it in a variable and prints the result.
Formula strip.
Micro-example. x = 7; print(x // 2, x % 2, x ** 2) prints 3 1 49 — floor division, remainder and power.
Examiner line. Write the syntax line first, state what it does, and finish with the exact printed output.
2. Python Program Flow Control Conditional blocks
Recall anchor. Branch with if, repeat with for or while, steer with break/continue. Trace the loop before writing its output.
Scored points.
- Use if, else and else if (elif) to select one path from several conditions.
- Use a simple for loop to visit each item of a sequence.
- Use for with range for a counted loop and state that the stop value is excluded.
- Use while when the number of repetitions is unknown before entry.
- Apply pass as a placeholder, continue to skip the rest of the current iteration and break to leave the loop.
- Explain the loop else block, which runs only when no break occurred.
- Combine a conditional and a loop in a program that validates input or accumulates a total.
Formula strip.
Micro-example. for i in range(1, 6, 2): print(i) prints 1, 3, 5 because the stop value 6 is excluded and the step is 2.
Examiner line. State the branch or loop type, trace the values for two or three iterations, and end with the complete output line.
3. Python Complex data types
Recall anchor. Sequence for order, dictionary for lookup, function for reuse. Choose the container before choosing the method.
Scored points.
- Create strings and apply string operations such as concatenation, repetition and membership.
- Define a list and slice it with start:end, remembering that the end index is excluded.
- State that a tuple is an ordered immutable sequence and show where immutability matters.
- Define a dictionary of key–value pairs and retrieve or update a value by key.
- Use built-in functions such as len, sorted, min, max and type on strings, lists and dictionaries.
- Write string, list and dictionary manipulation statements using the relevant methods.
- Organise code into functions with parameters and a return value to avoid repetition.
Formula strip.
Micro-example. For a = [1, 2, 3, 4], the slice a[1:3] evaluates to [2, 3]; a[::-1] gives [4, 3, 2, 1].
Examiner line. Name the data type, write the exact method or slice, and close with the resulting value or printed line.
4. Python File Operations
Recall anchor. Open → read/write → close. The file mode decides what is allowed, and with guarantees closure.
Scored points.
- Open a file with the correct mode: r for reading, w for writing, a for appending.
- State that w truncates an existing file while a preserves it.
- Use read() for the whole content, readline() for one line and readlines() for a list of lines.
- Use write() for one string and writelines() for a sequence of strings.
- Move the file pointer with seek() and inspect it with tell().
- Use a with statement so the file closes automatically even if an error occurs.
- Write a short program that reads a text file and writes an upper-case copy.
Formula strip.
Micro-example. f.readlines() returns a list such as ['first\n', 'second\n'], so a newline remains attached to each element.
Examiner line. State the mode first, show the read or write call, and end with the file content or the pointer position.
5. Python packages
Recall anchor. Import, then call the package's own functions. Name the package before writing the call.
Scored points.
- Import a package with import or import ... as and call its built-in functions.
- Use matplotlib to plot x–y data with plot, labels and show.
- Use numpy arrays and functions such as array, mean and shape for numeric work.
- Use pandas to read tabular data with read_csv and inspect it with head.
- Introduce Tkinter as the standard GUI toolkit and create a root window.
- Add and pack Tk widgets such as Label, Entry, Button and Frame.
- State that an IDE provides editing, debugging and running facilities in one place.
Formula strip.
Micro-example. np.mean([2, 4, 6]) returns 4.0; plt.plot([1,2],[3,4]) draws one line through those two points.
Examiner line. Name the package and import form, write one correct function call, and finish with the value, plot or widget produced.
Memory map
%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '17px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart LR
U1["1. Introduction to Python"]
A1["Python basic Operators"]
A2["Declaring and using Numeric data types: int"]
U2["2. Python Program Flow Control Conditional blocks"]
B1["For loop using ranges"]
B2["Loop manipulation using pass"]
U3["3. Python Complex data types"]
C1["Defining list and list slicing"]
C2["Python Functions"]
U4["4. Python File Operations"]
D1["Understanding read functions"]
D2["Manipulating file pointer using seek Programming"]
U5["5. Python packages"]
E1["Simple programs using the built-in functions of packages matplotlib"]
E2["GUI Programming: Tkinter introduction"]
U1 --> A1 --> A2 --> U2
U2 --> B1 --> B2 --> U3
U3 --> C1 --> C2 --> U4
U4 --> D1 --> D2 --> U5
U5 --> E1 --> E2
Rapid recall
- U1 — Python variables · Python basic Operators · Understanding python blocks
- U1 — Python Data Types · Declaring and using Numeric data types: int
- U2 — if · else and else if · Simple for loops in python · For loop using ranges
- U2 — Use of while loops in python · Loop manipulation using pass · continue · break and else
- U3 — Using string data type and string operations · Defining list and list slicing · Use of Tuple data type
- U3 — String · List and Dictionary · string manipulation methods · List manipulation · Dictionary manipulation
- U4 — Reading files · Writing files in python · read() · readline() · readlines()
- U4 — write() and writelines() Manipulating file pointer using seek Programming · using file operations
- U5 — Simple programs using the built-in functions of packages matplotlib · numpy · pandas etc
- U5 — GUI Programming: Tkinter introduction · Tk Widgets · Tkinter examples · Python programming with IDE
Final-answer checklist
- Reproduce the exact BCC302 spelling, for example "else and else if" and "write() and writelines() Manipulating file pointer using seek Programming".
- Write executable syntax: colons, indentation and parentheses must be correct.
- For a trace question show at least two iterations or one slice evaluation before the final output.
- For file questions state the mode and whether the file is truncated or appended.
- For package questions name the import form and the exact function called.
- End every program answer with the printed output or the produced file/plot in one clear line.