0% found this document useful (0 votes)
29 views

Sodapdf

The document contains code snippets for various Python programming tasks like finding prime factors of a number, calculating projectile motion, generating Fibonacci series, word count in a paragraph, list operations, employee database, set and tuple operations, and file handling.

Uploaded by

0p920vvkwl
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)
29 views

Sodapdf

The document contains code snippets for various Python programming tasks like finding prime factors of a number, calculating projectile motion, generating Fibonacci series, word count in a paragraph, list operations, employee database, set and tuple operations, and file handling.

Uploaded by

0p920vvkwl
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
You are on page 1/ 6

1)

n = input("Enter the number : ")


n = int(n)
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n = n/2

for i in range(3,int(n**0.5),2):
while n % i == 0:
maxPrime = i
n = n/i

if n > 2:
maxPrime = n

if maxPrime == -1:
print("No Prime Factor Found")

else:
print("Max Prime Factor : ", maxPrime)

2)

# Declare a value
a = -16

# Read velocity from the user


b = int(input(’Enter the velocity: ’))

# Read player height


pHeight = int(input(’Enter player height: ’))

# Calculate time using the formula


t = float(-b / (2 * a))
print("Time:", t, "seconds")

# Calculate the height using the formula h = (a * (t ** 2)) + (b * t)


h = (a * (t ** 2)) + (b * t)

# Print the result


print(’Height is:’, h, ’feet’)

# Add the player height to the ball height


h = h + pHeight

# Print the total height


print("Total Height is:", h, ’feet’)

3)

n = int(input("Enyter no. of series :"))


fiblist = [0,1]
for i in range(0,n):
fiblist.append(fiblist[i]+fiblist[i+1])
print("Series is: ",fiblist)

gratio = [fiblist[i]/float(fiblist[i-1]) for i in range(2,len(fiblist))]


print("Golden ratio :",gratio)

4)

# Define the input paragraph


str = "New Delhi is the Capital of India. Bangalore is a Capital of Karnataka. Karnataka is India. India is th
e world’s largest Democratic Country."

# Print the paragraph


print(’Entered Paragraph:\n’, str)

# Split the paragraph into words and count them


words = str.split()
wordCount = len(words)
print(’Total Number of words:’, wordCount) # Print the word count

# Create an empty dictionary to store word counts


counts = {}

# Run a loop to count the occurrences of each word


for word in words:
# Convert words to lowercase to ensure case-insensitive counting
word = word.lower()
if word in counts:
counts[word] += 1 # Increment the word count if the word is already in the dictionary
else:
counts[word] = 1 # Add the word to the dictionary with a count of 1

# Display the word counts


for word, count in counts.items():
print(’Word:’, word, ’Count:’, count)

# Input a word to search (converted to lowercase for case-insensitive search)


searchWord = input(’Enter the word to search: ’).lower()

# Find the word in the paragraph (converted to lowercase)


result = str.lower().find(searchWord)

# Check if the word was found and display the result


if result != -1:
print(searchWord + " Word found in Paragraph")
else:
print(searchWord + " Word not found in Paragraph")

5)
# Create an Empty List
numbers = []

while True:
print(’\n===== MENU =====’)
print(’1. Insert at Specific Position’)
print(’2. Remove Element from the List’)
print(’3. Add Element to the List’)
print(’4. Display the List’)
print(’5. Exit\n’)

choice = int(input(’Enter your choice: ’))

if choice == 1:
position = int(input(’Enter the position to insert: ’))
item = int(input(’Enter the item to insert at the given position: ’))
if 0 <= position <= len(numbers):
numbers.insert(position, item)
else:
print("Invalid position")

elif choice == 2:
if numbers:
position = int(input(’Enter the position to delete an item: ’))
if 0 <= position < len(numbers):
numbers.pop(position)
else:
print("Invalid position")
else:
print("List is empty")

elif choice == 3:
item = int(input(’Enter an item to add: ’))
numbers.append(item)

elif choice == 4:
print("======= List Content =======")
print(numbers)

elif choice == 5:
print(’Exiting.....’)
break

else:
print("Invalid Choice. Please choose a valid option.")

6)

Employee = {}

while True:
print("\nEmployee Database Menu:")
print("1. Create Employee")
print("2. Add New Employee")
print("3. Search Employee")
print("4. Delete Employee")
print("5. Display")
print("6. Exit")
Choice = int(input("Enter the Choice: "))

if Choice == 1:
n = int(input(’Enter the Number of Employees: ’))
for i in range(n):
print("\nEnter the Employee {0} Details".format(i+1))
EmpId = int(input("Enter the Employee ID: "))
EmpDetails = []
EmpName = input("Enter the Employee Name: ")
EmpDOB = input("Enter the DOB: ")
Designation = input("Enter the Designation: ")
EmpDetails.append(EmpName)
EmpDetails.append(EmpDOB)
EmpDetails.append(Designation)
Employee[EmpId] = EmpDetails

elif Choice == 2:
EmpId = int(input(’Enter the Employee ID: ’))
EmpDetails = []
EmpName = input(’Enter the Employee Name: ’)
EmpDOB = input(’Enter the DOB: ’)
Designation = input(’Enter the Designation: ’)
EmpDetails.append(EmpName)
EmpDetails.append(EmpDOB)
EmpDetails.append(Designation)
Employee[EmpId] = EmpDetails

elif Choice == 3:
EmpId = int(input(’Enter the Employee ID to Display: ’))
print(Employee.get(EmpId))

elif Choice == 4:
EmpId = int(input(’Enter the Employee ID to Delete: ’))
print(Employee.pop(EmpId))

elif Choice == 5:
Status = bool(Employee)
if Status == False:
print("No Employee Details Found to Print")
else:
print(Employee)

elif Choice == 6:
print("Exiting Employee Database.")
break

else:
print("Invalid Choice")

7)

setdata = set()
tupledata = tuple()

while True:
choice = input("Enter your Choice\nS: Set Operation\nT: Tuple Operation\nN: Terminate\n")

if choice == ’S’:
while True:
print("Choose The Set Operation")
print(’1. Add/Insert’)
print(’2. Remove/Delete’)
print("3. Update/Append")
print("4. Display/View")
print("0. Exit")
operation = input()

if operation == 1:
data = input("Enter the element to add : ")
setdata.add(data)
print(setdata)
elif operation == 2:
data = input("Enter the element to remove :")
setdata.discard(data)
print(setdata)
elif operation == 3:
data = input("Enter the Element to update : ")
setdata.update([data])
elif operation == 4:
print(setdata)
elif operation == 0:
break
else:
print("Invalid Choice")

elif choice == ’T’:


while True:
print("Choose the tuple Operation")
print("1. Add/Insert")
print("2. Delete Tuple")
print("3. Display/View")
print("0. Exit")
operation = input()
if operation == 1:
data = input("Enter the data you want to add: ")
tupledata =+ (data,)
print(tupledata)
elif operation == 2:
tupledata = ()
print(tupledata)
elif operation == 3:
print(tupledata)
elif operation == 0:
break
else:
print("Invalid Choice")

elif choice == ’N’:


break
8)

def write():
String = input("Enter the paragraph: ") # Fix the input prompt
with open(’Department CourcesHandled/PythonProgramming/LabPrograms/my_file.txt’, ’w’) as file:
file.write(String)

def read():
with open(’Department CourcesHandled/PythonProgramming/LabPrograms/my_file.txt’, ’r’) as file: # Fi
x the file path
data = file.read()
print("Original Content:")
print(data)
print("\nModified Content:")
print(data.title())

write()
read()

You might also like