Python Programming Basics
Class 9 computer science notes: Python variables, data types, if statements, loops and lists, with worked programs.
Last updated: 23 November 2026
Key points
- Python is a high-level, interpreted programming language that is popular because its code is short and reads almost like English. A first program is just one line: print("Hello, World!"). Statements do not need semicolons, and anything after a # symbol is a comment that Python ignores, so comments are used to explain what the code does.
- Variables store values and are created simply by assigning them, for example age = 15 or name = "Ayesha". Python has four basic data types: int (whole numbers), float (decimal numbers), str (text in quotes) and bool (True or False). You can change a value’s type with int(), float() and str(), and find its type with type(). Variable names must start with a letter or underscore, cannot contain spaces, and cannot be Python keywords such as print or if — total_marks is fine, but 2marks and total marks are not.
- The input() function reads text typed by the user, but it always returns a string, so numbers must be converted before doing arithmetic: marks = int(input("Enter marks: ")). Without int(), the program would treat "5" + "3" as joining text together to make "53" instead of adding to make 8.
- Python’s arithmetic operators are + (add), − (subtract), * (multiply), / (divide), // (floor division, which drops the remainder), % (modulus, which gives the remainder) and ** (power). So 7 // 2 is 3, 7 % 2 is 1, and 2 ** 3 is 8. Comparison operators (==, !=, <, >, <=, >=) compare values, and and, or and not combine conditions.
- Decisions are made with if, elif and else. For example: if marks >= 50: print("Pass") else: print("Fail"). Python uses indentation, not brackets, to show which lines belong to each block, so consistent spacing is essential. Note the difference between = (assignment) and == (comparison for equality).
- Loops repeat work. A for loop with range() repeats a fixed number of times: for i in range(1, 6): runs with i = 1, 2, 3, 4, 5. A while loop repeats while a condition is true, so it needs a line inside the loop that eventually makes the condition False — otherwise it runs forever, an infinite loop. A list stores several values in order, for example scores = [85, 90, 78]; indexing starts at 0, so scores[0] is 85, and len(scores) gives the number of items, which is 3.
Worked example
Worked example: tracing a loop that adds numbers
Consider this program: total = 0, then for i in range(1, 6): total = total + i, then print(total). Tracing it step by step: i = 1 gives total = 1, i = 2 gives total = 3, i = 3 gives total = 6, i = 4 gives total = 10, and i = 5 gives total = 15. The loop then ends because range(1, 6) stops before 6, so the program prints 15, which is the sum of the numbers from 1 to 5. A second useful trace uses a decision: marks = 75, then if marks >= 80: print('A') elif marks >= 60: print('B') else: print('C'). The first test (75 >= 80) is False, so Python moves to the second test (75 >= 60), which is True, so the program prints B and skips the else. Tracing code on paper like this, one line at a time, is the best way to prepare for exam questions that ask what a program prints.
Practice MCQs
Q1. Which symbol starts a comment in Python?
- //
- #
- /*
- --
Show answer
Answer: B. #. A # makes the rest of the line a comment in Python.
Q2. What does input() return in Python?
- An integer
- A float
- A string
- A boolean
Show answer
Answer: C. A string. input() always returns a string, so convert it with int() or float() for arithmetic.
Q3. What is the value of 7 // 2 in Python?
- 3.5
- 3
- 1
- 2
Show answer
Answer: B. 3. Floor division drops the remainder, so 7 // 2 = 3.
Q4. What is the index of the first item in a Python list?
- 0
- 1
- −1
- It has no index
Show answer
Answer: A. 0. Python lists are indexed from 0.
Q5. What does print("Hello") do in Python?
- Saves a file
- Displays Hello on the screen
- Creates a variable
- Deletes Hello
Show answer
Answer: B. Displays Hello on the screen. print() displays its output on the screen.
Q6. What is the value of int("5") + 1?
- 51
- 6
- "51"
- Error
Show answer
Answer: B. 6. int("5") is 5, so 5 + 1 = 6.
Q7. What is the value of 2 ** 3 in Python?
- 5
- 6
- 8
- 9
Show answer
Answer: C. 8. The ** operator is power: 2³ = 8.
Q8. What does the % operator do in Python?
- Division
- Remainder after division
- Power
- Multiplication
Show answer
Answer: B. Remainder after division. % gives the remainder, so 7 % 2 = 1.
Q9. Which numbers does range(1, 5) produce?
- 1, 2, 3, 4
- 1, 2, 3, 4, 5
- 0, 1, 2, 3, 4
- 5, 4, 3, 2, 1
Show answer
Answer: A. 1, 2, 3, 4. range(1, 5) starts at 1 and stops before 5.
Q10. What is len([10, 20, 30, 40])?
- 3
- 4
- 10
- 40
Show answer
Answer: B. 4. len() counts the items in the list, which is 4.
Q11. In Python, == is used for:
- Assigning a value
- Comparing two values for equality
- Adding two values
- Ending a program
Show answer
Answer: B. Comparing two values for equality. == compares values; = assigns a value.
Q12. How does Python show which lines belong inside an if block?
- Curly brackets {}
- Indentation
- Semicolons
- Square brackets []
Show answer
Answer: B. Indentation. Python uses indentation to mark blocks of code.
Q13. What does elif mean in Python?
- End if
- Else if
- Error if
- Else forever
Show answer
Answer: B. Else if. elif is short for ‘else if’ and tests another condition.
Q14. Which of these are the two boolean values in Python?
- Yes and No
- True and False
- 1 and 0
- On and Off
Show answer
Answer: B. True and False. Python booleans are True and False (with capital letters).
ILOMRA can open full notes and practice MCQs for almost any study topic. Search any topic on ILOMRA.