Basic Python programs


create a Python program that acts as a simple calculator. It should take two numbers and an operator (+, -, *, /) as input and provide the result of the operation.

Ans:

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

operation = input("Enter operation (+, -, *, /): ")

 

if operation == "+":

    print(num1 + num2)

elif operation == "-":

    print(num1 - num2)

elif operation == "*":

    print(num1 * num2)

elif operation == "/":

    print(num1 / num2)

else:

    print("Invalid operation")

 

2.  Write a Python program that determines whether a given number is even or odd.

Ans:

num = int(input("Enter a number: "))

if num % 2 == 0:

    print("Even")

else:

    print("Odd")

 

3.    Develop a Python program to calculate the factorial of a given positive integer.

Ans:

def factorial(n):

    if n == 0 or n == 1:

        return 1

    else:

        return n * factorial(n - 1)

 

num = int(input("Enter a number: "))

print("Factorial:", factorial(num))

 

4.   Create a Python program that generates the Fibonacci series up to a specified number of terms.

Ans:

def fibonacci(n):

    if n <= 0:

        return []

    elif n == 1:

        return [0]

    elif n == 2:

        return [0, 1]

    else:

        fib_series = [0, 1]

        for i in range(2, n):

            fib_series.append(fib_series[-1] + fib_series[-2])

        return fib_series

 

num = int(input("Enter the number of terms: "))

print("Fibonacci Series:", fibonacci(num))

 

5.   Write a Python program that checks if a given word is a palindrome (reads the same forwards and backwards) while ignoring spaces and letter case.

Ans:

def is_palindrome(s):

    s = s.lower().replace(" ", "")

    return s == s[::-1]

 

word = input("Enter a word: ")

if is_palindrome(word):

    print("Palindrome")

else:

    print("Not a palindrome")

 

6.       Design a Python program that takes five numbers as input, stores them in a list, and then displays the list along with the sum and maximum value of the numbers.

Ans:

my_list = []

 

for i in range(5):

    num = int(input("Enter a number: "))

    my_list.append(num)

 

print("List:", my_list)

print("Sum:", sum(my_list))

print("Maximum:", max(my_list))

 

7.  Develop a Python program to calculate the simple interest based on user-provided principal amount, rate of interest, and time in years.

Ans:

principal = float(input("Enter principal amount: "))

rate = float(input("Enter rate of interest: "))

time = float(input("Enter time (in years): "))

 

simple_interest = (principal * rate * time) / 100

print("Simple Interest:", simple_interest)

8.  Create a Python program that converts temperatures between Celsius and Fahrenheit. It should take a temperature value and a unit (C/F) as input and provide the converted temperature.

Ans:

def celsius_to_fahrenheit(celsius):

    return (celsius * 9/5) + 32

 

def fahrenheit_to_celsius(fahrenheit):

    return (fahrenheit - 32) * 5/9

 

temp = float(input("Enter temperature: "))

unit = input("Enter unit (C/F): ")

 

if unit.upper() == "C":

    print("Temperature in Fahrenheit:", celsius_to_fahrenheit(temp))

elif unit.upper() == "F":

    print("Temperature in Celsius:", fahrenheit_to_celsius(temp))

else:

    print("Invalid unit")

 

9.  Write a Python program to determine whether a given positive integer is a prime number or not.

Ans:

def is_prime(n):

    if n <= 1:

        return False

    for i in range(2, int(n**0.5) + 1):

        if n % i == 0:

            return False

    return True

 

num = int(input("Enter a number: "))

if is_prime(num):

    print("Prime")

else:

    print("Not a prime")

 

10 Write a Python program that takes a string as input and outputs its reverse.

Ans:

def reverse_string(s):

    return s[::-1]

 

input_string = input("Enter a string: ")

reversed_string = reverse_string(input_string)

print("Reversed String:", reversed_string)

 


Post a Comment