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

Python Programs CLO1 Final

The document outlines several Python programming exercises, including generating prime numbers in an interval, creating and manipulating lists, working with tuples, calculating factorials using recursion, and managing dictionaries. Each section includes an aim, a step-by-step algorithm, and source code examples. The exercises are designed to teach fundamental programming concepts and operations in Python.

Uploaded by

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

Python Programs CLO1 Final

The document outlines several Python programming exercises, including generating prime numbers in an interval, creating and manipulating lists, working with tuples, calculating factorials using recursion, and managing dictionaries. Each section includes an aim, a step-by-step algorithm, and source code examples. The exercises are designed to teach fundamental programming concepts and operations in Python.

Uploaded by

Yazhini K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programs - CLO 1

1. Prime Numbers in an Interval using for and while

Aim
To write a Python program to generate all prime numbers in a given interval using both for
and while loops.

Algorithm (10 Lines)


1. 1. Start the program and ask the user to enter lower and upper bounds.
2. 2. Use a while loop to iterate from the lower bound to the upper bound.
3. 3. For each number in this range, check if it is greater than 1.
4. 4. Use a for loop to test if the number has any divisors other than 1 and itself.
5. 5. If no divisors are found, it's a prime.
6. 6. Print the prime number.
7. 7. Continue to the next number in the interval.
8. 8. End for loop for divisor checking.
9. 9. End while loop for number iteration.
10. 10. Stop the program.

Source Code (15 Lines)

print("Prime numbers within an interval")


lower = int(input("Enter lower bound: "))
upper = int(input("Enter upper bound: "))
found = False
print(f"Prime numbers from {lower} to {upper} are:")
num = lower
while num <= upper:
if num > 1:
is_prime = True
for i in range(2, int(num**0.5)+1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=' ')
found = True
num += 1
if not found:
print("L - No prime number found.")
print("It does not fall on the entered lower or upper range.")
print("It does not fall between the lower or upper range.")

2. Create, Append, Remove Lists

Aim
To demonstrate creating a list, appending elements, and removing items using Python list
operations.

Algorithm (10 Lines)


11. 1. Start the program.
12. 2. Create an empty list.
13. 3. Ask the user how many elements they want to add.
14. 4. Use a loop to get elements from the user and append them.
15. 5. Display the current list.
16. 6. Ask which item to remove from the list.
17. 7. Check if the item exists in the list.
18. 8. Remove the item if found; else, show error.
19. 9. Display the updated list.
20. 10. End program.

Source Code (15 Lines)

print("List Creation, Append and Remove Demo")


my_list = []
n = int(input("How many items to add? "))
for _ in range(n):
item = input("Enter item to append: ")
my_list.append(item)
print("Current List:", my_list)
remove_item = input("Enter item to remove: ")
if remove_item in my_list:
my_list.remove(remove_item)
print("Updated List:", my_list)
else:
print(f"'{remove_item}' not found in list.")

3. Working with Tuples

Aim
To demonstrate tuple creation, accessing elements, and checking existence using Python.
Algorithm (10 Lines)
21. 1. Start the program.
22. 2. Ask the user for tuple elements (comma-separated).
23. 3. Convert input to a tuple.
24. 4. Display the tuple.
25. 5. Ask the user to input an element to search.
26. 6. Check if the element exists in the tuple.
27. 7. If yes, print the index.
28. 8. Display the length of the tuple.
29. 9. Display slicing and count examples.
30. 10. End program.

Source Code (15 Lines)

print("Working with Tuples")


elements = input("Enter tuple elements (comma-separated): ")
my_tuple = tuple(elements.split(','))
print("Your Tuple:", my_tuple)
search_item = input("Enter element to search: ")
if search_item in my_tuple:
print(f"'{search_item}' found at index:",
my_tuple.index(search_item))
else:
print(f"'{search_item}' not in tuple.")
print("Length:", len(my_tuple))
print("First 3 elements:", my_tuple[:3])
print("Count of first element:", my_tuple.count(my_tuple[0]))

4. Factorial using Recursion

Aim
To write a Python program to compute the factorial of a number using a recursive function.

Algorithm (10 Lines)


31. 1. Start the program.
32. 2. Define a recursive function factorial(n).
33. 3. If n is 0 or 1, return 1.
34. 4. Otherwise, return n * factorial(n - 1).
35. 5. Prompt the user to input a non-negative number.
36. 6. Validate the input.
37. 7. Call the factorial function.
38. 8. Store the result.
39. 9. Print the result.
40. 10. End program.

Source Code (15 Lines)

print("Factorial using Recursion")


def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a non-negative number: "))
if num < 0:
print("Factorial not defined for negative numbers.")
else:
result = factorial(num)
print(f"Factorial of {num} is {result}")

5. Working with Dictionaries

Aim
To demonstrate creating, updating, accessing, and deleting elements in a dictionary.

Algorithm (10 Lines)


41. 1. Start the program.
42. 2. Create an empty dictionary.
43. 3. Ask how many key-value pairs to add.
44. 4. Use a loop to get and add each pair.
45. 5. Display the dictionary.
46. 6. Ask the user for a key to update.
47. 7. Modify the value for the key.
48. 8. Ask which key to delete.
49. 9. Delete the key if it exists.
50. 10. Print the updated dictionary and end.

Source Code (15 Lines)

print("Working with Dictionaries")


my_dict = {}
n = int(input("How many key-value pairs to add? "))
for _ in range(n):
key = input("Enter key: ")
value = input("Enter value: ")
my_dict[key] = value
print("Current Dictionary:", my_dict)
update_key = input("Key to update: ")
if update_key in my_dict:
my_dict[update_key] = input("New value: ")
delete_key = input("Key to delete: ")
my_dict.pop(delete_key, None)
print("Updated Dictionary:", my_dict)

You might also like