Python · Syntax · Beginner

Functions

10 tasks

Basic exercises on functions in Python: creating functions, parameters, return statements, checking numbers and strings, working with lists, tuples, and simple calculations.

What functions are and why they exist

#
A function is a named block of code you can call by name, from anywhere, as many times as you need. The point is not to save typing — it is to give behavior a name, and to make code testable and reusable. Without functions, if you need to check whether a number is even in three different places, you copy the same if n % 2 == 0 check three times. When the logic changes, you fix it in three places and hope you did not miss one. With a function, you fix it once. A function also creates a clear contract: here is what goes in (parameters), here is what comes out (return value). You can test that contract in isolation without running the whole program. In these exercises every task is the same format: write a function that does one specific thing. This forces you to think about inputs and outputs separately from the rest of your code — which is the core skill.

def, parameters, and return

#
Defining a function in Python: ```python def square(n): return n * n result = square(5) print(result) # 25 ``` def announces the function definition. square is the name. n inside the parentheses is a parameter — a local variable that receives the value you pass when calling. return sends a value back to the caller and exits the function. A function without a return statement (or with a bare return) returns None. This is rarely what you want in exercises — always check that you are actually returning a value. pass is a placeholder that lets you write the function signature before implementing the body. You will see it in the starter code for every exercise here: ```python def square(n): pass # remove this and write the actual logic ``` Parameters are local: they exist only inside the function. Changing a parameter does not affect variables outside. If you want to communicate a result back, you return it — you do not print it inside the function (unless the function's job is printing, which is rare). Multiple parameters work the same way: ```python def max_of_two(a, b): if a >= b: return a return b ``` Once you hit return, the function stops. You do not need else after return.

Three function patterns

#
Most exercises in this topic fall into one of three patterns. Compute and return — apply an operation and send back the result: ```python def power(base, exp): return base ** exp print(power(2, 10)) # 1024 ``` Check and return a boolean — test a condition, return True or False: ```python def is_even(n): return n % 2 == 0 print(is_even(8)) # True print(is_even(7)) # False ``` Note that return n % 2 == 0 is a single expression. n % 2 == 0 already evaluates to True or False, so you return that directly — no need for an if. Iterate and build — loop over a sequence inside the function, accumulate a result, return it: ```python def multiply(sequence): result = 1 for n in sequence: result *= n return result print(multiply((3, 4, 100, 15))) # 18000 ``` String operations often combine a built-in method with return: ```python def to_upper(text): return text.upper() ``` Short but useful — functions do not have to be long to be worth writing.

Functions quick reference

#
**Function syntax** ```python def function_name(param1, param2): # body return value # call: result = function_name(arg1, arg2) ``` **Key rules** | Rule | Detail | |---|---| | `return` sends a value back | without it the function returns `None` | | `return` exits immediately | no code after it in the same branch runs | | Parameters are local | changing them does not affect variables outside | | `pass` is a placeholder | remove it before writing real logic | **Three core patterns** ```python # 1. Compute and return def power(base, exp): return base ** exp # 2. Check and return a boolean def is_even(n): return n % 2 == 0 # expression already gives True/False # 3. Iterate and build def multiply_all(nums): result = 1 for n in nums: result *= n return result ``` **Common mistakes** ```python # Forgetting return — function silently returns None: def square(n): n * n # computed but not returned! # Printing instead of returning: def square(n): print(n * n) # caller gets None, not the value # Unnecessary if/else for booleans: def is_even(n): # bad if n % 2 == 0: return True else: return False def is_even(n): # good return n % 2 == 0 ```
01

Square of a number using a function.

#

Write a function that takes one number and returns its square.

def square(n):
    pass


result = square(5)
print(result)
result = square(2)
print(result)
result = square(25)
print(result)
Solution
def square(n):
    return n * n


result = square(5)
print(result)
result = square(2)
print(result)
result = square(25)
print(result)


# or
def square(n):
    result = n * n
    return result

# or
def square(n):
    result = n ** 2
    return result

# or
def square(n):
    return n ** 2
02

Larger number using a function.

#

Write a function that takes two numbers and returns the larger of them. If they are equal, then return the second one.

def max_of_two(a, b):
    pass


print(max_of_two(10, 7))
Solution
def max_of_two(a, b):
    if a > b:
        return a
    else:
        return b


print(max_of_two(10, 7))


# OR if you look carefully, you can also do it like this, else is not really needed,
# because return finishes the function if it works inside a condition or at all:
def max_of_two(a, b):
    if a > b:
        return a
    return b


print(max_of_two(10, 7))

# or you can do this: first check equality
def bigger(a, b):
    if a == b:
        return a
    if a > b:
        return a
    return b
03

String length without len.

#

Write a function that takes a string and returns its length (pretend there is no len function).

def string_length(text):
    pass


print(string_length("python"))
Solution
def string_length(text):
    count = 0
    for _ in text:
        count += 1
    return count


print(string_length("python"))

# or you can name the counter more clearly
def get_length(text):
    count = 0
    for char in text:
        count += 1
    return count
04

Evenness using a function.

#

Write a function that takes a number and returns True if the number is even, otherwise False .

def is_even(n):
    pass


print(is_even(8))
Solution
def is_even(n):
    if n % 2 == 0:
        return True
    else:
        return False

print(is_even(8))


# OR
def is_even(n):
    if n % 2 == 0:
        return True
    return False


# OR
def is_even(n):
    return n % 2 == 0  # immediately return the comparison result.

# or you can do this using a variable
def is_even(n):
    result = n % 2 == 0
    return result
05

Sum of two numbers using a function.

#

Write a function that takes two numbers and returns their sum.

def add(a, b):
    pass


print(add(3, 4))
Solution
def add(a, b):
    return a + b


print(add(3, 4))
06

String in uppercase.

#

Write a function that takes a string and returns a new string with characters in uppercase.

def to_upper(text):
    pass


print(to_upper("hello"))
Solution
def to_upper(text):
    return text.upper()


print(to_upper("hello"))

# or you can do this: save the result first
def to_upper(text):
    result = text.upper()
    return result
07

Positive number using a function.

#

Write a function that takes a number and returns True if it is positive, otherwise False .

def is_positive(n):
    pass


print(is_positive(-3))
Solution
def is_positive(n):
    if n > 0:
        return True
    else:
        return False

print(is_positive(-3))


# OR
def is_positive(n):
    if n > 0:
        return True
    return False


# OR
def is_positive(n):
    return n > 0
08

Exponentiation.

#

Write a function that takes two numbers: a base and an exponent — and returns the result of raising the base to the exponent.

def power(base, exp):
    pass


print(power(2, 3))
Solution
def power(base, exp):
    result = 1
    for _ in range(exp):
        result *= base
    return result

print(power(2, 3))


# OR
def power(base, exp):
    return base ** exp
09

Product of tuple numbers.

#

Write a function that takes a tuple of numbers and returns their product.

def multiply(sequence):
    pass


print(multiply((3, 4, 100, 15)))
Solution
def multiply(sequence):
    result = 1
    for n in sequence:
        result *= n
    return result


print(multiply((3, 4, 100, 15)))


# OR better
def multiply(sequence):
    if len(sequence) == 0:  # <--- really good
        return 0

    result = 1
    for n in sequence:
        result *= n
    return result
10

Repeat string using a function.

#

Write a function that takes a string and a number, and returns this string repeated the specified number of times.

def repeat_text(text, count):
    pass


print(repeat_text(")", 3))
Solution
def repeat_text(text, count):  
    result = ""
    for _ in range(count):  # not cool, there is a cooler way below.
        result += text
    return result


print(repeat_text(")", 3))


# OR
def repeat_text(text, count):
    return text * count