Module 2 Flashcards
A function in a programming language:
Causes some effect:
–> like printing to the terminal ( print() )
–> finding the length of an object ( len() )
Evaluates a value
–> abs() = returns the absolute value of a number
function argument(s)
Arguments are the actual values that get passed on to the function THROUGH its parameters
ex: the print() function can take multiple arguments
print( “this”, end=” ,”)
function parameter
the variable listed inside the parentheses of a function definition
i.e.:
def myFunc(a: str, b: str) –> str:
return a + b
The function above takes two string arguments (values) and concats them to return a concatinated string value
the variables “a” and “b” in the definition above are the function’s parameters
What happens during a function invocation?
1 - Python checks if the name is legal (and isn’t already assigned to an existing function)
2 - checks if the number of arguments provided match the number required
3 - it passes your provided arguments into the function
4 - it executes the code, causes the desired effect and finishes its task
5 - it returns to your code (to the place just after function invocation) and resumes its execution
What kind of error does this tiny script throw?
print(Dave)
NameError - because the object “Dave” isn’t defined anywhere in the code
What kind of error does this throw?:
print “Dave”
SyntaxError
– > since the print() function isn’t built correctly it throws this error
For Python, how many instructions can one line contain?
One
The print() function begins its output from a _______ line each time
new
What does a print() function without arguments create?
A newline
“\n”
what is the backslash called when used inside strings?
the escape character
what does “\n” mean?
newline character which urges the console to start a new output line
The print() function puts a ______ between the outputted arguments as default separator
space
What is “the positional way” of passing arguments into a function
The position of each argument corresponds to each parameter it is passed on to.
For example:
def fuBar( a: str, b: int) -> str:
return a * b
and calling the function:
print(fuBar( 2 , “goose”))
Would result in an error because passing the number 2 in the first position when we should have passed the str, “goose”
The argument in position one only accepts a str not an int.
keyword argument
an argument that can be assigned a value and are identifiable within that function by the specific names they have
they have three elements
a keyword
an equal sign (an assignment operator)
and a value assigned to the argument
keyword arguments have to be put after the ______ positional argument
last
What does the “sep” keyword argument do in the print() function?
It separates the internal string by the value it is assigned.
print(“this”, “is”, “a”, “string”, sep=”_”)
becomes:
this_is_a_string
Built in functions are
always available and don’t have to be imported
Python 3.8 comes with _____ built-in functions
69
How to invoke a function (call a function)
use the function name, followed by parentheses
pass arguments to the function by placing them inside the parentheses
Python strings are delimited with _____
quotes or apostrophes (must be the same on either side of the str)
So “string” or ‘string’ not “string’
computer programs are collections of _________
instructions
If a number is preceded by a 0o or 0O what type of value is it?
Octal
If a number is preceded by a 0x or 0X what type of value is it?
Hexadecimal
What will the following code print?
And why?
print(“hello “ ‘world ‘ “it “ ‘is ‘ “I”)
hello world it is I
- Python will automatically concat strings that are next to each other