Robotics & Artificial Intelligence
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)
Related Questions
Tuples are …………… objects.
- Mutable
- Immutable
- Non-static
- Infinite
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.
Write a Python program to display all the elements of a tuple using the for loop.
Write a Python program to create a list of first ten natural numbers. Add the square of each element to another empty list.