Last time: Variables, types, defining functions
Today:
Exercise: What will this code do?
x = 1
y = 2
print(x,y)
y = x
x = y
print(x,y)
I actually wanted to swap x and y, how do I do it?
x = 1
y = 2
print(x,y)
z = x
x = y
y = z
print(x,y)
We also talked about the difference between local and global variables. For example.
def f(x):
zzz = 1
return x
zzz
We get an error because zzz is a local variable (inside the function) not global variable.
Will there be an error?
ggg = 1
def f(x):
return x+ggg
print(f(0))
No. No problem. Global variables can be used inside the function.
Now, what will the following code produce?
ggg = 1
def f(x):
ggg = 2
return x+ggg
f(0)
# What about this?
print(ggg)
Aha! Something funny is happening here. In fact, when we said ggg=2 inside the function, that ggg was a new local variable, not the ggg from outside the function. This is a safety measure that prevents your functions from messing up the values of your global variables.
If you really want to change the value, you use the global
keyword.
ggg = 1
def f(x):
global ggg
ggg = 2
return x+ggg
print(f(0))
print(ggg)
The global
keyword tells python that the ggg inside the function is actually a global variable, and not a new local variable that would disappear after the function is completed.
Let's import the library from the homework.
# importing graphfunction() and drawfunction()
import libmath9 as math9
def f(x):
return x**2
math9.graphfunction(f)
Now we want to make a partial function, e.g. $$f(x) = \left\{ \begin{array}{ll} 0 & \mbox{if } x < 0 \\ x & \mbox{otherwise } \end{array} \right.$$
(by the way this function is called the RELU function)
We need to use the if-else syntax
def relu(x):
if x > 0:
return x
else:
return 0.0 # note: it gets confused if we put 0 instead of 0.0
math9.graphfunction(relu)
Actually, we could have written it like this:
def relu(x):
if x > 0:
return x
return 0.0 # no need for else, because return will end the function anyway
math9.graphfunction(relu)
Like in the above example, you don't need to have an else every time there is an if.
def safe_div(x,y):
if y == 0:
print("Noooo. You are dividing by zero bruuuuh!")
return x/0.0000000000001
return x/y
safe_div(1,0)
0 < 1
1 < 0
type(1 != 1)
type(False)
So there is a type called bool
. It can have values True or False.
Exercise
Let's make this one:
$$f(x) =
\left\{
\begin{array}{ll}
0.5 & \mbox{if } 0 \leq x \leq 0.5 \\
0 & \mbox{otherwise }
\end{array}
\right.$$
def f(x):
if x >= 0:
if x <= 0.5:
return 0.5
return 0.0
math9.graphfunction(f)
Why not combine x >= 0
and x <= 0.5
. We need to and them:
def f(x):
if x >= 0 and x <= 0.5:
return 0.5
return 0.0
math9.graphfunction(f)
True and True
False and True
not True
True or False
Weird example: Life planner app
checking = 400
savings = 400
debt = 700
rich_relative = False
if checking + savings - debt < 200 and not rich_relative:
print("No more cold pressed juice for you!")
elif checking + savings - debt > 500 or rich_relative:
print("Ordering cold pressed juice and reserving VIP table at club...")
else:
print("Do what's in your heart.")
valid_day(year, month, day)
which tells you if a day exists or not.is_last_digit_prime(n)
which will return True if the last digit of n is prime