0% found this document useful (0 votes)
25 views4 pages

CS Practicals Code

-----------------------------------

Uploaded by

jayden meinrad
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)
25 views4 pages

CS Practicals Code

-----------------------------------

Uploaded by

jayden meinrad
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/ 4

CS PRACTICALS CODE

# 1. Update marks < 33 by adding 5

marks = [10, 45, 20, 67, 30]

marks = [mark + 5 if mark < 33 else mark for mark in marks]

print("Updated marks:", marks)

# 2. Remove duplicates

elements = [1, 2, 2, 3, 4, 4, 5]

unique_elements = list(set(elements))

print("List without duplicates:", unique_elements)

# 3. Update marks below average

marks = [10, 20, 30, 40, 50]

average = sum(marks) / len(marks)

marks = [mark + 5 if mark < average else mark for mark in marks]

print("Updated marks:", marks)

# 4. Insert element at specific position

lst = [1, 2, 3, 4, 5]

element = int(input("Enter element to insert: "))

position = int(input("Enter position: "))

lst.insert(position, element)

print("Updated list:", lst)

# 5. a) Delete by position

lst = [10, 20, 30, 40, 50]

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

if 0 <= position < len(lst):


del lst[position]

print("List after deletion:", lst)

# 5. b) Delete by value

lst = [10, 20, 30, 40, 50]

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

if value in lst:

lst.remove(value)

print("List after deletion:", lst)

# 5. c) Reverse in-place

lst = [1, 2, 3, 4, 5]

lst.reverse()

print("Reversed list:", lst)

# 6. a) Count occurrences

lst = [1, 2, 3, 1, 2, 1]

element = int(input("Enter element to count: "))

count = lst.count(element)

print(f"Element {element} occurs {count} times")

# 6. b) Positive and negative lists

lst = [-1, 2, -3, 4, -5]

positives = [x for x in lst if x > 0]

negatives = [x for x in lst if x < 0]

print("Original list:", lst)

print("Positive numbers:", positives)

print("Negative numbers:", negatives)


# 6. c) Largest element without max

lst = [1, 2, 3, 4, 5]

largest = sorted(lst)[-1]

print("Largest element:", largest)

# 6. d) Second largest element

lst = [1, 2, 3, 4, 5]

second_largest = sorted(lst)[-2]

print("Second largest element:", second_largest)

# 6. e) Median

lst = [1, 3, 3, 6, 7, 8, 9]

median = statistics.median(lst)

print("Median:", median)

# 7. Books containing 'a'

books = ["Java", "Python", "C++", "Scala"]

books_with_a = [book for book in books if 'a' in book.lower()]

print("Books with 'a':", books_with_a)

# 8. Swap even and odd indices

lst = [1, 2, 3, 4, 5]

for i in range(0, len(lst) - 1, 2):

lst[i], lst[i + 1] = lst[i + 1], lst[i]

print("Swapped list:", lst)

# 9. Menu-driven program for list operations

lst = [10, 20, 30, 40, 50]

while True:
print("1. Minimum element with index\n2. Mean\n3. Search\n4.
Frequency\n5. Reverse")

choice = int(input("Enter choice: "))

if choice == 1:

minimum = min(lst)

print("Minimum:", minimum, "Index:", lst.index(minimum))

elif choice == 2:

print("Mean:", sum(lst) / len(lst))

elif choice == 3:

element = int(input("Enter element to search: "))

print("Found at index:", lst.index(element) if element in lst else "Not


found")

elif choice == 4:

element = int(input("Enter element to count: "))

print("Frequency:", lst.count(element))

elif choice == 5:

lst.reverse()

print("Reversed list:", lst)

else:

break

You might also like