- LPU MCA Semester 1
- CAB106
- Unit 1
CAB106 · Unit 1: Python and OOP introduction
Python features, installation and IDEs
11 practice questions
Key points
- Python is interpreted, dynamically typed and portable
- Indentation defines blocks and names are case-sensitive
- pip installs packages; IDLE ships with the installer
- Run directly, a file has __name__ equal to __main__
Python is a high-level, general-purpose language created by Guido van Rossum and first released in 1991. It is interpreted (the standard CPython compiles source to bytecode that a virtual machine runs), dynamically typed, garbage collected, open source and portable across Windows, Linux and macOS. Blocks are defined by indentation rather than braces, and names are case-sensitive, so Name and name are different.
| Item | What it is |
|---|---|
| python.org installer | Installs CPython, pip and IDLE |
| pip | Installs packages from PyPI |
| IDLE | Editor and shell bundled with Python |
| VS Code, PyCharm | Popular third-party editors and IDEs |
| REPL | The interactive >>> prompt |
Source files use the .py extension and comments start with #. Running a script directly sets __name__ to __main__. Use python --version to check the installed version and pip install package to add libraries.
Exam trap: dynamic typing does not mean no types. Every value has a type; it is the name that is not tied to one, so x = 1 then x = 'a' is legal.
Try two questions
How does the standard CPython implementation run a .py file?
- It translates the whole program into native machine code before any statement is allowed to run
- It reads each source line as plain text and executes it directly with no compiled form at all
- It compiles the source to bytecode, which the Python virtual machine then executes step by step
- It converts the source to Java bytecode, which is then executed by a Java virtual machine
Show the answer
C. CPython compiles source to bytecode and the Python virtual machine interprets that bytecode.
Why not A: That describes an ahead-of-time compiler such as C, not CPython.
Why not B: CPython first compiles source to bytecode, so there is an intermediate form.
Why not D: Java bytecode belongs to the JVM; CPython uses its own bytecode and virtual machine.
Which feature lets the same pure-Python program run unchanged on Windows, Linux and macOS?
- Static typing, because every variable type is fixed before the program runs
- Native compilation, because each machine gets its own executable file built
- Platform independence, because the interpreter handles the operating system differences
- Manual memory management, because the programmer controls each allocation
Show the answer
C. The interpreter, written for each platform, hides OS differences from portable code.
Why not A: Python is dynamically typed, and typing has no link to portability.
Why not B: Python programs are not shipped as per-machine native executables.
Why not D: Python manages memory automatically.