1/44
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Selection Structures
Allow you to make decisions and execute different code blocks based on certain conditions. They enable your program to choose between alternative paths of execution, making programs dynamic and responsive.
Conditional Statements
Also known as decision-making statements. Evaluate a condition and execute different code blocks based on the result. Fundamental for decision-making in Python. Types: if, if-else, if-elif-else, switch (or elif used in Python).
If Statement (One-way Selection)
Executes a block of code only if a specified condition is True. If False, the block is skipped and the program continues. Syntax example: if condition: statement.
If-Else Statement (Two-way Selection)
Extends the if statement by providing an alternative code block when the condition is False. Executes one block if True, another if False.
If-Elif-Else Statement (Multi-way Selection)
Evaluates multiple conditions sequentially. Used when there are more than two possible outcomes. Creates decision trees and allows complex decision-making.
Nested Decision Making
Placing one or more if…else statements inside another. Used when multiple conditions need to be tested sequentially. Adds flexibility for complex program logic.
Relational (Comparison) Operators
Used to compare values and determine relationships. Examples: == (equal), != (not equal), > (greater than), < (less than), >= (greater or equal), <= (less or equal).
Boolean (Logical) Operators
Used to combine or manipulate boolean values. and → True if both operands are True. or → True if at least one operand is True. not → reverses logical state. Supports short-circuit evaluation for efficiency.
Short-Circuit Evaluation
Python evaluates logical expressions from left to right and stops when the result is known (e.g., in “and” if first is False, in “or” if first is True). Improves code efficiency.
Iterative Structures (Loops)
Allow repeating code blocks multiple times under specific conditions. Used to automate repetitive tasks and control program flow efficiently. Two main types: while and for loops.
While Loop
Repeats a block of code as long as a condition is True. Commonly used for reading input, data traversal, or iterative algorithms. Stops when the condition becomes False.
For Loop
Executes a block of code for each element in a sequence (list, tuple, string, or range). Ideal when the number of iterations is known. Syntax: for item in sequence: statements.
Loop Structure
Loop header defines iterations and ends with a colon (:). The loop body (indented lines) executes repeatedly per iteration. When the condition becomes False, the loop stops.
Nested Loops
Loops inside another loop. The inner loop runs multiple times for each iteration of the outer loop. Used for multidimensional data, pattern generation, and complex algorithms.
Break Statement
Used to exit a loop immediately even if its condition hasn’t become False. Helps improve efficiency by stopping unnecessary iterations once a condition is met.
Importance of Break
Used to exit early when conditions are met, to skip redundant code, or to exit nested loops for efficiency and performance.
Continue Statement
Skips the rest of the code in the current loop iteration and moves to the next iteration. Useful for bypassing unwanted steps while still continuing the loop.
Importance of Continue
Improves efficiency by avoiding unnecessary operations. Useful in filtering or skipping specific data within loops.
List
A mutable, ordered collection of elements (integers, strings, etc.). Allows storing and organizing data sequences and modifying them dynamically.
Purpose of Lists
Creating a List
Use square brackets [ ] and separate elements with commas. Lists can contain mixed data types.
Accessing List Elements
Elements are accessed using indices starting from 0. Example: list[0] for first element, list[-1] for last.
List Operators and Functions
Examples: L[i] – access by index; L[start:end] – slicing; L1 + L2 – concatenation; len(L) – number of elements; print(L) – display list; value in L – membership test.
List Methods (Insertion and Removal)
L.append(x) adds to end. L.extend(aList) adds another list. L.insert(i,x) inserts at position. L.pop() removes last item; L.pop(i) removes at index.
Searching and Sorting in Lists
in operator checks existence. index() returns element position. L.sort() arranges elements in ascending order.
Mutator Methods for Lists
insert(), append(), extend(), pop(), and sort() — these modify lists directly.
Importance of Lists
Provide dynamic storage, easy iteration, and organization of heterogeneous data. Core Python data structure for algorithms and applications.
Dictionaries
Mutable, unordered collections of key:value pairs. Keys are unique and associated with values. Written inside curly braces {key:value}.
Purpose of Dictionaries
Used for fast lookup, association, and data mapping (e.g., storing records or configurations).
Adding and Replacing Values in Dictionaries
Use d[key] = value to insert or update. If key exists, value is replaced; if not, key is added.
Accessing Values in Dictionaries
Use d[key] to get a value or d.get(key, default) to avoid KeyError. Use “key in d” to check existence.
Removing Keys from Dictionaries
Use d.pop(key[,default]) to remove and return value; d.clear() to remove all items.
Traversing a Dictionary
for key in d: loops through all keys. list(d.keys()), list(d.values()), list(d.items()) return lists of keys, values, and key-value tuples.
Dictionary Operations
len(d) – count entries. list(d.keys()) – all keys. list(d.values()) – all values. list(d.items()) – key:value tuples.
Importance of Dictionaries
Organize data efficiently using key-value mapping; enable fast data retrieval and structured storage.
Tuples
Ordered, immutable collections of elements. Similar to lists but cannot be changed after creation. Efficient and used for fixed data.
Tuple Packing and Unpacking
Packing: assigning multiple values into one tuple. Unpacking: extracting tuple elements into separate variables.
Basic Tuple Operations
Concatenation (+), repetition (*), and membership test (in). Tuples support indexing and iteration like lists.
Practical Uses of Tuples
Ensure data integrity (immutable), allow returning multiple values from functions, and define named tuples for readability.
Sets
Unordered collections storing unique elements only. Created with braces {} or set(). Do not allow duplicates.
Common Uses of Sets
Eliminate duplicates, perform fast membership testing, and find common elements (intersection) between sets.
Set Methods
.add() – add element; .clear() – remove all; .copy() – copy set; .difference() – elements not in others; .intersection() – common elements; .union() – combine sets; .update() – add elements from another iterable.
Advanced Set Methods
.differenceupdate() removes overlapping items; .discard(x) removes item if present; .isdisjoint(), .issubset(), .issuperset() test relationships; .symmetricdifference() returns non-common elements.
Comparison of Data Structures
List → ordered, changeable, allows duplicates. Dictionary → key-ordered, mutable, keys unique. Tuple → ordered, immutable. Set → unordered, unique elements only.