University of Delhi Syllabus ยท Unit 3

Built-in Data Structures

Strings, lists, tuples, sets and dictionaries

Learning Outcomes
  • Distinguish mutable and immutable objects
  • Manipulate strings using built-in functions
  • Use lists with slicing and functions
  • Apply tuples, sets and dictionaries

Mutable and Immutable Objects

Immutable objects such as numbers, strings and tuples cannot be changed after creation, whereas mutable objects such as lists, sets and dictionaries can. This affects how values are copied and passed to functions.

Strings

Strings support indexing, slicing and traversal along with many built-in functions such as upper, lower, split, join, find and replace, plus concatenation and repetition operators.

s = "Data Science" print(s.upper(), s[:4], s.replace(" ", "_"))

Lists

Lists are ordered, mutable sequences supporting creation, traversal, slicing and splitting, with methods like append, insert, pop and sort; lists can be passed to functions and built with comprehensions.

nums = [4, 1, 7, 3] nums.append(9) print(sorted(nums), [x*x for x in nums])

Tuples, Sets and Dictionaries

Tuples are immutable sequences, sets store unique unordered items with union and intersection operations, and dictionaries hold key-value pairs with fast lookup.

point = (3, 4) s = {1, 2, 2, 3} d = {"a": 1, "b": 2} print(point, s, d.get("a"))

Summary

This unit explored mutability, strings and their built-in functions, lists with slicing and functions, and the tuple, set and dictionary structures with their operations.

Exercises

  • Write a program to find the frequency of a character in a string and to replace one character with another.
  • Write a program to remove duplicate values from a list using a set.
  • Write a program to count the frequency of words in a sentence using a dictionary.