Grade Checker (if-elif-else)
This mirrors Example 5.1 from the chapter — an if-elif-else chain that checks marks against several conditions in order, and executes only the first block that matches.
Enter Marks → Grade
● liveConditional Statements
Conditional statements let a program make decisions and are the backbone of logical flow control. Python's two main conditional constructs are if-elif-else and match-case.
Why control structures matter
Decision making · Flow control · Iterations · Improved efficiency & flexibility · Real-world applications.
Code Blocks & Indentation
In Python, indentation (whitespace at the start of a line) is the only way to define a code block — it is mandatory and directly affects execution.
a. if-elif-else Statement
Rules
The if checks the first condition. If false, each elif checks another. If none are true, else runs. Multiple elif branches are allowed; only one else, and it must come last. Neither elif nor else is compulsory.
Syntax
if condition: code block elif condition: code block else: code block
b. match-case Statement Python 3.10+
Introduced in Python 3.10 as part of structural pattern matching, match-case is an alternative to the switch statement found in C++, Java, or JavaScript. It compares a value (the pattern) against multiple cases and runs the code for the first match. The underscore _ acts as the default case.
Cleaner
More readable than long if-elif-else chains.
Advanced patterns
Supports matching tuples, lists, and conditions within patterns.
Maintainable
Enhances code maintainability for complex conditional logic.
Looping Statements
Looping executes a set of instructions repeatedly until a condition is met — reducing the need to write the same code multiple times. A loop checks a condition before or after running its block; when the condition becomes false, it stops automatically.
(i) for loop
A for loop iterates over a sequence (list, string, etc.) — "do something for each item in this group." The code block runs once per item, until the sequence is exhausted.
break statement
Immediately stops the loop when a condition is met — prevents unnecessary iterations.
continue statement
Skips the current iteration and moves to the next one, without stopping the whole loop.
for loop with else
If the for loop finishes normally the statement in the else clause will be executed
(ii) while loop
A while loop repeats a block based on a condition evaluated before each iteration. Once the condition becomes False, the loop terminates.
Infinite loops
Occur when the condition never becomes False — usually because the loop control variable is never updated (e.g. forgetting i += 1).
while loop with break statement
The break statement is used to exit from the loop, even if the condition is still true.
while loop with continue statement
The continue statement allows the while loop to skip the current iteration and moves to the next one.
Nested while loop
A while loop can be placed inside another — Example 5.15 prints all combinations of i (1–3) and j (1–2).
Input validation
Repeatedly ask for input until valid data is entered.
Menu-driven programs
Keep displaying options until the user chooses to exit.
Game loops
Continuously run game logic until the player quits.
Lists
A list stores multiple items in a single variable — one of Python's four built-in collection types (the others being Tuple, Set, and Dictionary). Lists are created with square brackets [ ] and are the most flexible of the four.
Characteristics of Lists
Ordered
Items keep a defined order unless explicitly changed.
Mutable
Can add, remove, or modify elements after creation.
Allow duplicates
The same value can appear more than once.
Indexed
Each item has a unique index, starting from 0.
Slicing lists
Slicing is used to access a range of elements from a list.
Mofifying lists
Lists are mutable so that their elements can be changed.
List Methods Summary
| Method | Description |
|---|---|
| append() | Adds an item to the end |
| insert() | Inserts item at specified index |
| extend() | Adds multiple items |
| remove() | Removes first occurrence of item |
| pop() | Removes item at index |
| clear() | Removes all items |
| index() | Returns index of first occurrence |
| count() | Returns number of occurrences |
| sort() | Sorts the list in ascending order |
| reverse() | Reverses the list |
| copy() | Returns a shallow copy of the list |
Tuples
A tuple is like a list, but immutable — its contents cannot change once created. This makes tuples ideal for fixed collections such as coordinates, configuration settings, or constants. Tuples use parentheses ( ).
Limited methods
Because tuples are immutable, they only support count() and index().
Membership & length
Use in to test membership and len() to get the number of items — the same as lists.
Sets
A set stores multiple items, but is unordered, unindexed, and disallows duplicates — ideal for filtering duplicates and performing mathematical operations like union and intersection. Sets use curly braces { } or set().
Unordered & Unindexed
No guaranteed order; elements cannot be accessed by index.
No duplicates
Duplicate values are automatically removed.
Immutable elements, mutable container
Elements must be immutable (numbers, strings, tuples) — but the set itself can grow or shrink.
Set Methods Summary
| Method | Description |
|---|---|
| add() | Adds a single element |
| update() | Adds multiple elements |
| remove() | Removes specified element (error if not found) |
| discard() | Removes specified element (no error) |
| pop() | Removes a random element |
| clear() | Removes all elements |
| union() | Returns union of sets |
| intersection() | Returns common elements |
| difference() | Returns elements in one set but not the other |
| symmetric_difference() | Returns elements in either set but not both |
| issubset() | Checks if one set is a subset of another |
| issuperset() | Checks if one set is a superset of another |
| isdisjoint() | Checks if sets have no common elements |
Dictionaries
A dictionary stores data as key–value pairs — also called associative arrays or hash maps in other languages. Instead of numeric indices, dictionaries use unique keys to access values, making them ideal for structured data.
Key–Value pairs
Each item pairs a key with its associated value.
Ordered (3.7+)
From Python 3.7 onward, dictionaries preserve insertion order.
Unique keys
A repeated key overwrites the previous value.
Immutable keys
Keys must be strings, numbers, or tuples — lists/dicts cannot be keys.
Dictionary Methods Summary
| Method | Description |
|---|---|
| get(key) | Returns the value for the key |
| keys() | Returns a view of all keys |
| values() | Returns a view of all values |
| items() | Returns a view of all key-value pairs |
| update(dict2) | Updates the dictionary with another dictionary |
| pop(key) | Removes the item with the specified key |
| popitem() | Removes the last inserted item |
| clear() | Removes all items |
| copy() | Returns a shallow copy of the dictionary |
| setdefault() | Returns value of a key; sets default if not found |
List, Tuple, Set & Dictionary — Comparison
All four are built-in Python collection types, but each fits a different purpose.
| Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Syntax | [ ] | ( ) | { } or set() | {key: value} |
| Mutable | Yes | No | Yes | Yes |
| Ordered | Yes | Yes | No | Yes (3.7+) |
| Indexed | Yes (0-based) | Yes (0-based) | No | No (access via keys) |
| Allows duplicates | Yes | Yes | No | Keys: No, Values: Yes |
| Element access | By index | By index | Not allowed | By key |
| Use case | General-purpose collection | Fixed data, read-only collections | Unique items, set operations | Structured key-value mapping |
| Common methods | append(), insert(), pop() | count(), index() | add(), remove(), union() | get(), keys(), items() |
Quick Recap
Tap a question to reveal the answer.
How many else blocks can be used in an if structure?
Only one — and it must come last in the chain.
Which symbol represents the default case in match-case?
The underscore _.
What is the default start value of range()?
0 — range(stop) starts counting from 0 by default.
Which statement stops a loop immediately: continue, break, stop, or exit?
break.
What is the index of the first element in a list?
0 — Python indexing always starts at 0.
Which data structure does not allow duplicates?
Set. (Dictionary keys also can't duplicate, but values can.)
What does a & b represent for two sets?
Intersection — the elements common to both sets.
Which method safely retrieves a value from a dictionary without raising an error?
get() — it returns None (or a default) if the key doesn't exist, instead of raising a KeyError.
What is a tuple, and why is it called immutable?
A tuple is an ordered collection of items in parentheses. It is immutable because its contents cannot be changed, added to, or removed once created.
Differentiate between break and continue.
break exits the loop entirely. continue skips only the current iteration and moves on to the next one.