Python Program 1 to 13
Python Program 1 to 13
choice = input("Enter 'C' for Celsius to Fahrenheit or 'F' for Fahrenheit to Celsius: ").upper()
if choice == 'C':
celsius = float(input("\nEnter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius} Celsius is {fahrenheit} Fahrenheit.")
elif choice == 'F':
fahrenheit = float(input("\nEnter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print(f"{fahrenheit} Fahrenheit is {celsius} Celsius.")
else:
print("Invalid choice.")
Output : -
2) Write a program in python to swap two variables without using temporary variable.
Output : -
pg. 1
3) Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal
binary = bin(decimal)[2:]
octal = oct(decimal)[2:]
hexadecimal = hex(decimal)[2:].upper()
Output : -
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"{num1} + {num2} = {num1 + num2}")
elif choice == '2':
print(f"{num1} - {num2} = {num1 - num2}")
elif choice == '3':
print(f"{num1} * {num2} = {num1 * num2}")
elif choice == '4':
print(f"{num1} / {num2} = {num1 / num2}")
else:
print("Invalid input")
Output : -
pg. 2
5) Write a program in python to find out maximum and minimum number out of three
user entered number.
Output : -
6) Write a program which will allow user to enter 10 numbers and display largest odd
number from them. It will display appropriate message in case if no odd number is
found.
numbers = []
for i in range(10):
num = int(input(f"Enter number {i+1}: "))
numbers.append(num)
odd_numbers = []
for num in numbers:
if num % 2 != 0:
odd_numbers.append(num)
if odd_numbers:
print(f"Largest odd number: {max(odd_numbers)}")
else:
print("No odd number found.")
Output : -
pg. 3
7) Write a Python program to check if the number provided by the user is an
Armstrong number.
if sum_of_powers == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Output : -
if num == num[::-1]:
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")
Output : -
pg. 4
9) Write a Python program to perform following operation on given string input:
a) Count Number of Vowel in given string
b) Count Length of string (do not use Len ())
c) Reverse string
d) Find and replace operation
e) check whether string entered is a palindrome or not.
vowel_count = 0
for char in s:
if char in 'aeiouAEIOU':
vowel_count += 1
length = 0
for _ in s:
length += 1
reversed_string = s[::-1]
is_palindrome = s == s[::-1]
Output : -
pg. 5
10) Define a procedure histogram () that takes a list of integers and prints a histogram
to the screen. For example, histogram ([4, 9, 7]) should print the following:
****
*********
*******
def histogram(numbers):
for num in numbers:
print('*' * num)
histogram([4, 9, 7])
Output : -
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
pg. 6
print("\nFibonacci series:")
for i in range(num):
print(fibonacci(i), end=' ')
Output : -
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
print("\nFactorial series:")
for i in range(num + 1):
print(f"{i}! = {factorial(i)}")
Output : -
13) Write a program in Python to implement readline, readlines, write line and
writelines file handling mechanisms.
pg. 7
print("Read line:", f.readline())
print("Read lines:", f.readlines())
Output : -
pg. 8