PPL Experiment No-7
PPL Experiment No-7
Aim: Write a python program to understand and use the concept of functions.
Tools Used:
1. Python 3.x interpreter
2. Text editor or Integrated Development Environment (IDE) such as VS Code, PyCharm, or
IDLE
3. (Optional) Compiler for C to compare constructs
Problem Statement 1: Using function, write a Python program to analyze if the input number is
prime or not.
The Prime Number Analyzer program is designed to determine whether a given number is a
prime number using Python functions. A prime number is a natural number greater than 1 that
has no positive divisors other than 1 and itself.
3.The input number is passed to the is_prime function.Based on the function's return value, the
program prints whether the number is prime or not.
The program should demonstrate Python’s such as functions, conditionals (if, else), loops (for),
mathematical operations, user input (input()), output (print()), and return statements to determine
if a number is prime.
Theory:
Python Functions:
Python Functions is a block of statements that return the specific task. The idea is to put some
commonly or repeatedly done tasks together and make a function so that instead of writing the
same code again and again for different inputs, we can do the function calls to reuse code
contained in it over and over again.
Syntax-
def function_name(parameter1, parameter2, ...):
"""Optional docstring: Describe the function's purpose."""
# Function body
return result # Optional
e.g def add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")
Algorithm:
End.
Flowchart:
Pseudo Code:
START
Prompt the user: "Enter a number to check if it's prime."
4. Else:
END
Problem Statement 1: Using function, write a Python program to analyze if the input number is
prime or not.
Program:
Output -
Problem Statement 2:
Implement a simple Python calculator that takes user input and performs basic arithmetic operations
(addition, subtraction, multiplication, division) using functions.
Program-
Output
Conclusion: Understood the basic programming constructs from C to Python by focusing on
Python's syntax and input/output functions.