Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views15 pages

Cs Practiical-Merged PDF

This document is a practical file for computer science students, containing various programming exercises and solutions. It covers topics such as tuples, dictionaries, lists, and basic input/output operations in Python. Each exercise includes a problem statement, a sample solution, and example outputs.

Uploaded by

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

Cs Practiical-Merged PDF

This document is a practical file for computer science students, containing various programming exercises and solutions. It covers topics such as tuples, dictionaries, lists, and basic input/output operations in Python. Each exercise includes a problem statement, a sample solution, and example outputs.

Uploaded by

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

COMPUTER

SCIENCE
PRACTICAL
FILE BY
AGASTYAMARWAH XI-D

ROLL NO -7
INDEX
Questions page no.
Q1 Input 4 element tuple counting 1
Q2 character no. of times input salary 2
Q3 and names in dictionary converting 3
Q4 no. into corresponding creating 2 4
Q5 list: positive & Negative Finding 5
Q6 Largest and 2nd largest no. Finding 6
Q7 Median Not containing duplicates 7
Q8 Inputting elements at positions 8
Q9 9
Q10 Deleting elements and values 10
Q11 Finding highest 2 values in dictionary 11
Q12 Creating dictionary from a string 12
Q13 Storing names & phones no. in 13
dictionary
Q. WAP to input 4 element tuple and unpack it to four
variables .then recreate tuple with swapped as 1st element
with 3rd and 2nd element with 4th element
Sol:# Input 4 element tuple
tpl = tuple(input("Enter 4 elements separated by space:
").split())
# Unpacking
a, b, c, d = tpl
# Swapping
swapped_tpl = (c, d, a, b)
print("Original Tuple:", tpl)
print("Swapped Tuple:", swapped_tpl)

Output Example:
Enter 4 elements separated by space: 1 2

3 4
Original Tuple: ('1', '2', '3', '4')
Swapped Tuple: ('3', '4', '1', '2')
Q. Write a program to count the number of times a character
appears in a given string
Sol:string = input("Enter a string: ")
char = input("Enter the character to count: ")
count = string.count(char)
print(f"The character {char} appears {count} times in the
string.")

Output Example:
Enter a string: hello world
Enter the character to count: o
The character 'o' appears 2 times in the
string.
Q. Write a program to enter names of employees and their
salaries as input and store them in a dictionary. Here n is to
input by the user.
Sol:n = int(input("Enter number of employees: "))
employees = {}
for _ in range(n):

name = input("Enter employee name: ")


salary = float(input("Enter salary: "))
employees[name] = salary
print("Employees Dictionary:", employees)

Output Example:
Enter number of employees: 2
Enter employee name: Alice
Enter salary: 50000
Enter employee name: Bob
Enter salary: 60000
Employees Dictionary: {'Alice': 50000,
'Bob': 60000}
Q. Write a program to convert a number entered by the user
into its corresponding number in words. for example if the
input is 876 then the output should be ‘Eight Seven Six’.
Sol:def number_to_words(n):

words = {0: "Zero", 1: "One", 2: "Two", 3: "Three", 4:


"Four", 5: "Five",
6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"}
return - -.join(words[int(digit)] for digit in str(n))

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


print(f"{num} in words is: {number_to_words(num)}")

Output Example:
Enter a number: 876
876 in words is: Eight Seven Six
Q. Write a program to read a list of n integers (positive as
well as negative). Create two new lists, one having all positive
numbers and the other having all negative numbers from the
given list. Print all three lists.
Sol:n = int(input("Enter number of integers: "))
numbers = [int(input("Enter number: ")) for _ in range(n)]
positive = [num for num in numbers if num > 0]
negative = [num for num in numbers if num < 0]

print("Original List:", numbers)


print("Positive Numbers:", positive)
print("Negative Numbers:", negative)

Output Example:
Enter number of integers: 5
Enter number: -1 Enter number:
2 Enter number: -3 Enter
number: 4 Enter number: 5
Original List: [-1, 2, -3, 4, 5]
Positive Numbers: [2, 4, 5]
Negative Numbers: [-1, -3]
Q. Write a program to find the largest and the second largest
elements in a given list of elements.
Sol:n = int(input("Enter number of elements: "))
elements = [int(input("Enter element: ")) for _ in range(n)]
largest = second_largest = float( -inf )
for num in elements:
if num > largest:

second_largest = largest
largest = num
elif largest > num > second_largest:
second_largest = num

print("Largest Element:", largest)


print("Second Largest Element:", second_largest)
Output Example:
Enter number of elements: 5 Enter
element: 4 Enter element: 1 Enter
element: 2 Enter element: 3
Enter element: 5
Largest Element: 5 Second Largest
Element: 4
Q. Write a program to read a list of n integers and find their
median. Note: The median value of a list of values is the
middle one when they are arranged in order. If there are two
middle values then take their average. Hint: Use an inbuilt
function to sort the list.
Sol:n = int(input("Enter number of integers: "))
numbers = [int(input("Enter number: ")) for _ in range(n)]
numbers.sort()
ifn%2==0:

median = (numbers[n // 2 - 1] + numbers[n // 2]) / 2


else:
median = numbers[n // 2]

print("Sorted List:", numbers)


print("Median:", median)
Output Example:
Enter number of integers: 5
Enter number: 1 Enter
number: 4 Enter number: 2
Enter number: 3 Enter
number: 5 Sorted List: [1, 2,
3, 4, 5] Median: 3
Q. Write a program to read a list of elements. Modify this list
so that it does not contain any duplicate elements i.e. all
elements occurring multiple times in the list should appear
only once.
Sol:n = int(input("Enter number of elements: "))
elements = [input("Enter element: ") for _ in range(n)]
unique_elements = list(set(elements))

print("Original List:", elements)


print("List after removing duplicates:", unique_elements)

Output Example:
Enter number of elements: 5 Enter
element: apple Enter element:
banana Enter element: apple
Enter element: orange Enter
element: banana Original List:
['apple', 'banana', 'apple', 'orange',
'banana'] List after removing
duplicates: ['orange', 'banana',
'apple']
Q. Write a program to create a list of elements. Input an
element from the user that has to be inserted in the list. Also
input the position at which it is to be inserted.
Sol:elements = input("Enter elements of the list separated by
space: ").split()
new_element = input("Enter element to insert: ")
position = int(input("Enter position to insert the element: "))
elements.insert(position, new_element)
print("Updated List:", elements)

Output Example:
Enter elements of the list separated by
space: 1 2 3
Enter element to insert: 4
Enter position to insert the element: 1
Updated List: ['1', '4', '2', '3']
Q. Write a program to read elements of a list and do the
following. a) The program should ask for the position of the
element to be deleted from the list and delete the element at
the desired position in the list. b) The program should ask for
the value of the element to be deleted from the list and
delete this value from the list.
Sol:elements = input("Enter elements of the list separated by space:
").split()
# Delete by position

pos = int(input("Enter position to delete: "))

if 0 <= pos < len(elements):

elements.pop(pos)

else:

print("Position out of range")

# Delete by value

val = input("Enter value to delete: ")

elements = [elem for elem in elements if elem != val]

print("Updated List:", elements)


Output Example:
Enter elements of the list separated by
space: 1 2 3 4 Enter position to delete: 1
Enter value to delete: 3 Updated List:
['1', '4']
Q. Write a Python program to find the highest 2 values in a
dictionary.
Sol:data = {'a': 10, 'b': 20, 'c': 30, 'd': 5}
sorted_values = sorted(data.values(), reverse=True)
highest_two = sorted_values[:2]
print("Highest two values:", highest_two)

Output Example:
Highest two values: [30, 20]
Q. Write a Python program to create a dictionary from a string
‘w3resource’ such that each individual character mates a key and its
index value for fist occurrence males the corresponding value in
dictionary. Expected output : {'3': 1, 's': 4, 'r': 2, 'u': 6, 'w': 0, 'c': 8,'e':
3, 'o': 5}
Sol:# Given string

s = 'w3resource'

# Creating dictionary to store characters and their first index

char_index_dict = {}

# Loop through the string and store the index of each character

for index, char in enumerate(s):

if char not in char_index_dict: # Check if character is already in


dictionary

char_index_dict[char] = index

# Display the resulting dictionary

print("Character Dictionary:", char_index_dict)


Expected Output:
Character Dictionary: {'w': 0, '3': 1, 'r': 2,
'e': 3, 's': 4, 'o': 5, 'u': 6, 'c': 8}
Q. Write a program to input your friend’s, names and their phone numbers and
store them in the dictionary as the key-value pair. Perform the following
operations on the dictionary: a) Display the Name and Phone number for all
your friends. b) Add a new key-value pair in this dictionary and display the
modified dictionary
Sol:n = int(input("Enter number of friends: "))

contacts = {}

for _ in range(n):

name = input("Enter friend*s name: ")

phone = input("Enter phone number: ")

contacts[name] = phone

print("Friend*s Contacts:", contacts)

# Add new friend

new_name = input("Enter new friend*s name: ")

new_phone = input("Enter new friend*s phone number: ")

contacts[new_name] = new_phone

print("Updated Contacts:", contacts)

Output Example:
Enter number of friends: 2 Enter friend's name: Alice Enter
phone number: 1234567890 Enter friend's name: Bob Enter
phone number: 0987654321 Friend's Contacts: {'Alice':
'1234567890', 'Bob': '0987654321'} Enter new friend's name:
Charlie Enter new friend's phone number: 1122334455 Updated
Contacts: {'Alice': '1234567890', 'Bob': '0987654321', 'Charlie':
'1122334455'}

You might also like