Skip to content

Python Basics, Control Flow & Core Structures

Python is a high-level, interpreted, dynamically typed programming language known for its clear syntax and extensive ecosystem. In CBSE Information Practices (Code 065), strong Python foundations are essential for manipulating NumPy arrays, Pandas Series, and DataFrames.


1. Python Tokens

A Token is the smallest individual unit in a Python program.

  1. Keywords: Reserved words with predefined meanings (e.g., if, for, def, return, True, False, None, import).
  2. Identifiers: User-defined names for variables, functions, and objects.
    • Rules: Must start with a letter (az,AZ) or underscore (_). Cannot start with a digit. Cannot be a keyword.
  3. Literals: Constant values assigned to variables (e.g., Integer: 42, Float: 3.14, String: 'CBSE', Boolean: True).
  4. Operators: Symbols that trigger mathematical or logical computation (e.g., +, -, *, /, //, %, **, ==, !=, and, or, not).
  5. Punctuators / Delimiters: Symbols used to organize code structures (e.g., ( ), [ ], { }, :, ,, .).

2. Mutable vs Immutable Data Types

mermaid
graph TD
    DT["Python Data Types"] --> Imm["Immutable (Value CANNOT be changed in-place)"]
    DT --> Mut["Mutable (Value CAN be changed in-place)"]
    
    Imm --> I1["Integers, Floats, Complex, Booleans"]
    Imm --> I2["Strings (`str`)"]
    Imm --> I3["Tuples (`tuple`)"]
    
    Mut --> M1["Lists (`list`)"]
    Mut --> M2["Dictionaries (`dict`)"]
    Mut --> M3["Sets (`set`)"]

WARNING

Attempting in-place modification on an immutable type raises a TypeError:

python
s = "Python"
s[0] = "J"  # TypeError: 'str' object does not support item assignment

3. Control Flow Statements

3.1 Conditional Branching (if-elif-else)

python
score = 88

if score >= 90:
    grade = 'A1'
elif score >= 80:
    grade = 'A2'
elif score >= 70:
    grade = 'B1'
else:
    grade = 'Pass'

print(f"Student Grade: {grade}")  # Output: Student Grade: A2

3.2 Iteration Loops (for & while)

The range(start, stop, step) Function

python
# Generates sequence from start up to (stop - 1) with specified step
list(range(1, 10, 2))  # [1, 3, 5, 7, 9]
list(range(5, 0, -1))  # [5, 4, 3, 2, 1]

Loop Control: break vs continue

  • break: Immediately terminates the loop and jumps to the next statement outside.
  • continue: Skips the remainder of the current iteration and jumps to the next loop cycle.

4. Core Data Structures: Lists & Dictionaries

4.1 Lists (Ordered, Mutable Sequences)

python
marks = [75, 82, 90, 65, 88]

# Common List Operations:
marks.append(95)       # Adds 95 to the end -> [75, 82, 90, 65, 88, 95]
marks.insert(1, 80)    # Inserts 80 at index 1
marks.pop()            # Removes and returns the last element (95)
marks.sort(reverse=True) # Sorts descending

4.2 Dictionaries (Key-Value Mappings)

A dictionary is a mutable collection of key-value pairs where keys must be unique and immutable.

python
student = {
    'RollNo': 101,
    'Name': 'Ananya Sharma',
    'Stream': 'Commerce with IP',
    'Marks': {'English': 92, 'IP': 98, 'Accountancy': 94}
}

# Accessing & Modifying:
print(student['Name'])           # 'Ananya Sharma'
student['City'] = 'New Delhi'    # Adds new key-value pair
keys_list = list(student.keys()) # ['RollNo', 'Name', 'Stream', 'Marks', 'City']

TIP

In Pandas, creating a DataFrame from a dictionary of lists is one of the most common Class 12 IP board questions:

python
import pandas as pd
data = {'Subject': ['IP', 'Maths', 'CS'], 'AvgScore': [92, 85, 88]}
df = pd.DataFrame(data)

Flügel Information Practices Academic Treatise (CBSE Code 065) • Contact Editorial TeamPrivacy Policy