Explain the main purpose of using a function in a program.
The main purpose of functions is to group code that is executed multiple times.
The function below makes use of the ‘name’ variable.
def hello(name):
print(‘Hello ‘ + name)
hello(‘Alice’)
hello(‘Bob’)
Describe what is happening with the following code example.
def plusOne(number):
return number + 1
newNumber = plusOne(5)
print(newNumber)
What does the end= keyword do in python?
print(‘Hello’, end=”?”)
print(‘World’)
What does the sep keyword do in python?
print(‘cat’, ‘rat’, ‘dog’ , sep=’ABC’)
What scope do the following variables have?
spam = 42
def eggs():
spam = 42
print(‘Anything’)
spam = 42 # global variable
def eggs():
spam = 42 # local variable
print(‘Anything’)
What type of code should be indented?
Anything inside of a routine, such as a function, loop, or decision should be indented.
What symbol is used to insert a comment into python?
The pound or hash symbol, i.e. #
What is the function of the following command
C:\Users\admin>python -m pydoc pass
In Python, Pydoc is a help/documentation module. You can use this on your Windows terminal to understand what a function in python does.
What does the def keyword do in Python?
The def keyword means define, or in other words, “I’m starting a function”.
Is python picky about where functions are written?
Yes, python functions must be written before they are called.