You've learnt how a computer works INSIDE โ now it's time to TALK to it! ๐ฃ๏ธ๐ป But a computer only understands 0s and 1s, and we certainly don't want to write programs in binary. That's where a programming language comes in. This chapter introduces you to Python โ the language you will use for the rest of Class 11 AND Class 12. We'll answer: What is Python? Why is it so popular? How does Python run your code? What is the difference between Interactive Mode and Script Mode? Let's write our very first program! ๐๐
Focus especially on Features (Pluses) and Limitations (Minuses) of Python, Interactive Mode vs Script Mode, and how the Python Interpreter works โ these are the favourite 1-mark and 2-mark questions from this chapter!
4.1 ๐ Introduction
A program is a set of instructions given to a computer to perform a specific task. The language in which we write these instructions is called a programming language.
graph LR
HUMAN["๐งโ๐ป Programmer\n(thinks in English)"]
LANG["๐ Programming Language\n(Python code)"]
TRANS["โ๏ธ Translator\n(Python Interpreter)"]
MACHINE["๐ป Computer\n(understands only 0s and 1s)"]
HUMAN --> LANG --> TRANS --> MACHINE
style LANG fill:#2196F3,color:#fff
style TRANS fill:#FF9800,color:#fff
style MACHINE fill:#4CAF50,color:#fff
Programming languages are broadly of two types:
Low-Level Language
Close to the machine's hardware; difficult for humans to read and write
Machine LanguageAssembly LanguageHigh-Level Language
Close to human language (English-like); easy to read, write and debug
PythonJavaC++Think of it this way: Speaking to a computer in machine language is like talking to someone in a secret code of just 0 and 1. A high-level language like Python lets you speak in near-English, and a translator converts it into the computer's code for you!
4.2 ๐ What is Python?
Python is a high-level, interpreted, general-purpose, object-oriented programming language. It was created by Guido van Rossum at the National Research Institute for Mathematics and Computer Science (CWI) in the Netherlands, and was first released in February 1991.
Creator
Guido van Rossum, a Dutch programmer
NetherlandsFirst Released
Python 0.9.0 was released publicly in 1991
1991Name Origin
Named after the BBC comedy show "Monty Python's Flying Circus" โ NOT the snake!
Monty PythonCurrent Version
Python 3.x series (Python 3.0 came in 2008; Python 2 ended in 2020)
Python 3Python is based on (influenced by) two languages:
graph TD
ABC["๐ค ABC Language\n(simple, teaching language)"]
MODULA["๐งฉ Modula-3\n(modules & exceptions)"]
PY["๐ PYTHON\n(1991)"]
ABC --> PY
MODULA --> PY
style PY fill:#2196F3,color:#fff
"Who developed Python? When was it released?" โ 1-mark question! Answer: Python was developed by Guido van Rossum and was released in 1991. It is based on two languages โ ABC and Modula-3.
Where is Python used? Almost everywhere!
Web Development
Building websites and web applications
DjangoFlaskAI & Machine Learning
Teaching computers to learn from data
TensorFlowscikit-learnData Science
Analysing and visualising large amounts of data
PandasMatplotlibGames & Automation
Simple games, scripts that automate boring tasks
Pygamescripts4.3 โ Python โ Pluses (Advantages)
Python is loved by beginners AND professionals. Here's why:
Easy to Use
Simple, English-like syntax; programs are short and compact
Great first languageExpressive Language
Fewer lines of code do MORE work compared to other languages
One-line swap of two variablesInterpreted Language
Code is executed line by line, which makes debugging easy
Errors shown line by lineIts Completeness
"Batteries included" โ a huge standard library comes built in
mathrandomstatisticsCross-Platform
The same program runs on Windows, Linux, macOS without changes
Platform independentFree & Open Source
Free to download and use; its source code is freely available
No licence feeVariety of Usage
Used for web, AI, data science, games, scientific computing, and more
General purposeExpressive? See for yourself! Swapping two numbers:
# In Python โ just ONE line!
a, b = 5, 10
a, b = b, a
print(a, b)
Output:
10 5
In many other languages, you need a THIRD variable and three separate statements to do the same swap!
Easy ยท Expressive ยท Interpreted ยท Completeness ยท Cross-platform ยท Free & open source ยท Variety of usage Remember it as: "Every Excited Indian Child Codes For Victory!" ๐
4.4 โ Python โ Minuses (Limitations)
No language is perfect! Python has a few limitations too:
Not the Fastest Language
Being interpreted, Python is slower than fully compiled languages
Slower than C/C++Not Strong on Type-Binding
A variable can hold any type of value; some type mismatch errors are caught only at runtime
x = 5 then x = "five"Not Easily Convertible
Python's syntax is quite different from other languages, so converting a Python program to another language is not easy
Python โ C is hardLesser Libraries (in some areas)
Compared to older languages like C, Java and Perl, some specialised areas (like mobile app development) have fewer Python libraries
Weak in mobile appsStudents often write "Python is a compiled language" โ โ WRONG! Python is an interpreted language. Your source code is converted to bytecode internally, but that bytecode is then interpreted by the Python Virtual Machine (PVM), not converted into a stand-alone machine code file like in C/C++.
"Write any two advantages and two disadvantages of Python." โ 2-mark question, asked very often! Answer: Advantages: (i) Easy to use with simple syntax, (ii) Free and open source. Disadvantages: (i) Slower than compiled languages like C/C++, (ii) Not strong on type-binding.
4.5 โ๏ธ How Python Executes a Program
Python uses an interpreter to run programs. Let's first understand the two types of language translators.
4.5.1 Compiler vs Interpreter ๐
Compiler
Translates the WHOLE program into machine code at once, then runs it
CC++Interpreter
Translates and executes the program LINE BY LINE
PythonJavaScript| Feature | Compiler | Interpreter |
|---|---|---|
| Translation | Entire program at once | One line (statement) at a time |
| Error Reporting | Shows ALL errors together after compiling | Stops at the FIRST error it finds |
| Execution Speed | Faster (already translated) | Slower (translates while running) |
| Debugging | Harder | Easier |
| Example | C, C++ | Python |
Analogy: A Compiler is like a translator who translates an ENTIRE book into Hindi first and then hands it to you. An Interpreter is like a live translator standing next to a speaker โ translating each sentence as soon as it is spoken!
4.5.2 Inside the Python Interpreter ๐ฌ
When you run a Python program, it goes through these steps behind the scenes:
graph LR
SRC["๐ Source Code\n(hello.py)"]
COMP["โ๏ธ Compiler\n(inside Python)"]
BYTE["๐ Bytecode\n(.pyc file)"]
PVM["๐ฅ๏ธ PVM\n(Python Virtual\nMachine)"]
OUT["๐ค Output"]
SRC --> COMP --> BYTE --> PVM --> OUT
style SRC fill:#2196F3,color:#fff
style BYTE fill:#FF9800,color:#fff
style PVM fill:#F44336,color:#fff
style OUT fill:#4CAF50,color:#fff
- You write the source code in a file with the
.pyextension. - Python internally compiles it into bytecode โ a low-level, platform-independent set of instructions (sometimes saved as
.pycfiles). - The Python Virtual Machine (PVM) reads the bytecode and executes it line by line on your computer.
Because bytecode is platform-independent! The same bytecode can run on ANY computer (Windows, Linux, macOS) that has a Python Virtual Machine installed. The PVM takes care of talking to the actual hardware.
"What is the difference between a compiler and an interpreter?" โ 2-mark question! Answer: A compiler translates the entire program into machine code at once and reports all errors together. An interpreter translates and executes the program line by line and stops as soon as it finds an error. Python uses an interpreter.
4.6 ๐ ๏ธ Working in Python
To write and run Python programs, you need a Python interpreter installed on your computer (download it free from python.org). The standard installation comes with IDLE.
4.6.1 Python IDLE ๐ฅ๏ธ
IDLE stands for Integrated Development and Learning Environment. It is the default editor that comes with Python.
Python Shell
The window that shows the >>> prompt and executes commands immediately
Interactive ModeEditor Window
A text editor to write, save and run complete programs
Script ModeSyntax Colouring
Keywords, strings and comments are shown in different colours
Easy to read codeDebugger
Helps find and fix errors step by step
Debug menuOther popular Python tools (IDEs and distributions):
| Tool | What it is |
|---|---|
| Anaconda | A free Python distribution with many data-science packages pre-installed |
| Jupyter Notebook | A web-based tool to write code and see output in "cells" |
| Spyder | An IDE that comes with Anaconda, popular for scientific work |
| PyCharm | A professional IDE for Python |
| Visual Studio Code | A popular free code editor with Python support |
You can still practise every program in these notes using the Python Lab on CSIP12 โ it runs Python right inside your browser! Just click the Run button on any code block.
Python code can be run in two modes:
graph TD
MODES["๐ WORKING IN PYTHON"]
INT["โก Interactive Mode\n(Python Shell, >>> prompt)\nOne command at a time"]
SCR["๐ Script Mode\n(.py file)\nWhole program at once"]
MODES --> INT
MODES --> SCR
style INT fill:#FF9800,color:#fff
style SCR fill:#2196F3,color:#fff
4.6.2 Interactive Mode โก
In Interactive Mode, you type one command at a time at the Python prompt >>> (called the chevron or primary prompt), and Python executes it immediately and shows the result.
>>> 2 + 3
5
>>> 10 * 4
40
>>> print("Hello, Python!")
Hello, Python!
>>> 7 / 2
3.5
Analogy: Interactive Mode is like chatting with a friend on WhatsApp โ you send ONE message and get a reply INSTANTLY. But the chat isn't saved as a neat document you can hand in later!
Advantages
Instant result for each command; great for testing small pieces of code and using Python as a calculator
Quick testingDisadvantages
Commands CANNOT be saved for later use; you must retype everything; not suitable for long programs
Not savedTo leave the Python shell, type exit() or quit().
4.6.3 Script Mode ๐
In Script Mode, you write the complete program in a file, save it with the .py extension, and then run the whole file at once.
Steps to create and run a program in IDLE (Script Mode):
graph LR
S1["1๏ธโฃ File โ New File\n(opens Editor)"]
S2["2๏ธโฃ Type the\nprogram"]
S3["3๏ธโฃ File โ Save\n(name.py)"]
S4["4๏ธโฃ Run โ Run Module\n(or press F5)"]
S5["5๏ธโฃ Output appears\nin Python Shell"]
S1 --> S2 --> S3 --> S4 --> S5
style S1 fill:#9C27B0,color:#fff
style S3 fill:#FF9800,color:#fff
style S4 fill:#F44336,color:#fff
style S5 fill:#4CAF50,color:#fff
Example โ save this as welcome.py:
# My first Python program
name = "Riya"
print("Hello,", name)
print("Welcome to Python programming!")
Output:
Hello, Riya
Welcome to Python programming!
Advantages
Program is SAVED permanently; can be edited and re-run any number of times; suitable for long programs
ReusableDisadvantages
Output is seen only after the whole program runs; slightly more steps to test a tiny command
Extra stepsInteractive Mode vs Script Mode โ The Critical Comparison:
| Feature | Interactive Mode | Script Mode |
|---|---|---|
| How code is typed | One statement at a time at the >>> prompt |
Whole program in the Editor window |
| Execution | Each statement runs immediately | Whole file runs when you press F5 |
| Saving | Code CANNOT be saved | Code is saved as a .py file |
| Editing | Previous commands can't be edited easily | Program can be edited and re-run anytime |
| Best for | Testing small code, quick calculations | Writing complete, long programs |
"Differentiate between Interactive Mode and Script Mode of Python." โ 2-mark question, asked almost EVERY year!
Answer: In Interactive Mode, statements are typed one at a time at the >>> prompt and executed immediately, but the code cannot be saved. In Script Mode, the complete program is written in a file, saved with a .py extension, and can be executed and modified any number of times.
4.7 ๐จ๏ธ Understanding the print() Function
The print() function is used to display output on the screen. It is the very first function every Python programmer learns!
print("Hello, World!") # prints text (a string)
print(25) # prints a number
print(10 + 5) # prints the RESULT of an expression
print("Sum =", 10 + 5) # prints text and a value together
Output:
Hello, World!
25
15
Sum = 15
Text (String)
Must be enclosed in single or double quotes
'Hi'"Hi"Numbers
Written without quotes
253.14Expressions
Python calculates first, then prints the result
10 + 5 โ 15Multiple Items
Separated by commas; printed with a space in between
Comma separatedprint("10 + 5") prints 10 + 5 (it's text inside quotes!), whereas print(10 + 5) prints 15. Anything inside quotes is printed exactly as it is.
print and Print are NOT the same. Writing Print("Hi") gives an error: NameError: name 'Print' is not defined.
Comments โ lines starting with # are comments. Python ignores them; they are written to explain the code to humans.
# This whole line is a comment
print("Python is fun!") # this is an inline comment
Every Python statement at the top level must start at the beginning of the line (no extra spaces before it). An unexpected space at the start gives an IndentationError. You'll learn more about indentation in the Python Fundamentals chapter.
โ ๏ธ Common Errors and Misconceptions
| Mistake | What's Wrong | Correct Understanding |
|---|---|---|
| โ Python is named after the snake | It's a popular myth | โ Named after the comedy show "Monty Python's Flying Circus" |
| โ Python is a compiled language | Python does not produce a stand-alone machine code file | โ Python is an interpreted language (source โ bytecode โ PVM) |
| โ Code typed in Interactive Mode is saved automatically | The shell doesn't save your commands | โ
Use Script Mode (.py file) to save programs |
โ Python files are saved with .pyt or .pt extension |
Wrong extension | โ
Python source files use the .py extension |
โ Print() and print() are the same |
Python is case-sensitive | โ
Only print() (all lowercase) is correct |
โ print("5 + 3") will print 8 |
Text in quotes is printed as it is | โ
It prints 5 + 3; use print(5 + 3) to get 8 |
| โ IDLE stands for "Integrated Development Language Editor" | Wrong full form | โ IDLE = Integrated Development and Learning Environment |
๐ Quick Revision โ Exam Ready!
Python โ One-Line Facts:
- Developer โ Guido van Rossum (Netherlands)
- Released โ 1991
- Based on โ ABC and Modula-3
- Named after โ Monty Python's Flying Circus
- Type โ High-level, interpreted, object-oriented, general-purpose
- File extension โ
.py - Default editor โ IDLE (Integrated Development and Learning Environment)
Pluses vs Minuses:
| โ Pluses | โ Minuses |
|---|---|
| Easy to use | Not the fastest language |
| Expressive language | Not strong on type-binding |
| Interpreted language | Not easily convertible |
| Completeness (big standard library) | Lesser libraries in some areas (e.g., mobile apps) |
| Cross-platform | |
| Free & open source | |
| Variety of usage |
Execution Flow:
- Source code (
.py) โ Bytecode (.pyc) โ PVM โ Output
Two Modes:
| Interactive Mode | Script Mode | |
|---|---|---|
| Prompt / Window | >>> Python Shell |
Editor window |
| Saved? | No | Yes (.py) |
| Run | Immediately, per statement | Whole file (F5) |
๐ฏ Sample Board Exam Questions
Q1: Very Short Answer [1 mark each]
a) Who developed Python? โ Guido van Rossum
b) In which year was Python first released? โ 1991
c) What is the extension of a Python source file? โ .py
d) What is the full form of IDLE? โ Integrated Development and Learning Environment
e) What does the >>> symbol in the Python shell indicate?
โ It is the Python prompt (chevron), showing that Python is ready to accept a command in Interactive Mode
f) Name the two languages on which Python is based. โ ABC and Modula-3
Q2: Short Answer [2 marks]
Q: Differentiate between Interactive Mode and Script Mode.
In Interactive Mode, the user types one statement at a time at the >>> prompt and Python executes it immediately; the commands cannot be saved for future use.
In Script Mode, the complete program is typed in an editor, saved as a .py file, and executed as a whole; it can be edited and re-run any number of times.
Q3: Short Answer [2 marks]
Q: Write any two advantages and two limitations of Python.
Advantages: (i) Python is easy to use with simple, English-like syntax. (ii) Python is free and open source, and it is cross-platform. Limitations: (i) Python is slower than compiled languages like C/C++. (ii) Python is not strong on type-binding โ some type-related errors are found only while the program runs.
Q4: Short Answer [2 marks]
Q: Why is Python called an interpreted language? Explain briefly how a Python program is executed.
Python is called an interpreted language because its programs are executed line by line by the Python interpreter, rather than being fully translated into machine code beforehand.
The source code (.py) is first converted into bytecode, which is then executed by the Python Virtual Machine (PVM) to produce the output.
Q5: Output Based [2 marks]
Q: Write the output of the following code:
print("5 + 3")
print(5 + 3)
print("Total =", 5 * 3)
Answer:
5 + 3
8
Total = 15
โ๏ธ Practice Problems
-
What is a programming language? Differentiate between low-level and high-level languages with one example each.
-
Explain any four features (pluses) of Python.
-
What is meant by "Python is a cross-platform language"? How does bytecode make this possible?
-
Differentiate between a compiler and an interpreter. Which one does Python use?
-
Write the steps to create, save and run a Python program in IDLE.
-
Use Python in Interactive Mode as a calculator to find: (a) 125 ร 8 (b) 256 รท 16 (c) 15 + 27 โ 9. Write the commands you typed and the results shown.
-
Write a program in Script Mode that prints your name, class and school name on three separate lines.
-
Find the error(s) in the following code and rewrite it correctly:
Print("Hello World") print(Welcome to Python) -
A friend says, "I typed a long program in the Python shell, and now I want to run it again tomorrow." What problem will your friend face? Suggest the correct approach.
-
List any three areas where Python is used today, with one example of a library or tool for each.