4.1 Introduction to Python
1. Why were high-level languages developed? Name some.
Computers execute only machine language, which is difficult for humans to write. High-level languages such as C, C++, Java and Python were developed because they are easier for humans to use. Programs in them must be converted to machine language by translators.
2. Does Python use a compiler or an interpreter? How does it execute a program?
Python uses an interpreter that translates and executes code line by line. When a program is run, the source code is first converted into bytecode, which the Python interpreter then executes.
3. What is bytecode? State its features.
- It is a low-level set of instructions that the Python interpreter can understand.
- It is not machine code and cannot run directly on hardware.
- It is platform independent, so the same program can run on different systems.
- It helps programs execute faster by avoiding repeated compilation.
4. Who developed Python? When was the first official version released? Why is it named Python?
Python was developed by Guido van Rossum, a Dutch computer programmer, who began working on it in the late 1980s. The first official version, Python 0.9.0, was released in 1991. He named it after the British comedy group Monty Python's Flying Circus, as he was a fan of the show and thought the name was short, memorable and a bit quirky.
5. List the main features of Python.
- Simple syntax: code looks almost like plain English, easy to read, write and understand.
- Libraries and frameworks: a vast collection of reusable tools.
- Cross-platform: runs on Windows, Mac and Linux without significant modification.
- Open source: source code is free to use, modify and distribute; improved by a global community.
- Concise: usually needs fewer lines of code than C++ or Java.
- Versatile: used in web development, data science, automation, AI and more.
6. Differentiate between a library and a framework.
A library is a reusable collection of functions, classes and modules designed for specific tasks such as mathematical data handling, graphics or networking.
A framework provides a complete structure for developing software applications. Examples: Django and Flask (web development); Pandas, TensorFlow and Scikit-learn (AI, machine learning and data science).
7. Why is Python said to be open source? What are its benefits?
Its source code is freely available for anyone to use, modify and distribute. This makes Python cost-free, highly flexible and continuously improved by a large global community.
8. What does cross-platform compatibility mean?
Python code can run on multiple operating systems, including Windows, Mac and Linux, without requiring significant modifications.
4.2 Python IDEs
9. What is an IDE?
An Integrated Development Environment is a software application that provides a comprehensive platform for writing, debugging and testing code. It enhances productivity, simplifies development and improves code quality.
10. Is an IDE enough to run a Python program?
No. You may use any suitable IDE to write the program, but a Python interpreter must be installed on the computer to run it.
11. Which IDE is used in this book? Write the steps to open it on GNU Linux.
IDLE version 3.10.12 with GNU Linux 22.04.5 LTS.
- Press the Super key to launch the search bar on the desktop.
- Type "idle" (upper or lower case) in the search bar.
- Click the IDLE icon to open the IDLE Shell.
12. What is the Python prompt? What does it indicate?
The symbol >>> is the Python prompt. It indicates that the interpreter is ready to execute instructions.
Interactive and script modes
13. What are the two modes of using the Python interpreter?
Interactive mode and script mode.
14. What is interactive mode? How is it used?
Interactive mode executes individual statements instantly and gives immediate results. A statement is typed at the >>> prompt and Enter is pressed; the result appears on the next line and the prompt reappears.
>>> print("Hello World!!")
Hello World!!
>>> print(5+2)
7
>>> (12+5)/2
8.515. How can the IDLE shell be used as a calculator?
In interactive mode, arithmetic expressions such as addition, subtraction, multiplication and division can be typed at the prompt. On pressing Enter the result is shown, e.g. (12+5)/2 gives 8.5.
16. What is the limitation of interactive mode?
The code cannot be saved for later use. To run the same code again it must be retyped. Script mode overcomes this.
17. What is script mode? What is the file extension of a Python program?
In script mode, a program containing more than one instruction is written in a file, saved, executed, and edited as needed. Python files are saved with the extension .py, by default in the current working directory (e.g. /home/linux/).
18. Write the steps to create, save and run a program in script mode in IDLE.
- Choose File → New File (or press Ctrl+N).
- Type the Python statements in the new file.
- Save with a suitable name and the
.pyextension (e.g. test.py). - Run with Run → Run Module (or press F5).
- The output appears in the Python shell.
19. What is the output of this program saved as test.py?
print("Hello World!!")
print(5+2)
print((12+5/2))
print("Hello World!!")
print(5+2)
print((12+5/2))Hello World!!
7
14.5The last line follows operator precedence: 5/2 = 2.5 is calculated first, then 12 + 2.5 = 14.5.
20. Differentiate between interactive mode and script mode.
| Interactive mode | Script mode |
|---|---|
| Executes one statement at a time | Executes a file with many instructions |
Instant result at the >>> prompt | Run using Run Module / F5 |
| Code cannot be saved | Code is saved as a .py file and can be edited and reused |
| Good for testing single lines | Good for complete programs |
21. What is the function of print()?
print() displays a message or the result of an expression on the screen, e.g. print("Hello World!!") shows Hello World!!.
Let us do – practical questions
22. What are the outputs of the following? (i) print("Welcome to Python programming") (ii) print(25 + 12) (iii) print("Hello" + "World")
Welcome to Python programming
37
HelloWorld23. Write your observations on the print() function.
- It displays whatever is given inside its brackets.
- Text in quotation marks is printed exactly as written, without the quotes.
- An expression such as 25 + 12 is evaluated first and only the result (37) is printed.
- Each print() starts its output on a new line.
24. Compare the working of the + operator in print(25 + 12) and print("Hello" + "World").
With numbers, + performs arithmetic addition (25 + 12 gives 37). With text (strings), + joins them together, called concatenation ("Hello" + "World" gives HelloWorld, with no space added). So the same operator behaves differently depending on the type of data.
25. Write Python instructions to find the area of a circle with diameter 12 cm.
Radius = diameter / 2 = 6 cm; area = π r².
print(3.14 * (12/2) * (12/2))
Output: 113.04 (square cm).
26. Write Python instructions to find the perimeter of a rectangle with length 7 cm and breadth 5 cm.
Perimeter = 2 × (length + breadth).
print(2 * (7 + 5))
Output: 24 (cm).
27. Fill in the blanks.
- Python uses an ______ to translate code: interpreter.
- The Python prompt is ______: >>>.
- Python files have the extension ______: .py.
- The shortcut to run a module in IDLE is ______: F5.
- The shortcut to create a new file in IDLE is ______: Ctrl+N.