[go: up one dir, main page]

0% found this document useful (0 votes)
10 views10 pages

List Solutions and Answer

The document contains various Python programs and assignments demonstrating list operations, including finding the largest number, removing duplicates, generating Fibonacci sequences, and modifying list elements. It also covers list comprehensions, membership tests, and methods for appending, extending, and deleting elements from lists. Additionally, it includes error handling and checks for list properties such as emptiness.

Uploaded by

23053053
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)
10 views10 pages

List Solutions and Answer

The document contains various Python programs and assignments demonstrating list operations, including finding the largest number, removing duplicates, generating Fibonacci sequences, and modifying list elements. It also covers list comprehensions, membership tests, and methods for appending, extending, and deleting elements from lists. Additionally, it includes error handling and checks for list properties such as emptiness.

Uploaded by

23053053
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/ 10

#WAP TO GET THE LARGEST NUMBER FROM A LIST.

(USER INPUT)

numbers = list(map(int, input("Enter numbers: ").split()))

largest = numbers[0]

for num in numbers[1:]:

if num > largest:

largest = num

print("Largest number is:", largest)

#WAP TO REMOVE DUPLICATE FROM A LIST.(USER INPUT)

numbers = list(map(int, input("Enter numbers separated by space: ").split()))

unique_numbers = list(set(numbers))

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

#WAP TO FIND THE LIST OF WORDS THAT ARE LONGER THAN N FROM A GIVEN LIST

OF WORDS

words = input("Enter words separated by space: ").split()

n = int(input("Enter the minimum word length (N): "))

long_words = [word for word in words if len(word) > n]

print("Words longer than", n, "characters:", long_words)

LIST COMPREHENSION

#FACTORIAL OF 5

factorial = 1

for i in range(1, 6):

factorial *= i

print("Factorial of 5 is:", factorial)


#FIBONACI

n = int(input("Enter number of Fibonacci terms: "))

a, b = 0, 1

fibonacci = []

for _ in range(n):

fibonacci.append(a)

a, b = b, a + b

print("Fibonacci sequence:", fibonacci)

#CHECK THE NUMBER IS DIVISIBLE BY 2 & 5

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

if num % 2 == 0 and num % 5 == 0:

print(num, "is divisible by both 2 and 5")

else:

print(num, "is NOT divisible by both 2 and 5")

Assignment 1: Create a List Using list() Constructor

my_list = list(("apple", "banana", "cherry", 10, 20, 5.5))

print(my_list)

# ✅ Assignment 2: Modify Elements by Index

x = ["apple", 100, False, 45.6]

x[0] = "banana"

x[1] = 200

x[2] = True

print(x)
# ✅ Assignment 3: Replace Multiple Elements (Full Slice)

x[0:4] = ["A", 22.5, True, 0]

print(x)

# ✅ Assignment 4: Replace Elements with Step Slicing

x[0:4:2] = [99, "X"]

print(x)

# ✅ Assignment 5: Membership Test

nums = list(range(1, 11))

if 106 in nums:

print("106 is in the list")

else:

print("106 is not in the list")

# ✅ Assignment 6: Insert Item at Specific Index

nums.insert(0, 0)

print(nums)

# ✅ Assignment 7: Append Items to End

nums.append(11)

nums.append(12)

nums.append(13)

print(nums)
# ✅ Assignment 8: Extend List Without Extra Variable

nums.extend([14, 15, 16])

print(nums)

# ✅ Assignment 9: Extend List Using Another List

extra = [17, 18, 19]

nums.extend(extra)

print(nums)

# ✅ Assignment 10: Extend with Tuple

nums.extend(("x", "y", "z"))

print(nums)

# Explanation: .extend() treats a tuple like any other iterable and adds each item
individually.

# ✅ Assignment 11: Access Characters of a String

s = "Python"

for i in range(len(s)):

print(s[i]) # positive indexing

for i in range(-1, -len(s)-1, -1):

print(s[i]) # negative indexing

# ✅ Assignment 12: Type of Each Element

lst = ["Name", 90.5, 100, True]

for item in lst:

print(type(item))
# ✅ Assignment 13: Use len() Function

print(len(["Name", 90.5, 100, True]))

# ✅ Assignment 14: Create Mixed-Type List

lst = ["Hello", 42, 3.14, False]

print(lst[0])

print(lst[1])

print(lst[2])

print(lst[3])

# ✅ Assignment 15: Modify Element at Index 2

lst = [1, 2, 3, 4]

print("Before:", lst)

lst[2] = False

print("After:", lst)

# ✅ Assignment 16: Replace First Two Elements

lst = [1, 2, 3, 4]

print("Original:", lst)

lst[0:2] = ["x", "y"]

print("Modified:", lst)

# ✅ Assignment 17: Check Membership for a Word

words = ["java", "python", "c++"]


if "python" in words:

print("python is present")

# ✅ Assignment 18: Append List Inside a List vs Extend

list1 = [1, 2, 3]

list1.append([100, 200])

print("append result:", list1)

list1 = [1, 2, 3]

list1.extend([100, 200])

print("extend result:", list1)

# ✅ Assignment 19: Combine List with Set

s = {"a", "b"}

lst = [1, 2, 3]

lst.extend(s)

print(lst)

# Sets are unordered; elements are added individually.

# ✅ Assignment 20: Insert Multiple Elements Using Slicing

lst = [1, 2, 3, 4]

lst[1:3] = [111, 222, 333]

print(lst)

# Length increases due to more items inserted than replaced.

# ✅ Assignment 21: Remove Specific Value

x = ["Soumili", 78.67, 34, True]


x.remove(78.67)

print(x)

# ✅ Assignment 22: Remove by Index Using pop()

x.pop(0)

print(x)

# ✅ Assignment 23: Remove Last Two Items Without Index

a = [2, 5, 2, 4, 21, 35]

a.pop()

print(a)

a.pop()

print(a)

# ✅ Assignment 24: Clear the Entire List

b = [1, 3, 5, 76, 46, 78, 321, 23, 11]

b.clear()

print(b)

print(type(b))

# ✅ Assignment 25: Delete Entire List Using del

a = [2, 5, 2, 4, 21, 35]

del a

# print(a) # NameError: name 'a' is not defined

# Explanation: 'a' is deleted and no longer exists.


# ✅ Assignment 26: Delete an Element by Index

c = [2, 4, 6, 8, 9, 4]

del c[5]

print(c)

# ✅ Assignment 27: Traverse List with Loop

d = [2, 4, 5, 7, 73, 64, 4]

for item in d:

print(item)

# ✅ Assignment 28: Print First 3 Elements Using Index

for i in range(0, 3):

print(d[i])

# ✅ Assignment 29: Remove Non-Existent Item

try:

d.remove(999)

except ValueError:

print("Item not found in list.")

# ✅ Assignment 30: Pop All Items in a Loop

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

while lst:

print("Popped:", lst.pop())

print("Remaining:", lst)
# ✅ Assignment 31: Delete Middle Index Using del

x = ["a", "b", "c", "d", "e"]

del x[2]

print(x)

# ✅ Assignment 32: Clear Then Append

x = [1, 2, 3]

x.clear()

x.append("one")

x.append("two")

x.append("three")

print(x)

# ✅ Assignment 33: Check if List is Empty

if len(x) == 0:

print("List is empty")

else:

print("List is not empty")

# ✅ Assignment 34: Mixed Operations

x = []

x.append(10)

x.append(20)

x.append(30)

x.remove(20)
x.insert(1, 15)

x.pop()

print(x)

# ✅ Assignment 35: Delete Multiple Indices

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

del lst[3] # Delete higher index first

del lst[1] # Then lower

print(lst)

You might also like