OIM3640 - Problem Solving and Software Design

2025 Spring

Session 15 (3/13)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Class Review
  • Revisit Quiz 3
  • Lecture:
    • Dictionaries
    • Tuples and Sets (if time permits)

Welcome/News/Announcements

  • Next Class:
    • API and parsing JSON data
    • Please register on OpenWeather and get an API Key (free tier for Current weather and forecasts collection, not One Call API 3.0) before next class.
    • Do not share your API key with others or post it on GitHub.
  • Communications
    • Meet with me in person during office hours at least once this semester.
    • Email - specify course # in subject title, e.g., "OIM3640: GitHub settings"
    • Use Slack/GitHub when asking code-related questions
  • Questions?

What we have learned so far...

  • Variables, Expressions, Statements
  • Types: int, float, string, boolean, Nonetype, string, list
  • Functions
  • Conditional Statements: if...elif...else
  • Iterations: for, while
  • Pseudo-code
  • Debugging

Revisit Quiz 3

  • Code review and optimization
  • Feature enhancements
  • Real-world applications
    • What are common password attack methods?
    • Can your verify whether a password has been exposed in a data breach?
  • What can AI do?
  • Build a Password Management Tool?

Data Structures in Python

Choosing Data Structures

  • Programs often have to work with many objects of various types. For example:
    • A portfolio of stocks
    • A table of stock prices
    • A collection of customer orders
  • Main choices:
    • list: ordered data, access by index
    • tuple: ordered data, immutable, access by index
    • dict: (normally treated as) unordered data, actually insertion-ordered (since Python3.7), access by key
    • set: unordered collection of unique items, cannot access by index or key

Considerations when Choosing Data Structures

  • Data order:
    • Do you need to maintain insertion order or allow arbitrary ordering?
  • Mutability:
    • Do you need it to be mutable (changeable) or immutable (fixed)?
  • Fast access:
    • Do you need fast access to elements by key or index?
  • Uniqueness:
    • Do you need to ensure that elements are unique within the collection?

API

API (Application Programming Interface)

  • An API is a set of protocols, tools, and standards for building software applications, that allows different applications to communicate with each other.

  • Example: Let's say you want to know the current price of Bitcoin

API Example: OpenWeather

  • Create an API key from https://openweathermap.org/api
  • Download the sample code, weather.py, from OIM3640/resources/code
  • Use the urllib.request library to send HTTP requests to the OpenWeather API and retrieve data in JSON format
  • Remember to protect sensitive data (e.g., API keys, API tokens, passwords)
    • Create .env file and store your API key, e.g., OPENWEATHER_API_KEY=...
    • Install python-dotenv in terminal: python -m pip install python-dotenv
    • import os
      from dotenv import load_dotenv
      
      load_dotenv()
      APIKEY = os.getenv("OPENWEATHER_API_KEY")
      
    • Add .env to .gitignore file to prevent committing it to a public repository.

Recommendations on API

- Introduction to **API**

- Review/Exercises - Relationships between different data types - **Pair Programming**: Exercise 2.2/2.3 in Tuples