Computers are super fast, but they only understand 0s and 1s. So how do we tell a computer what to do? We write programs in a programming language โ and the friendliest one to start with is Python! ๐ This chapter answers the BIG questions: What is a program and a programming language? Why is Python so popular? How does Python run our code? What are Interactive Mode and Script Mode? And how do we write our very first program? Let's say hello to Python! ๐
The most tested topics are Features of Python, Compiler vs Interpreter, and Interactive Mode vs Script Mode. Learn the comparison tables well โ "differentiate between" questions come straight from them. Then type every example yourself in the Python Lab โ that is the fastest way to learn!
2.1 ๐ Introduction โ Programs and Languages
A program is a set of step-by-step instructions given to a computer to do a task. Writing these instructions is called programming, and the person who writes them is a programmer.
Analogy: A program is like a recipe ๐ณ. The recipe tells the cook each step in order โ "boil water, add noodles, wait 2 minutes, add masala". A program tells the computer each step in the same way. Miss a step or change the order, and the result goes wrong!
A programming language is a special language with its own words and rules (grammar) that we use to write programs.
2.1.1 Low-Level vs High-Level Languages ๐ช
graph TD
PL["๐ฃ๏ธ PROGRAMMING LANGUAGES"]
LOW["โ๏ธ Low-Level Languages\n(close to the machine)"]
HIGH["๐ง High-Level Languages\n(close to English)"]
ML["0๏ธโฃ1๏ธโฃ Machine Language\n(only 0s and 1s)"]
AL["๐ค Assembly Language\n(short codes like ADD, MOV)"]
HL["๐ Python, Java, C++\n(English-like words)"]
PL --> LOW
PL --> HIGH
LOW --> ML
LOW --> AL
HIGH --> HL
style LOW fill:#795548,color:#fff
style HIGH fill:#4CAF50,color:#fff
| Low-Level Language | High-Level Language |
|---|---|
| Close to machine (0s and 1s or short codes) | Close to English |
| Hard for people to read and write | Easy to read, write and correct |
| Works only on one type of computer | Works on many types of computers |
| e.g. Machine language, Assembly language | e.g. Python, Java, C++ |
2.1.2 Language Translators ๐
A computer understands only machine language. So a program written in a high-level language must first be translated into machine language. The software that does this is called a language translator.
Compiler
Translates the WHOLE program in one go, and then it runs. Errors are shown together after translation
CC++Interpreter
Translates and runs the program ONE LINE at a time. Stops at the first error
PythonAssembler
Translates assembly language into machine language
AssemblyAnalogy: Suppose you need a speech in Japanese translated. ๐ฏ๐ต
- A compiler is like a translator who writes down the translation of the whole speech first, and then hands it over.
- An interpreter is like a translator who stands beside you and translates each sentence as you speak it.
| Compiler | Interpreter |
|---|---|
| Translates the whole program at once | Translates one line at a time |
| Shows all errors together, after translating | Stops at the first error it finds |
| Program runs faster after translation | Runs slower, line by line |
| Finding mistakes is harder for beginners | Finding mistakes is easier โ you know exactly which line failed |
| e.g. C, C++ | e.g. Python |
"Differentiate between a compiler and an interpreter." โ 2 marks Answer: A compiler translates the entire program into machine code at once and reports all errors together after translation. An interpreter translates and executes the program one line at a time and stops as soon as it finds an error. Python uses an interpreter.
2.2 ๐ What is Python?
Python is a high-level, interpreted, general-purpose programming language that is famous for being simple and easy to read.
Created by
Guido van Rossum in the Netherlands
Guido van RossumReleased in
February 1991
1991Named after
The British comedy show "Monty Python's Flying Circus" โ NOT the snake!
Monty PythonCost
Free and open source โ anyone can download and use it
FreeWhere is Python used? Almost everywhere! ๐
Data Analysis
Studying data and making charts โ you will do this in Class 12 IP with Pandas
PandasMatplotlibArtificial Intelligence
Chatbots, face recognition and smart assistants
AI and MLWebsites
The back end of popular websites
InstagramYouTubeGames and Apps
Simple games and desktop tools
Pygame2.2.1 Features (Advantages) of Python โ
| Feature | What it means |
|---|---|
| Easy to learn and use | Uses simple, English-like words; programs are short and clear |
| Interpreted | Runs line by line, so errors are easy to find |
| Free and open source | Free to download, use and share; its source code is open |
| Platform independent (portable) | The same program runs on Windows, Linux and macOS |
| Huge library | Comes with ready-made modules for maths, dates, files and much more |
| Case sensitive | Name and name are treated as two different things |
| Dynamically typed | You don't need to tell Python the type of a variable โ it works it out itself |
| Used everywhere | Data science, AI, web, games, school projectsโฆ |
Look how short a Python program is compared with C++ for the same job:
C++ Python
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโ
#include <iostream> print("Hello World")
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Python is Free, Easy, Portable, Interpreted and has a Collection (huge library) of modules โ Python is EPIC with an F!
2.2.2 Limitations of Python โ
Nothing is perfect โ Python has a few weak points too:
Slower
Being interpreted it runs slower than compiled languages like C++
Not the fastestWeak in Mobile Apps
Not commonly used to build Android or iPhone apps
Few mobile appsSome Errors Show Late
Type errors are found only when that line runs
Runtime errors"Write any four features of Python." โ 2 marks Answer: (1) It is easy to learn with a simple, English-like syntax. (2) It is an interpreted language. (3) It is free and open source. (4) It is platform independent โ the same program runs on different operating systems.
2.3 โ๏ธ How Python Runs Your Program
When you run a Python program, the Python interpreter reads it and runs it for you, one line at a time.
graph LR
A["๐ Your Program\n(hello.py)"]
B["๐ Python Interpreter\n(reads line 1, runs it,\nthen line 2, โฆ)"]
C["๐ฅ๏ธ Output\non the screen"]
E["โ Error message\n(if a line is wrong)"]
A --> B --> C
B -.-> E
style B fill:#FF9800,color:#fff
style C fill:#4CAF50,color:#fff
style E fill:#F44336,color:#fff
Because Python runs line by line, if line 3 has a mistake, lines 1 and 2 are already done โ and Python stops at line 3 with an error message that tells you which line is wrong.
print("Line 1 runs")
print("Line 2 runs")
print("Line 3 runs")
Output:
Line 1 runs
Line 2 runs
Line 3 runs
Fun fact: Behind the scenes, Python first turns your program into a simpler form called bytecode, which is then run by the Python Virtual Machine (PVM). You don't have to do anything for this โ it happens automatically!
2.4 ๐ ๏ธ Working in Python
To write Python programs, we need Python installed on the computer. The free official version from python.org comes with IDLE.
IDLE = Integrated Development and Learning Environment โ a simple program where you can type, save and run Python code.
Python Shell
The window with the `>>>` prompt โ commands run instantly
Interactive ModeEditor Window
A blank page where you write a full program and save it
Script ModeOther popular tools to write Python: Jupyter Notebook, Spyder, VS Code, PyCharm โ and the Python Lab on this website! ๐
Python can be used in two modes:
2.4.1 Interactive Mode โก
In Interactive Mode, you type one statement at a time after the prompt >>> and Python runs it immediately.
>>> 5 + 3
8
>>> 20 - 7
13
>>> 6 * 4
24
>>> print("I love Python")
I love Python
Analogy: Interactive Mode is like talking to a calculator ๐งฎ โ press the keys, see the answer at once. But once you close it, everything you typed is gone!
- Good for: quick testing, small calculations, checking how a command works.
- Problem: the statements cannot be saved โ you have to type them again next time.
To leave the shell, type exit() or quit().
2.4.2 Script Mode ๐
In Script Mode, you write the whole program in the Editor, save it as a file with the .py extension, and then run it.
graph LR
S1["1๏ธโฃ File โ New File"]
S2["2๏ธโฃ Type the program"]
S3["3๏ธโฃ File โ Save\n(marks.py)"]
S4["4๏ธโฃ Run โ Run Module\n(or press F5)"]
S5["5๏ธโฃ See output\nin the Shell"]
S1 --> S2 --> S3 --> S4 --> S5
style S3 fill:#FF9800,color:#fff
style S4 fill:#F44336,color:#fff
style S5 fill:#4CAF50,color:#fff
Code Example: marks.py
# Program to show total marks
print("Report Card")
print("Maths + Science =", 85 + 90)
Output:
Report Card
Maths + Science = 175
Analogy: Script Mode is like writing an essay in your notebook โ๏ธ โ you can save it, read it again, correct it and use it any number of times.
| Interactive Mode | Script Mode |
|---|---|
Type one statement at a time at >>> |
Type the whole program in the Editor |
| Each statement runs immediately | Program runs when you press F5 (Run Module) |
| Statements cannot be saved | Program is saved as a .py file |
| Good for testing small code | Good for long programs |
| Hard to edit what you already typed | Easy to edit and run again |
"Differentiate between Interactive Mode and Script Mode in Python." โ 2 marks (asked very often!)
Answer: In Interactive Mode, we type one statement at a time at the >>> prompt and it runs immediately, but the code cannot be saved. In Script Mode, we write the complete program in a file, save it with a .py extension, and can run and edit it any number of times.
>>> yourself!The >>> is Python's prompt โ Python shows it to say "I'm ready". If you type >>> in a script file, you will get a SyntaxError.
2.5 ๐จ๏ธ Your First Program โ the print() Function
The print() function shows output on the screen. It is the first thing every Python learner uses!
print("Hello, World!")
print("Welcome to Class 11 IP")
print(100)
print(12 + 8)
print("Total =", 12 + 8)
Output:
Hello, World!
Welcome to Class 11 IP
100
20
Total = 20
Text
Text must be inside quotes โ single or double
Quotes neededNumbers
Numbers are written without quotes
No quotesCalculations
Python solves it first and then prints the answer
Result printedMany Items
Separate items with commas โ Python puts a space between them
Space addedprint("12 + 8") prints 12 + 8 exactly as written, because anything in quotes is text.
print(12 + 8) prints 20, because without quotes Python calculates it.
Print("Hi") or PRINT("Hi") gives an error โ NameError: name 'Print' is not defined. Always write print in small letters.
Comments โ anything after a # is a comment. Python ignores it; comments are notes for people reading the code.
# This program greets the user
print("Good Morning!") # this prints a greeting
Output:
Good Morning!
Code Example: about_me.py
# A small program about me
print("Name : Aman")
print("Class : 11")
print("Subject: Informatics Practices")
print("Age next year:", 16 + 1)
Output:
Name : Aman
Class : 11
Subject: Informatics Practices
Age next year: 17
- Write
printin small letters. - Put text inside quotes; the opening and closing quotes must match (
"โฆ"or'โฆ'). - Start each statement at the beginning of the line โ an extra space at the start gives an IndentationError.
โ ๏ธ Common Errors and Misconceptions
| Mistake | What's Wrong | Correct Understanding |
|---|---|---|
| "Python is named after the snake" | It is named after a comedy show | Named after Monty Python's Flying Circus |
| "Python is a compiled language" | Python runs line by line | Python is an interpreted language |
| "A compiler translates one line at a time" | That is an interpreter | A compiler translates the whole program at once |
| "Code typed in Interactive Mode is saved" | The shell does not save statements | Use Script Mode and save a .py file |
Typing >>> in a script |
>>> is only the shell's prompt |
Never type >>> yourself |
Print("Hi") |
Python is case sensitive | Write print("Hi") |
print(Hello) |
Text without quotes is treated as a name | Write print("Hello") |
print("Hi') |
Opening and closing quotes don't match | Use "Hi" or 'Hi' |
print("5 + 5") expecting 10 |
Quotes make it text | print(5 + 5) prints 10 |
| "Free software means Python has no features" | Free = no cost and open code | Python is free and very powerful |
๐ Quick Revision โ Exam Ready!
- Program โ a set of step-by-step instructions for the computer
- Low-level (machine, assembly) โ close to the machine; High-level (Python, Java) โ close to English
- Translators โ Compiler (whole program at once), Interpreter (line by line), Assembler (assembly โ machine)
- Python โ created by Guido van Rossum, released in 1991, named after Monty Python's Flying Circus
- Python is โ high-level, interpreted, free and open source, portable, case sensitive, dynamically typed, with a huge library
- Limitations โ slower than compiled languages, rarely used for mobile apps, some errors show only when the line runs
- Python runs line by line and stops at the first error (internally: bytecode โ PVM)
- IDLE โ Integrated Development and Learning Environment (Shell + Editor)
- Interactive Mode โ
>>>prompt, runs immediately, not saved - Script Mode โ
.pyfile, run with F5, saved and reusable - print() โ shows output; text in quotes, numbers without; commas add a space
- # โ starts a comment, ignored by Python
๐ฏ Sample Exam Questions
Q1: Very Short Answer [1 mark each]
a) Who developed Python? Answer: Guido van Rossum
b) What is the extension of a Python program file?
Answer: .py
c) Which symbol is the prompt of Python's Interactive Mode?
Answer: >>>
d) Which type of translator does Python use? Answer: An interpreter
e) What is the full form of IDLE? Answer: Integrated Development and Learning Environment
f) Which key runs a program in IDLE's Script Mode? Answer: F5
Q2: Short Answer [2 marks]
Differentiate between a high-level and a low-level language.
Answer: A high-level language is close to English, easy to read and write, and works on many types of computers โ e.g. Python. A low-level language is close to machine code (0s and 1s or short codes), is hard for people to use, and depends on the type of computer โ e.g. assembly language.
Q3: Short Answer [2 marks]
Differentiate between Interactive Mode and Script Mode.
Answer:
| Interactive Mode | Script Mode |
|---|---|
One statement at a time at >>> |
Whole program written in a file |
| Runs immediately | Runs when the file is run (F5) |
| Cannot be saved | Saved with .py extension, reusable |
Q4: Output Based [2 marks]
Write the output of the following code:
print("Sum is", 15 + 25)
print("15 + 25")
print(100 - 1)
Answer:
Sum is 40
15 + 25
99
Q5: Error Finding [2 marks]
Find and correct the errors in the following code:
Print("Hello")
print("Python')
Answer:
print("Hello") # print must be in small letters
print("Python") # quotes must match
Q6: Short Answer [3 marks]
What is an interpreter? Why is Python's interpreter helpful for beginners? Write one limitation of an interpreted language.
Answer: An interpreter is a language translator that translates and runs a program one line at a time. It helps beginners because it stops at the first error and shows the line number, so mistakes are easy to find and correct. Limitation: interpreted programs usually run slower than compiled programs.
โ๏ธ Practice Problems
- What is a program? Explain with an everyday example other than a recipe.
- Why must a high-level language program be translated? Name the three types of language translators.
- Write any four features and two limitations of Python.
- Why is Python called a platform-independent language?
- Write the steps to create, save and run a program in IDLE.
- In Interactive Mode, type
7 + 3,7 * 3and7 - 3. Write the output you get for each. - Write a program that prints your name, class and school on three separate lines.
- Write a program that prints the text
Area of rectangle =followed by the result of12 * 5. - Find the output:
print("Price:", 50 + 20)andprint("50 + 20"). - Find the error:
print(Welcome to Python). Correct it. - What is a comment? Write a program with one full-line comment and one comment after a statement.
- Your friend typed a long program in Interactive Mode and lost it after closing IDLE. Which mode should they have used and why?