Close this search box.

Python def function Beginner Project

Credit: Canva


Python def function can be created and used in two ways, first by using the built-in function and second by using the programmer’s custom function. This Python beginner project helps explore the Python def function.

What’s Next!

This article explores the Python def function. The Beginner Project is designed to challenge a couple of problems based on common real-life problems to examine the concept of the Python def function.

Table of Contents

Project 1: Calculator Functions

Create a program that implements basic calculator functions (addition, subtraction, multiplication, division) as separate functions. The program should ask the user to input the operation and the two numbers, and then call the corresponding function to perform the calculation.

					num1 = 10
num2 = 20
operator = input("Choose any one operator: + , - , * , / : ")

def addition(a,b):
    result = a+b
    return  result

def subtraction(a,b):
    result = a-b
    return  result

def multiplication(a,b):
    result = a*b
    return  result

def division(a,b):
    result = a/b
    return  result

if operator ==  '+' :
elif  operator == '-' :
elif  operator == '*' :

Explanation: The program starts by asking the numbers from the user, next ask the operator to perform the calculation. Now, four functions are created and the expressions are according to each operator. Finally, by using the Python conditional statement, the condition is checked and whichever is met true, is displayed by the Python interpreter.

Project 2: Palindrome Checker

Write a function that checks if a given string is a palindrome (reads the same forwards and backwards). The program should ask the user to input a string and then call the function to check if it is a palindrome.

					Palindrome_String = list(input("Enter a word: "))

copy_string =  Palindrome_String.copy()

def check_palindrome(word):
    if Palindrome_String == copy_string:
        print(f"{Palindrome_String} == {copy_string} is a Palindrome.")
        print(f"{Palindrome_String} == {copy_string} is not a Palindrome.") 


Explanation: The program starts with taking a string, later it is copied and the copy version of the string is reversed by using the Python function reverse().

Now, the def function is defined along with Python if-else statements to check whether the user’s input string is palindrome or not. Inside the conditional statements, a comparison is set between the original string and copied string; if the string from forward and backward spells the original word, then the string is palindrome, otherwise not.

Project 3: Prime Number Checker

Develop a function that checks if a given number is a prime number. The program should ask the user to input a number and then call the function to check if it is prime.

					number = int(input("Enter a number: "))
#number = 19

def Prime_Check(number):
    if number <=1:
        print(number,' There is no Prime number for the number <=1.')
        for x in range(2,number):
            if  (number % x) ==0:
                print(number,"is divisible by",x,". So it is not a prime number.")
            print(number,"is a prime number.")



Explanation: The program starts with asking a number from the user. A function is defined, and inside it, a Python conditional statement is used to check whether the given number is a Prime number or not. If the number <=1, then no Prime number; next, the Python interpreter moves to the for loop and accesses each number from 2 to the user’s number to check the factorial of the given number, if it is found, then the number is not a Prime number, otherwise the given number is Prime number.

Project 4: Word Counter

Create a function that counts the number of words in a given sentence. The program should ask the user to input a sentence and then call the function to count the words.

					def sentence_take(sentence):
    return sentence

sentence = input("Enter a sentence: ")
words = sentence.split()
count = len(words)


Explanation: The program starts by asking for a sentence from the user, it is broken by using the split() function, and each word is counted using the len() function and is displayed by the print() function.

Let’s take another example:

					sentence = ['a','b','c','d','a','b','c','d']
count = 0
for x in sentence:
    count = count+1

Explanation: The program starts with a for loop which iterates to each element in the sentence. With each iteration, the initial value of the count is upgraded by 1; finally, the number of words is displayed using the print() function.

Project 5: Fibonacci Sequence Generator

Write a function that generates the Fibonacci sequence up to a specified number of terms. The program should ask the user to input the number of terms and then call the function to generate the Fibonacci sequence.

					number = 5 #Fibonacci Sequence Generate upto the number 
fibonacci_series = [0,1]
for i in range(2, number):
    series = fibonacci_series[-1]+fibonacci_series[-2]
if number <= 0:
    print("Enter a number greater than 0.")

Explanation: The program starts with asking a number from the user, if the number is less than or equal to 0, then the user is required to enter the number again, otherwise the program moves ahead for the Fibonacci Sequence Generate.

A for loop with range() function from 2 to number is defined to generate the Fibonacci Sequence, we access the last two numbers from the list by negative indexing and each time the generated number is added to the variable fibonacci_series. Finally, the Fibonacci Sequence is displayed using the print() function.

Recent Articles