Jed Rembold
September 10, 2025
Examining the code to the right, what is the returned value if I
evaluate the expression func2(1,5)?
def func1(x):
for i in range(3):
x *= 2
return x + x
def func2(y, z):
A = func1(y+1) % 8
z, A = A + z, y
return z ** A
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/constant|||
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 |
| Code | Description |
|---|---|
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 radians to degrees |
math.radians(|||x|||) |
Converts 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
if statement is only run
if the condition is metA 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)
True or
FalseA = True
if A:
A = False
print(A)iffyif and
elseelif
statementelif chained
statements as you wantelif block will ever be
entered!if |||condition 1|||:
|||Run this code if condition 1 is true|||
elif |||condition 2|||:
|||Run this code if condition 1 is false|||
|||but condition 2 is true|||
elif |||condition 3|||:
|||Run this code if both condition 1 and|||
|||2 fail but condition 3 is true|||
else:
|||This code runs if all above|||
|||conditions fail|||
Python defines two types of operators that work with Boolean data: relational operators and logical operators
Relational operators compare values of other types and produce a
boolean
True/False
result:
== |
Equals | != |
Not equals | |||
< |
Less than | <= |
Less than or equal too | |||
> |
Greater than | >= |
Greater than or equal to |
Be careful! == compares two
values. A single = assigns a
variable. The odds are high you’ll use one when you meant the other at
least once this semester!
Logical operators act on Boolean pairings
| Operator | Description |
|---|---|
A and B |
True if both terms True, False otherwise |
A or B |
True if any term is True, False otherwise |
not A |
True if A False, False if A True (opposite) |
or is
still True if both options are
Truenot with and and
or
not A or BWhat value is printed when the code to the right runs?
TrueFalseA = 10
B = 4
C = 1
A *= B
if A > 40 and C != 10 % 4:
print(B+C)
else:
print(A < B or not (C == 10 // 4))
Example: if n=0, then the
x % n == 0 is never actually checked in the
statement
n != 0 and x % n == 0
since n != 0 already is
False and
False and anything is always
False
x % n == 0 statement would have errored out
if n=0We have already seen for loops in
Karel, but let’s expand on their use:
The more general syntax of a for loop
looks like:
for |||variable name||| in |||sequence|||:
|||code to loop over|||
|||variable name||| is a variable name
that will be assigned every value in the sequence over the course of the
loop|||sequence||| is any expression that
produces an object which supports iteration
range() fill this
rolerange() iterablerange() function handles
this and produces the needed iterable objectBe careful, the range function will stop
one step before the final stop value.
Providing just a stop argument:
for n in range(5):
print(n)Providing a start and stop:
for n in range(1,11):
print(n)Providing a start, stop, and step:
for n in range(10,0,-1):
print(n)