Lecture 11 Slides - Q1 Review

1 of 19

Quiz 1 Review

CS 110

2 of 19

Reminders and Announcements

  1. Quiz 1 on Wed (no Tutorial this week)
    1. Deadline for any grading issues (tutorials, MQs, etc. is the start of Q1). LOOK AT CANVAS NOW.
    2. Optional Review PM Review Session - TODAY (Monday) 6pm - Annenberg G01
      1. Submit questions for the review session here!
  2. Exercise 4 is focused on reading and giving feedback on programs rather than writing them
    • You’ll be assigned a peer review via Canvas this evening / early tomorrow. In the meantime, you can read Instructions on the Exercise 4 assignment page.

This Week

  • Monday - Q1 Review (In-Person MQ 7)
  • Wednesday - Quiz 1 in-class
  • Friday - Intro to Control Flow
    • Ex 4 due

3 of 19

Quiz 1 - Wednesday (2/4)

  • In-person here in the Auditorium.
  • Taken on the Lockdown Browser on your personal computer; details on how to set it up are on our Canvas page as well as on the Quiz 1 page.
  • If you have NOT tried the Lockdown Browser version of the Practice Quiz DO IT ASAP. YOU WILL NOT RECEIVE EXTRA TIME IF YOU COME TO CLASS WITHOUT IT SETUP.
  • Problems are closer to our mini-quiz and the canvas practice quizzes than to the HW problems
  • Covers everything up to and including today
  • Specifically won't emphasize memorization though the functions highlighted on the study guide you should know how to call without any other help
  • CHARGE YOUR COMPUTER THE NIGHT BEFORE.

4 of 19

Quiz 1 - Wednesday (2/4) Logistics

  • Please arrive at the auditorium on-time (there will be a hard deadline of 50 minutes; if you arrive late, that will eat into your time)
  • When you get here do not sit directly next to another person. There should be at least one seat between you and the closest person. You may use the Balcony.
  • As soon as you get seated, go ahead and open your computer and make sure you are connected to the EDUROAM network (we log IP addresses).
  • Have your Wildcard out and available to be scanned.
  • Open the Lockdown Browser application, and get logged into Canvas. The Quiz will automatically appear and enable at the start of your registered class time.
  • If you are found to be violating the academic honesty policy, you'll be asked to show your Wildcard to a proctor.
  • If at any time you receive an error saying you're not connected to the network. You need to raise your hand IMMEDIATELY and a proctor will come help you.

5 of 19

Variable Scope

5

6 of 19

Scope

  1. Scope — refers to the part of the program where a variable is accessible
  2. Global variable — a variable which is defined in the main body of a file (best avoided)
  3. Local variable — a variable which is defined inside a function. Not accessible from outside the function
    1. The parameter names in the function definition are local variables, with the caveat that their value comes from what we pass into the function when we call it.

6

7 of 19

Scope 1: Consider the following program...

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

7

8 of 19

Scope 1: Local variables cannot be accessed outside function

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

TAKEAWAY: If you want access to a value after the function ends, you have to return it. WE CAN TURN THIS INTO A GOOD PROGRAMMING PRACTICE.

8

local variable

greeting variable was local to the demo_1 function. Throws error.

9 of 19

name = 'Lindsay'

def demo_2(name):

print(name)

demo_2('Walter')

TAKEAWAY: Local variables take precedence over global variables. THIS IS A BAD PROGRAMMING PRACTICE

Scope 2: Which name will print to the screen?

9

Global variable

Parameters are local variables. �They take precedence over global variables

10 of 19

Review Time

10

Reviewing and having a hard time understanding what's going on? Try pythontutor.com to help visualize what's going on!

11 of 19

Operators - What's in x,y,z?

x = 0

y = 6

z = 2

x = z ** z

y = (y % 2) + 5

z = x - (y - 4) // 3

12 of 19

Sequences - Value and Type

some_stuff = [(8, 9, 8), (1, "b", 3), ("a", 6), [0, 7]]

result_a = some_stuff[2][0]

result_b = some_stuff[some_stuff[1][0]][1]

thing = 1

result_c = some_stuff[2 + 2 - thing]

13 of 19

Calling Functions - Values of x,y,z, pineapple

def never(a, b):

return a % b

def gonna(a, b=1.0):

return b + a

def give():

return 5

def you(a=5):

return a

def up(pizza):

print("pizza")

x = gonna(5)

y = never(3, x)

z = gonna(x, b=give())

pineapple = up("hello")

14 of 19

Lists - What gets outputted? (run in Python!)

favorite_colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue']

favorite_colors.append('Indigo')

favorite_colors.append('Violet')

favorite_color = favorite_colors.pop(2)

favorite_colors.append(favorite_color)

favorite_color = favorite_colors.pop(0)

favorite_colors.append(favorite_color)

print(favorite_colors) # what will be printed out?

15 of 19

Calling Functions (run in Python!)

1. colors = ["red", "pink", "purple", "orange", "teal", "blue"]

2.

3. def car(coordinates, size, color="brown"):

4. print(coordinates, color, size)

5. print(coordinates, color, size)

6. print(coordinates, color, size)

7.

8. car((10, 10), 50, color="blue")

9. car((500, 500), 80, color=colors[5 % 3])

10. car((100, 130), 60)

11. car((100, 100), color="yellow", 80)

12. car((354, 487), 10, color=colors[len(colors)])

13. car((100, 100), "blue", color=10)

16 of 19

Writing Functions

Write a function random_quote that has the following inputs:

  1. quotes (a list) - a required parameter

It should print a random quote from the list (Hint: You can assume you have already imported all the functions from the random module).

Example Function Call:

quotes = ["it's a me, mario", 'here we go!', 'mama mia!', 'wahoo!']

random_quote(quotes)

Example Output:

here we go!

17 of 19

Solution

def random_quote(quotes):

print(choice(quotes))

# Alternate Solution

def random_quote(quotes):

length = len(quotes)

print(randint(0, length -1))

18 of 19

Writing Functions

Write a reporter repeat_word that has the following inputs:

  • word (string) - a required parameter
  • n (int) - a required parameter
  • delimiter (string) - an optional parameter that defaults to a space (" ")

It should give back a string containing the specified word repeated n-times separated by the delimiter parameter.

Example Function Call:

result = repeat_word("banana", 10, delimiter='~')

print(result)

Example Output:

banana~banana~banana~banana~banana~banana~banana~banana~banana~banana

19 of 19

Example Solution

def repeat_word(word, n, delimiter=" "):

final_string = (word + delimiter) * (n - 1)

final_string = final_string + word

return final_string # NEEDS TO RETURN!