The document shows examples of how to manipulate lists in Python. It demonstrates concatenating two lists using addition and the extend method. An empty list is created and elements are added using list literals. User input is taken to create a list by splitting on spaces and converting to integers. An element is searched for in the list and a message is printed depending on whether it is found.
The document shows examples of how to manipulate lists in Python. It demonstrates concatenating two lists using addition and the extend method. An empty list is created and elements are added using list literals. User input is taken to create a list by splitting on spaces and converting to integers. An element is searched for in the list and a message is printed depending on whether it is found.
In [3]: # Create an empty List using the List constructor
my_list = list()
# Add elements to the List using List Literals
my_list += [1, 2, 3]
# Display the List
print(my_list) [1, 2, 3]
In [4]: # Get a string from the user
user_input = input("Enter a list of numbers separated by spaces: ")
# Split the string into a List of substrings
my_list = user_input.split()
# Convert the substrings to integers
my_list = [int(i) for i in my_list]
# Display the List
print("The list is:", my_list)
# Get the element to search for from the user
search_element = int(input("Enter an element to search for: "))
# Check if the element is in the List
if search_element in my_list: # If the element is in the List, display a message print(search_element, "is in the list.") else: # If the element is not in the List, display a different message print(search_element, "is not in the list.") Enter a list of numbers separated by spaces: 94 93 99 97 96 96 98 The list is: [94, 93, 99, 97, 96, 96, 98] Enter an element to search for: 96 96 is in the list.