Jed Rembold
September 8, 2025
It is very common to want to adjust an existing variable value
balance = balance + depositPython gives you a shorter expression to describe this relationship:
balance += depositYou can do this with any operation (op) following the general form:
variable op= expressionYou can name multiple variables at once by separating with commas
A, B, C = 1, 2, 3
python (or however you access Python on your
system)
>>>exit()What is the printed value of A in the
code below?
>>> A = 10
>>> B = 5 % 3
>>> C = A * B ** B
>>> A -= B + A // 2
>>> A, B, C = C, A, B
>>> A
??
.py. Such files are called
modules.printWe’ve seen how to output the value of a variable from within Python’s REPL, but that won’t work within a program
Python’s built-in print() function
will display whatever is between the () to
the terminal
>>> A = 10
>>> print(A)
10If you want to display several things, separate each thing by a
comma inside the print statement. This will
insert a space between each when printed.
>>> print(1,3,5)
1 3 5
The more general form of a function definition looks like:
def |||name|||(|||parameters|||):
|||statements in function body|||
|||name||| is your
chosen name for the function|||parameters||| is a
comma-separated list of variable names that will have
each input value assigned to themYou can return or output a value from the function by including a return statement in the function body
return |||some expression|||
|||some expression|||
is the value you want to return or outputreturn statement
is included, Python will by default return
None
Convert Fahrenheit temperatures to their Celsius equivalent
def f_to_c(f):
return 5 / 9 * (f - 32)
Using the function:
print(f_to_c(45))Computes the volume of a cylinder of height
h and radius
r
def cylinder_volume(r, h):
return 3.14159 * r**2 * h
Using the function:
vol1 = cylinder_volume(2,10)
vol2 = cylinder_volume(10,2)
print(vol1 + vol2)| Function | Description |
|---|---|
abs(x) |
The absolute value of x |
max(x,y,...) |
The largest of all the arguments |
min(x,y,...) |
The smallest of all the arguments |
round(x) |
The value of x rounded to the nearest integer |
int(x) |
The value of x truncated to an integer |
float(x) |
The value of x as a decimal |
Most common is to use
import |||lib||| to grab everything in a
library
import math
var = math.sqrt(4))Can also use
from |||lib||| import |||function||| to grab
specific functions from the library
from math import sqrt
var = sqrt(4))math definitions| Code | Description |
|---|---|
math.pi |
The mathematical constant \(\pi\) |
math.e |
The mathematical constant \(e\) |
math.sqrt(x) |
The square root of x |
math.log(x) |
The natural logarithm of x |
math.log10(x) |
The base 10 logarithm of x |
math.sin(x) |
The sine of x in radians |
math.cos(x) |
The cosine of x in radians |
math.asin(x) |
The arcsin of x |
math.degrees(x) |
Converts from radians to degrees |
math.radians(x) |
Converts from degrees to radians |
Wherever possible, Python runs code one line at a time, from top to bottom
It will not, however, enter indented blocks unless it has to
A common paradigm then is to define functions first, and then include the code to run those functions beneath
def func1(a):
return a + 1
def func2(b):
return b // 2
print(func1(2) + func2(2))Karel programs always run the first defined function in the code when the program was run. General Python programs do not follow the same convention.
Python programs need to specify what part of the code is supposed to be executed when a program is run if everything is just function definitions. This commonly happens at the bottom of the program:
if __name__ == '__main__':
|||function_to_run()|||
|||function_to_run||| is the name of
whatever function you want to execute when the program is run
directlyPatterns of this sort are commonly called boilerplate
def print_odds(des_num):
"""
Prints the first desired number of odd numbers
starting from 1.
"""
value = 1
for i in range(des_num):
print(value)
value += 2
if __name__ == '__main__':
print_odds(100)
quad which takes 3
arguments a,b,c and returns the greater of
the two solutions (the \(+\) one)