MCS-201 · Unit 9

Introduction to Python

Section 1 of 7

Block 3 · Unit 9Introduction to Python12 min · Section 1 of 7

9.1Python and C Compared

Covers IGNOU MCS-201 §9.0

You already know C, so the quickest way into Python is to see what changes. This section explains the key properties of Python and compares them with C point by point.

What kind of language Python is

Each part of that definition has a meaning you can test:

  • High-level: you work with numbers, strings, lists and objects, never with addresses or bytes, unless you choose to.
  • General-purpose: it is not tied to one kind of problem.
  • Dynamically typed: types belong to values, and a variable is just a name that can be attached to a value of any type. Types are checked while the program runs.
  • Automatic memory management: objects are freed when nothing refers to them any more (garbage collection); there is no malloc or free.
  • “Batteries included”: a large standard library comes with every installation (files, dates, mathematics, networking, JSON, databases, testing and more).

How Python runs a program

C is compiled ahead of time into machine code, and the machine code runs directly on the processor. Python (in its standard implementation, CPython) works in two steps:

From source code to execution
C:       hello.c  --compiler-->  machine code (executable)  --runs on-->  CPU

Python:  hello.py --compiled to--> bytecode (.pyc)  --interpreted by-->  Python virtual machine --> CPU

The whole file is first compiled to bytecode, a compact set of instructions for the Python virtual machine, and the virtual machine then executes that bytecode one instruction at a time. Because each bytecode instruction is carried out by the interpreter rather than directly by the CPU, Python code usually runs much slower than equivalent C code.

Comparison table

AspectPythonC
Level and purposeHigh-level, general-purposeLower-level, general-purpose, close to the hardware
ExecutionCompiled to bytecode, then interpreted by a virtual machineCompiled to machine code ahead of time
SpeedSlower (often 10–100 times for pure loops)Fast
TypingDynamic: no declarations; a name can refer to values of different typesStatic: every variable is declared with one type
Type safetyStrong: "5" + 5 is an errorWeak in places: implicit conversions, casts, pointer tricks
MemoryAutomatic (garbage collection)Manual (malloc/free)
PointersNo pointer variables or pointer arithmetic; every name is a reference to an objectPointers used everywhere
BlocksMarked by indentationMarked by braces { }
Code lengthShort; many built-in data structures (lists, dictionaries, sets)Longer; data structures written by hand
ErrorsSyntax errors found before running; other errors raise exceptions at run timeMost type errors found at compile time; many run-time errors are undefined behaviour
Typical usesWeb back ends, data science, AI, scripting, automation, teachingOperating systems, embedded systems, databases, compilers and interpreters (CPython itself is written in C)

Key takeaways

  • Python is high-level, general-purpose, dynamically and strongly typed, with automatic memory management.
  • CPython compiles source to bytecode, then a virtual machine interprets it.
  • Python trades execution speed for development speed; C is faster and closer to the hardware.
  • Functions are objects and can have several names; Python has references, not pointer arithmetic.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does “dynamically typed” mean for Python?

Show answer

Answer: B. Types belong to values and are checked while the program runs

A name can refer to values of different types over time, but each value has a definite type, checked at run time. “Values have no types” would be untyped, which Python is not.

Question 2 · multiple choice

How does CPython execute a .py file?

Show answer

Answer: C. It compiles the file to bytecode and a virtual machine interprets the bytecode

The source is compiled to bytecode (cached in .pyc files) and executed by the Python virtual machine.

Question 3 · multiple choice

What does this program print?

Python · quiz_types.py
a = 10
b = "10"
print(a + int(b), str(a) + b)
Show answer

Answer: D. 20 1010

int(b) converts the text to the number 10, so a + int(b) is 20; str(a) + b joins two strings, giving “1010”.

Question 4 · multiple choice

Which feature does Python provide that C leaves to the programmer?

Show answer

Answer: C. Automatic memory management

Python’s garbage collector frees objects that are no longer used; in C you must call free.

Question 5 · multiple choice

A Python file has a valid print statement on line 1 and a missing colon on line 5. What happens when it is run?

Show answer

Answer: B. Nothing runs; a SyntaxError is reported for line 5

The whole file is compiled before execution starts, and compilation fails at line 5, so no line runs.

Question 6 · short answer

Give three reasons why a team might write a data-analysis script in Python rather than C, and one reason to choose C instead.

Show worked answer

Python needs much less code, because lists, dictionaries and string handling are built in; it manages memory automatically, so there are no malloc/free bugs; and it has mature libraries such as pandas and NumPy for data analysis. C would be the better choice when raw speed or control over memory and hardware matters, for example in an embedded device or a performance-critical engine (which is why NumPy’s own number-crunching core is written in C).

↑ ↓ move↵ openTab switch type