Abstraction and Programming Practice
CS 110
Reminders and Announcements
Next Week
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
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:�
4
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
Functions
Blocks of code encased inside a function name
Dealing with inputs to functions
Positional parameters
Keyword parameters
Calling Functions - Summary
print("Go", "Wildcats", sep="", end="!!!\n")
Function name
Positional (non-optional) Arguments
Keyword (Optional) Arguments
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
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
Calling Functions - Summary
result = input("What is your name? ")
Variable to store the reported data into
Recall: The Big Ideas with Functions...
12
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
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)
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
Dealing with inputs to functions
Positional parameters
Keyword parameters