0% found this document useful (0 votes)
5 views14 pages

Python Answers

The document contains a comprehensive list of Python exercises, covering various topics such as list manipulation, set operations, and basic statistics. Each exercise includes code snippets that demonstrate how to perform specific tasks like removing duplicates, finding averages, and manipulating data structures. The exercises are designed to help users practice and enhance their Python programming skills.
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)
5 views14 pages

Python Answers

The document contains a comprehensive list of Python exercises, covering various topics such as list manipulation, set operations, and basic statistics. Each exercise includes code snippets that demonstrate how to perform specific tasks like removing duplicates, finding averages, and manipulating data structures. The exercises are designed to help users practice and enhance their Python programming skills.
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

List of python exercises

1.

```

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

s = sum(nums)

avg = s / len(nums)

print("Sum =", s)

print("Average =", avg)

```

2.

```

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

res = []

for i in nums:

if i not in res:

[Link](i)

print("List after removing duplicates:")

print(*res)

```

3.

```

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

unique = list(set(nums))

[Link]()

print("Second largest element:", unique[-2])

```
4.

```

l1 = list(map(int, input("Enter first list:\n").split()))

l2 = list(map(int, input("Enter second list:\n").split()))

merged = l1 + l2

[Link]()

print("Merged and Sorted List:")

print(*merged)

```

5.

```

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

counts = {}

for i in nums:

counts[i] = [Link](i, 0) + 1

print("Element counts:")

for k in counts:

if counts[k] == 1:

print(k, "->", counts[k], "time")

else:

print(k, "->", counts[k], "times")

```

6.

```

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

k = int(input("Enter k:\n"))
k = k % len(nums)

rotated = nums[-k:] + nums[:-k]

print("Rotated List:")

print(*rotated)

```

7.

```

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

even = []

odd = []

for i in nums:

if i % 2 == 0:

[Link](i)

else:

[Link](i)

print("Even numbers:")

print(*even)

print("Odd numbers:")

print(*odd)

```

8.

```

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

print("Maximum =", max(nums))

print("Minimum =", min(nums))

```
9.

```

l1 = list(map(int, input("List 1:\n").split()))

l2 = list(map(int, input("List 2:\n").split()))

common = []

for i in l1:

if i in l2 and i not in common:

[Link](i)

print("Common elements:")

print(*common)

```

10.

```

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

res = [i for i in nums if i % 2 != 0]

print("List after removing even numbers:")

print(*res)

```

11.

```

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

prod = 1

for i in nums:

prod *= i

print("Product =", prod)

```
12.

```

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

print("Largest Difference =", max(nums) - min(nums))

```

13.

```

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

if nums == sorted(nums):

print("The list is sorted.")

else:

print("The list is not sorted.")

```

14.

```

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

neg = [i for i in nums if i < 0]

print("Negative numbers:")

print(*neg)

```

15.

```

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

pos = neg = zero = 0

for i in nums:

if i > 0:

pos += 1

elif i < 0:
neg += 1

else:

zero += 1

print("Positive:", pos)

print("Negative:", neg)

print("Zero:", zero)

```

16.

```

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

val = int(input("Enter value to remove:\n"))

res = [i for i in nums if i != val]

print("Updated List:")

print(*res)

```

17.

```

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

val = int(input("Enter element to search:\n"))

if val in nums:

print("Element found at index", [Link](val))

else:

print("Element not found")

```

18.

```

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

elem = int(input("Enter element:\n"))


pos = int(input("Enter position:\n"))

[Link](pos, elem)

print("Updated List:")

print(*nums)

```

19.

```

nested = eval(input())

flat = []

for sub in nested:

for i in sub:

[Link](i)

print("Flattened List:")

print(*flat)

```

20.

```

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

n = len(nums) + 1

total = n * (n + 1) // 2

print("Missing number:", total - sum(nums))

```

21.

```

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

non_zero = [i for i in nums if i != 0]

zeros = [i for i in nums if i == 0]

print("Updated List:")
print(*(non_zero + zeros))

```

22.

```

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

target = int(input("Enter target:\n"))

seen = set()

pairs = []

for num in nums:

if target - num in seen:

[Link]((target - num, num))

[Link](num)

print("Pairs:")

for p in pairs:

print(p)

```

23.

```

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

rev = []

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

[Link](nums[i])

print("Reversed List:")

print(*rev)

```

24.

```

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


largest = max(nums)

print("Largest element:", largest)

print("Frequency:", [Link](largest))

```

25.

```

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

mid = len(nums)//2

print("First half:")

print(*nums[:mid])

print("Second half:")

print(*nums[mid:])

```

1 Create a Set from List

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

s = set(lst)

print(s)

2 Add Elements

s = {1, 2, 3}

[Link](4)

[Link]([5, 6])

print(s)

3 Remove and Discard

s = {1, 2, 3}

[Link](2)

[Link](4)

print(s)

print("No error occurred while discarding 4.")

4 Clear a Set
s = {1, 2, 3}

[Link]()

print(s)

5 Union of Sets

A = {1, 2, 3}

B = {3, 4, 5}

print([Link](B))

6 Intersection of Sets

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

print([Link](B))

7 Difference of Sets

A = {1, 2, 3}

B = {2, 3, 4}

print([Link](B))

8 Symmetric Difference

A = {1, 2, 3}

B = {3, 4, 5}

print(A.symmetric_difference(B))

9 Subset Check

A = {1, 2}

B = {1, 2, 3}

print([Link](B))

10 Superset Check

A = {1, 2, 3, 4}

B = {2, 3}

print([Link](B))

11 Disjoint Check

A = {1, 2}

B = {3, 4}

print([Link](B))
12 Remove Duplicates from List

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

lst = list(set(lst))

print(lst)

13 Count Unique Words

sentence = " is easy and is fun"

words = set([Link]())

print(words)

print("Count:", len(words))

14 Find Missing Numbers

given = {1, 2, 4, 6, 7, 10}

full = set(range(1, 11))

print(full - given)

15 Find Common Elements Across Multiple Sets

A = {1, 2, 3}

B = {2, 3, 4}

C = {3, 4, 5}

print([Link](B, C))

16 Merge Multiple Sets

A = {1, 2}

B = {2, 3}

C = {3, 4}

print([Link](B, C))

17 Find Elements Only in One Set

A = {1, 2, 3}

B = {3, 4, 5}

print(A.symmetric_difference(B))

18 Update Set In-place

A = {1, 2}

B = {2, 3, 4}

[Link](B)
print(A)

19 Pop Element

A = {1, 2, 3}

popped = [Link]()

print("Popped element:", popped)

print("Remaining set:", A)

20 Frozen Set Example

fs = frozenset({1, 2, 3})

print(fs)

print("Cannot modify frozen set.")

1 .append

lst = [1, 2, 3]

element = 4

[Link](element)

print(lst)

2 .extend

lst = [1, 2, 3]

lst2 = [4, 5, 6]

[Link](lst2)

print(lst)

3 .insert

lst = [1, 2, 4]

index = 2

element = 3

[Link](index, element)

print(lst)

4 .remove

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

value = 2
[Link](value)

print(lst)

5 .pop With Index

lst = [1, 2, 3, 4]

index = 2

popped = [Link](index)

print("Popped element:", popped)

print("Remaining list:", lst)

5 .pop Without Index

lst = [1, 2, 3, 4]

popped = [Link]()

print("Popped element:", popped)

print("Remaining list:", lst)

6 .clear

lst = [1, 2, 3, 4]

[Link]()

print(lst)

7 .index

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

value = 2

print([Link](value))

8 .count

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

value = 2

print([Link](value))

9 .sort

lst = [3, 1, 4, 2]

[Link]()

print(lst)

9 sorted

lst = [3, 1, 4, 2]
new_lst = sorted(lst)

print(new_lst)

10 .reverse

lst = [1, 2, 3, 4]

[Link]()

print(lst)

11 .copy

lst = [1, 2, 3]

lst_copy = [Link]()

lst_copy.append(4)

print("Original list:", lst)

print("Copied list:", lst_copy)

You might also like