- LPU MCA Semester 1
- CAB107
- Unit 2
CAB107 · Unit 2: Python for AI and data science
Python basics, Jupyter Notebook and essential libraries
10 practice questions · in the MTE and ETE syllabus
Key points
- Notebooks use code cells and markdown cells, saved as .ipynb
- Lists are mutable, tuples are immutable
- b = a copies the reference, not the list
- dict.get returns a default for a missing key
Python is a high-level, interpreted language that dominates data science because of its readable syntax and library ecosystem. Jupyter Notebook is an interactive environment made of cells: code cells hold code that is executed and show its output, while markdown cells hold formatted text that is rendered. Notebooks are saved with the .ipynb extension.
Core libraries: NumPy (arrays), Pandas (tables), Matplotlib and Seaborn (plots), scikit-learn (ML).
Key built-in types:
| Type | Mutable | Example |
|---|---|---|
| list | Yes | [1, 2, 3] |
| tuple | No | (1, 2, 3) |
| dict | Yes | {'a': 1} |
| set | Yes | {1, 2} |
a = [1, 2, 3]
b = a
b.append(4)
print(a)
t = (1, [2, 3])
t[1].append(4)
print(t)
The first print shows [1, 2, 3, 4] because b = a copies the reference, not the list. The second shows (1, [2, 3, 4]): the tuple itself cannot be reassigned, but a mutable list stored inside it can change.
d.get('c', 0) returns 0 when the key is missing instead of raising KeyError.
Exam trap: assignment does not copy a list, and an immutable container can still hold mutable objects.
Try two questions
How does a markdown cell differ from a code cell in a Jupyter Notebook?
- A markdown cell executes Python statements, while a code cell stores formatted text, headings and links
- A markdown cell is limited to holding images, whereas a code cell is limited to holding tables
- A markdown cell holds formatted text and is rendered, while a code cell holds code that is executed
- A markdown cell runs on the GPU for faster rendering, whereas a code cell is run on the main CPU
Show the answer
C. Markdown cells document the work; code cells run and show outputs.
Why not A: This swaps the roles of the two cell types.
Why not B: Both cell types hold mixed content; the distinction is rendering versus execution.
Why not D: Cell type does not decide the processor used.
Which statement correctly compares a Python list and a tuple?
- A tuple can be changed after it has been created, while a list cannot be changed afterwards
- Both can be changed freely after creation, but only tuples are allowed to hold strings
- Neither of them can hold values of more than one data type inside the same container
- A list can be changed after creation, while a tuple cannot be changed after creation
Show the answer
D. Lists are mutable; tuples are immutable sequences.
Why not A: This reverses mutability; tuples are the immutable one.
Why not B: Tuples are immutable and both may hold strings.
Why not C: Both can hold mixed data types.