Bird
Raised Fist0
Pythonprogramming~20 mins

Function definition and syntax in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a function with default and keyword arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", greeting="Hi"))
AHello, Alice!\nHello, Bob!
BHello, Alice!\nHi, Bob!
CHi, Alice!\nHi, Bob!
DError: missing required positional argument
Attempts:
2 left
๐Ÿ’ก Hint
Check how default arguments work and how keyword arguments override them.
โ“ Predict Output
intermediate
2:00remaining
Return value of a function with multiple return statements
What is the output of this code?
Python
def check_number(x):
    if x > 0:
        return "Positive"
    elif x == 0:
        return "Zero"
    else:
        return "Negative"

print(check_number(-5))
ANegative
BZero
CPositive
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Look at the input value and which condition it satisfies.
๐Ÿ“ Syntax
advanced
2:00remaining
Identify the syntax error in function definition
Which option contains a syntax error in the function definition?
Adef add(a, b):\n return a + b
Bdef divide(a, b):\n return a / b
Cdef multiply(a, b)\n return a * b
Ddef subtract(a, b):\n return a - b
Attempts:
2 left
๐Ÿ’ก Hint
Check for missing colons in function headers.
โ“ Predict Output
advanced
2:00remaining
Output of a function using *args and **kwargs
What is the output of this code?
Python
def info(*args, **kwargs):
    return f"Args: {args}, Kwargs: {kwargs}"

print(info(1, 2, 3, name='Alice', age=30))
AArgs: (1, 2, 3), Kwargs: {'name': 'Alice', 'age': 30}
BArgs: [1, 2, 3], Kwargs: {'name': 'Alice', 'age': 30}
CArgs: (1, 2, 3), Kwargs: [('name', 'Alice'), ('age', 30)]
DArgs: 1, 2, 3, Kwargs: name='Alice', age=30
Attempts:
2 left
๐Ÿ’ก Hint
Remember that *args collects positional arguments as a tuple and **kwargs collects keyword arguments as a dictionary.
๐Ÿง  Conceptual
expert
3:00remaining
Effect of mutable default arguments in function definitions
What will be the output after running this code?
Python
def append_item(item, lst=[]):
    lst.append(item)
    return lst

print(append_item(1))
print(append_item(2))
A[1]\n[2]
BError: mutable default argument
C[1]\n[1]
D[1]\n[1, 2]
Attempts:
2 left
๐Ÿ’ก Hint
Think about how default mutable arguments behave across multiple function calls.

Practice

(1/5)
1. What keyword do you use to start defining a function in Python?
easy
A. function
B. define
C. func
D. def

Solution

  1. Step 1: Recall Python function syntax

    In Python, functions are defined using the keyword def.
  2. Step 2: Compare options

    Only def uses the correct keyword def. Others are not valid Python keywords.
  3. Final Answer:

    def -> Option D
  4. Quick Check:

    Function definition starts with def [OK]
Hint: Remember: def starts every function in Python [OK]
Common Mistakes:
  • Using 'function' instead of 'def'
  • Using 'func' or 'define' which are not Python keywords
2. Which of the following is the correct syntax to define a function named greet that takes no parameters?
easy
A. def greet():
B. function greet():
C. def greet[]:
D. def greet:

Solution

  1. Step 1: Check function header syntax

    The correct syntax uses def, function name, parentheses for parameters, and a colon.
  2. Step 2: Validate each option

    def greet(): matches this exactly: def greet():. Others have wrong keywords, brackets, or missing parentheses.
  3. Final Answer:

    def greet(): -> Option A
  4. Quick Check:

    def + name + () + : is correct syntax [OK]
Hint: Function name + () + : after def is always needed [OK]
Common Mistakes:
  • Omitting parentheses after function name
  • Using square brackets instead of parentheses
  • Using 'function' keyword instead of 'def'
3. What will be the output of this code?
def add(x, y):
    return x + y

print(add(3, 4))
medium
A. 7
B. 34
C. TypeError
D. None

Solution

  1. Step 1: Understand function behavior

    The function add takes two numbers and returns their sum.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Return sends back the sum; print shows it [OK]
Common Mistakes:
  • Concatenating numbers as strings (getting '34')
  • Forgetting to return value (getting None)
  • Passing wrong number of arguments causing TypeError
4. Find the error in this function definition:
def multiply(a, b)
    return a * b
medium
A. Indentation error in return statement
B. Missing colon at the end of function header
C. Wrong function name
D. Parameters should be in square brackets

Solution

  1. Step 1: Check function header syntax

    The function header must end with a colon (:).
  2. Step 2: Identify missing colon

    The code misses the colon after def multiply(a, b), causing a syntax error.
  3. Final Answer:

    Missing colon at the end of function header -> Option B
  4. Quick Check:

    Function header must end with : [OK]
Hint: Always put : after function header line [OK]
Common Mistakes:
  • Forgetting colon after function header
  • Incorrect indentation of return line
  • Using wrong brackets for parameters
5. You want to write a function is_even that returns True if a number is even, otherwise False. Which is the correct function?
hard
A. def is_even(n): return n % 2
B. def is_even(n): return n / 2 == 0
C. def is_even(n): if n % 2 == 0: return True else: return False
D. def is_even(n): if n % 2: return True else: return False

Solution

  1. Step 1: Understand even number check

    A number is even if remainder when divided by 2 is zero (n % 2 == 0).
  2. Step 2: Evaluate each option

    def is_even(n): if n % 2 == 0: return True else: return False correctly returns True if remainder is zero, else False. def is_even(n): return n / 2 == 0 uses division instead of modulo. def is_even(n): if n % 2: return True else: return False returns True when remainder is non-zero (odd). def is_even(n): return n % 2 returns remainder directly (not boolean).
  3. Final Answer:

    def is_even(n): if n % 2 == 0: return True else: return False -> Option C
  4. Quick Check:

    Use modulo == 0 to check even [OK]
Hint: Use n % 2 == 0 to check even numbers [OK]
Common Mistakes:
  • Using division instead of modulo
  • Returning remainder instead of boolean
  • Confusing condition for even check