WorksheetsUnderstanding Python Functions
Total questions: 20
Worksheet time: 10mins
What is the correct syntax to define a function in Python?
create function_name(parameters)
def function_name(parameters):
function_name(parameters) =>
function function_name(parameters)
How do you call a function in Python after defining it?
Use the function name followed by parentheses.
Call the function using its variable name.
Execute the function by typing its name in quotes.
Invoke the function with square brackets.
What is the purpose of the return statement in a function?
To print output to the console
The purpose of the return statement in a function is to exit the function and return a value to the caller.
To create a loop within the function
To define a function's parameters
Can a function return multiple values in Python? If so, how?
Yes, a function can return multiple values in Python by returning them as a tuple.
Multiple values can be returned by printing them instead of returning.
You can return multiple values by using global variables.
A function can only return one value in Python.
What will happen if a function does not have a return statement?
The function will return None.
The function will return a random value.
The function will return an empty string.
The function will throw an error.
How can you assign the result of a function to a variable?
variable := functionName(arguments)
let variable = functionName(arguments)
variable = functionName(arguments);
functionName(arguments) -> variable
What is the difference between a function parameter and an argument?
A function parameter is a variable that holds the return value; an argument is the function itself.
A function parameter is the value passed to the function; an argument is the function's name.
A function parameter is part of the function definition; an argument is the value supplied to the function when called.
A function parameter is used only in recursive functions; an argument is used in all functions.
What is the output of the following code: def func(): return 5; print(func())?
10
5.0
None
5
How do you define a function that takes parameters in Python?
def function_name(parameter1, parameter2):
function_name: parameter1, parameter2
function_name(parameter1, parameter2) =>
function_name(parameter1; parameter2)
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
Which of the following is the correct way to add the number 4 to mylist?
mylist + 4
mylist.append(4)
mylist.append([4])
4.append(mylist)
The code snippet above outputs
error
None
NaN
-1
Assume the key is the student's id and the value is the grade. Which of the following code will get the grade of student id 102?
grades[102]
grades.get(102)
grades[2]
grades[3]
grades.key(102)
How are lists and tuples different in Python?
Lists can be modified after creation, while tuples cannot be modified after creation.
Lists and tuples are the same in Python
Lists and tuples cannot store different data types in Python
Tuples can be modified after creation, while lists cannot be modified after creation
What will be the output of the code given below?
# Code snippet starts
org = 'QuizOrbit'
print(org[-1:-6:-1])
# Code snippet ends
tibrO
Orbit
Quiz
QuizOrbit
A string is an _______ data type.
Mutable
Immutable
What is the output of the following code?
print(3 >= 3)
3 >= 3
True
False
None
What type of variable is used to store whole numbers in Python?
float
boolean
int
string
What is the output of the expression '5' + '3' in Python?
53.0
53
8
53.5
