Saturday, April 14, 2018

Sample Program – Python List

Posted by AnithaKamatchi on Saturday, April 14, 2018 in , | No comments


pythonList.py

subjects = ['Maths','Physics','Chemistry']
marks = [190,178,185]

print("*****Score Details*****")
print(subjects[0],':',marks[0])
print(subjects[1],':',marks[1])
print(subjects[2],':',marks[2])

list_1,list_2 = [1,2,3,4],[1,7,8]

print('\nList 1: ',list_1,'\nList count: ',len(list_1),'\nMax Value: ',max(list_1),'\nMin Value: ',min(list_1))
print('List 2: ',list_2 )

#inserting into list_2
list_2.append(10)
print('List 2 after append: ',list_2 )

#extending list_2 with sequence of values
list_2.extend([9,11,12])
print('List 2 after extend: ',list_2 )

#reversing the list
list_2.reverse()
print("Reverse list: ",list_2)

#finding the index of object in list
print('Index of 9 in list 2: ',list_2.index(9))

#sorting the list desc
list_2.sort(reverse= True)
print("List after sorting: ",list_2)

#removing object from the list
list_2.remove(11)
print("List after removing 11: ",list_2)

#updating object in the list
list_2[0]=13
print("List after updating index 0 with 13: ",list_2)

#displaying a range from a list
print("List with first 5 values: ",list_2[0:5])
print("List from 2nd index: ",list_2[1:])



output:
*****Score Details*****
Maths : 190
Physics : 178
Chemistry : 185

List 1:  [1, 2, 3, 4]
List count:  4
Max Value:  4
Min Value:  1
List 2:  [1, 7, 8]
List 2 after append:  [1, 7, 8, 10]
List 2 after extend:  [1, 7, 8, 10, 9, 11, 12]
Reverse list:  [12, 11, 9, 10, 8, 7, 1]
Index of 9 in list 2:  2
List after sorting:  [12, 11, 10, 9, 8, 7, 1]
List after removing 11:  [12, 10, 9, 8, 7, 1]
List after updating index 0 with 13:  [13, 10, 9, 8, 7, 1]
List with first 5 values:  [13, 10, 9, 8, 7]
List from 2nd index:  [10, 9, 8, 7, 1]



0 comments:

Post a Comment