Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Python Programs

The document discusses several examples of Python programs, including: 1. A program that reads numbers from a user and counts odd and even numbers. 2. A program that defines geometry functions in a module to calculate square, circle, and rectangle areas. 3. A program that defines calculator functions in a module to perform basic math operations. 4. A program that counts alphabet occurrences in a text file. 5. Two programs that check if a string is a palindrome by comparing it to its reverse.

Uploaded by

Priyanshu Ahir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Python Programs

The document discusses several examples of Python programs, including: 1. A program that reads numbers from a user and counts odd and even numbers. 2. A program that defines geometry functions in a module to calculate square, circle, and rectangle areas. 3. A program that defines calculator functions in a module to perform basic math operations. 4. A program that counts alphabet occurrences in a text file. 5. Two programs that check if a string is a palindrome by comparing it to its reverse.

Uploaded by

Priyanshu Ahir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

📚

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?

n = int(input("Enter the value of n: "))


numbers = []

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?

1. Create a file named geometry.py and define the module:

# geometry.py
def square_area(side):
return side * side

def circle_area(radius):
return 3.14 * radius ** 2

def rectangle_area(length, width):


return length * width

2. Create a program that imports and uses the functions from the geometry module:

# main_program.py

import geometry

side = float(input("Enter the side length of the square: "))


print(f"Area of the square: {geometry.square_area(side)}")

radius = float(input("Enter the radius of the circle: "))


print(f"Area of the circle: {geometry.circle_area(radius)}")

length = float(input("Enter the length of the rectangle: "))


width = float(input("Enter the width of the rectangle: "))
print(f"Area of the rectangle: {geometry.rectangle_area(lengt

3. How can you create a user-defined module in Python called


'calculator,' containing functions for addition, subtraction,

Python Programs 2
multiplication, and division? Additionally, could you share a
sample program that imports this module and makes use of its
functions?

1. Create a file named calculator.py and define the module:

# calculator.py

def add(x, y):


return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y != 0:
return x / y
else:
return "Cannot divide by zero"

2. Create a program that imports and uses the functions from the calculator

module:

# main_program.py

import calculator

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


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

print(f"Addition: {calculator.add(num1, num2)}")


print(f"Subtraction: {calculator.subtract(num1, num2)}")

Python Programs 3
print(f"Multiplication: {calculator.multiply(num1, num2)}")
print(f"Division: {calculator.divide(num1, num2)}")

4. How can a basic Python program be developed to


interactively prompt the user for a file name, read the content of
the specified file, and then count the occurrences of each
alphabet in the file?

file_name = input("Enter the file name: ")

with open(file_name, 'r') as file:


content = file.read().lower()
alphabet_count = {}

for char in content:


if char.isalpha():
alphabet_count[char] = alphabet_count.get(char, 0

print("\nAlphabet Occurrences:")
for char, count in alphabet_count.items():
print(f"{char}: {count}")

5. How would you create a straightforward Python program to


determine if a given string is a palindrome?
We have 2 methods for this question

1. First Method:

def is_palindrome(s):
s = s.lower() # Convert to lowercase for case-insensitivi

# Check if the string is equal to its reverse


reversed_s = s[::-1]
return s == reversed_s

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

# Check if the string is equal to its reverse


reversed_s = s[::-1]
return s == reversed_s

# 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.")

Python Programs 5

You might also like