Python Practice › Function Programs
Python Function Programs with Solutions
39 function programs that practise defining functions, parameters, return values, *args, nested functions and small menu-driven systems. Each solution is written as a reusable function and tested with several inputs.
All programs
- #1Function to add two numbers
Write a Python function that adds two numbers and returns the result.Easy - #2Print a string 10 times
Write a Python function that prints a string 10 times.Easy - #3Multiplication table function
Write a Python function that prints the multiplication table of a number.Easy - #4Maximum of three numbers
Write a Python function to find the maximum of three numbers.Easy - #5Sum of a list
Write a Python function to add up the numbers in a list.Easy - #6Product of a list
Write a Python function to multiply all the numbers in a list.Easy - #7Reverse a string
Write a Python function to reverse a string.Easy - #8Is a number in a range?
Write a Python function to check whether a number is within a range (inclusive).Easy - #9Unique elements in order
Write a Python function that returns a new list with the unique elements of a list, in order.Easy - #10Prime check function
Write a Python function that returns True if a number is prime.Easy - #11Even numbers from a list
Write a Python function that returns the even numbers of a list.Easy - #12Squares of 1 to 10
Write a Python function that creates and prints a list of the squares of 1 to 10.Easy - #13Run Python code stored in a string
Write a Python function that executes Python code stored in a string.Medium - #14Function inside a function
Write a Python program that defines and uses a function inside another function.Easy - #15LCM function
Write a Python function to find the LCM of two numbers.Easy - #16Sum from 0 to n
Write a Python function to calculate the sum of numbers from 0 to 10.Easy - #17HCF function
Write a Python function to find the HCF of two numbers.Easy - #18Function with *args
Write a Python function that accepts any number of numbers with*argsand prints their cubes.Easy - #19Factorial function (recursive)
Write a Python function to get the factorial of a number.Easy - #20Fibonacci terms up to n terms
Write a Python function that returns the first n Fibonacci numbers starting 1, 1.Easy - #21Does any pair sum to 10?
Write a Python function that checks whether any two numbers in a list add up to 10.Easy - #22Unique values with dict.fromkeys
Write a Python function to get the unique values of a list in order.Easy - #23Duplicate characters
Write a Python function that returns the characters that appear more than once.Easy - #24Square every dictionary value
Write a Python function that returns a new dictionary with every value squared.Easy - #25Dictionary keyed by first and last letters
Write a Python function that maps the first and last letter of each word to the word.Easy - #26Primes from 1 to 100
Write a Python function that returns the prime numbers from 1 to 100.Easy - #27Odd numbers from 1 to 100
Write a Python function that returns the odd numbers from 1 to 100.Easy - #28Login function
Write a Python function that accepts a username and password and reports whether login succeeded.Easy - #29Return the sum
Write a Python function that returns the sum of two numbers, and show the difference between print and return.Easy - #30Fruit shop management system
Write a Python program with functions to add stock, sell fruit and show the inventory of a fruit shop.Medium - #31Leap year function
Write a Python function to check whether a year is a leap year.Easy - #32Reverse an integer
Write a Python function to reverse an integer, keeping the sign.Easy - #33Library management system
Write a Python program with functions to add, issue, return and list books in a library.Medium - #34Add two binary numbers
Write a Python function to add two binary numbers given as strings.Easy - #35Search for words in a string
Write a Python function that reports which of the given words appear in a string.Easy - #36Length of the last word
Write a Python function that returns the length of the last word in a string.Easy - #37Validate a mobile number
Write a Python function to check whether a mobile number is valid: 10 digits starting with 6, 7, 8 or 9 (optionally with +91).Easy - #38Integer to words
Write a Python function that converts an integer from 0 to 999 into English words.Medium - #39All permutations of a string
Write a Python function that returns all permutations of a string, without itertools.Easy