Python Function Practice Exercises

  • By Pooja Kulkarni
  • March 28, 2025
  • Python
Python Function Practice Exercises

Python Function Practice Exercises

Enhance your coding skills with Python Function Practice Exercises. Solve real-world problems, improve logic, and master Python functions with hands-on practice.

 

Problems are arranged in increasing difficulty: 

  • Warmup – these can be solved using basic  comparisons and methods 
  • Level 1 – these may involve if/then conditional  statements and simple methods 

 

WARMUP SECTION: 

LESSER OF TWO EVENS: Write a function that returns  the lesser of two given numbers if both numbers are  even, but returns the greater if one or both numbers are  odd 

lesser_of_two_evens(2,4) –> 2 

lesser_of_two_evens(2,5) –> 5 

Solution : 

ANIMAL CRACKERS: Write a function takes a two-word  string and returns True if both words begin with same  letter 

animal_crackers(‘Levelheaded Llama’) — > True 

animal_crackers(‘Crazy Kangaroo’) — > False 

solution : 

MAKES TWENTY: Given two integers, return True if the  sum of the integers is 20 or if one of the integers is 20. If  not, return False 

makes_twenty(20,10) –> True 

makes_twenty(12,8) –> True 

makes_twenty(2,3) –> False

Solution 

For Free, Demo classes Call: 02071171500

Registration Link: Python Classes in Pune!

LEVEL 1 PROBLEMS 

OLD MACDONALD: Write a function that capitalizes the  first and fourth letters of a name 

old_macdonald(‘macdonald’) –> MacDonald 

Note: ‘macdonald’.capitalize() returns ‘Macdon ald’ 

Solution :

MASTER YODA: Given a sentence, return a sentence  with the words reversed 

master_yoda(‘I am home’) –> ‘home am I’ master_yoda(‘We are ready’) — 

> ‘ready are We’ 

Note: The .join() method may be useful here. The .join()  method allows you to join together strings in a list with  some connector string. For example, some uses of the  .join() method: 

>>> “–“.join([‘a’,’b’,’c’]) 

>>> ‘a–b–c’ 

This means if you had a list of words you wanted to turn  back into a sentence, you could just join them with a  single space string: 

>>> ” “.join([‘Hello’,’world’]) 

>>> “Hello world”

Solution : 

ALMOST THERE: Given an integer n, return True if n is  within 10 of either 100 or 200 

almost_there(90) –> True 

almost_there(104) –> True 

almost_there(150) –> False 

almost_there(209) –> True 

NOTE: abs(num) returns the absolute value of a  number

 

Do visit our channel to learn More: Click Here

 

Author:-

Pooja Kulkarni

Call the Trainer and Book your free demo Class for Python Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2025 | SevenMentor Pvt Ltd