0% found this document useful (0 votes)
4 views6 pages

Python Module 2 Lists Answers

The document provides a series of Python programming examples demonstrating the use of for loops, list operations, and built-in functions. It includes programs for calculating sums, generating Fibonacci series, checking for prime numbers, and manipulating lists. Additionally, it explains concepts like break and continue statements, references, and the id() function.

Uploaded by

hegdea645
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)
4 views6 pages

Python Module 2 Lists Answers

The document provides a series of Python programming examples demonstrating the use of for loops, list operations, and built-in functions. It includes programs for calculating sums, generating Fibonacci series, checking for prime numbers, and manipulating lists. Additionally, it explains concepts like break and continue statements, references, and the id() function.

Uploaded by

hegdea645
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/ 6

21. Explain the for loop with an example.

for i in range(1, 6):

print(i)

22. Write a Python program to print the sum of first 10 natural numbers using for loop.

sum = 0

for i in range(1, 11):

sum += i

print("Sum:", sum)

23. Write a Python program to print multiplication table using for loop.

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

for i in range(1, 11):

print(f"{num} x {i} = {num * i}")

24. Write a Python program to print Fibonacci series up to n terms.

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

a, b = 0, 1

for _ in range(n):

print(a)

a, b = b, a + b

25. Write a Python program to check whether a number is prime or not.

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

if num > 1:

for i in range(2, num):

if num % i == 0:
print("Not Prime")

break

else:

print("Prime")

else:

print("Not Prime")

26. Write a Python program to find the factorial of a number using for loop.

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

fact = 1

for i in range(1, num+1):

fact *= i

print("Factorial:", fact)

27. Write a Python program to count the number of digits in a number.

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

count = 0

while num > 0:

num //= 10

count += 1

print("Digits:", count)

28. Write a Python program to reverse a number.

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

rev = 0

while num > 0:

rev = rev * 10 + num % 10


num //= 10

print("Reversed number:", rev)

29. Write a Python program to print a pattern.

for i in range(1, 6):

print("*" * i)

30. Explain the use of break and continue statements with examples.

for i in range(1, 6):

if i == 3:

break

print(i)

# Output: 1 2

for i in range(1, 6):

if i == 3:

continue

print(i)

# Output: 1 2 4 5

31. What is a list in Python?

A list is an ordered, mutable, and indexed collection of items in Python. It can contain elements of

different data types.

32. How to create a list? Give example.

Example:

my_list = [1, 2, 3, 'apple', 5.5]


33. Write a Python program to access elements from a list.

my_list = [10, 20, 30, 40]

print(my_list[0])

print(my_list[2])

34. Write a Python program to update elements of a list.

my_list = [1, 2, 3]

my_list[1] = 20

print(my_list)

35. Write a Python program to delete elements from a list.

my_list = [10, 20, 30, 40]

del my_list[1]

print(my_list)

my_list.remove(40)

print(my_list)

36. Explain list operations: +, *, membership, length, slicing.

- '+' combines two lists

- '*' repeats list elements

- 'in' checks membership

- len() gives list length

- Slicing: list[start:end]

37. Write a program to demonstrate list operations.

a = [1, 2]

b = [3, 4]
print(a + b)

print(a * 2)

print(2 in a)

print(len(b))

38. Explain built-in list functions with examples.

- append(): Adds element at end

- insert(): Inserts at position

- pop(): Removes by index

- remove(): Removes by value

- sort(): Sorts the list

- reverse(): Reverses the list

39. Write a program to demonstrate use of append(), insert(), remove(), pop().

l = [1, 2, 3]

l.append(4)

l.insert(1, 10)

l.remove(3)

l.pop()

print(l)

40. Explain references and id() function in lists.

A variable stores a reference to the object. id() returns the memory address of the object.

Example:

l1 = [1, 2, 3]

l2 = l1
print(id(l1), id(l2)) # Same IDs

You might also like