Search

## Python def function Beginner Project

### Overview

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.

### 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: + , - , * , / : ")

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 == '-' :
print(num1,'-',num2,"=",subtraction(num1,num2))
elif  operator == '*' :
print(num1,'*',num2,"=",multiplication(num1,num2))
else:
print(num1,'/',num2,"=",division(num1,num2))

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()
copy_string.reverse()

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

check_palindrome(Palindrome_String)

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.')
else:
for x in range(2,number):
if  (number % x) ==0:
print(number,"is divisible by",x,". So it is not a prime number.")
break
else:
print(number,"is a prime number.")

Prime_Check(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)

sentence_take(sentence)
print(count)

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
print(count)

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]
fibonacci_series.append(series)
if number <= 0:
print("Enter a number greater than 0.")
else:
print(fibonacci_series)

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