Announcements
Q: What’s going to be on the quiz?
A: Everything through the last homework
In-Class Exercises
- We’ll walk through the solutions
- Turn in on the submit server for credit
- Only open during the lecture
- Can’t turn in late
- Turn in one
.zip
with all the .py
files
- Unlimited attempts during lecture
In-Class Exercises
Create three files now:
Complete exercises in lecture \(\rightarrow\) zip & submit at end
Expressions
“Small units of computation”
# this is a comment
1 + 3
Printing
The print
function
# this is a comment
print(1 + 3)
Assignment
- Expression on right of
=
evaluated
- Variable on left of
=
gets result
Reassignment:
x = 1 + 3
print(x)
x = x * 2
print(x)
Operators
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
** |
Exponentiation |
// |
Quotient |
% |
Remainder |
Operators
They behave differently with text:
x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
Operators
They behave differently with text:
x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
Operators
They behave differently with text:
x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
Operators
They behave differently with text:
x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
Exercise 1
This code should:
- Add 3 and 4 and assign to
y
- Multiply
y
by 5
- Print out the current value of
y
- Then, add 1 to
y
- Print out the final
y
# Lecture 3 - In Class
y = 3 + 4
print(y * 5)
y = y + 1
print(y)
Submit on the submit server!
Functions
Anatomy of a print()
statement:
- The name
print
- Parentheses
()
- Argument - what is inside the parentheses
The print
function is special:
- It does something
- It doesn’t return anything
print
x = 2
print(x)
y = print(x)
print(y)
print
x = 2
print(x)
y = print(x)
print(y)
print
x = 2
print(x)
y = print(x)
print(y)
print
x = 2
print(x)
y = print(x)
print(y)
Function Returns
- Most functions return something after they run:
x = 5/4
print(x)
y = int(x)
print(y)
int
function called on 1.25
(a float) returns 1
(an int)
Types!
- Review Think Python Chapter 1 (1.5)
int
- integer, whole numbers
float
- numbers with decimal
str
- “string” - text information
- Functions
int
, float
, and str
convert between types
- Some conversions are “impossible”
- Function
type
tells us the type of a variable
Types
# Example 1
x = 2 + 2
y = float(x)
print(y)
# Example 2
a = 1 + 4
b = str(a) + str(a)
print(b)
# Example 3
s = "two"
t = int(s) + float(s)
print(t)
Defining Functions
def function_name():
print("The function does something!")
Function with argument:
def function_name(arg):
print("The argument is: " + str(arg))
Exercise 2
- Write a function
exercise_2
- Your function should take an argument
- Your function should print out
"Today is Monday"
on one line
- Your function should print out the argument (by itself) on the next line
Desired output:
Today is Monday
Hello!
Today is Monday
9
# Lecture 3 - In Class
def exercise_2( ): # you need to add an argument
# your code goes here
# you probably want to use two print statements
Submit on the submit server!
Function Return
- We usually want our functions to give us something back
Math:
\(f(x) = x + 1\)
Python:
Definition vs. Call
Definition:
def my_func(my_arg):
my_arg = my_arg + 1
return my_arg + 1
result = my_func(2)
print(result)
Execution Order
Code in the visualizer
def f(x):
return x + 1
a = 3
b = f(a)
print(b)
c = f(b)
print(c)
Exercise 3
- Write a function
exercise_3
- Your function should take an integer argument
- Your function should repeat the argument twice
2
repeated becomes 22
12
repeated becomes 1212
- Your function should return the result – an integer
Exercise 3
Desired output:
x = exercise_3(5)
print(x)
55
y = exercise_3(21)
print(y)
2121
(nothing is printed)
# Lecture 3 - In Class
def exercise_3( ): # you need to add an argument
return # write the statement to return here
Submit on the submit server!
Next Week
Reading: Think Python Chapter 5
Homework 3 due Sunday, 14 Sep 11:55 PM