OIM3600 - Computer Science for Business Students

Session 12 (10/07)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Class Review
  • Lecture:

Welcome/News/Announcements

  • Homework Python 2 is due end of Tomorrow (Tuesday, 10/08)
  • Homework feedback is provided in your GitHub issues or via PR.
    • Please respond.
  • Make sure you understand the code you use in your exercises and homework.
    • i.e. I may ask you to come to my office and explain any code that looks "interesting".
  • Communications:
    • Slack - for debugging, clarifications, real-time discussion, casual chat, etc.
    • Email - specify course # in subject title, e.g., "OIM3600: GitHub settings"
    • Meet with me in person during office hours at least once this semester.
  • Questions?

NotebookLM

What we have learned so far

  • Code represent: 🔠🔢🈶😂🟥🟩🟦🖼 📽 🎼
  • Scratch
  • Git/GitHub
  • Programming
    • Functions: arguments, side effects, return values
    • Conditionals
    • Boolean expressions
    • Loops
    • Variables
    • Types: int, float, string, boolean, Nonetype, list, ...
    • I/O

Quick Quiz - Dictionaries

  1. Are dictionaries mutable?
  2. Is it true that dict keys must be unique, but their values can be duplicated?
  3. What is the output?
    person = {'name': 'Guido', 'age': 68 }
    print('Guido' in person)
    
  4. How do you access the value associated with the key 'age' in person?
  5. How do you add a new key-value pair 'city': 'Belmont, CA' to person?
  6. How do you retrieve the value associated with a key, say 'country', in person safely, without raising an error if the key doesn't exist?
  7. How can you create a dictionary letter_count that counts the frequency of each letter in a given string?

Python Recommendations

Homework Feedback & Coding Style

  • GitHub Issues
    • Reply with feedback or questions if further discussion is needed.
    • Reply with evidence (e.g., code, screenshots) and close if resolved.
  • Functionality
    • Use pseudocode for complex or unclear code/algorithms.
    • Write additional test code when necessary.
  • Documentation
    • Add docstrings for every function that you create.
    • Write clear and relevant comments.
  • Styles
    • Use lowercase and underscore for folder and file names.
    • Choose clear and meaningful variable and function names.

How to Debug

  • Format code first: Alt (⌥ option if macOS) + Shift + F in VSCode.
  • print()
  • PythonTutor.com
  • Debugging in VS Code
    1. Set breakpoints
    2. Run debugger: Press F5 to Start Debugging
      1. "Continue" ( F5 ): Keep running until you hit a stopping point.
      2. "Step Over" ( F10 ): Go through code line by line, skip functions.
      3. "Step Into" ( F11 ): Go inside functions and trace their lines.
    3. Inspect Variables: Use "VARIABLES" panel or "WATCH" panel.

Lectures

Conversions Between Data St ructure Types

  1. string ⇔ list
  2. string ⇔ dictionary
  3. string ⇔ tuple
  4. string ⇔ set
  5. list ⇔ dictionary
  6. list ⇔ tuple
  7. list ⇔ set
  8. dictionary ⇔ tuple
  9. ...

API (Application Programming Interface)

API Example: OpenWeather

  • Get an API key from https://openweathermap.org/api
  • Download the sample code, weather.py, from 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 the .gitignore to prevent committing it to a public repository.