ILOMRAHomeStudy topicsFree toolsTest zoneSearch any topic
Computer Science · Grade 9 (Matric / O-Level)

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

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?

  1. //
  2. #
  3. /*
  4. --
Show answer

Answer: B. #. A # makes the rest of the line a comment in Python.

Q2. What does input() return in Python?

  1. An integer
  2. A float
  3. A string
  4. 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?

  1. 3.5
  2. 3
  3. 1
  4. 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?

  1. 0
  2. 1
  3. −1
  4. It has no index
Show answer

Answer: A. 0. Python lists are indexed from 0.

Q5. What does print("Hello") do in Python?

  1. Saves a file
  2. Displays Hello on the screen
  3. Creates a variable
  4. 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?

  1. 51
  2. 6
  3. "51"
  4. Error
Show answer

Answer: B. 6. int("5") is 5, so 5 + 1 = 6.

Q7. What is the value of 2 ** 3 in Python?

  1. 5
  2. 6
  3. 8
  4. 9
Show answer

Answer: C. 8. The ** operator is power: 2³ = 8.

Q8. What does the % operator do in Python?

  1. Division
  2. Remainder after division
  3. Power
  4. Multiplication
Show answer

Answer: B. Remainder after division. % gives the remainder, so 7 % 2 = 1.

Q9. Which numbers does range(1, 5) produce?

  1. 1, 2, 3, 4
  2. 1, 2, 3, 4, 5
  3. 0, 1, 2, 3, 4
  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])?

  1. 3
  2. 4
  3. 10
  4. 40
Show answer

Answer: B. 4. len() counts the items in the list, which is 4.

Q11. In Python, == is used for:

  1. Assigning a value
  2. Comparing two values for equality
  3. Adding two values
  4. 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?

  1. Curly brackets {}
  2. Indentation
  3. Semicolons
  4. Square brackets []
Show answer

Answer: B. Indentation. Python uses indentation to mark blocks of code.

Q13. What does elif mean in Python?

  1. End if
  2. Else if
  3. Error if
  4. 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?

  1. Yes and No
  2. True and False
  3. 1 and 0
  4. On and Off
Show answer

Answer: B. True and False. Python booleans are True and False (with capital letters).

Want to study something else?

ILOMRA can open full notes and practice MCQs for almost any study topic. Search any topic on ILOMRA.

Take the timed MCQ test for this topic.

Related topics