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

Coding

The document discusses various operations that can be performed on lists in Python. It includes examples of how to: 1) Display lists in reverse order, print even/odd indexed elements. 2) Access, replace, insert, delete elements from a list by index or value. 3) Append elements to an empty list, sort the list, search for an element. 4) Find length, minimum, maximum values of a list. Sort a list, search for an element. 5) Calculate mean, variance, and standard deviation of numbers in a list.

Uploaded by

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

Coding

The document discusses various operations that can be performed on lists in Python. It includes examples of how to: 1) Display lists in reverse order, print even/odd indexed elements. 2) Access, replace, insert, delete elements from a list by index or value. 3) Append elements to an empty list, sort the list, search for an element. 4) Find length, minimum, maximum values of a list. Sort a list, search for an element. 5) Calculate mean, variance, and standard deviation of numbers in a list.

Uploaded by

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

1a) To display the list elements (in reverse order, even,

odd position elements)

CODE
list1=[1,2,3,4,5,6]

print(list1[::-1])

print(list1[::2])

print(list1[1::2])

OUTPUT
[6, 5, 4, 3, 2, 1]

[1, 3, 5]

[2, 4, 6]

1-b--------------------------------------------------------
# Create a list of integers

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

# Access an element based on index

print("Accessing elements:")

print("Element at index 2:", my_list[2]) # Output: 30

# Replace an element based on index

print("\nReplacing elements:")

my_list[1] = 25

print("Updated list:", my_list) # Output: [10, 25, 30, 40,


50]

# Insert an element at a specific index

print("\nInserting elements:")

my_list.insert(3, 35)

print("Updated list:", my_list) # Output: [10, 25, 30, 35,


40, 50]

# Delete an element based on index

print("\nDeleting elements:")
del my_list[4]

print("Updated list:", my_list) # Output: [10, 25, 30, 35,


50]

# Remove an element based on value

my_list.remove(30)

print("Updated list:", my_list) # Output: [10, 25, 35, 50]

OUTPUT

Accessing elements:

Element at index 2: 30

Replacing elements:

Updated list: [10, 25, 30, 40, 50]

Inserting elements:

Updated list: [10, 25, 30, 35, 40, 50]

Deleting elements:

Updated list: [10, 25, 30, 35, 50]

Updated list: [10, 25, 35, 50]

1-C----------------------------------------------------------------------------------------------------------------

# Create an empty list

my_list = []

# Append elements to the list

my_list.append(50)

my_list.append(30)

my_list.append(10)

my_list.append(40)

my_list.append(20)
# Display the original list

print("Original list:", my_list) # Output: [50, 30, 10, 40, 20]

# Sort the list in ascending order

my_list.sort()

# Display the sorted list

print("Sorted list:", my_list) # Output: [10, 20, 30, 40, 50]

# Search for an element in the list

search_element = 30

if search_element in my_list:

print(f"The element {search_element} is found at index:",


my_list.index(search_element))

else:

print(f"The element {search_element} is not found in the list.")

OUTPUT

Original list: [50, 30, 10, 40, 20]

Sorted list: [10, 20, 30, 40, 50]

The element 30 is found at index: 2

1-d----------------------------------------------------------------------------------------------------------------

list1= [1,2,3,1,4,2,5,3,6]

list2= [ ]

for i in (list1):

if i not in(list2):

list2.append(i)

print(list2)

OUTPUT

[1, 2, 3, 4, 5, 6]
I-e----------------------------------_______________________________________________

32, 2, 3, 5, 6]
E,To count the no. of times an element occurs in a
list.
code
list1=[]
n=int(input("range"))
for i in range(n):
list1.append(int(input()))
o=int(input("enter the element to check occurence"))
c=0
for i in range (len(list1)):
if o==list1[i]:
c+=1
print(o,"occured",c,"times")
OUTPUT
range10
1
1
1
1
1
2
1
31
1
2
enter the element to check occurence1
1 occured 7 times

1-
f-------------------------------------------------------------------------------------------------------------------
-

a) To find the length of the elements, minimum and


maximum values in a list
CODE
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
print("the length",len(list1))
print("the maximum of",max(list1))
print("the minimum no",min(list1))
OUTPUT
range5
1
5
2
52
1
the length 5
the maximum of 52
the minimum no 1
(2-a)To sort given list of elements
code
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
if list1[i] > list1[j]:
temp=list1[i]
list1[i]=list1[j]
list1[j]=temp
print(list1)
OUTPUT
range5
1
5
6
4
2
[1, 2, 4, 5, 6]
(2-b)To search the given number in a list
CODE
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
v=int(input("enter the element to search"))
for i in list1:
if i==v:
print(v,"element is found at ",i)
print(list1)
OUTPUT
range5
1
2
3
4
5
enter the element to search5
5 element is found at 5
[1, 2, 3, 4, 5]

(2-C)to find Mean, Variance and Standard Deviation for a list of


numbers.
CODE
import math
list1=[]
n=int(input("range"))
for i in range(n):
list1.append(int(input()))
while(1):
option=int(input("enter the option
1.mean\n2.variance\n3.standard deviation\n"))
if(option==1):
sum=0
for i in range(0,len(list1)):
sum=sum+list1[i]
mean=sum/n
print(mean)
elif(option==2):
variance=0
for i in range(0,n):
variance+=((list1[i]-mean)**2)
variance1=variance/n
print(variance1)
elif(option==3):
deviation=math.sqrt(variance1)
print(deviation)
else:
break
OUTPUT
range5
1
2
3
4
5
enter the option 1.mean
2.variance
3.standard deviation
1
3.0
enter the option 1.mean
2.variance
3.standard deviation
2
2.0
enter the option 1.mean
2.variance
3.standard deviation
3
1.4142135623730951
enter the option 1.mean
2.variance
3.standard deviation

You might also like