#!/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? - a template for define objects - a blueprint for some sort of thing that we'd like to represent - we looked at the Professor class What is an object? - a specific instance of a class felix = Professor("Felix") # are the numbers in the list themselves objects? # in python, the answer is yes because EVERYTHING in python # is an object ls = [2, 17, 65, 17, 17] What is a class attribute? - variables that belong to a class - each instance has these variables - essentially traits of the class felix.name What is a method? - a function within a class - a function that belongs to a class - you call this function on an instance of a class # calling a Professor method felix.greet() # calling a list method answer = ls.count(17) # max is not a list method, it is a general # python function that does not belong to a specific class print(max(ls)) """ # 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 = [] # return self 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") felix = Professor("Felix") # we write methods like this felix.greet() felix.teach("DS 2000") print(felix.courses_taught) # What's with the self keyword????? # behind the scenes, what is python doing??? # python does this translation for you # so that the instance of the object actually gets # passed in as the first parameter, self Professor.greet(felix) Professor.teach(felix, "CS 4120") print(felix.courses_taught) print() # How familiar are you with pokemon? #A. very # B. somewhat # C. I've heard the name? # D. not """ Class Design: Pokemon ---- Attributes: - type (water, fire, grass) - str - health - int - attacks (tackle, growl, etc.) - list of strings - defense - int - name - str Methods: - run - use item - attack -> also a more specific for different moves? - sleep - evolve - tell us whether or not they are conscious """ """ 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 = [3, 5, 19] # [0, 1, 2] print(numbers) for num in numbers: print(num) num = num * 2 print(num) # there is no code reseting an item/value in the list # there is no code that looks like: # numbers[index] = new_value # you'd need a loop `for index in range(len(numbers)):` print(numbers) print() """ 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: # word is not a word, it is a letter 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} # this will loop by key through the dictionary for name in ages: print(name) name = name.upper() print(name) # if you have no code like dict[key] = new value # if you want to put in a new key # dict[new key] = value # if you want to remove a key # del dict[key] print(ages) """ 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: # we are resetting/updating the dictionary # dict[key] = new value # we have no code like # list[index] = new value # this is okay because dictionaries are mutable # row is the dictionary itself, not a copy row["name"] = row["name"].upper() print(data) """ 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 ?!?!