#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 11/18/2022 DS 2000 Lecture 20 - classes and objects, part 2 Logistics: - Homework 8 is due today - this is your *last* HW for DS 2000 - there is a significant amount of extra credit available - due on Friday - OH this week - 2 - 8pm today - Next week: - yes, we have class on 11/22 - yes, there will be a remote option - yes, "I'm out of town" is a valid reason to attend remotely - yes, I do expect you to attend :) - remote attendance (https://bit.ly/remote-ds2000-muzny) (We *will* be using poll everywhere today) Three ways to participate in multiple choice questions 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) """ """ Warm-up -1 ---- How are you feeling about the end of HW 8? A. Relieved B. Sad C. Suddenly free D. Energized about DS 2001 project E. Some combination of these things? """ """ Review: classes and objects ---- What is a class? What is an object? What is a class attribute? What is a method? """ # An example class # this tells python "I'm defining a class now!" class Professor: # constructor def __init__(self, prof_name): print("Create a professor") # set up code for your class attributes # self.attribute = value self.name = prof_name # no courses taught to begin with self.courses_taught = [] def greet(self): print("Hello, I'm a professor") def teach(self, course_name): # whenever I access an attribute, I need to use self self.courses_taught.append(course_name) # Write an example line of code to instantiate a Professor # print("Prof example") """ Class Design: Pokemon ---- Attributes: Methods: """ """ Writing a larger program: Pokemon Battle --- Write a program, pokemon.py, that defines a Pokemon class then instantiates two Pokemon and has them battle each other. """ """ Exercise 0: lists and mutability ---- After the following code runs, what will the list `numbers` contain? """ print("Exercise 0") numbers = list(range(3)) for num in numbers: num = num * 2 """ A. [0, 1, 2] B. [0, 2, 4] C. [0, 1, 2, 0, 2, 4] D. Something else E. Error """ """ Exercise 1: strings and mutability ---- After the following code runs, what will the value of the string `words` be? """ print("Exercise 1") words = "cat otter turtle" for word in words: if word == "cat": words = "CAT" """ A. "CAT otter turtle" B. "CAT" C. "cat otter turtle" D. Something else E. Error """ """ Exercise 2: strings and lists ---- After the following code runs, what will the value of the list `punctuation` be? """ print("Exercise 2") punctuation = [".", "?", "!"] for i in range(len(punctuation)): if punctuation[i] == "?": punctuation[i] = "CAT" """ A. [".", "?", "!"] B. [".", "CAT", "!"] C. ["CAT"] D. Something else E. Error """ """ Exercise 3: dictionaries and mutability --- What is the value of `ages` after the following code runs? """ print("Exercise 3") ages = {"Felix": 31, "Dylan": 33} for name in ages: name = name.upper() """ A. {"Felix": 31, "Dylan": 33} B. {"FELIX": 31, "DYLAN": 33} C. {} D. Something else E. Error """ """ Exercise 4: dictionaries and lists and mutability --- What is the value of "data" after the following code runs? """ data = [{"name": "Felix", "age": 31}, {"name": "Dylan", "age": 33}] for row in data: row["name"] = row["name"].upper() """ A. [{"name": "Felix", "age": 31}, {"name": "Dylan", "age": 33}] B. [{"name": "FELIX", "age": 31}, {"name": "DYLAN", "age": 33}] C. [{"NAME": "Felix", "age": 31}, {"NAME": "Dylan", "age": 33}] D. Something else E. Error """ """ Review: mutability --- With lists and dictionaries: - if you are using square brackets, you are editing the *contents* of the list/dict - if you are not, the values *in* the list/dict won't change IF they themselves are not mutable Data types that are immutable: - strings - ints, floats, bool Data types that are mutable: - lists, dicts, sets - classes that we implement ourselves NoneType: - this is a data type (because that's convenient to test for) - it can only take the value None print(type(None)) """ # Next Time # --- # - APIs # - trivia games ?!?!