Computational Thinking and Programming

Lesson Organising information: unordered structures

Understanding Dictionaries

  • Dictionaries in Python are mutable mappings that store data as key-value pairs, making them useful for organizing related information. You can add, modify, access, and delete elements using keys. They also support various built-in methods for accessing keys, values, and items, making dictionaries an efficient and flexible data structure for retrieving values based on meaningful keys.
  • Recommended reading:
  • Dictionaries: Data structures that store key-value pairs.
  • Keys: Immutable elements used to identify values in a dictionary.
  • Values: Elements associated with keys in a dictionary.
  • Accessing Elements: Retrieve values using keys.
  • dict.keys(): Returns an iterable view of dictionary keys.
  • dict.values(): Returns an iterable view of dictionary values.
  • dict.items(): Returns an iterable view of (key, value) tuple pairs.
  • Adding Elements: Use dict[key] = value or dict.update() to add items.
  • Modifying Elements: Change values using dict[key] = value or dict.update().
  • Deleting Elements: Remove items with del dict[key] or dict.clear().

How To Construct Classes and Define Objects

  • In Python, object-oriented programming (OOP) is a paradigm focused on creating classes and objects. Classes are blueprints for creating objects, while objects are instances of these classes. Python’s __init__ constructor method initializes attributes for each object. OOP is useful for creating reusable, maintainable, and organized code. You can create multiple objects from a class, each with its own distinct attributes.
  • Recommended reading:
  • Object-Oriented Programming (OOP): A paradigm that focuses on creating reusable code patterns using classes and objects.
  • Class: A blueprint for creating objects, defining attributes and methods.
  • Object: An instance of a class, representing a realized version of the class.
  • Methods: Functions defined within a class to operate on its objects.
  • __init__ (Constructor): Method that initializes an object's attributes during instantiation.
  • self: A reference to the current instance of the class, used to access attributes and methods.
  • Dot Operator: Used to call methods or access attributes of an object.
  • Multiple Objects: Classes can create many objects, each with its own attributes, allowing for efficient code reuse.