Ramjas International School
Sector 4, R. K. Puram New Delhi - 110022
ANNUAL PRACTICAL EXAM
2024-25
COMPUTER SCIENCE
083
PROGRAM FILE
BATCH 2
Submit To: Submit By:
Mr. Gajendra Sharma Name: Ruchika Goyal
PGT Computer Science Class/Section: XI-D
Roll Number: 30
String Programs:
1. Find all the vowels in a given string.
def find_vowels(s):
vowels = "aeiouAEIOU"
found_vowels = [char for char in s if char in vowels]
return found_vowels
# Example usage
string = input("Enter a string: ")
vowel_list = find_vowels(string)
print("Vowels in the string:", vowel_list)
2. Count the number of words in a sentence.
# Function to count words in a sentence
def count_words(sentence):
words = sentence.split() # Split the sentence into words using spaces
return len(words) # Return the number of words
# Input from the user
text = input("Enter a sentence: ")
# Function call and output
word_count = count_words(text)
print("Number of words in the sentence:", word_count)
3. Replace all occurrences of a substring in a string.
# Function to replace all occurrences of a substring
def replace_substring(text, old_sub, new_sub):
return text.replace(old_sub, new_sub) # Using the replace() method
# Input from the user
text = input("Enter the original string: ")
old_sub = input("Enter the substring to be replaced: ")
new_sub = input("Enter the new substring: ")
# Function call and output
modified_text = replace_substring(text, old_sub, new_sub)
print("Modified string:", modified_text)
4. Check if a string starts and ends with the same character.
# Function to check if a string starts and ends with the same character def
check_start_end(string):
if len(string) == 0: # Check if the string is empty return False return string[0].lower() ==
string[-1].lower() # Compare first and last character (case-insensitive)
# Input from the user text = input("Enter a string: ")
# Function call and output if check_start_end(text):
print("The string starts and ends with the same character.")else:
print("The string does not start and end with the same character.")
5. Find the first non-repeating character in a string.
def first_non_repeating_char(s):
char_count = {}
# Count occurrences of each character for char in s:
char_count[char] = char_count.get(char, 0) + 1
# Find the first character with a count of 1 for char in s:
if char_count[char] == 1: return cha return None
# Return None if no unique character is found
# Example Usage
s = "swiss" print(first_non_repeating_char(s)) # Output: "w"
List Programs:
6. Find the sum of all even numbers in a list.
def sum_of_evens(lst):
return sum(num for num in lst if num % 2 == 0)
# Example Usage
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(sum_of_evens(numbers)) # Output: 30
7. Split a list into two halves.
def split_list(lst):
mid = len(lst) // 2 # Find the middle index
return lst[:mid], lst[mid:]
# Example Usage
numbers = [1, 2, 3, 4, 5, 6, 7]
first_half, second_half = split_list(numbers)
print(first_half) # Output: [1, 2, 3]
print(second_half) # Output: [4, 5, 6, 7]
8. Find the product of all elements in a list.
def product_of_list(lst):
product = 1 # Initialize product as 1
for num in lst:
product *= num # Multiply each element
return product
# Example Usage
numbers = [1, 2, 3, 4, 5]
print(product_of_list(numbers)) # Output: 120
9. Remove all occurrences of a specific element from a list.
def remove_element(lst, target):
new_list = [] # Create an empty list to store non-target elements for num in lst:
if num != target: # Keep only elements that are not equal to the target
new_list.append(num)
return new_list
# Example Usage
numbers = [1, 2, 3, 4, 2, 5, 2, 6]
target = 2
print(remove_element(numbers, target)) # Output: [1, 3, 4, 5, 6]
10.Find the index of the maximum element in a list.
def index_of_max(lst):
if not lst: # Check if the list is empty
return -1 # Return -1 if the list has no elements
max_index = 0 # Assume the first element is the maximum
max_value = lst[0]
for i in range(1, len(lst)): # Start from index 1
if lst[i] > max_value:
max_value = lst[i]
max_index = i # Update max index return max_index
# Example Usage
numbers = [10, 25, 38, 45, 30, 45]
print(index_of_max(numbers)) # Output: 3 (first occurrence of 45)
Tuple Programs:
11.Convert a tuple of strings into a single concatenated string.
def concatenate_tuple_strings(tup):
result = "" # Initialize an empty string
for word in tup:
result += word # Concatenate each string
return result
# Example Usage
words = ("Hello", "World", "Python")
print(concatenate_tuple_strings(words)) # Output: "HelloWorldPython"
12.Find the length of each element in a tuple of strings.
def length_of_each_string(tup):
lengths = [] # Create an empty list to store length for word in tup:
lengths.append(len(word)) # Append length of each word return lengths
# Example Usage
words = ("apple", "banana", "cherry")
print(length_of_each_string(words)) # Output: [5, 6, 6]
13.Unpack a tuple into several variables.
# Defining a tuple
fruits = ("apple", "banana", "cherry")
# Unpacking the tuple into variables
fruit1, fruit2, fruit3 = fruits
# Printing the variables
print(fruit1) # Output: apple
print(fruit2) # Output: banana
print(fruit3) # Output: cherry
14.Multiply all elements in a tuple of numbers.
def multiply_tuple_elements(tup):
product = 1 # Initialize product as 1for num in tup:
product *= num # Multiply each element return product
# Example Usage
numbers = (2, 3, 4, 5)
print(multiply_tuple_elements(numbers)) # Output: 120
15.Create a nested tuple and access its elements.
# Creating a nested tuple
nested_tuple = ((1, 2, 3), ("apple", "banana", "cherry"), (True, False))
# Accessing elements
print(nested_tuple[0]) # Output: (1, 2, 3) -> First inner tuple
print(nested_tuple[1][1]) # Output: banana -> Second inner tuple, second element
print(nested_tuple[2][0]) # Output: True -> Third inner tuple, first element
Dictionary Programs:
16.Create a dictionary from two lists: one for keys, another for values.
def create_dict(keys, values):
dictionary = {} # Initialize an empty dictionary
for i in range(len(keys)): # Iterate over index positions
dictionary[keys[i]] = values[i] # Assign key-value pairs
return dictionary
# Example Usage
keys = ["name", "age", "city"]
values = ["Alice", 25, "New York"]
print(create_dict(keys, values))
# Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
17.Update the value of a specific key in a dictionary.
def update_value(dictionary, key, new_value):
if key in dictionary: # Check if the key exists
dictionary[key] = new_value # Update the value
else:
print("Key not found!") # Message if the key is not in the dictionary
# Example Usage
student = {"name": "Alice", "age": 16, "grade": "A"}
update_value(student, "age", 17) # Updating age from 16 to 17
print(student)
# Output: {'name': 'Alice', 'age': 17, 'grade': 'A'}
18.Remove a key-value pair from a dictionary.
def remove_key(dictionary, key):
return dictionary.pop(key, "Key not found!") # Removes key and returns value or
message
# Example Usage
student = {"name": "Alice", "age": 16, "grade": "A"}
print(remove_key(student, "age")) # Output: 16 (removed value)
print(student)
# Output: {'name': 'Alice', 'grade': 'A'}
19.Find the sum of all values in a numeric dictionary.
def sum_of_values(dictionary):
return sum(dictionary.values()) # Directly sum all values
# Example Usage
numbers = {"a": 10, "b": 20, "c": 30}
print(sum_of_values(numbers)) # Output: 60
20.Check if a value exists in a dictionary (not the key).
def value_exists(dictionary, target_value):
return target_value in dictionary.values() # Check directly
# Example Usage
student = {"name": "Alice", "age": 16, "grade": "A"}
print(value_exists(student, 16)) # Output: True
print(value_exists(student, "B")) # Output: False