0% found this document useful (0 votes)
6 views

Python_Class9_List

Uploaded by

hendjosie57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python_Class9_List

Uploaded by

hendjosie57
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)# Program to create a string list

# Initialize an empty list


string_list = []

# Get the number of strings to add


num_strings = int(input("Enter the number of strings you want to add: "))

# Loop to add strings to the list


for i in range(num_strings):
string = input(f"Enter string {i + 1}: ")
string_list.append(string)

# Display the created string list


print("\nThe created string list is:")
print(string_list)

2)# Program to create a numeric list and delete the 4th element

# Initialize an empty list


numeric_list = []

# Get the number of elements to add


num_elements = int(input("Enter the number of elements you want to add: "))

# Loop to add numbers to the list


for i in range(num_elements):
number = int(input(f"Enter number {i + 1}: "))
numeric_list.append(number)

# Display the original list


print("\nThe original numeric list is:")
print(numeric_list)

# Check if the list has at least 4 elements


if len(numeric_list) >= 4:
# Remove the 4th element (index 3)
removed_element = numeric_list.pop(3)
print(f"\nThe 4th element ({removed_element}) has been removed.")
else:
print("\nThe list has less than 4 elements. No element removed.")

# Display the updated list


print("\nThe updated numeric list is:")
print(numeric_list)

3)# Program to display the length of a list

# Initialize a list with some elements


my_list = [10, 20, 30, 40, 50]

# Display the list


print("The list is:", my_list)

# Get the length of the list


list_length = len(my_list)

# Display the length of the list


print("The length of the list is:", list_length)

4)# Program to insert an element at the beginning of a list

# Initialize a list
my_list = [10, 20, 30, 40]

# Display the original list


print("Original list:", my_list)

# Input the element to insert


element = input("Enter the element to insert at the beginning: ")

# Insert the element at the beginning


my_list.insert(0, element)

# Display the updated list


print("Updated list:", my_list)

5)Python program to get the positive and negative indices of a list without function.

# Example list
my_list = [10, 20, 30, 40, 50]
print("Element | Positive Index | Negative Index")
print("-----------------------------------------")

# Iterate through the list


for i in range(len(my_list)):
print(f" {my_list[i]} | {i} | {i - len(my_list)}")

6) reverse a list
my_list = [10, 20, 30, 40, 50]

# Reversing the list manually


reversed_list = []
for i in range(len(my_list) - 1, -1, -1):
reversed_list.append(my_list[i])

# Print the reversed list


print("Original List:", my_list)
print("Reversed List:", reversed_list)

7)WAP in python to create a numeric list and print only the negative and positive integers.

user_input = input("Enter numbers separated by spaces: ")


num_list = [int(num) for num in user_input.split()]

# Separate positive and negative integers


positive_integers = [num for num in num_list if num > 0]
negative_integers = [num for num in num_list if num < 0]

# Output the results


print("Original List:", num_list)
print("Positive Integers:", positive_integers)
print("Negative Integers:", negative_integers)

You might also like