0% found this document useful (0 votes)
18 views22 pages

Cs Record

This document is a certificate and index of laboratory work completed by a student named Ria Verma in Computer Science for the academic year 2023-24 at Bright Riders School, Abu Dhabi. It includes a list of Python programming tasks and their corresponding codes and outputs. The tasks cover various programming concepts such as input/output, conditionals, loops, and data structures.

Uploaded by

Arnav Verma
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)
18 views22 pages

Cs Record

This document is a certificate and index of laboratory work completed by a student named Ria Verma in Computer Science for the academic year 2023-24 at Bright Riders School, Abu Dhabi. It includes a list of Python programming tasks and their corresponding codes and outputs. The tasks cover various programming concepts such as input/output, conditionals, loops, and data structures.

Uploaded by

Arnav Verma
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/ 22

BRIGHT RIDERS SCHOOL

Post Box No. 39665, Abu Dhabi, UAE

ACADEMIC YEAR : 2023-24

ROLL NO : 22

NAME : Ria Verma

CLASS : XI

SUBJECT : COMPUTER SCIENCE

SUB CODE : 083


BRIGHT RIDERS SCHOOL
Post Box No. 39665, Abu Dhabi, UAE

CERTIFICATE

This is to certify that Cadet ______________________________________

Roll No: __________________________ has satisfactorily completed the laboratory work

in COMPUTER SCIENCE (083) Python laid down in the regulations of CBSE for the

purpose of Practical Examination _________ in Class XI to be held in Bright Riders

School, Abu Dhabi on______________.

(Mrs.Nithya)
PGT for Computer Science

Examiners:
1 Name:Mrs.Nithya Signature:
(Internal)
INDEX
S.NO DATE PROGRAMS PAGE NO SIGNATURE

PYTHON PROGRAMS

1. Python program to Input two numbers and display


1 the larger and smaller number.
Python program to Input three numbers and display
2. the largest / smallest number.
Write a program to input the value of x and n and
3. print the sum of the following series:
1+x+x2+x3+x4+ ............xn
Write a program to find the sum of following series
4. x + x2/2 + ……….xn/n
Python program to Determine whether a number is
5. a perfect number.
Python program to Determine whether a number is
6. an Armstrong number.
Python program to Determine whether a number is
7. a palindrome.
Python program to Input a number and check if the
8. number is a prime or composite number.

Python program to Display the terms of a


9. Fibonacci series.

Python program to Compute the greatest common


10. multiple common divisor and least of two integers.

Python program to Count and display the number


of vowels, consonants, uppercase, lowercase
11.
characters in string.

12. Python program to Input a string and determine


2 whether it is a palindrome or not.
Generate the pattern using the nested loop:

13.

Write a python program to find the largest/smallest


14. number in a list.
Write a python program to input a list of numbers
15.
and swap elements at the even location with the
elements at the odd location.
Write a python program to add the integers in a list
16. and display the sum.
Write a python program that displays options for
inserting or deleting elements in a list. If user
17. 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.
Write a python program to input names of n
students and store them in a tuple. Also input a
18.
name from the user and find if this student is
present in the tuple or not.
Write a python program to find the largest/smallest
19. number in a Tuple.
Write a python program to add the integers in a
20. Tuple and display the sum.
Write python program to search for the given
21. element in the tuple.

Write a python program to find the highest two


22. values in a dictionary
Write a Python program to sort a dictionary by
23. key.
Write a Python program to print a dictionary where
24. the keys are numbers between 1 and 15 (both
included) and the values are square of keys.
Write a Python program to create a third dictionary
from two dictionaries having some common keys,
25. in way so that the values of common keys are
added in the third dictionary.

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: "))

if num1 > num2:


larger = num1
smaller = num2
elif num2 > num1:
larger = num2
smaller = num1
else:
print("Both numbers are equal.")

print(f"Larger number: {larger}")


print(f"Smaller number: {smaller}")
OUTPUT:
Enter the first number: 7.5
Enter the second number: 3.2
Larger number: 7.5
Smaller number: 3.2

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: "))

largest = max(num1, num2, num3)

smallest = min(num1, num2, num3)

print(f"Largest number: {largest}")


print(f"Smallest number: {smallest}")

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

for i in range(n + 1):


series_sum += x**i

print(f"The sum of the series is: {series_sum}")

OUTPUT:
Enter the value of x: 2
Enter the value of n: 3
The sum of the series is: 15.0

4. Write a program to find the sum of following series x + x^2/2 + ……….x^n/n


CODING:
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

series_sum = 0

for i in range(1, n + 1):


series_sum += x**i / i

print(f"The sum of the series is: {series_sum}")

OUTPUT:
Enter the value of x: 3
Enter the value of n: 4
The sum of the series is: 17.416666666666668

5. Python program to Determine whether a number is a perfect number.


CODING:
def is_perfect_number(number):
divisor_sum = 0

for i in range(1, number):


if number % i == 0:
divisor_sum += i

return divisor_sum == number

num = int(input("Enter a number: "))

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.

6. Python program to Determine whether a number is an Armstrong number.


CODING:
def is_armstrong_number(number):
num_str = str(number)
num_digits = len(num_str)
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
return armstrong_sum == number

num = int(input(" Enter a number: "))


if is_armstrong_number(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")

OUTPUT:
Enter a number: 1634
1634 is an Armstrong number.

7. Python program to Determine whether a number is a palindrome.


CODING:
def is_palindrome(number):
num_str = str(number)
return num_str == num_str[::-1]
num = int(input(" Enter a number: "))
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")

OUTPUT:
Enter a number: 121
121 is a palindrome.

8. Python program to Input a number and check if the number is a prime or


composite number.
CODING:
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
num = int(input(" Enter a number: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is a composite number.")
OUTPUT:
Enter a number: 13
13 is a prime number.
9. Python program to Display the terms of a Fibonacci series.
CODING:
def fibonacci_series(n):
fib_series = []
a, b = 0, 1
for _ in range(n):
fib_series.append(a)
a, b = b, a + b
return fib_series
num_terms = int(input(" Enter a number: "))
series = fibonacci_series(num_terms)
print("Fibonacci series:")
print(series)

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

return vowel_count, consonant_count, uppercase_count, lowercase_count

input_string = input("Enter a string: ")

vowels, consonants, uppercase, lowercase = count_characters(input_string)

print(f"Number of vowels: {vowels}")


print(f"Number of consonants: {consonants}")
print(f"Number of uppercase characters: {uppercase}")
print(f"Number of lowercase characters: {lowercase}")

OUTPUT:
Enter a string: Hello World123
Number of vowels: 3
Number of consonants: 7
Number of uppercase characters: 2
Number of lowercase characters: 8

12. Python program to Input a string and determine whether it is a palindrome or


not.
CODING:
user_input = input("Enter a string: ")

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

14. Write a python program to find the largest/smallest number in a list.


CODING:
def find_largest_and_smallest(numbers):
if not numbers:
return None, None

largest = smallest = numbers[0]

for number in numbers:


if number > largest:
largest = number
elif number < smallest:
smallest = number

return largest, smallest

numbers_list = [int(x) for x in input("Enter a list of numbers separated by space:


").split()]

largest_num, smallest_num = find_largest_and_smallest(numbers_list)

if largest_num is not None and smallest_num is not None:


print(f"Largest number: {largest_num}")
print(f"Smallest number: {smallest_num}")
else:
print("The list is empty.")

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)

print("List after swapping elements at even and odd locations:", result_list)


OUTPUT:
Enter a list of numbers separated by space: 1 2 3 4 5 6 7 8
List after swapping elements at even and odd locations: [2, 1, 4, 3, 6, 5, 8, 7]

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)

print(f"The sum of the integers is: {sum_of_numbers}")


OUTPUT:
Enter a list of integers separated by space: 10 25 5 8 12
The sum of the integers is: 60

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")

def delete_by_value(lst, value):


if value in lst:
lst.remove(value)
print(f"Element {value} deleted.")
else:
print(f"Element {value} not found in the list.")

def delete_by_position(lst, position):


if 0 <= position < len(lst):
deleted_element = lst.pop(position)
print(f"Element at position {position} ({deleted_element}) deleted.")
else:
print(f"Invalid position. Position should be between 0 and {len(lst) - 1}.")

def delete_by_slice(lst, start, end):


if 0 <= start <= end <= len(lst):
del lst[start:end]
print(f"List slice from index {start} to {end} deleted.")
else:
print("Invalid slice indices.")

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.")

elif choice == '2':


display_delete_submenu()
delete_choice = input("Enter your deletion choice (1-3): ")

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.")

elif choice == '3':


print("Current List:", user_list)

elif choice == '4':


print("Exiting the program. Goodbye!")
break

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: "))

students = tuple(input(f"Enter the name of student {i + 1}: ") for i in range(n))

search_name = input("Enter a name to check in the tuple: ")

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.

19. Write a python program to find the largest/smallest number in a Tuple.


CODING:
def find_largest_and_smallest(numbers_tuple):
if not numbers_tuple:
return None, None

largest = smallest = numbers_tuple[0]

for number in numbers_tuple:


if number > largest:
largest = number
elif number < smallest:
smallest = number

return largest, smallest

numbers_tuple = tuple(map(int, input("Enter a tuple of numbers separated by space:


").split()))

largest_num, smallest_num = find_largest_and_smallest(numbers_tuple)

if largest_num is not None and smallest_num is not None:


print(f"Largest number: {largest_num}")
print(f"Smallest number: {smallest_num}")
else:
print("The tuple is empty.")

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)

print(f"The sum of the integers is: {sum_of_numbers}")

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())

search_element = input("Enter the element to search for: ")

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

highest_values = sorted(values, reverse=True)[:2]

return highest_values

input_dict = {"a": 10, "b": 25, "c": 15, "d": 30, "e": 20}

highest_values = find_highest_two_values(input_dict)

if highest_values is not None:


print(f"The highest two values are: {highest_values[0]} and {highest_values[1]}")
else:
print("The dictionary should have at least two values for comparison.")

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)

print("Sorted Dictionary by Key:")


for key, value in sorted_dict.items():
print(f"{key}: {value}")

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)}

print("Dictionary with keys as numbers between 1 and 15 and values as square of


keys:")
print(square_dict)

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 = {}

for key in set(dict1.keys()) | set(dict2.keys()):


if key in dict1 and key in dict2:
third_dict[key] = dict1[key] + dict2[key]
elif key in dict1:
third_dict[key] = dict1[key]
elif key in dict2:
third_dict[key] = dict2[key]

return third_dict

dict1 = {"a": 10, "b": 20, "c": 30}


dict2 = {"b": 5, "c": 15, "d": 25}

third_dict = create_third_dict(dict1, dict2)

print("Third Dictionary with added values of common keys:")


print(third_dict)

OUTPUT:
Third Dictionary with added values of common keys:
{'a': 10, 'b': 25, 'c': 45, 'd': 25}

You might also like