Jed Rembold
February 13, 2023
When the final line of the code to the right is run, what type of
variable is x?
integerfloatstringdef func(A):
m = str(A)
n = m + m + m
print(n)
y = 5.0
x = func(y)
print(type(x))
So far we have used a positional way to assign arguments to parameters
>>> def func( first, second, third ):
print( first, second, third )
>>> func(1,2,3)
1 2 3
>>> func(2,6,4)
2 6 4
Arguments may also be specified by a keyword, in which the caller precedes the argument with a parameter name and equals sign
Always stores the argument value in the specified parameter
>>> def func( first, second, third ):
print( first, second, third )
>>> func(third=4, first=2, second=6)
2 6 4Keyword arguments can appear in any order
All keyword arguments must come after any positional arguments!
Python allows you to specify a default value for a parameter, which is will use if one is not directly supplied
Do so by adding an equals sign and a value after the parameter name
def introduction(name='Jed', age=37):
print('My name is ', name, ' and I am ', age)
If, when calling a function, you are providing any further arguments after a default parameter, you must indicate them through keywords
>>> introduction()
My name is Jed and I am 37
>>> introduction('Bob', 25)
My name is Bob and I am 25
>>> introduction('Larry')
My name is Larry and I am 37
>>> introduction(age=68)
My name is Jed and I am 68
You can return any type of variable
from a function, including GObject graphical
objects
Can be useful to write simple functions that bundle together common tasks
For instance, to create a filled circle centered at some location:
def make_filled_circ(x_cent, y_cent, radius, color='black'):
circle = GOval( x_cent-radius, y_cent-radius,
2*radius, 2*radius)
circle.set_color(color)
circle.set_filled(True)
return circlereturn any type of variable from
a function, including GObject graphical
objectsimport so generally don’t want extraneous
print statements or to be running any code directlyimport the library infrom pgl import GRect, GLabel
import random
def create_filled_rect(
x_cent, y_cent, width, height, fill_col='black', border_col=None
):
"""
Creates a GRect object with the desired fill color.
If a border color is specified, also draws the
border in the desired color.
"""
rect = GRect(x_cent-width/2, y_cent-height/2, width, height)
rect.set_filled(True)
if border_col is None:
rect.set_color(fill_col)
else:
rect.set_color(border_col)
rect.set_fill_color(fill_col)
return rect
def random_color():
"""
Returns a random opaque color as a hex string.
"""
color = "#"
for i in range(6):
color += random.choice("0123456789ABCDEF")
return color
def create_centered_label(x_cent, y_cent, text, font=None):
"""
Creates a GLabel object and centers it on the coordinates
x_cent and y_cent.
"""
label = GLabel(text)
if font is not None:
label.set_font(font)
label.set_location(x_cent - label.get_width() / 2,
y_cent + label.get_ascent() / 2 )
return label
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
Consider the code to the left. When the final value of
x is printed, what will its value be?
NoneVegas stays in
Vegas…We’ll annotate the stack frames by hand as the earlier code runs:
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
return, compute the return value and
substitute that value in place of the function callRiddle me this. What would be the printed value of z at the end of the code to the right?
def f(x,y):
z = (x + 3) ** 2
return y + z
x = 1
z = x + f(y=x,x=2)
print(z)
abs,
str, print,
etc.def func1(x,y):
return z + func2(x,y)
def func2(x,y):
def func3(x):
return (y + x) ** 2
z = x - func3(y)
return z - y
z = 1
print(func1(2,z))
In Python, assigning any value to a variable means that the variable is assumed to be local
Can lead to issues though:
def increment():
x = x + 1
x = 0
increment()There are a few ways to address this, but we’ll focus on one in particular when it comes to PGL