Python · Syntax · Beginner
Functions
Basic exercises on functions in Python: creating functions, parameters, return statements, checking numbers and strings, working with lists, tuples, and simple calculations.
Quick topic start and explanations before exercises (exercises below):
def, parameters, and return
#Three function patterns
#Functions quick reference
#Exercises:
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
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
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
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
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))
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
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
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
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
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