Functions Flashcards
A block of code designed to perform a specific task
Function
Every function contains two pieces - what are they called?
Header & body
What does the keyword ‘return’ tell Python?
We are about to exit a function
A header specifies what?
Name of function & its arguments
The body specifies what?
The work that the function does
Every function header begins with what?
def
For every function, the parenthesis enclosing the function argument(s) must be followed by what
A colon :
Every line of code must be indented by how many spaces?
Four
What is the final line of code called?
Return statement
Running the function is the same as saying what?
Calling the function
What is the goal when coding?
Write as little as possible
What function would you use if you forgot what something does?
Help()
e.g help(round)
When you write a function and provide a description what is that called?
Docstring
When you call help() on a function, what will it show?
Docstring
Good programmers use what?
Docstrings
What does this mean ‘\n’?
New line
What does sep do?
It adds a separator between strings to be printed
What does the keyword “pass” do?
Literally nothing. It’s a placeholder bc after we begin a code block, Python requires at least one line of code
What does ndigits=-1 round to?
The nearest 10
What does ndigit=-2 round to?
The nearest 100
How can you comment & uncomment all at once?
Highlight then press ctrl + /
What is the purpose of a function?
Allows to not have to continually write code over and over again
What is a way to store code that should only run when your file is executed as a script
if __name__ == “__main__”
This allows you to execute code when the file runs as a script, but not when it’s imported as a module
if __name__ == “__main__”
What is the difference between returning and printing?
When you return something, you can set it equal to another variable and that way you call the results in that variable
Printing is just printing the value