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)

  1. Distinguish mutable and immutable objects.
  2. Write any four list methods.
  3. How is a tuple with one item written?
  4. What is a dictionary?
  5. What does the get() method of a dictionary do?
  6. What is a NumPy array?
  7. What is the difference between modes w and a?
  8. Why is the with statement used with files?
  9. What is pickling?
  10. What is a CSV file?

Medium Answer Questions (5 marks each)

  1. Explain lists and their commonly used methods.
  2. Compare lists, tuples and dictionaries.
  3. Explain the dictionary functions with examples.
  4. Explain how lists and dictionaries are passed to functions.
  5. Explain text and binary files and the file modes.
  6. Explain reading and writing CSV and JSON files.

Long Answer Questions (15 marks each)

  1. 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.
  2. Explain dictionaries and write a program that counts the frequency of each letter in a sentence using a dictionary.
  3. 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.
  4. 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