Python Practical File
Here is a list of some programs made using python code to perform different functions:-
Program 1: Simple Calculator
Description
This program takes two numbers and an operator from the user and performs the corresponding
arithmetic operation (addition, subtraction, multiplication, or division).
Code
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")
if operation == '+':
result = num1 + num2
print(f"The sum is: {result}")
elif operation == '-':
result = num1 - num2
print(f"The difference is: {result}")
elif operation == '*':
result = num1 * num2
print(f"The product is: {result}")
elif operation == '/':
if num2 != 0:
result = num1 / num2
print(f"The division is: {result}")
else:
print("Error: Division by zero.")
else:
print("Invalid operation.")
Program 2: Finding the Largest of Three Numbers
Description
This program finds and prints the largest number among three numbers entered by the user.
Code
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print(f"The largest number is: {largest}")
Program 3: Even or Odd Number Check
Description
This program checks whether a given integer is even or odd using the modulo operator.
Code
num = int(input("Enter an integer: "))
if (num % 2) == 0:
print(f"{num} is Even")
else:
print(f"{num} is Odd")
Program 4: Factorial of a Number
Description
This program calculates the factorial of a non-negative integer entered by the user. The factorial
of a number n (denoted by n!) is the product of all positive integers less than or equal to n.
Code
num = int(input("Enter a non-negative integer: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial = factorial * i
print(f"The factorial of {num} is {factorial}")
Program 5: Fibonacci Series
Description
This program generates the Fibonacci sequence up to a specified number of terms. The
sequence starts with 0 and 1, and each subsequent term is the sum of the two preceding ones.
Code
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence up to", nterms, ":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1, end=" ")
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
print() # for a newline at the end
Program 6: Prime Number Check
Description
This program checks if a given integer is a prime number. A prime number is a natural number
greater than 1 that has no positive divisors other than 1 and itself.
Code
num = int(input("Enter an integer: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
print(f"{num} is not a prime number")
break
else:
print(f"{num} is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(f"{num} is not a prime number")
Program 7: String Reversal
Description
This program reverses a string entered by the user using slicing.
Code
string = input("Enter a string: ")
reversed_string = string[::-1]
print(f"The original string is: {string}")
print(f"The reversed string is: {reversed_string}")
Program 8: Sum of Digits of a Number
Description
This program calculates the sum of the digits of an integer entered by the user.
Code
num = int(input("Enter an integer: "))
sum_of_digits = 0
temp_num = abs(num) # Use absolute value to handle negative numbers
correctly
while temp_num > 0:
digit = temp_num % 10
sum_of_digits += digit
temp_num //= 10
print(f"The sum of digits of {num} is: {sum_of_digits}")