Python Programs
Python Programs
Python Programs
1. How can a Python program be created to interactively read 'n'
numbers from a user, count the occurrences of odd and even
numbers, and finally, display the results?
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
numbers.append(num)
odd_count = 0
for num in numbers:
if num%2 != 0:
odd_count += 1
even_count = n - odd_count
print("\nResults:")
print(f"Number of odd numbers: {odd_count}")
print(f"Number of even numbers: {even_count}")
Python Programs 1
2. How would you design a user-defined module in Python,
named 'geometry', which includes functions to calculate the
area of a square, circle, and rectangle? Additionally, could you
provide a sample program that imports this module and utilizes
its functions?
# geometry.py
def square_area(side):
return side * side
def circle_area(radius):
return 3.14 * radius ** 2
2. Create a program that imports and uses the functions from the geometry module:
# main_program.py
import geometry
Python Programs 2
multiplication, and division? Additionally, could you share a
sample program that imports this module and makes use of its
functions?
# calculator.py
2. Create a program that imports and uses the functions from the calculator
module:
# main_program.py
import calculator
Python Programs 3
print(f"Multiplication: {calculator.multiply(num1, num2)}")
print(f"Division: {calculator.divide(num1, num2)}")
print("\nAlphabet Occurrences:")
for char, count in alphabet_count.items():
print(f"{char}: {count}")
1. First Method:
def is_palindrome(s):
s = s.lower() # Convert to lowercase for case-insensitivi
Python Programs 4
# Input from the user
user_input = input("Enter a string: ")
if is_palindrome(user_input):
print("The given string is a palindrome.")
else:
print("The given string is not a palindrome.")
2. Second Method:
def is_palindrome(s):
s = s.lower() # Convert to lowercase for case-insensitivi
if is_palindrome(user_input):
print("The given string is a palindrome.")
else:
print("The given string is not a palindrome.")
Python Programs 5