Lecture 5 Slides - Programming Practice & Abstraction

1 of 16

Abstraction and Programming Practice

CS 110

2 of 16

Reminders and Announcements

  • Ex 1 due tonight (very similar to Tutorial but with more specific instructions)
    • Don't forget about edSTEM! (But don't use it as a last-minute lifeline)
    • You can re-submit up to 10 times - the latest submission is considered the "official" one
  • If you're watching the recording, make sure to complete MQ 2 by Sunday at 11:59pm
    • If you're here in class follow the MQ instructions and you'll receive credit around noon today
  • All submitted work is graded - if you don't have a grade for the Tutorial but you attended, see the pinned post on edSTEM.

Next Week

  • Monday - MLK Jr. Day - NO CLASS
  • Wednesday - Graphics Functions (Pre-Recorded + MQ 3) + Tutorial 2
  • Friday - Programming Practices + MQ 4 (Exercise 2 due)

3 of 16

Naming Variables: Case Sensitivity

Python is case-sensitive.

Consider the following code: https://goo.gl/PJQana

x = 1

x = 2

X = 5

Y = 2

Y = 5

y = 7

3

4 of 16

Naming Variables: Class Conventions

Although Python doesn’t care what you name your variables (beyond the rules specified in the previous slide), there are some conventions that most people follow:�

  • Ensure your variable names are mnemonic
  • “Snake case” for functions and variable names
    • first_name = 'Beyonce'
    • last_name = 'Knowles'
  • “Snake case” for file names too: hello_world.py

4

5 of 16

Writing programs that mean something (mnemonic)

x = 65�y = 3�z = x * y�print(z)

rate = 65�time = 3�distance = rate * time�print(distance)

5

6 of 16

Functions

Blocks of code encased inside a function name

7 of 16

Dealing with inputs to functions

Positional parameters

  • Positional parameters are non-optional (required)
  • The order in which they are passed matters

Keyword parameters

  • Keyword parameters are always optional and are always specified by their keyword
  • They are always passed into a function after the positional parameters, but the order of keyword parameters doesn’t matter.
  • Keyword parameters always have a default value

8 of 16

Calling Functions - Summary

print("Go", "Wildcats", sep="", end="!!!\n")

Function name

Positional (non-optional) Arguments

Keyword (Optional) Arguments

  • Function calls always consist of the function name and a set of parentheses.
  • Inside the parentheses go any inputs to the function. If there are multiple inputs, we separate them with a comma.

9 of 16

Turtles!

# Consider the following program:

left_turn(fred, 45)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

Note: all of these are in the lecture files for today if you'd like to run them in IDLE

9

10 of 16

Turtles!

# Consider the following program:

left_turn(fred, 45)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

left_turn(fred, 90)

forward(fred, 100)

Let's modify this code to take in a side length from the user typing something in!

10

11 of 16

Calling Functions - Summary

result = input("What is your name? ")

Variable to store the reported data into

  • Some functions are reporter functions that return a value. If a function is reporting some data back to us, we usually want to store it in a variable so we can access that data later
  • Not all functions are reporters. For example, let's see if the print function reports any data back to us…

12 of 16

Recall: The Big Ideas with Functions...

  • Functions are a way of encapsulating, organizing, and reusing code
  • They help us “abstract” away complexity and allow you to reason about your code at a higher level:
    • You may not want to know exactly what steps the turtle will take to make the square…you just want to know that it CAN.
  • They allow you to perform the same operations on different data values — also called arguments.

12

13 of 16

def name_of_function():

statement 1

statement 2…

13

Keyword def marks the start of function header

A function name to uniquely identify it (snake case)

colon (:) marks end of function header

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

A function with no inputs

14 of 16

For every single input (or parameter) our function accepts, we will define a variable for the function to use to store that input.

(Small vocabulary note: when we talk about what inputs a function accepts, we call those inputs parameters but when we talk about the actual pieces of data we give the function as inputs, we call them arguments)

15 of 16

def name_of_function(parameters):

statement(s)

15

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

16 of 16

Dealing with inputs to functions

Positional parameters

  • Positional parameters are required
  • The order in which they are passed matters

Keyword parameters

  • Keyword parameters are always optional and are always specified by their keyword
  • They are always passed into a function after the positional parameters, but the order of keyword parameters doesn’t matter.
  • Keyword parameters always have a default value