Python Exam 2 Flashcards
parameter
value passed into a function by its caller
def <name>(<name>):
<statement(s)></name></name>
multiple parameters
a function can accept multiple parameters.
Separate each parameter by a comma
When calling has to have every parameter needed
value semantics
When numbers and strings are passed as parameters, their values are copied
Modifying the parameters will not affect the variable passed in
math.ceil(value)
rounds up
math.floor(value)
rounds down
math.log(value)
logarithm
requires import math
math.sqrt(value)
square root
requires import math
math.sinh(value)
math.cosh(value)
math.tanh(value)
hyperbolic sine/cosine/tangent of an angle in radians
requires import math
math.degrees(value)
math.radians(value)
convert to degrees and radians and back
requires import math
e
2.7182818
requires import math
pi
3.1515926
requires import math
abs(value)
absolute value
min(value1, value2)
smallest of 2 values
max(value1, value2)
largest of 2 values
round(value)
nearest whole number
math functions
imply calling these functions produces not visible result
Math function calls use a python feature called return values that cause them to be treated as expressions
The program runs the function, computes the answer, and then replaces the call with its computer result value
To see the result, we must print it or store it in a variable
return
to send a value as the result of a function call
why return and not print
we can compute several things before printing
what does import random do?
it imports the random module
random functions taught in class
random.random()
random.randint(min,max)
random.random()
returns a random float in the range [0.0, 1.0)
random.randint(min,max)
returns a random int in the rand [min, max)
strings
a type that stores a sequence of text characters
can concatenate strings using +
can multiply strings using *
can print strings and get them as input from the command line
index
characters of a string are numbered with 0-based indexes
first character’s index:0
Last character’s index: 1 less than len of string or -1
accesssing characters in a string
string[index]
accessing sub strings
part = string[start:stop]
in this string name = “Mary”, how do you print “ar” ?
mid = name[1:3]
print(mid)
in this string name = “Mary”, how do you print “Mar” ?
mid = name[:3]
print(mid)
in this string name = “Mary”, how do you print “ary” ?
mid = name[1:]
print(mid)
find a string
find(str)
returns index of where found (first entry only)
returns -1 if not found
lower()
a new string with all lowercase letters
upper()
a new string with all uppercase letters
len(string)
the length of the string
endswith(str)
whether a string ends with a specified substring. case sensitive
startswith(str)
whether a string starts with a specified substring
case sensitive
islower()
whether all the characters in the string are lowercase
isupper()
whether all the characters in the string are uppercase
isalpha()
whether the string contains only alphanumeric characters
isdigit()
whether the string contains only digits
f-strings
build formatted strings by embedding expressions into strings
ex: print(f”Hello, {name}”) with name being a variable
ASCII values
char values assigned to numbers internally by the computer
ord
ordinal value. the number version of a letter
chr
character value. the letter corresponding to a number