Weekly Programming Topics
Composite Data Types
Lesson 3 · Composite Data Types
Composite Data Types
Composite Data Types
What is in this lesson?
Composite data types Enumerations Dictionaries and Sets in Python 11th January 2026 Tram Tran 2
Primitive Data Types (recap)
Primitive data types are the basic, built-in data types in programming languages. They represent single values and are immutable. ▶ int: integer numbers ▶ float: real numbers ▶ bool: carry one of two values True or False ▶ char: one single character Primitive data types are essential for creating more complex data types called composite data types. 11th January 2026 Tram Tran 3
Composite data types
▶ Composite data types are complex, user-defined structures built from primitives data types. ▶ They are either included in the programming language or created by programmers. ▶ array ▶ tuple ▶ dict ▶ set ▶ Classes and Objects ▶ Abstract data types representing complex entities in a program 11th January 2026 Tram Tran 4
Composite Data Types: An Example Let’s take student data as an example. 11th January 2026 Tram Tran 5
Composite Data Types: An Example Let’s take student data as an example. 11th January 2026 Tram Tran 5
C Structures (struct)
▶ A struct in C is a programmer-created data type that combines different types of variables in a structure variable. ▶ The variables are calledstruct members. struct Date { int day; char month[10]; int year; }; struct Date d;
d.day = 5; d.year = 2026; int y = d.year;
11th January 2026 Tram Tran 6
Python Classes [1, Ch. 10] A class specifies the attributes and methods for a particular type. 1 class Date: 2 def __ ini t_ _ (self, day: int, month: str, year: int):
3 self . day = day 4 self . month = month 5 self . year = year
▶ The self parameter is required in every method of a class. It references the specific object that the method is supposed to operate on. ▶ The __init__ method is known as an initialiser method since it initialises the object’s data attributes. 11th January 2026 Tram Tran 7
Using Composite Data Types
▶ In Python, class is considered as a blueprint of the custom data type that we define. ▶ To use it as a variable for storing custom data, we need to create an instance of the class.
1 d = Date (5 , " January " , 2026)
11th January 2026 Tram Tran 8
Datetime Composite Data Type in Python Python provides a built-indatetime module for handling dates and times. 1 from da tet im e import date 3 # Create a date object for January 05, 2026
4 m e e t i n g _ d a t e = date ( year =2026 , month =1 , day =5)
6 print (m e e t i n g _ d a t e) 7 # Output: 2026 -01 -05 11th January 2026 Tram Tran 9
Nested Composite Data Types
A composite data type can have fields/ members/ attributes that are also composite data types. 1 class Date: 2 def __ ini t_ _ (self, day: int, month: str, year: int):
3 self . day = day 4 self . month = month 5 self . year = year
7 class Booking: 8 def __ ini t_ _ (self, room: str, date: Date, time: str):
9 self . room = room 10 self . date = date 11 self . time = time
11th January 2026 Tram Tran 10
Enumerations
C Enumerations (enum)
▶ Enumeration is a composite data type, holds constant values. ▶ Enumeration assigns meaningful names to a set of integers. enum Month {
JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}; 11th January 2026 Tram Tran 11
struct Date { int day; enum Month month; int year;
}; int main() {
enum Month m = JANUARY;
printf("Month number: %d\n", m); return 0; } 11th January 2026 Tram Tran 12
enum Month {
JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}; ▶ Enumerations are by default numbered incrementally in ascending order; ▶ but you can break this by explicitly defining the number directly. ▶ enum values start at 0 by default. ▶ In C, anenum is internally represented as anint. 11th January 2026 Tram Tran 13
Why use constants and enumerations?
▶ To give meaning to numbers, improve readability ▶ Prevent errors ▶ Easier to maintain ▶ Logical grouping of related values 11th January 2026 Tram Tran 14
Dictionaries and Sets in Python
Dictionaries [1, Ch. 9]
Concept A dictionary is an object that stores a collection of data. ▶ Each element in a dictionary has a pair of: key and value. ▶ A key is used to locate a specific value. ▶ You can create a dictionary by enclosing the elements inside a set of curly braces ({}).
1 p h o n e b o o k = {2 ’ Chris ’: ’ 555 -1111 ’, 3 ’ Katie ’: ’ 555 -2222 ’, 4 ’ Joanne ’: ’ 555 -3333 ’ 5 } 11th January 2026 Tram Tran 15
Retrieving, Adding, and Removing Dictionary Elements [1, Ch. 9] ▶ Retrieve a value: 1 < dictionary_name >[ < key >] ▶ Add a new key-value pair or assign a value to an existing key:
1 < dictionary_name >[ < key >] = < value >
▶ Remove an existing key-value pair: 1 del < dictionary_name >[ < key >] 11th January 2026 Tram Tran 16
The in and not in Operators [1, Ch. 9] ▶ Accessing a nonexistent key in a dictionary raises aKeyError. ▶ Use thein operator to check if a key exists before accessing it. ▶ Use not in to check if a key does not exist. 1 if ’ Chris ’ in p h o n e b o o k: 2 print (p h o n e b o o k [ ’ Chris ’ ]) 4 if ’ Tom ’ not in p h o n e b o o k: 5 print (’ Tom is not found. ’) 11th January 2026 Tram Tran 17
Iterate over a Dictionary [1, Ch. 9] You can use thefor loop to iterate over all the keys in a dictionary. 1 for < key > in < dictionary >: 2 < statement > 3 < statement > ▶ This loop iterates once for each element in the dictionary. ▶ Each time the loop iterates, the variable<key> is assigned a key. 11th January 2026 Tram Tran 18
Sets [1, Ch. 9]
Concept A set is an object that stores a collection of unique values in the same way as mathematical sets. ▶ All the elements in a set must be unique. ▶ Sets are unordered. ▶ The elements that are stored in a set can be of different data types. 1 # Create an empty set
2 myset = set ()
3 # Create a set with some e lem en ts
4 myset = set ([ ’a ’ , ’b ’ , ’c ’ ])
11th January 2026 Tram Tran 19
Adding and Removing Elements [1, Ch. 9] ▶ Add a new element: 1 < Set >. add (< value >) ▶ Remove an existing element: 1 < Set >. remove (< value >) ▶ Discard an element: 1 < Set >. discard (< value >) The only difference betweenremove and discard is that when the specified item is not found in the set, theremove method raises a KeyError, but thediscard method does not. 11th January 2026 Tram Tran 20
Iterate over a Set [1, Ch. 9] You can use thefor loop to iterate over all the elements in a set. 1 for < element > in < Set >: 2 < statement > 3 < statement > Similarly, you can use thein and not in operators to check if an element exists in a set. 11th January 2026 Tram Tran 21
Set Operations [1, Ch. 9]
Given two setsset1 and set2, in Python: ▶ set1∪set2 1 set1 . union ( set2 ) 2 set1 | set2 ▶ set1∩set2 1 set1 . i n t e r s e c t i o n ( set2 ) 2 set1 & set2 ▶ set1\ set2 1 set1 . d i f f e r e n c e ( set2 ) 2 set1 - set2 ▶ set1⊆set2 1 set1 . i ss ubs et ( set2 ) ▶ set1⊇set2 1 set1 . i s s u p e r s e t ( set2 )
11th January 2026 Tram Tran 22
Recap
In this lecture, we’ve learned: ▶ What are composite data types, and what are some composite data types in C and Python? ▶ Nested composite data types ▶ Enumerations in C ▶ Dictionaries and Sets in Python 11th January 2026 Tram Tran 23
References
Gaddis, TonyStarting out with Python. Pearson Education Limited. Sixth Edition, 2024. 11th January 2026 Tram Tran 24