Python Basics Class 9
Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. Today it is one of the most popular pr...
Introduction to Python: Why It Matters and How to Get Started
Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. Today it is one of the most popular programming languages in the world, used in web development, data science, artificial intelligence, automation, scientific research, and education. CBSE introduced Python as the programming language for Class 9 and above because it is readable, beginner-friendly, and powerful.
"High-level" means Python's syntax is close to English and hides the low-level details of how the computer manages memory. Compare these two ways to print "Hello" on screen — in Assembly language you need several cryptic instructions; in Python you write simply: print("Hello"). That clarity is why Python is taught to beginners.
Python is an interpreted language. This means Python code is executed line by line by an interpreter rather than being compiled into machine code first. The advantage is that you see results immediately and can fix errors one at a time. The slight disadvantage is that interpreted programs typically run slower than fully compiled programs, though for most student-level tasks this difference is imperceptible.
To start using Python, you need to install it from python.org (it is free and open source). IDLE, the Integrated Development and Learning Environment that comes with Python, is perfectly adequate for Class 9 work. You write code in the editor, press F5 (Run), and see output in the Shell window. Alternatively, you can type commands directly in the interactive Shell and see results immediately, line by line.
Python's interactive shell is excellent for experimenting. Type 2 + 3 and press Enter; Python instantly shows 5. Type print("India") and press Enter; Python prints India. This instant feedback loop makes learning much more engaging than compile-and-run workflows.
Exam tip: CBSE Class 9 Python questions may ask you to identify and correct syntax errors in a given code snippet. The most common beginner mistakes are missing colons after if and for statements, incorrect indentation, and forgetting quotation marks around strings. Review these error types carefully.
Python Data Types, Variables, and Basic Input/Output
In Python, every piece of data has a type that determines what operations you can perform on it. The four fundamental data types for Class 9 are integers, floats, strings, and booleans.
An integer (int) is a whole number — positive, negative, or zero — with no decimal point: 42, -7, 0. A float is a number with a decimal point: 3.14, -0.5, 100.0. A string (str) is a sequence of characters enclosed in single or double quotes: "Hello", 'CBSE', "2024". A boolean (bool) has only two possible values: True or False (capital letters are required in Python). Booleans are the result of comparisons: 5 > 3 evaluates to True.
A variable is a named storage location that holds a value. In Python, you create a variable simply by assigning a value to a name: age = 15 creates a variable called age and stores the integer 15 in it. name = "Riya" stores the string "Riya". Python is dynamically typed, meaning you do not declare the data type explicitly — Python infers it from the value assigned.
Variable names in Python must start with a letter or underscore, contain only letters, digits, and underscores, and must not be a Python keyword (words like if, for, while, print are reserved). Variable names are case-sensitive: score and Score are two different variables.
The input() function collects text input from the user. name = input("Enter your name: ") displays the prompt, waits for the user to type something, and stores the typed text as a string in the variable name. Important: input() always returns a string. If you need a number, you must convert it: age = int(input("Enter age: ")) or marks = float(input("Enter marks: ")).
The print() function displays output. print("Hello, " + name) joins two strings and prints them. The sep and end parameters customise output: print("a", "b", "c", sep="-") prints a-b-c.
Exam tip: a common Class 9 exam question gives a program and asks what the output will be. Trace through the code step by step, noting what each variable holds after each line, to predict the output accurately.
Python Operators and Conditional Statements
Operators are symbols that perform operations on values. Python has several categories of operators, and mastering them is essential for writing any meaningful program.
Arithmetic operators perform mathematics: + (addition), - (subtraction), * (multiplication), / (division — always returns a float in Python 3), // (floor division — returns an integer by discarding the decimal), % (modulus — returns the remainder), ** (exponentiation). Example: 17 // 5 gives 3, and 17 % 5 gives 2, because 17 = 3 × 5 + 2.
Comparison operators compare two values and return True or False: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to). Note that == checks equality while = assigns a value — confusing these is a very common bug.
Logical operators combine boolean conditions: and (True only if both sides are True), or (True if at least one side is True), not (reverses True to False or False to True).
Conditional statements allow a program to take different actions based on conditions. The if statement runs a block of code only when a condition is True:
if marks >= 33: print("Pass")
The else clause runs when the if condition is False:
if marks >= 33: print("Pass") else: print("Fail")
The elif (else if) clause checks additional conditions:
if marks >= 75: print("Distinction") elif marks >= 60: print("First Division") elif marks >= 33: print("Pass") else: print("Fail")
Python uses indentation (spaces at the start of lines) to define code blocks — not curly braces like C or Java. Consistent indentation (four spaces is standard) is not optional; incorrect indentation causes an IndentationError.
Exam tip: write a Python program to input a number and check if it is positive, negative, or zero — this is an extremely common Class 9 board exam question. Make sure your if-elif-else structure is correctly indented and your comparison operators are correct.