What should be the output of the following program and why?
my_list=[2,3,4,5,6,7]
print(my_list[-7]) Answer
The program will produce an error.
The list my_list has 6 elements, so valid negative indices range from -1 to -6. The index -7 is out of range, hence Python cannot access any element at that position.
What should be the output of the following program and why?
list_new=[34, "Hi", 56.7, "Hello"]
list_new.extend(["India", "Happy"])
print(list_new) Answer
[34, 'Hi', 56.7, 'Hello', 'India', 'Happy']
The extend() function is used to add multiple elements at the end of a list. Here, the elements "India" and "Happy" are added to the existing list list_new as separate elements, not as a single list.
You can create a list with different data items by placing them inside single quotes.
Answer
False
Reason — A list is created by placing data items inside square brackets [ ], separated by commas.
The value of a list item can be easily updated.
Answer
True
Reason — A list is mutable, so its elements can be modified or updated after the list is created.
Each data item in the list is assigned a unique index value.
Answer
True
Reason — Each element in a list is assigned a unique index value that helps in identifying and accessing the data items in the list.
In Python, you cannot change the sequence of the index value of a list.
Answer
True
Reason — The index values of a list are fixed and are automatically assigned starting from 0 in sequence. While the elements of a list can be changed or rearranged, the sequence of index values itself cannot be changed.
You cannot remove the elements of a tuple.
Answer
True
Reason — A tuple is immutable, so its elements cannot be removed or modified individually. Only the entire tuple can be deleted.
You can sort the data items of the list using the sort() function.
Answer
True
Reason — The sort() function is used to arrange the elements of a list in ascending or descending order, so it can be used to sort the data items of a list.
You can use the index() method to return the index value of the tuple element.
Answer
True
Reason — In Python, a tuple supports the index() method, which returns the position (index) of the first occurrence of the given element in the tuple (and raises an error if the element is not found).
You can place an element in a list at your desired place using append() function.
Answer
False
Reason — The append() function adds an element only at the end of a list. To place an element at a desired position, the insert() function is used.
The in operator checks whether some elements are present in the tuple.
Answer
True
Reason — The in operator is used to check whether an element is present in the tuple.
To print all the elements of the tuple, you cannot use looping statements.
Answer
False
Reason — Looping statements can be used to print all the elements of a tuple.
You can get the ............... value of an element using the index() function.
- First
- Second
- Index
- Face
Answer
First
Reason — The index() method returns the index (position) of the first occurrence of the given element in a tuple/list. So, if the element appears multiple times, index() gives the first index value where it is found.
Python provides ............... function to place an element at a desired place in the list.
- extend()
- insert()
- append()
- add()
Answer
insert()
Reason — The insert() function is used to place an element at a desired position in a list.
The ............... function in Python is used to convert a list into a tuple.
- list()
- convert()
- tuple()
- list()
Answer
tuple()
Reason — The tuple() function is used to convert a list into a tuple.
A list created within another list is called a .............. .
- Substring
- Sub
- Nested list
- Tuple
Answer
Nested list
Reason — A list created within another list is called a nested list.
The ............... function in Python is used to add elements to an empty list.
- add()
- append()
- concatenate()
- fill()
Answer
append()
Reason — The append() function is used to add elements to an empty list.
To print an individual item of the tuple, you need to specify the ................ value of the item inside the square brackets.
- Order
- Index
- Zero
- Negative
Answer
Index
Reason — To access or print an individual element of a tuple, its index value is specified inside square brackets.
The ............... keyword is used to print the list elements in descending order.
- Sort
- Index
- Descend
- Reverse
Answer
Reverse
Reason — The keyword reverse=True is used with the sort() function to arrange and print list elements in descending order.
Tuples are ............... objects.
- Mutable
- Immutable
- Non-static
- Infinite
Answer
Immutable
Reason — Tuples are immutable objects, which means their elements cannot be changed after creation.
Fill in the blanks:
- Python offers a wide range of data structures often referred to as ................ .
- In an ................ list, the square brackets [] do not hold any value.
- The ................ function in Python can be used when we want to know the number of elements stored in a list.
- A tuple is an ................ sequence of items having any number of elements of different data types.
- Sorting is the process of ................ and displaying the elements of a list in ascending or descending order.
Answer
- Python offers a wide range of data structures often referred to as sequences.
- In an empty list, the square brackets [] do not hold any value.
- The len() function in Python can be used when we want to know the number of elements stored in a list.
- A tuple is an ordered sequence of items having any number of elements of different data types.
- Sorting is the process of arranging and displaying the elements of a list in ascending or descending order.
Write a Python program to demonstrate the difference between lists and tuples.
Answer
# Demonstrating difference between List and Tuple
# Creating a list
my_list = [10, 20, 30]
print("Original List:", my_list)
# Updating list element
my_list[1] = 25
print("Updated List:", my_list)
# Creating a tuple
my_tuple = (10, 20, 30)
print("Original Tuple:", my_tuple)
my_tuple[1] = 25
# Trying to update tuple element (will cause error)Write a Python program to display all the elements of a tuple using the for loop.
my_tuple = (23, 45, 67, 89, 12)
for i in my_tuple:
print(i)23
45
67
89
12
Write a Python program to create a list of first ten natural numbers. Add the square of each element to another empty list.
numbers = [1,2,3,4,5,6,7,8,9,10]
squares = []
for i in numbers:
squares.append(i * i)
print("Natural numbers:", numbers)
print("Squares of numbers:", squares)Natural numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Squares of numbers: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Write a Python program to convert a tuple into a list and a list into a tuple.
marks_tuple = (82, 64, 91, 79, 94)
marks_list = list(marks_tuple)
print("Tuple:", marks_tuple)
print("List after conversion:", marks_list)
numbers_list = [10, 20, 30, 40, 50]
numbers_tuple = tuple(numbers_list)
print("List:", numbers_list)
print("Tuple after conversion:", numbers_tuple)Tuple: (82, 64, 91, 79, 94)
List after conversion: [82, 64, 91, 79, 94]
List: [10, 20, 30, 40, 50]
Tuple after conversion: (10, 20, 30, 40, 50)
Write a Python program to search an element in the list.
number = [45, 67, 12, 56, 78]
e = int(input("Enter the element to search: "))
if e in number:
print("Element is found in the list.")
else:
print("Element is not found in the list.")Enter the element to search: 67
Element is found in the list.
Write a Python program to generate the square of all the elements of a list.
numbers = [2, 3, 4, 5, 7]
for i in numbers:
print(i * i)4
9
16
25
49
Write a Python program to add all the elements of the list.
numbers = [2, 3, 4, 5, 7]
total = 0
for i in numbers:
total = total + i
print("Sum of elements:", total)Sum of elements: 21
Write a program in Python to create a list of your classmates containing their names and roll numbers. Access the roll number of the student whose name is stored at 2nd index.
classmates = [
["Amit", 1],
["Neha", 2],
["Rohit", 3],
["Sneha", 4]
]
print("Roll number:", classmates[2][1])Roll number: 3
Find out one situation where lists are not suitable but the tuples are suitable.
Answer
Lists are not suitable when the data is fixed and should not be changed during program execution. In such cases, tuples are suitable because they are immutable (elements cannot be modified, added, or removed).
Example: Storing the days of the week.
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")Benefit: A tuple prevents accidental changes, making the data safer and more reliable (also slightly more memory-efficient than a list).