Cs Record
Cs Record
ROLL NO : 22
CLASS : XI
CERTIFICATE
in COMPUTER SCIENCE (083) Python laid down in the regulations of CBSE for the
(Mrs.Nithya)
PGT for Computer Science
Examiners:
1 Name:Mrs.Nithya Signature:
(Internal)
INDEX
S.NO DATE PROGRAMS PAGE NO SIGNATURE
PYTHON PROGRAMS
13.
1. Python program to Input two numbers and display the larger and smaller number.
CODING:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
2. Python program to Input three numbers and display the largest / smallest
number.
CODING:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
OUTPUT:
Enter the first number: 12.5
Enter the second number: 5.8
Enter the third number: 9.2
Largest number: 12.5
Smallest number: 5.8
3. Write a program to input the value of x and n and print the sum of the following
series: 1+x+x^2 +x^3 +x^4 + ............x^n
number.
CODING:
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
series_sum = 0
OUTPUT:
Enter the value of x: 2
Enter the value of n: 3
The sum of the series is: 15.0
series_sum = 0
OUTPUT:
Enter the value of x: 3
Enter the value of n: 4
The sum of the series is: 17.416666666666668
if is_perfect_number(num):
print(f"{num} is a perfect number.")
else:
print(f"{num} is not a perfect number.")
OUTPUT:
Enter a number: 28
28 is a perfect number.
OUTPUT:
Enter a number: 1634
1634 is an Armstrong number.
OUTPUT:
Enter a number: 121
121 is a palindrome.
OUTPUT:
Enter the number of terms in the Fibonacci series: 8
Fibonacci series:
[0, 1, 1, 2, 3, 5, 8, 13]
10. Python program to Compute the greatest common multiple common divisor and
least of two integers.
CODING:
def gcd(x, y):
while y:
x, y = y, x % y
return x
def lcm(x, y):
return (x * y) // gcd(x, y)
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
greatest_common_divisor = gcd(num1, num2)
least_common_multiple = lcm(num1, num2)
print(f"Greatest Common Divisor (GCD): {greatest_common_divisor}")
print(f"Least Common Multiple (LCM): {least_common_multiple}")
OUTPUT:
Enter the first integer: 24
Enter the second integer: 36
Greatest Common Divisor (GCD): 12
Least Common Multiple (LCM): 72
11. Python program to Count and display the number of vowels, consonants,
uppercase, lowercase characters in string.
CODING:
def count_characters(string):
vowels = "aeiouAEIOU"
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0
if char.isalpha():
if char in vowels:
vowel_count += 1
elif char in consonants:
consonant_count += 1
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
OUTPUT:
Enter a string: Hello World123
Number of vowels: 3
Number of consonants: 7
Number of uppercase characters: 2
Number of lowercase characters: 8
cleaned_string = ''.join(user_input.split()).lower()
if cleaned_string == cleaned_string[::-1]:
print(f"{user_input} is a palindrome.")
else:
print(f"{user_input} is not a palindrome.")
OUTPUT:
Enter a string: radar
radar is a palindrome.
13. Generate the pattern using the nested loop: Pattern 1: *, **, ***, ****, *****
Pattern 2: 12345, 1234, 123, 12, 1 Pattern 3: A, AB, ABC, ABCD, ABCDE
CODING:
print("Pattern 1:")
for i in range(1, 6):
print('*' * i)
print("\nPattern 2:")
for i in range(5, 0, -1):
print(''.join(map(str, range(1, i + 1))))
print("\nPattern 3:")
for i in range(1, 6):
print(''.join(chr(ord('A') + j) for j in range(i)))
OUTPUT:
Pattern 1:
*
**
***
****
*****
Pattern 2:
12345
1234
123
12
1
Pattern 3:
A
AB
ABC
ABCD
ABCDE
OUTPUT:
Enter a list of numbers separated by space: 15 8 23 45 12 5
Largest number: 45
Smallest number: 5
15. Write a python program to input a list of numbers and swap elements at the
even location with the elements at the odd location.
CODING:
def swap_elements_at_even_odd_locations(numbers):
length = len(numbers)
if length < 2:
print("List should have at least two elements for swapping.")
return numbers
for i in range(0, length - 1, 2):
numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
return numbers
numbers_list = [int(x) for x in input("Enter a list of numbers separated by space:
").split()]
result_list = swap_elements_at_even_odd_locations(numbers_list)
16. Write a python program to add the integers in a list and display the sum.
CODING:
numbers_list = [int(x) for x in input("Enter a list of integers separated by space:
").split()]
sum_of_numbers = sum(numbers_list)
17. Write a python program that displays options for inserting or deleting elements
in a list. If user chooses a deletion option, display a submenu and ask if element is to
be deleted with value or by using its position or a list slice is to be deleted.
CODING:
def display_menu():
print("Options:")
print("1. Insert an element into the list")
print("2. Delete an element from the list")
print("3. Display the current list")
print("4. Exit")
def display_delete_submenu():
print("Delete Options:")
print("1. Delete by value")
print("2. Delete by position")
print("3. Delete by list slice")
user_list = []
while True:
display_menu()
choice = input("Enter your choice (1-4): ")
if choice == '1':
element = input("Enter the element to insert: ")
user_list.append(element)
print(f"Element {element} inserted into the list.")
if delete_choice == '1':
element_to_delete = input("Enter the element value to delete: ")
delete_by_value(user_list, element_to_delete)
elif delete_choice == '2':
position_to_delete = int(input("Enter the position to delete: "))
delete_by_position(user_list, position_to_delete)
elif delete_choice == '3':
start_index = int(input("Enter the starting index of the slice to delete: "))
end_index = int(input("Enter the ending index of the slice to delete: "))
delete_by_slice(user_list, start_index, end_index)
else:
print("Invalid choice for deletion.")
else:
print("Invalid choice. Please choose a number between 1 and 4.")
OUTPUT:
Options:
1. Insert an element into the list
2. Delete an element from the list
3. Display the current list
4. Exit
Enter your choice (1-4): 1
Enter the element to insert: 42
Element 42 inserted into the list.
Options:
1. Insert an element into the list
2. Delete an element from the list
3. Display the current list
4. Exit
Enter your choice (1-4): 3
Current List: ['42']
Options:
1. Insert an element into the list
2. Delete an element from the list
3. Display the current list
4. Exit
Enter your choice (1-4): 2
Delete Options:
1. Delete by value
2. Delete by position
3. Delete by list slice
Enter your deletion choice (1-3): 2
Enter the position to delete: 0
Element at position 0 (42) deleted.
Options:
1. Insert an element into the list
2. Delete an element from the list
3. Display the current list
4. Exit
Enter your choice (1-4): 3
Current List: []
Options:
1. Insert an element into the list
2. Delete an element from the list
3. Display the current list
4. Exit
Enter your choice (1-4): 4
Exiting the program. Goodbye!
18. Write a python program to input names of n students and store them in a tuple.
Also input a name from the user and find if this student is present in the tuple or
not.
CODING:
n = int(input("Enter the number of students: "))
if search_name in students:
print(f"{search_name} is present in the tuple.")
else:
print(f"{search_name} is not present in the tuple.")
OUTPUT:
Enter the number of students: 3
Enter the name of student 1: Alice
Enter the name of student 2: Bob
Enter the name of student 3: Charlie
Enter a name to check in the tuple: Bob
Bob is present in the tuple.
OUTPUT:
Enter a tuple of numbers separated by space: 10 25 5 8 12
Largest number: 25
Smallest number: 5
20. Write a python program to add the integers in a Tuple and display the sum.
CODING:
numbers_tuple = tuple(map(int, input("Enter a tuple of integers separated by space:
").split()))
sum_of_numbers = sum(numbers_tuple)
OUTPUT:
Enter a tuple of integers separated by space: 10 25 5 8 12
The sum of the integers is: 60
21. Write a python program to search for the given element in the tuple.
CODING:
elements_tuple = tuple(input("Enter a tuple of elements separated by space: ").split())
if search_element in elements_tuple:
print(f"{search_element} is present in the tuple.")
else:
print(f"{search_element} is not present in the tuple.")
OUTPUT:
Enter a tuple of elements separated by space: apple banana orange mango
Enter the element to search for: banana
banana is present in the tuple.
22. Write a python program to find the highest two values in a dictionary
CODING:
def find_highest_two_values(dictionary):
values = list(dictionary.values())
if len(values) < 2:
return None, None
return highest_values
input_dict = {"a": 10, "b": 25, "c": 15, "d": 30, "e": 20}
highest_values = find_highest_two_values(input_dict)
OUTPUT:
The highest two values are: 30 and 25
23. Write a Python program to sort a dictionary by key.
CODING:
def sort_dict_by_key(input_dict):
sorted_dict = dict(sorted(input_dict.items()))
return sorted_dict
input_dict = {"b": 25, "a": 10, "d": 30, "c": 15, "e": 20}
sorted_dict = sort_dict_by_key(input_dict)
OUTPUT:
Sorted Dictionary by Key:
a: 10
b: 25
c: 15
d: 30
e: 20
24. Write a Python program to print a dictionary where the keys are numbers
between 1 and 15 (both included) and the values are square of keys.
CODING:
square_dict = {key: key**2 for key in range(1, 16)}
OUTPUT:
Dictionary with keys as numbers between 1 and 15 and values as square of keys:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169,
14: 196, 15: 225}
25. Write a Python program to create a third dictionary from two dictionaries having
some common keys, in way so that the values of common keys are added in the
third dictionary.
CODING:
def create_third_dict(dict1, dict2):
third_dict = {}
return third_dict
OUTPUT:
Third Dictionary with added values of common keys:
{'a': 10, 'b': 25, 'c': 45, 'd': 25}