-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.py
More file actions
16 lines (11 loc) · 835 Bytes
/
list.py
File metadata and controls
16 lines (11 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Program to print second largest of given numbers
#create an empty list
numbers=[]
n=int(input("Length of the list : ")) #taking the length of list as input from user
for i in range(1,n+1): #looping means to execute a block of code in repeated manner, In this case the loop will iterate n times as the given range id from 1->n
num=int(input("Enter number " + str(i) + ": ")) #Taking numbers as input from user and typecasting "i" which is an intger to string, as we can't concatenate string and integer
numbers.append(num) #appending the numbers to the list
numbers.sort() #sorting the list in ascending manner using sort() method
sorted_list=(sorted(numbers)) #Sorted list
# print(sorted_list)
print( "The second largest number is : " + str(sorted_list[-2])) #accessing the second element from last using negative index