TECHSOFT ACADEMY
--------------------------------------------------------------------------------------------
2) sort() method
--------------------------------------------------------------------------------------------
-> In list by default insertion order is preserved. If we want to sort the elements
of list according to default natural sorting order then we should go for sort()
method.
For numbers ====> default natural sorting order is Ascending order.
For strings ====> default natural sorting order is alphabetical order.
Example-
l = [4, 6, 1, 88, 44, 5, 10, 50]
print(l) #[4,6,1,88,44,5,10,50]
l.sort()
print(l) #[1, 4, 5, 6, 10, 44, 50, 88]
Example-
l = ['Cat', 'Egg', 'Apple', 'Appy', 'Ball', 'Dog']
print(l) #['Cat', 'Egg', 'Apple', 'Appy', 'Ball', 'Dog']
l.sort()
print(l) #['Apple', 'Appy', 'Ball' 'Cat', 'Dog', 'Egg']
Note: To use sort() method, compulsory list should contain only homogeneous
elements. otherwise we will get TypeError.
Example-
BY MS KUMAR 1
l = ['Cat', 'Egg', 'Apple', 'Appy', 'Ball', 'Dog', 10, 5, 20, 15]
print(l) #['Cat', 'Egg', 'Apple', 'Appy', 'Ball', 'Dog', 10, 5, 20, 15]
l.sort() #TypeError: '<' not supported between instances of 'int' and 'str'
To sort in reverse of default natural sorting order:
-> We can sort according t reverse of default sorting order by using reverse=True
argument
Example-
l = [10, 5, 20, 15]
print(l) #[10, 5, 20, 15]
l.sort()
print(l) #[5, 10, 15, 20]
Example-
l = [10, 5, 20, 15]
print(l) #[10, 5, 20, 15]
l.sort(reverse=True)
print(l) #[20, 15, 10, 5]
BY MS KUMAR 2
--------------------------------------------------------------------------------------------
Aliasing and cloning of list objects:
--------------------------------------------------------------------------------------------
BY MS KUMAR 3
BY MS KUMAR 4
Q. Difference between = operator and copy() method.
-> = operator meant for aliasing
-> copy() method meant for cloning
--------------------------------------------------------------------------------------------
Using Mathematical operators for List Objects:
--------------------------------------------------------------------------------------------
-> We can use + and * operators for list objects.
1) Concatenation operator(+):
-> We can use +operator to concatenate 2 lists into a single list.
Example-
x = [10, 20, 30, 40]
y = [1000, 2000, 3000]
c=x+y
print(x) #[10, 20, 30, 40]
print(y) #[1000, 2000, 3000]
print(c) #[10, 20, 30, 40, 1000, 2000, 3000]
Note: To use + operator compulsory both arguments should be list objects,
otherwise we will get TypeError.
Example-
a = [10, 20, 30]
b = 1000
c=a+b #TypeError: can only concatenate list (not "int") to list
print(a)
BY MS KUMAR 5
print(b)
print(c)
2) Repetition Operator (*)
-> We can use repetition operator * to repeat elements of list specified numbers
of times.
Example-
x = [10, 20, 30]
y = x*4
print(x) #[10, 20, 30]
print(y) #[10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30]
--------------------------------------------------------------------------------------------
List Comprehensions:
--------------------------------------------------------------------------------------------
-> It is very easy and compact way of creating list objects from any iterable
objects(list, range, tuple, strings, dictionary, set etc) based on some condition.
Syntax:
l = [expression for x in iterable if condition]
Example-
l = []
for x in range(1, 101):
if x%2==0:
l.append(x)
print(l)
BY MS KUMAR 6
Example-(list comprehension)
l = [x for x in range(1, 101) if x%2==0]
print(l)
Example-
l = []
for x in range(1, 101):
l.append(x*x)
print(l)
Example-(List comprehension)
l = [x*x for x in range(1, 101)]
print(l)
Example-(list comprehension)
l = [x*x*x for x in range(1, 101)]
print(l)
Example-
s = 'Welcome To HazariBaaGh'
l = [ x for x in s if x.isupper()]
print(s) #'Welcome To HazariBaaGh'
print(l) #['W', 'T', 'H', 'B', 'G']
Example 1:
BY MS KUMAR 7
s = 'Python is very easy to learn'
list = s.split() #['Python', 'is', 'very', 'easy', 'to', learn]
list2 = [x[0] for x in list]
print(list2)
Example 2:
s = 'Python is very easy to learn'
list2 = [x[0] for x in s.split() ]
print(list2)
Example 3:
list2 = [x[0] for x in 'Python is very easy to learn'.split() ]
print(list2)
Example 4:
print([x[0] for x in 'Python is very easy to learn'.split()])
Example 5:
num1 = [10, 20, 30, 40]
num2 = [30, 40, 50, 60]
num3 = [x for x in num1 if x in num2]
print(num3) #[30, 40]
Example 5:
num1 = [10, 20, 30, 40]
num2 = [30, 40, 50, 60]
num3 = [x for x in num1 if x not in num2]
print(num3) #[10, 20]
BY MS KUMAR 8
--------------------------------------------------------------------------------------------
words = "the quick brown fox jumps over the lazy dog"
wordlist = words.split() #['the', 'quick','brown', 'fox', 'jumps', 'over', 'the',
'lazy', 'dog']
list = [ [word.upper(), len(word)] for word in wordlist]
print(list)
Example- Find the highest length of word in words
words = "the quick brown fox jumps over the lazy dog"
wordlist = words.split() #['the', 'quick','brown', 'fox', 'jumps', 'over', 'the',
'lazy', 'dog']
list = [ len(word) for word in wordlist]
print(max(list))
------------------------------------------------------------------------------------------
List Comprehension
===========================================================================
Example-
numbers = [2, 3, 4 ,5, 7]
l = [x*x for x in numbers]
print(numbers)
print(l)
Example-
numbers = [2,3,4,5,7,5,3,45]
ls = [x for x in numbers if x%2==0]
print(numbers) #[2, 3, 4, 5, 7, 5, 3, 45]
print(ls) #[2, 4]
BY MS KUMAR 9
Example-
import math
ls = [math.factorial(x) for x in range(1, 11)]
print(ls) #[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
Example-
ls = [3**x for x in range(1,11)]
print(ls) #[3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049]
--------------------------------------------------------------------------------------------
Packing
------------------------------------------------------------------------------------------
Example-
a=10
b=20
c=30
print(a) #10
print(b) #20
print(c) #30
Example-
a,b,c=10,20,30
print(a,b,c) #10 20 30
Example-
a=10
b=20
BY MS KUMAR 10
c=25
d=30
l = [a,b,c,d]
print(a) #10
print(b) #20
print(c) #25
print(d) #30
print(l) #[10, 20, 25, 30]
--------------------------------------------------------------------------------------------
Unpacking
--------------------------------------------------------------------------------------------
Example-
ls = [100, 200, 300, 400]
print(ls) #[100, 200, 300, 400]
Example-
ls = [100, 200, 300, 400]
print(ls) #[100, 200, 300, 400]
a,b,c,d = ls
print(a) #100
print(b) #200
print(c) #300
print(d) #400
BY MS KUMAR 11
Example-
ls = [100, 200, 300, 400]
print(ls) #[100, 200, 300, 400]
a,b,c,d,e = ls #ValueError: not enough values to unpack (expected 5, got 4)
print(a)
print(b)
print(c)
print(d)
print(e)
--------------------------------------------------------------------------------------------
Read Multiple values from the CMD
--------------------------------------------------------------------------------------------
Example-
n=input('Enter any number=')
print(n)
print(type(n))
Output:
Enter any number=67
67
<class 'str'>
Output:
Enter any number=6 3 8
638
BY MS KUMAR 12
<class 'str'>
Example-(Read three values from user)
a,b,c = [eval(x) for x in input('Enter three number=').split()]
res = a + b
print("Sum of three no=",res)
Output:
Enter three number=6 5 4
Sum of three no= 15
Example-(Read two values from user)
a,b = [eval(x) for x in input('Enter two number=').split()]
res = a + b
print("Sum of two no=",res)
Output:
Enter two number=6 7
Sum of two no= 13
BY MS KUMAR 13