Assignment Python
Assignment Python
first
number divided by the second, with exactly two decimal place displayed.
ANS.
if num2 != 0:
print(f"Result: {result:.2f}")
else:
ANS>
if user_input.upper() == 'A':
print("Apple")
print("Banana")
print("Coconut")
else:
ANS>
def is_prime(number):
# A prime number is greater than 1 and has no divisors other than 1 and itself
if number <= 1:
return False
if number % i == 0:
return True # No divisors other than 1 and itself, it's a prime number
if is_prime(num):
else:
fibonacci_series = [0, 1]
fibonacci_series.append(next_number)
return fibonacci_series
# Taking input for the length of Fibonacci series from the user
result = fibonacci_series(length)
print("Fibonacci Series:")
print(result)
Q5:- WAP that converts lower case letter to upper case letter and vice-versa.
ANS>
def convert_case(string):
converted_string = ""
if char.islower():
converted_string += char.upper()
elif char.isupper():
converted_string += char.lower()
else:
return converted_string
converted_result = convert_case(user_input)
print("Converted String:")
print(converted_result)
Q6:- WAP to program to print a table of given number.
ANS,
def multiplication_table(number):
multiplication_table(num)
Q7:- WAP that sums a series of (positive) integer entered by the user excluding all numbers that
are greater than 100.
ANS.
def sum_positive_integers():
total_sum = 0
while True:
try:
if num == 0:
total_sum += num # Add to total sum if the number is positive and <= 100
else:
except ValueError:
return total_sum
# Calling the function to sum positive integers excluding numbers greater than 100
result = sum_positive_integers()
2 2
3 3 3
4 4 4 4
Ans.
def print_pattern(rows):
print_pattern(num_rows)
Q9:- WAP to print the following pattern
* *
* * *
* * * *
Ans.
def print_pattern(rows):
print_pattern(num_rows)
Q10:- W.A.P to perform the following operations on list
ANS.
def main():
while True:
print("\nOperations:")
print("1. Max")
print("2. Min")
print("3. Count")
print("4. Extend")
print("5. Insert")
print("6. Pop")
print("7. Length")
print("8. Return")
print("9. Append")
print("10. Reverse")
print("Enter 'q' to quit")
if choice == '1':
my_list.extend(extend_list)
my_list.insert(index, value)
popped_value = my_list.pop()
my_list.append(new_value)
my_list.reverse()
else:
if __name__ == "__main__":
main()
Q11:- W.A.P to perform the following operations on dictionary
accessing
updating
adding
Removing
Copy
Clear
Declare
ANS.
def main():
my_dict = {
'name': 'John',
'age': 30,
while True:
print("\nOperations:")
print("1. Accessing")
print("2. Updating")
print("3. Adding")
print("4. Removing")
print("5. Copy")
print("6. Clear")
if choice == '1':
if key in my_dict:
print("Value:", my_dict[key])
else:
if key in my_dict:
my_dict[key] = new_value
else:
my_dict[new_key] = new_value
if key in my_dict:
del my_dict[key]
else:
new_dict = my_dict.copy()
my_dict.clear()
print("Dictionary cleared.")
new_dict = dict()
else:
main()
Q13:- W.A.P to input the integer values in the list & stores in another list only those values
which are even number & desplays the resulting list.
ANS>
def main():
even_numbers = []
while True:
if user_input.lower() == 'q':
break
try:
number = int(user_input)
if number % 2 == 0:
even_numbers.append(number)
except ValueError:
if __name__ == "__main__":
main()
Q14:- W.A.P to show the concept of multiple inheritance.
ANS.
# Base class 1
class BaseClass1:
def method1(self):
# Base class 2
class BaseClass2:
def method2(self):
def method3(self):
obj = DerivedClass()
return a + b
return a - b
return a * b
if b != 0:
return a / b
else:
def main():
while True:
print("\nMenu:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Quit")
if choice == '1':
print("Result:", result)
print("Result:", result)
print("Result:", result)
break
else:
if __name__ == "__main__":
main()
Q16:- W.A.P that accepts 3 integer and return true if any of the integer is 0, otherwise it returns
false.
return True
else:
return False
def main():
if result:
else:
if __name__ == "__main__":
main()
Q17:- write a program to prompt the user to enter a list of words stores in a list only those words
whose first letter occurs within word. The program should display the resulting list?
def filter_words(words):
filtered_words = []
if word[0].lower() in word[1:].lower():
filtered_words.append(word)
return filtered_words
def main():
result = filter_words(words)
print("Resulting List:")
print(result)
if __name__ == "__main__":
main()
Q18:- W.A.P to do following operations on files:-
Opening
Copy
Remove
Append
Rename
Tell
def create_file(filename):
shutil.copyfile(source, destination)
def remove_file(filename):
try:
os.remove(filename)
except FileNotFoundError:
file.write(content + "\n")
print(f"Content appended to '{filename}'")
os.rename(filename, new_name)
def tell_file_position(filename):
position = file.tell()
def main():
filename = "sample.txt"
create_file(filename)
while True:
print("\nOperations:")
print("6. Exit")
if choice == '1':
new_filename = input("Enter new filename for copy: ")
copy_file(filename, new_filename)
remove_file(filename)
append_file(filename, content)
rename_file(filename, new_name)
tell_file_position(filename)
break
else:
if __name__ == "__main__":
main()