Unit summary
- Lists and dictionaries are mutable, while tuples and strings are immutable.
- Lists support append, insert, remove, pop and sort, and a list comprehension builds lists.
- Tuples are fixed collections and can be unpacked.
- Dictionaries store key-value pairs and get() avoids missing-key errors.
- Mutable arguments can be changed by a function.
- The math module and NumPy support numerical work, and NumPy arrays are vectorised.
- open() with modes r, w, a and b handles text and binary files, and with closes them.
- pickle stores Python objects in binary form.
- CSV stores tables as text and JSON stores nested data as text.
Glossary
| Term | Meaning |
|---|---|
| Binary file | A file of raw bytes. |
| CSV | A text format for tables with comma-separated values. |
| Dictionary | A collection of key-value pairs. |
| File mode | A letter that states how a file is opened. |
| Immutable | Cannot be changed after creation. |
| JSON | A text format for nested key-value data. |
| Key | The unique, immutable name used to find a value. |
| List | An ordered mutable collection in square brackets. |
| List comprehension | A one-line way to build a list. |
| math module | A module of mathematical functions. |
| Mutable | Can be changed after creation. |
| ndarray | The array object of NumPy. |
| NumPy | A library for fast array computation. |
| pickle | A module that stores Python objects as bytes. |
| Serialisation | Converting an object into a storable form. |
| Text file | A file of readable characters. |
| Tuple | An ordered immutable collection in round brackets. |
| Unpacking | Assigning the items of a tuple to separate variables. |
| Vectorised | Applied to a whole array without an explicit loop. |
| with statement | A block that closes a file automatically. |
Question bank
Short Answer Questions (2 marks each)
- Distinguish mutable and immutable objects.
- Write any four list methods.
- How is a tuple with one item written?
- What is a dictionary?
- What does the get() method of a dictionary do?
- What is a NumPy array?
- What is the difference between modes w and a?
- Why is the with statement used with files?
- What is pickling?
- What is a CSV file?
Medium Answer Questions (5 marks each)
- Explain lists and their commonly used methods.
- Compare lists, tuples and dictionaries.
- Explain the dictionary functions with examples.
- Explain how lists and dictionaries are passed to functions.
- Explain text and binary files and the file modes.
- Explain reading and writing CSV and JSON files.
Long Answer Questions (15 marks each)
- Explain lists and tuples with their functions. Write a program to find the even values of the tuple (1, 2, 5, 7, 9, 2, 4, 6, 8, 10) as another tuple and its maximum and minimum.
- Explain dictionaries and write a program that counts the frequency of each letter in a sentence using a dictionary.
- Explain the math module and NumPy. Write a menu driven program with NumPy to create an array of ones, find the maximum and minimum, find a dot product and reshape an array.
- Explain file handling in Python. Write a program to copy the odd-numbered lines of one file to another, and explain how pickle, CSV and JSON files are used.
Sources
- Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit III
