Preliminary Software Engineering
Unit 1 - Programming Fundamentals
Unit 1 - Programming Fundamentals
  • 1 - Programming Fundamentals
    • Programming Fundamentals Content
  • 2 - Python
    • Expected Python Knowledge
    • GitHub
    • Learning Python
    • Data Structures and File Management
      • Data Structures
        • Lists
        • Arrays
          • Single and Multi-Dimensional Arrays
        • Lists vs Arrays
          • Activities
        • Tuples
        • Sets
        • Dictionaries
          • Activities
      • File Handling
        • Loops, Lists, Dictionaries
        • Activities
  • 3 - Theory Content
    • Theory Content Explained
      • NESA Directional Verbs
      • Responding to Directional Verbs
  • 4 - Software Development and Management
    • Approaches to Software Development
      • Waterfall Model
      • Agile Model
      • Summary: Waterfall vs Agile
      • Activities
    • Software Development Steps
      • Sample Exam Question
      • Requirements Definition
        • Understanding the Need
        • Key Questions to Ask
        • Examples
        • Activities
        • Sample Exam Question
      • Determining Specifications
        • Functional Specifications
        • Non-Functional Specifications
        • Activities
      • Design
        • Top-Down Design Approach
        • Bottom-Up Design Approach
      • Development
        • Optimising Code
      • Integration
        • Example: Payment Program
        • Activity
        • Application Programming Interface (API)
          • Example: OpenWeather API
          • Example: WeatherAPI
          • Activity: Prepare Spells
      • Testing and Debugging
        • Test Data
          • Activities
        • Testing the System
          • Activities
        • Debugging
          • Types of Errors
            • Activities
          • Python Debugger
            • Activities
          • VS Code Debugger
            • Activities
      • Installation
        • Activities
        • Sample Exam Question
      • Maintenance
  • Charts and Algorithms
    • Example: IPO Charts and Pseudocode
      • Activities
    • Algorithms, Flowcharts, Pseudocode
      • Pseudocode Activities
      • Flowchart Activities
      • Sample Exam Questions
    • Structure Charts
      • Activities
      • Sample Exam Questions
    • Data Flow Diagrams
      • Activities
    • Data Dictionaries
      • Activities
    • Decision Trees
      • Activities
      • Sample Exam Questions
    • Gantt Chart
    • Class Diagrams
      • Sample Exam Question
    • Storyboards
      • Sample Exam Question
  • Testing and Debugging
    • Test Data
      • Activities
    • Testing the System
      • Activities
    • Debugging
      • Types of Errors
        • Activities
      • Python Debugger
        • Activities
      • VS Code Debugger
        • Activities
  • Computational Thinking
    • Decomposition
    • Abstraction
    • Activities
  • Version Control
    • Git
    • GitHub
    • Activities
  • Number Systems
    • Binary Systems
    • Hexadecimal Numbers
    • Using Two's Complement
    • Activities
  • 5 - Assessment Task 1
    • Data Science Project
      • Before we Start
        • Setting up GitHub Repository
        • Setting Up Markdown Documentation
      • Examples of API Usage
        • Starter Code: NASA Scenario
        • Starter Code: Spell Book
        • Starter: Pokédex Explorer
        • Starter Code: Weather App
        • Example: OpenWeather API
        • Example: WeatherAPI
        • Example: Prepare Spells
    • Task Guide
      • Requirements Definition
      • Determining Specifications
        • Use Cases
      • Design
        • Gantt Chart
        • Structure Chart
        • Algorithms
        • Data Dictionary
      • Development
        • Comments vs DocStrings
        • UI - main.py
        • Create Python Module
          • Example: NASA Module
          • Example: WeatherFetch Module
          • Example: SpellBook Module
      • Integration
        • Example: Pokedex
      • Testing and Debugging
        • Commit Changes
      • Installation
      • Maintenance
    • Submitting Your Task
Powered by GitBook
On this page
  • Mutability
  • Key Features
  • Dictionary Methods
  1. 2 - Python
  2. Data Structures and File Management
  3. Data Structures

Dictionaries

A dictionary in Python is a data structure that stores an unordered collection of items.

Unlike lists or tuples, which are indexed by a range of numbers, dictionaries are organised by key-value pairs.

Each key in a dictionary is mapped to a specific value, allowing you to associate related data in a way that makes it easy to look up values based on their corresponding keys.

One of the key characteristics of dictionaries is that the keys must be unique and immutable, meaning that each key can only appear once in a dictionary, and it must be a type that does not change, such as a string, number, or tuple. The values, however, can be of any type and can even be duplicated.

To create a dictionary, place comma-separated key-value pairs inside curly braces {}, with a colon : separating each key from its associated value.

student_grades = {'Alice': 85, 'Bob': 92, 'Charlie': 78} 

Alternatively, you can create an empty dictionary using empty curly braces {} and then add key-value pairs to it as needed.


Mutability

Dictionaries in Python are mutable, which means you can change, add, or remove key-value pairs after the dictionary has been created. This mutability makes dictionaries an excellent choice for situations where the data may need to be updated or expanded over time.


Key Features

  1. Dictionaries are particularly useful when you need to associate pairs of items.

  2. One of the standout features of dictionaries is their ability to provide fast access to data. By using a key, you can quickly retrieve the corresponding value without needing to search through all the items, as you would with a list.


Dictionary Methods

get(): returns the value associated with a specified key. If the key is not found, it returns a default value (which you can specify) instead of raising a KeyError.

student_grades = {'Alice': 85, 'Bob': 92}
grade = student_grades.get('Alice')  # Output: 85
missing_grade = student_grades.get('Charlie', 'Not Found')  # Output: 'Not Found'

keys(): returns a view object that contains a list of all the keys in the dictionary.

student_grades = {'Alice': 85, 'Bob': 92}
all_keys = student_grades.keys()  # Output: dict_keys(['Alice', 'Bob'])

values(): returns a view object that contains a list of all the values in the dictionary.

student_grades = {'Alice': 85, 'Bob': 92}
all_values = student_grades.values()  # Output: dict_values([85, 92])

items(): returns a view object that contains a list of all the key-value pairs in the dictionary as tuples.

student_grades = {'Alice': 85, 'Bob': 92}
all_items = student_grades.items()  # Output: dict_items([('Alice', 85), ('Bob', 92)])

update(): updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs. If a key already exists, its value is updated; if the key doesn't exist, it is added.

student_grades = {'Alice': 85, 'Bob': 92}
new_grades = {'Charlie': 78, 'Bob': 95}
student_grades.update(new_grades)
# Output: {'Alice': 85, 'Bob': 95, 'Charlie': 78}

pop(): removes the key-value pair associated with a specified key and returns the value. If the key is not found, it raises a KeyError unless you provide a default value.

student_grades = {'Alice': 85, 'Bob': 92}
removed_value = student_grades.pop('Bob')  # Output: 92
# student_grades is now {'Alice': 85}

popitem(): removes and returns the last key-value pair inserted into the dictionary. Dictionaries maintain insertion order, so popitem() will return items in LIFO (last-in, first-out) order.

student_grades = {'Alice': 85, 'Bob': 92}
last_item = student_grades.popitem()  # Output: ('Bob', 92)
# student_grades is now {'Alice': 85}

clear(): removes all key-value pairs from the dictionary, leaving it empty.

student_grades = {'Alice': 85, 'Bob': 92}
student_grades.clear()
# student_grades is now {}

setdefault(): returns the value of a specified key. If the key does not exist, it inserts the key with the specified default value and returns that default value.

student_grades = {'Alice': 85, 'Bob': 92}
grade = student_grades.setdefault('Charlie', 78)  # Adds 'Charlie' with value 78
# student_grades is now {'Alice': 85, 'Bob': 92, 'Charlie': 78}

copy(): returns a shallow copy of the dictionary. This means that the new dictionary is a copy of the original, but if the dictionary contains nested objects, they are not copied.

student_grades = {'Alice': 85, 'Bob': 92}
grades_copy = student_grades.copy()

fromkeys(): creates a new dictionary with specified keys, all set to a specified default value.

keys = ['Alice', 'Bob', 'Charlie']
default_value = 0
new_dict = dict.fromkeys(keys, default_value)
# Output: {'Alice': 0, 'Bob': 0, 'Charlie': 0}

Looping through a Dictionary

for key, value in student.items():
    print(f"{key}: {value}")

PreviousSetsNextActivities

Last updated 2 months ago