1 of 18

CSE 163

Section AX

TA 1 & TA 2

Question of the Day: What class are you most excited to take next quarter?

Or,

If you could add one class to your major that doesn’t exist, what would it be?

2 of 18

Important Dates and Reminders

  • Programming Practice #3 due today @ 11:59PM
  • HW3: Education & Section Check-In #4 due tomorrow (Friday, 7/17) @ 11:59PM
  • Project proposals due this Sunday (7/19) @ 11:59PM
    • Grades will release early Thursday after (graded on completion with comments)
    • No resubmissions!

Reminders

3 of 18

Office Hours?

4 of 18

Game Plan

What we’ve Learned so far:

  • Classes
    • self
    • Fields
      • Privacy with _field
    • Functions
    • Instances
  • Equality (Reference vs Identity)
  • Sorting

What we’ll cover today:

  • Type Annotations
  • Classes Review
  • objects-win26.ipynb

4

5 of 18

Type Annotations

6 of 18

Type Annotations

  • A way for other users to understand the expected types of parameters, variables, and outputs
  • [variable]: [type]
  • int, float, str, bool
  • Data structures:
    • list[<type>], set[<type>]
    • dict[<key type>, <value type>]
    • tuple[<type>, …, <type>]
  • Multiple types with | (the pipe)

def div(a: int, b: int | None) -> float:

if b is None:

return a / 2

else:

return a / b

def new_func(names: list[str]) –> None:

# This function’s parameter can take

# only strings in its list and returns

# None

first: str = names[0]

We can have inline type annotations!

7 of 18

String Formatting

(a.k.a. f strings)

8 of 18

String formatting

  • A way to format strings with nicer syntax.
  • f”some text { <code> }”

a = 3

b = 4

c = 5

f"{a}**2 + {b}**2 = {c}**2 = {c**2}"

“3**2 + 4**2 = 5**2 = 25”

animal_info = {"tom" : "cat", "jerry" : "mouse"}

f"tom is a { animal_info['tom'] }"

"tom is a cat"

Note the difference in which quote we use!

9 of 18

Classes Review

10 of 18

Defining a Class

  • A class encapsulates and abstracts data and logic that you can use in your programs
  • Contains
    • Initializer
    • Fields
    • Methods

class Student:

def __init__(self, number, name):

self._number = number

def __repr__(self):

...

def get_courses(self):

...

11 of 18

Classes vs. Objects

  • Objects are instances of classes
  • Classes are like blueprints; objects are like houses
  • Multiple objects can be instantiated from the same class

class Student:

def __init__(self, number, name):

...

def __repr__(self):

...

def get_courses(self):

...

new_student = Student(number, name)

12 of 18

Private vs. Public fields

  • Private fields are accessed only within a class
  • Public fields can be accessed by the user
  • Use underscores (_) in front of the names of private fields

class Student:

def __init__(self, number, name):

self.name = name

self._number = number

student1 = Student(123, “Adrian Salguero”)

student1.name # “Adrian Salguero”

13 of 18

Dunder methods

  • __init__
    • Called automatically when a new object is created. Used to initialize the object’s fields.
  • __str__
    • Defines how an object is represented as a string. Used when you print the object.
  • __getitem__
    • Allows objects to be indexed using square brackets. Called when you write something like obj[key]

Essentially, they are built-in functions that Python lets us write with convenient syntax

class Student:

def __init__(self, number, name):

self.name = name

self._number = number

def __str__(self):

return

f"Student({self.number},

'{self.filename}')"

student1 = Student(123, “Adrian Salguero”)

print(student1)

# Student(123, “Adrian Salguero”)

# Without __str__:

# <__main__.Student object at 0x105348290>

14 of 18

Practice Problems!

15 of 18

Section code:

16 of 18

Solutions – Course

17 of 18

Solutions – Type Annotations

18 of 18

Solutions – Major