You are on page 1of 3

9-10-2023

Monday, October 9, 2023 7:04 PM

sort()

sort() is used to sort the list elements. Sorting means arranging either in ascending order
or in descending order. Default order is ascending order. In order to sort in descending
order we need to give value as "reverse=True"

l=[4,9,2,1,7,5]
l.sort() ascending order=small value to
print(l) greater value

output: descending order=greater value to


[1,2,4,5,7,9] smaller value

l=[4,9,2,1,7,5]
l.sort(reverse=True) step 1:
print(l) [1,2,4,5,7,9]

output: step 2:
[9,7,5,4,2,1] [9,7,5,4,2,1]

names=['grapes','banana','apple','orange']
names.sort()
print(names)

output:
D:\>py program1.py
['apple', 'banana', 'grapes', 'orange']

names=['grapes','banana','apple','orange']
names.sort(reverse=True)
print(names)

output:
['orange','grapes','banana','apple']

name=['ramya','rani','raju']
name.sort()
print(name)

Python 22 batch 7 pm Page 1


output:
D:\>py program1.py
['raju', 'ramya', 'rani']

Write a program to find largest value in the list.

n=int(input("Enter number of values in the list:"))


list=[] n=5
for x in range(n): list=[]
value=int(input("Enter the value in the list:")) range(5)
list.append(value) 0,1,2,3,4
list.sort()
print(list) Enter value in the list:23
print("{} is the largest value in the list".format(list[-1])) value=23
[23]
Enter the value in the list:44
value=44
output: [23,44]
D:\>py program1.py Enter the value in the list:65
Enter number of values in the list:5 value=65
Enter the value in the list:23 [23,44,65]
Enter the value in the list:44 Enter the value in the list:21
Enter the value in the list:65 value=21
Enter the value in the list:21 [23,44,65,21]
Enter the value in the list:17 Enter the value in the list:17
[17, 21, 23, 44, 65] value=17
65 is the largest value in the list [23,44,65,21,17]
[17,21,23,44,65]

[33,66,77,88,99,100]

Aliasing and cloning of a list

Aliasing:

The process of assigning a reference variable for existing list(storing the same list in
another reference variable) is called aliasing

l1=[10,20,30,40]
l2=l1
10 20 30 40
Python 22 batch 7 pm Page 2
l1=[10,20,30,40]
l2=l1
10 20 30 40
print(id(l1))
print(id(l2))
l1
l1=[10,20,30,40] l2
l2=l1
l1[1]=777 [10,777,30,40]
print(l1) a.[10,20,30,40]
print(l2) b.[10,777,30,40]

output:
D:\>py program1.py
[10, 777, 30, 40]
[10, 777, 30, 40]

cloning:

Python 22 batch 7 pm Page 3

You might also like