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.
#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.
#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.
#def average(a, b):
pass
Solution
def average(a, b):
return (a + b) / 2
Counting vowels.
#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.
#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.
#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.
#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.
#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.
#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.
#def count_words(text):
pass
Solution
def count_words(text):
if text == "":
return 0
return len(text.split())
Multiple of ten.
#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.
#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.
#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.
#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.
#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.
#def is_even_length(text):
pass
Solution
def is_even_length(text):
return len(text) % 2 == 0
Last digit of a number.
#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.
#def trim_edges(text):
pass
Solution
def trim_edges(text):
if len(text) <= 2:
return ""
return text[1:-1]
Small, Medium, or Large.
#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.
#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("!", "")