sort():
====
It is a predefine function in Python while working with List data Type.The main
objective of sort() function is to sort the list objects from ascending to
decending
order.sort() function is only applicable for List data type
Ex1:
===
import time
L1=[1000,2,18,21,7,5]
print("Before_Operations:",L1)
print()
L2=L1.sort()
print("After_Operations:",L2)
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
L1=[1000,2,18,21,7,5]
print("Before_Operations:",L1)
print()
L1.sort()
print("After_Operations:",L1)
print()
L1.sort(reverse=True)
print("Reverse of List is:",L1)
print()
time.sleep(2)
print('End of an application')
Problem_1:
======
import time
L1=(1000,2,18,21,7,5)
print("Before_Operations:",L1)
print()
L1.sort()
print("After_Operations:",L1)
print()
L1.sort(reverse=True)
print("Reverse of List is:",L1)
print()
time.sleep(2)
print('End of an application')
OUTPUT:
======
Before_Operations: (1000, 2, 18, 21, 7, 5)
Traceback (most recent call last):
File "test1.py", line 5, in <module>
L1.sort()
AttributeError: 'tuple' object has no attribute 'sort'
->sorted():
========
It is a predefine function in Python while working with List data type.The main
objective of sorted() is to sort any data or information.sorted() function return
the
data or information inside list itself.
Ex1:
===
import time
L1=[1000,2,18,21,7,5]
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
L1=(1000,2,18,21,7,5)
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
Ex3:
==
import time
L1={1000,2,18,21,7,5}
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
Ex4:
==
import time
L1={7:1001,2:5002,1:1009}.keys()
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
L1={7:1001,2:5002,1:1009}.values()
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
Ex6:
===
import time
L1={7:1001,2:5002,1:1009}.items()
print("Before_Operations:",L1)
print()
L2=sorted(L1)
print("After_Operations:",L2)
print()
L2=sorted(L1,reverse=True)
print("Reverse of List is:",L2)
print()
time.sleep(2)
print('End of an application')
->Tuple data type:
============
Python supports Tuple data type.If you want to represent more than one object or
group of objects
as a single entity then we can go with Tuple data type.In Python Tuple data type
can be represent as
() and tuple().
Note:
====
While working with Tuple data type () are optional.
Ex1:
===
import time
T1=(1001)
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
T1=(1001,)
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
Ex1:
===
import time
T1=()
print(T1)
print()
print(type(T1))
print()
T2=tuple()
print(T2)
print()
print(type(T2))
print()
time.sleep(2)
print('End of an application')
Ex4:
===
import time
T1=1001,
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
T1=1001,1002,1003,1004,1005
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
Ex6:
===
import time
T1=(1001,1002,1003,1004,1005)
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
Properties for Tuple data type:
===================
->Inseration is preserved
->Duplicate objects are allowed
->Hetrogenious objects are allowed
->Tuple is a immutable object(State_Less object)
->Tuple is not dynamic or not growable
->Indexing and slice operator is applicable
->Inseration is preserved(Input_Data==Output_Data):
==================================
Ex1:
===
import time
T1=eval(input('Enter the Tuple_data_type:'))
print(T1)
print()
print(type(T1))
print()
print('====Fetching the data or information=====')
for x1 in T1:
print(x1)
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
C:\Users\Admin\Desktop>Python test1.py
Enter the Tuple_data_type:(1001,1002,1003,1004,1005)
(1001, 1002, 1003, 1004, 1005)
<class 'tuple'>
====Fetching the data or information=====
1001
1002
1003
1004
1005
End of an application
C:\Users\Admin\Desktop>Python test1.py
Enter the Tuple_data_type:1001,1002,1003,1004,1005
(1001, 1002, 1003, 1004, 1005)
<class 'tuple'>
====Fetching the data or information=====
1001
1002
1003
1004
1005
End of an application
C:\Users\Admin\Desktop>
->Duplicate objects are allowed:
======================
Ex1:
===
import time
T1=eval(input('Enter the Tuple_data_type:'))
print(T1)
print()
print(type(T1))
print()
print('====Fetching the data or information=====')
for x1 in T1:
print(x1)
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
Enter the Tuple_data_type:(10,20,30,40,50,10,20,30)
(10, 20, 30, 40, 50, 10, 20, 30)
<class 'tuple'>
====Fetching the data or information=====
10
20
30
40
50
10
20
30
End of an application
->Hetrogenious objects are allowed:
=======================
Ex1:
===
import time
T1=eval(input('Enter the Tuple_data_type:'))
print(T1)
print()
print(type(T1))
print()
print('====Fetching the data or information=====')
for x1 in T1:
print(x1)
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
C:\Users\Admin\Desktop>Python test1.py
Enter the Tuple_data_type:(1001,"Laptop_1",25000.0,"Dell")
(1001, 'Laptop_1', 25000.0, 'Dell')
<class 'tuple'>
====Fetching the data or information=====
1001
Laptop_1
25000.0
Dell
End of an application
->Tuple is Immutable object(State_Less object):
==============================
Ex1:
===
import time
T1=(10001,10002,10003,10004,10005)
print("====Before_Immutable_Operations====")
print(T1)
print()
print(type(T1))
print()
print("====After_Immutable_Operations====")
T1[0]=20001
T1[1]=20002
T1[2]=20003
T1[3]=20004
T1[4]=20005
print(T1)
print()
print(type(T1))
print()
time.sleep(2)
print('End of an application')
OUTPUT:
=====
====Before_Immutable_Operations====
(10001, 10002, 10003, 10004, 10005)
<class 'tuple'>
====After_Immutable_Operations====
Traceback (most recent call last):
File "test1.py", line 9, in <module>
T1[0]=20001
TypeError: 'tuple' object does not support item assignment
->Tuple is a not Dynamic and not Growable:
============================
->max()
->min()
->len()
Ex1:
===
import time
T1=(1001,1002,1003,1004,1005)
print(T1)
print()
print(type(T1))
print()
print("Max_Object:",max(T1))
print()
print("Min_Object is:",min(T1))
print()
print("Length is:",len(T1))
print()
time.sleep(2)
print('End of an application')
Ex2:
===
import time
T1=(1001,1002,1003,1004,1005)
print(T1)
print()
print(type(T1))
print()
T1.append(1009)
print(T1)
print()
time.sleep(2)
print('End of an application')