Lecture 8 Slides - Writing Reporters + Modules

1 of 28

functions

Please Don't Destroy

(our data)

CS 110

2 of 28

Reminders and Announcements

  • Ex 3 due this Friday - do not wait till the end of the week to start it

This Week

  • Monday - Reporters and Modules
  • Wednesday - (Pre-Recorded + MQ 5) Sequences of Data + Tutorial 3
  • Friday - Scope + MQ 6
    • Ex 3 due

3 of 28

def name_of_function(parameters):

statement(s)

3

Keyword def marks the start of function header

A function name to uniquely identify it (snake case)

parameters (the way we pass data to a function)

colon (:) marks end of function header

function body has 1 or more statements, which have same indentation level (usually 4 spaces)

A function with inputs

4 of 28

The squareface function Challenge

Inputs:

center_x (int) - center x-coordinate

center_y (int) - center y-coordinate

width (int) - width of the face

eye_color (str, optional) - (defaults to blue)

face_color (str, optional) - (defaults to gold)

Output:

A squareface (similar to the one on the right hand of the screen)

4

5 of 28

The squareface function Challenge

Inputs:

center_x (int) - center x-coordinate

center_y (int) - center y-coordinate

width (int) - width of the face

eye_color (str, optional) - (defaults to blue)

face_color (str, optional) - (defaults to gold)

Output:

A squareface (similar to the one on the right hand of the screen)

Eyes should be 1/20 the width of the face; 1/10 the height of the face, offset by ⅕ the width of the face in both directions.

5

6 of 28

Our Function Writing Experience so Far:

So far we have only written functions that are DESTRUCTIVE.

def say_hello(name, time_of_day):

print('Hello there,' + name + '!')

print('How are you this ' + time_of_day + '?')

say_hello(“Connor”, “Morning”)

square(100, 100, 50, color="blue")

square_face(100, 100, 200, eye_color=”blue”, face_color=”green”)

7 of 28

Our Function Writing Experience so Far:

So far we have only written functions that are DESTRUCTIVE.

def say_hello(name, time_of_day):

print('Hello there,' + name + '!')

print('How are you this ' + time_of_day + '?')

make_square(the_canvas, 100, 100, 50, color="blue")

8 of 28

Writing Reporter Functions

Remember, reporters are functions that give us back some value so that we can use it later. While we call reporter functions the same way as all functions, we have to take the extra step of either storing or using the piece of data it gives us back.

result = input("Hello, what is your name? ")

9 of 28

Writing Reporter Functions

Remember, reporters are functions that give us back some value so that we can use it later. While we call reporter functions the same way as all functions, we have to take the extra step of either storing or using the piece of data it gives us back.

result = input("Hello, what is your name? ")

10 of 28

def name_of_function(parameters):

statement(s)

10

Keyword def marks the start of function header

A function name to uniquely identify it (snake case)

parameters (the way we pass data to a function)

colon (:) marks end of function header

function body has 1 or more statements, which have same indentation level (usually 4 spaces)

A function with inputs

11 of 28

def name_of_function(parameters):

statement(s)

return some_variable

11

Keyword def marks the start of function header

A function name to uniquely identify it (snake case)

parameters (the way we pass data to a function)

colon (:) marks end of function header

function body

A reporter function with inputs

"Report" or Return Statement (note, return is NOT a function, it's a keyword like def)

12 of 28

For example…

Here's a function called get_square_area.It has one required input named height. It returns a calculated area to be used later in the program.

def get_square_area(height):

area = height * height

return area

# Invoking the function

result = get_square_area(5)

print(result)

# Say we had 15 of the same square, well then…

print("Area of 15 of those bad bois:", result * 15)

13 of 28

Modules

13

14 of 28

  • A module is a file containing Python definitions and statements intended for use in other Python programs
  • There are many Python modules that come with Python as part of the standard library (e.g. random, tkinter, datetime)
  • There are also third-party modules you can install using a command line tool called PIP (we’ll talk about that in a future lesson)
  • You can also make your own!

14

What is a Module?

15 of 28

  • To use a module, you need to import it (we’ve already seen example of this when we use tkinter).
  • Importing a module is how you tell Python where to go to find a particular set of functions.
  • How do you find out what functions a module has available?
    • Use the documentation (google is your friend)!
      • Official
      • Or Unofficial

from module_name import function_name

15

Using modules

16 of 28

from random import randint

num_1 = randint(0, 100)

num_2 = randint(0, 100)

num_3 = randint(0, 100)

print(num_1, num_2, num_3)

16

Example: importing a specific function

17 of 28

from random import randint, choice

num_1 = randint(0, 100)

num_2 = randint(0, 100)

num_3 = randint(0, 100)

print(num_1, num_2, num_3)

print(choice("hello?"))

17

Example: Importing more than one thing!

18 of 28

from random import randint, choice

num_1 = randint(0, 100)

num_2 = randint(0, 100)

num_3 = randint(0, 100)

random_colors = [“red”, “green”, “blue”]

one_random_color = choice(random_colors)

18

Example: Importing more than one thing!

Spoilers for Wednesday!

19 of 28

Classes

19

20 of 28

Review: Data Types

3.156

"Go Wildcats!"

15

21 of 28

Review: Data Types

3.156

"Go Wildcats!"

15

float

string

int

22 of 28

Review: Data Types & Variables

magic_pi = 3.156

slogan = "Go Wildcats!"

num_people_awake = 15

23 of 28

A way to define new compound types of data

To define new types of data in programming, we use a Class which…

  • gives this new type of object – name
  • defines the features of this type of object – properties
  • defines the behaviors of this type of object – methods

You can think of a Class as a blueprint that describes the nature of some type of data.

  • We can then create instances of that class

24 of 28

For example…

Say we want to represent a DOG in our programs:

  • What should we use as its name?
  • What are some properties of a dog?
  • What are some methods (behaviors) of a dog?
  • What about an instance of a dog?

25 of 28

For example…

Say we want to represent a DOG in our programs:

  • What should we use as its name?
    • Dog (we capitalize custom class names)
  • What are some properties of a dog?
    • name, breed, fur_color, slobberiness
  • What are some methods (behaviors) of a dog?
    • bark, walk, chase, run
  • What about an instance of a dog?
    • air_bud = Dog("Air Bud", "Golden Retriever", …)

26 of 28

For example…

Say we want to represent a DOG in our programs:

  • What should we use as its name?
    • Dog (we capitalize custom class names)
  • What are some properties of a dog?
    • name, breed, fur_color, slobberiness
  • What are some methods (behaviors) of a dog?
    • bark, walk, chase, run

27 of 28

Right now, we don't need to be able to make our own classes

  • We'll do it later – I promise.
  • The reason we need to know about this now is that the data types we've been using (and will use) are examples of Classes.
  • Which means they have properties and methods we can use whenever we want!
  • We can call these methods via a specific incantation:
    • instance_of_class.method_name(...)

28 of 28

str (the String class) - all methods

"hello".capitalize()

"hello".upper()

"HELLO".lower()

"hello".title()

test_string = "lOl"

test_string.upper()

test_string.swap()

test_string.center(20)

test_string.center(20).strip()

test_string.rjust(20)

txt = " banana "

x = txt.strip()

print("of all fruits", x, "is my fave")