Skip to content
Learnearn.uk » A Level Computer Science Home » User Defined Types

User Defined Types

Introduction

What are user defined types?

Sometimes the use of in-built data types / structures are not sufficient to represent the data you are working with. It is therefore necessary to construct your own data types/structures. User defined types can make programs much easier to code, read and debug.

Non-composite vs Composite types

Non composite types are user defined types that contain only one data type, for instance enumerators.

Composite data types are user defined types that contain more than one data type, for instance a record

Enumerators

Enumerators

Enumerators bind a group of names to constant values and are useful for things such as days of the weeks or months of the year,

Enumerators in Python

Enumerators were added to Python 3.4+

 

import enum

class Months(enum.Enum):
    JAN = 1
    FEB = 2
    MAR = 3
    APR = 4
    MAY = 5
    JUN = 6
    JUL = 7
    AUG = 8
    SEP = 9
    OCT = 10
    NOV = 11
    DEC = 12

print(Months.JAN)
# prints <Months.JAN: 1>

print(Months['JAN'])
# also prints <Months.JAN: 1>  // useful for passing variable references
    
print(Months(1))
 also prints <Months.JAN: 1>  // find a month from its number

print(Months.JAN.value)
# prints 1   //useful if you need to find a month's number

print(Months(1).name)
# prints the month's name 

print(Months['JAN'].value > Months['FEB'].value)
# Prints False //Useful for finding out which month has a higher value

Pointers

Pointers

A pointer is a datatype that refers to a location in memory. It is a form of indirect referencing. Obtaining the value stored at the memory location is known as dereferencing.

Python note

Python does not use pointers, though many other languages (such as C) do. For a discussion why Python doesn’t use pointers, take a look at this article.

Records

Records

Records are composite data types that contain references to at least 1 other datatype within them. The advantage of records is that all related data is tied together in one place. They are incredibly useful and popular in many languages.

#Works for Python 3.3+

import types

giraffe = types.SimpleNamespace()

giraffe.name = "Gerry"
giraffe.height = 350
giraffe.age = 15
giraffe.origin = "Africa"

CIE Pseudocode & Records

In the exam the main use of records will be when reading & writing records from/to random files.

 

 

Sets

Sets

Sets are a composite data type that are unordered, iterable, mutable and contain no duplicate elements.

Python sets are in built and are incredibly useful for certain tasks where you need to compare 2 sets in some way.

The main functions are:

  • Union
  • Intersection
  • Difference
  • Symmetric difference

Python Sets Tutorial

Can’t access YouTube? Try the Google Drive Version instead.

 

 

 

 

Python Sets have lots of useful methods that can be found here.

 

Challenges

Challenges

Challenge 1

Create a function called months_between that finds out the number of months between 2 months

Challenge 2

Create a list of zoo animals in records and print out the details of the animals in a headed table format.

This video may help you.

Challenge 3

Adapt challenge 2 so that the program asks the user to input some parameters(such as maximum height) and only prints out the relevant information.

 

 

Resources

User Defined Types Presentation

Python Sets Presentation

Exam Questions

May 17 31 – Qn 1

Nov 18 33 Qn 1

Nov 19 31 Qn 5

Nov 19 32 Qn6