1. LPU MCA Semester 1
  2. CAB106
  3. 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.

ItemWhat it is
python.org installerInstalls CPython, pip and IDLE
pipInstalls packages from PyPI
IDLEEditor and shell bundled with Python
VS Code, PyCharmPopular third-party editors and IDEs
REPLThe 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

Question 1 · Tier 2, Understand

How does the standard CPython implementation run a .py file?

  1. It translates the whole program into native machine code before any statement is allowed to run
  2. It reads each source line as plain text and executes it directly with no compiled form at all
  3. It compiles the source to bytecode, which the Python virtual machine then executes step by step
  4. 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.

Question 2 · Tier 2, Understand

Which feature lets the same pure-Python program run unchanged on Windows, Linux and macOS?

  1. Static typing, because every variable type is fixed before the program runs
  2. Native compilation, because each machine gets its own executable file built
  3. Platform independence, because the interpreter handles the operating system differences
  4. 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.