01. Document and Structure Code 1 Flashcards
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.
- In the context of a function, what is its correct name?
- When you call a function and pass a value, what is its correct name?
def hello(name):
print(‘Hello ‘ + name)
hello(‘Alice’)
hello(‘Bob’)
- A parameter = the variable inside the function.
- An argument = the value passed in the function call.
Describe what is happening with the following code example.
def plusOne(number):
return number + 1
newNumber = plusOne(5)
print(newNumber)
- The function, plusOne is being called and passed the value of 5. This is the argument.
- The functions parament ‘number’ is then assigned the value of 5.
- The return statement then adds 1 to this parameter resulting in the value of 6.
- The value of 6 is then returned and assigned to the variable ‘newNumber’
- The print statement will then print the value of 6.
What does the end= keyword do in python?
print(‘Hello’, end=”?”)
print(‘World’)
- By default, the print statement in python is set with the newline character.
- This can be suppressed using the end= keyword.
- So in the example above, the two words would print on the same line and only be separated by the question mark, i.e. Hello?World.
What does the sep keyword do in python?
print(‘cat’, ‘rat’, ‘dog’ , sep=’ABC’)
- For print statements, the separator character defaults to a single space character.
- This can be changed using sep=
- Using the example of print(‘cat’, ‘rat’, ‘dog’ , sep=’ABC’)
- The print statement output would be catABCratABCdog
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.