Jed Rembold
February 1, 2023
Examining the code to the right, what is the output value if I
evaluate the expression func2(10)?
def func1(x):
if x > 10:
return 3 + x
else:
return 0
def func2(y):
A = func1(y+1)
if y % 2 == 0 or A ** 3 > 30:
A -= 5
return A + y
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 erred out if
n=0[, ]) with
commas separating each element of the sequence
['This', 'is', 'a', 'list']['Great', 4, 'storing', 5 * 10][
]
>>> A = [2, 4, 6, 8]
>>> print(A[1])
4
>>> B = "Spaghetti"
>>> print(B[6])
't'
+ operator concatenates sequences
+ will add two integers,
but will concatenate two strings>>> A = 'fish'
>>> B = 'sticks'
>>> print(A + B)
'fishsticks'
>>> A = [1, 'fish']
>>> B = [2, 'fish']
>>> print(A + B)
[1, 'fish', 2, 'fish']
The number of elements in a sequence is commonly called its
length, and can be given by the
len( ) function
Simply place the sequence you desire to know the length of between the parentheses:
>>> len("spaghetti")
9You can have sequences of 0 length as well!
>>> A = ""
>>> B = [ ]
>>> print( len(A) + len(B) )
0What would be the printed output of the code to the right?
A = "hots"
B = ["fire", A + A]
C = A[3] + A[1]
B += [C + C[0]]
D = B[0] + B[1] + B[2]
print(len(D))
inputTo retrieve data from a user, we can use Python’s built-in
input() function
The form will generally look like:
variable = input(prompt_text)
variable is the variable name you want
to assign the user’s typed input toprompt_text is the string that will be
displayed on the screen to communicate to the user what they should be
doingThe input() function always
returns a string
If you want to get an integer from the user, you will need to convert it yourself after retrieving it
num = int(input('Pick a number between 1 and 10: '))Python programs specify what part of the code is supposed to be executed when a program is run using a few special lines at the end 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
# File: AddTwoIntegers.py
"""
This program adds two integers entered by the user.
"""
def add_two_integers():
print("This program adds two integers.")
n1 = int(input("Enter n1? "))
n2 = int(input("Enter n2? "))
total = n1 + n2
print(f"The sum is: {total}")
# Startup boilerplate
if __name__ == '__main__':
add_two_integers()
Using a variable to track/control a loop state
finished = False
while not finished:
line = input("Enter a number: ")
if line == "":
finished = True
else:
print(line)Building up a sequence from nothing using concatenation
new = ""
word = "fantastical"
i = 0
while i < len(word):
new += word[i]
i += 2Most common is to use import to grab
everything in a library
import math
var = math.sqrt(4))Can also use from ... import 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 |