Python · Syntax · Beginner
Functions level 2
Advanced Python function practice: string and number handling, logical tests, list comprehensions, filtering, counting, finding values, and writing small reusable functions.
Quick topic start and explanations before exercises (exercises below):
Patterns at this level
#String methods, slices, and function patterns reference
#Exercises:
Even or Odd.
#Write a function that takes a number and returns "Even" if the number is even, and "Odd" if it is odd.
def even_or_odd(n):
pass
Solution
def even_or_odd(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"
# or you can do this: first get the remainder
def even_or_odd(n):
rest = n % 2
if rest == 0:
return "Even"
return "Odd"
Reverse a string using a function.
#Write a function that takes a string and returns it in reversed form.
def reverse_text(text):
pass
Solution
def reverse_text(text):
return text[::-1]
# or you can do this using a loop
def reverse_text(text):
result = ""
for char in text:
result = char + result
return result
Arithmetic mean.
#Write a function that takes two numbers and returns their arithmetic mean.
def average(a, b):
pass
Solution
def average(a, b):
return (a + b) / 2
Counting vowels.
#Write a function that takes a string and returns the number of vowels in it. Vowels: a, e, i, o, u, y (case does not matter).
def count_vowels(text):
pass
Solution
def count_vowels(text):
vowels = "aeiouy"
count = 0
for ch in text.lower():
if ch in vowels:
count += 1
return count
Divisibility by 3 and 5.
#Write a function that takes a number and returns True if it is divisible by both 3 and 5, otherwise False .
def divisible_by_3_and_5(n):
pass
Solution
def divisible_by_3_and_5(n):
return n % 3 == 0 and n % 5 == 0
String without spaces.
#Write a function that takes a string and returns it without spaces.
def remove_spaces(text):
pass
Solution
def remove_spaces(text):
result = ""
for ch in text:
if ch != " ":
result += ch
return result
# or like this:
def remove_spaces(text):
return text.replace(" ", "")
Sum of positive elements.
#Write a function that takes a tuple of numbers and returns the sum of positive elements.
def sum_positive(numbers):
pass
Solution
def sum_positive(numbers):
total = 0
for n in numbers:
if n > 0:
total += n
return total
Palindrome check.
#Write a function that takes a string and returns True if it is a palindrome.
def is_palindrome(text):
pass
Solution
def is_palindrome(text):
return text == text[::-1]
# or you can do this: prepare the reversed string separately
def is_palindrome(text):
reversed_text = text[::-1]
return text == reversed_text
Positive, Negative, or Zero.
#Write a function that takes a number and returns a string: "Positive", "Negative", or "Zero".
def describe_number(n):
pass
Solution
def describe_number(n):
if n > 0:
return "Positive"
elif n < 0:
return "Negative"
else:
return "Zero"
Number of words in a string.
#Write a function that takes a string and returns the number of words in it. Words are separated by spaces.
def count_words(text):
pass
Solution
def count_words(text):
if text == "":
return 0
return len(text.split())
Multiple of ten.
#Write a function that takes a number and returns True if it is a multiple of 10, otherwise False .
def is_multiple_of_ten(n):
pass
Solution
def is_multiple_of_ten(n):
return n % 10 == 0
First and last character using a function.
#Write a function that takes a string and returns its first and last character as a single string.
def first_and_last(text):
pass
Solution
def first_and_last(text):
if text == "":
return ""
return text[0] + text[-1]
Yes, if greater than 100.
#Write a function that takes a number and returns the string "Yes" if the number is greater than 100, otherwise "No" .
def more_than_hundred(n):
pass
Solution
def more_than_hundred(n):
if n > 100:
return "Yes"
return "No"
# Or like this using a ternary operator:
def more_than_hundred(n):
return "Yes" if n > 100 else "No"
Counting the letter a.
#Write a function that takes a string and returns the number of letters "a" in it (case does not matter).
def count_a(text):
pass
Solution
def count_a(text):
count = 0
for ch in text.lower():
if ch == "a":
count += 1
return count
# or
def count_a(text):
return text.lower().count("a")
Absolute difference.
#Write a function that takes two numbers and returns their absolute difference.
def abs_diff(a, b):
pass
Solution
def abs_diff(a, b):
diff = a - b
if diff < 0:
diff = -diff
return diff
# or you can do this using abs
def difference(a, b):
return abs(a - b)
Even length of a string.
#Write a function that takes a string and returns True if its length is even, otherwise False .
def is_even_length(text):
pass
Solution
def is_even_length(text):
return len(text) % 2 == 0
Last digit of a number.
#Write a function that takes an int number and returns the last digit of this number as an int.
def last_digit(n):
pass
Solution
def last_digit(n):
if n < 0:
n = -n
return n % 10
# or you can do this using a string
def last_digit(n):
return int(str(abs(n))[-1])
Without the first and last letter.
#Write a function that takes a string and returns it without the first and last letter.
def trim_edges(text):
pass
Solution
def trim_edges(text):
if len(text) <= 2:
return ""
return text[1:-1]
Small, Medium, or Large.
#Write a function that takes a number and returns a string: - "Small" — if the number is less than 10 - "Medium" — if the number is from 10 to 100 - "Large" — if the number is greater than 100
def size_label(n):
pass
Solution
def size_label(n):
if n < 10:
return "Small"
elif n <= 100:
return "Medium"
else:
return "Large"
Removing exclamation marks.
#Write a function that takes a string and returns it without all exclamation marks "!" .
def remove_exclamations(text):
pass
Solution
def remove_exclamations(text):
result = ""
for ch in text:
if ch != "!":
result += ch
return result
# or you can do this using replace
def remove_exclamation(text):
return text.replace("!", "")