0% found this document useful (0 votes)
2 views4 pages

Practical

The document contains Python code for various functions and programs including a menu-driven calculator for basic arithmetic operations, a function to count word frequency in a paragraph, a function to analyze a string for character types, and a program to sort words in a sentence by their length. Each section includes source code and sample inputs/outputs. The code demonstrates fundamental programming concepts such as functions, loops, and conditionals.

Uploaded by

vbf9745448906
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Practical

The document contains Python code for various functions and programs including a menu-driven calculator for basic arithmetic operations, a function to count word frequency in a paragraph, a function to analyze a string for character types, and a program to sort words in a sentence by their length. Each section includes source code and sample inputs/outputs. The code demonstrates fundamental programming concepts such as functions, loops, and conditionals.

Uploaded by

vbf9745448906
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write a menu-driven program to perform addition, subtraction, multiplication, division, floor


division, and exponentiation and modulus operations based on user input using python functions.

SOURCE CODE
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
def multiplication(a, b):
return a * b
def division(a, b):
if b == 0:
return "Division by zero is not allowed."
return a / b
def floor_division(a, b):
if b == 0:
return "Division by zero is not allowed."
return a // b
def exponentiation(a, b):
return a ** b
def modulus(a, b):
if b == 0:
return "Division by zero is not allowed."
return a % b
while True:
print("\n===== MENU =====")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Floor Division")
print("6. Exponentiation")
print("7. Modulus")
print("8. Exit")
choice = int(input("Enter your choice (1-8): "))
if choice == 8:
print("Exiting the program...")
break
if choice >= 1 and choice <= 7:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == 1:
print("Result =", addition(num1, num2))
elif choice == 2:
print("Result =", subtraction(num1, num2))
elif choice == 3:
print("Result =", multiplication(num1, num2))
elif choice == 4:
print("Result =", division(num1, num2))
elif choice == 5:
print("Result =", floor_division(num1, num2))
elif choice == 6:
print("Result =", exponentiation(num1, num2))
elif choice == 7:
print("Result =", modulus(num1, num2))
else:
print("Invalid choice! Please select a valid option.")
2. Write a python function named word_frequency(text) that accepts a paragraph and print the
frequency of each word.
Sample input
"apple banana apple orange banana apple"
Output:
apple: 3
banana: 2
orange: 1

SOURCE CODE
def word_frequency(text):
words = [Link]() # Split the paragraph into words
frequency = {}

for word in words:


if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
for word, count in [Link]():
print(f"{word}: {count}")

text = "apple banana apple orange banana apple"


word_frequency(text)

3. Write a python function named analyze_string(s) that takes a paragraph as input and count the
number of:
• Uppercase letters
• Lowercase letters
• Digits
• Special characters

Source Code

def analyze_string(s):
uppercase = 0
lowercase = 0
digits = 0
special = 0

for char in s:
if [Link]():
uppercase += 1
elif [Link]():
lowercase += 1
elif [Link]():
digits += 1
elif not [Link](): # Ignore spaces
special += 1

print("Uppercase letters:", uppercase)


print("Lowercase letters:", lowercase)
print("Digits:", digits)
print("Special characters:", special)

text = "Hello World! Python3 @2025"


analyze_string(text)
4. Write a python program to take a sentence and return all words sorted by their length and
display them as a list.
Sample Output
Input: "Python is powerful and fun"
Output: ['is', 'and', 'fun', 'Python', 'powerful']

Source code
def sort_by_length(sentence):

words = [Link]()

# Manual sorting using nested loops

for i in range(len(words)):

for j in range(i + 1, len(words)):

if len(words[i]) > len(words[j]):

words[i], words[j] = words[j], words[i]

return words

sentence = input("Enter a sentence: ")

result = sort_by_length(sentence)

print(result)

You might also like