Class 9 · Unit 5 · 8 marks

Introduction to Python

From thinking like a programmer to the full practical file, with every program runnable in the browser.

1Thinking like a programmer

A program is a list of instructions that a computer follows exactly and in order. Programmers break a task into small steps (an algorithm) and then write them in a language like Python. Python is popular for AI because it reads almost like English and has ready-made libraries for data, images and language. Warm up by programming a robot.

Program the robot to reach the ⭐. Add moves, set how many steps each one takes, then press Run. Your program is shown in Python on the right.


  

2Python basics: print, variables and data types

print() shows output and # starts a comment that Python ignores. A variable is a name that stores a value, like age = 14. Every value has a data type: int (whole number), float (decimal), str (text in quotes) or bool (True or False). input() reads what the user types, always as a string, so we convert it with int() or float().

3Operators and expressions

Operators do work on values. Arithmetic: + - * / // % **. Comparison: == != > < >= <=, which give True or False. Logical: and, or, not. Assignment: = and shortcuts like +=. An expression combines values and operators, such as 2 + 3 * 4; Python does ** first, then * / // %, then + -, and brackets before everything.

Operator playground. Change a and b and watch every Python operator give its answer. Tap a row to see how it works.

4Making decisions: if, elif and else

An if statement runs a block of code only when its condition is True. elif (else if) checks another condition, and else runs when none of the conditions were True. Python checks from the top and runs only the first block whose condition is True. The colon : and the indentation (4 spaces) show which lines belong to each block.

Follow the arrows. Pick a program and move the slider. The highlighted path in the flowchart and the highlighted lines of code are the ones Python actually runs.

Output

5Loops: for and while

A loop repeats code. A for loop repeats once for each value, often from range(start, stop, step), which counts from start up to but not including stop. A while loop repeats as long as its condition is True, checking it before every round. If the condition never becomes False, the loop runs for ever: an infinite loop.

Watch i hop. Set where the range starts and stops and how big each jump is, choose what the loop body does, then press Play.

Output
RoundiWhat the body doesOutput

6Lists

A list stores many values in one variable, in order: num = [23, 12, 5, 9, 65, 44]. Each item has an index starting at 0; negative indexes count from the end, so num[-1] is the last item. A slice like num[1:4] gives the items from index 1 up to, but not including, index 4. Lists can change: append, insert, extend, remove, pop, sort and reverse.

A list is a row of numbered boxes. Pick a list, choose an operation, and run it. The numbers above each box are its index; the numbers below are its negative index.


  
Python shell

7Practical file programs

These are the programs in the Class 9 practical file: printing, input, lists, and if / for / while. Run each one, use 👣 Step through to watch it work, then change the values or the input and run it again. For your file, copy the program and its output.

Try it yourself