-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced python functions.py
More file actions
99 lines (67 loc) · 2.96 KB
/
Copy pathAdvanced python functions.py
File metadata and controls
99 lines (67 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#############-Advanced python functions-##############
# 1.------------Lambda function------------
##It is small anonymous function . Anonymous means the function does not have a name
##It is useful when a function is needed only once and does not need to be reused elsewhere
#syntax: lambda arguments:expression
#for example
#--adding two numbers--
add=lambda a,b:a+b
print(add(3,2)) #output is 5
#----squaring----
square=lambda x:x*x
print(square(4)) #output is 16
#why use lambda function--
#reduces code length, easy to use for simple operations
#commonly used with ***map(),filter(),and sorted()***
#real life example--->imagine an online shopping application that needs to calculate an 18% GST
gst=lambda amount:amount*0.18 #18% means 0.18 because % is per hundred 18% = 18/100 = 0.18
print(gst(1000)) #output is 180.0
# 2.------------Map function----------
#it is applies the same operation to every item in a collection
#syntax: map(function,iterable)
numbers=[1,2,3,4,5]
result=map(lambda x:x*2,numbers)
print(list(result)) #output [2, 4, 6, 8, 10]
# 3.-----------Filter function--------
#it is selects only the items that satisfy a condition
#syntax: filter(function,iterable)
numbers=[1,2,3,4,5,6]
even_numbers=filter(lambda x:x%2==0,numbers)
print(list(even_numbers)) #[2, 4, 6]
# 4.----------Reduce function--------
#it is combines all values into a single value, it repeatedly performs an operation until only one result remains
#syntax: from functools import reduce
from functools import reduce
numbers=[1,2,3,4]
result=reduce(lambda x,y:x+y,numbers)
print(result) #output : 10
# 5.----------Zip function----------
#it is combines multiple collections together
name=["raju","ravi","rani"]
marks=[80,90,99]
result=list(zip(name,marks))
print(result) #output: [('raju', 80), ('ravi', 90), ('rani', 99)]
# 6.----------Enumerate function-------
# it is automatically adds the index numbers
fruits=["apple","banana","mango"]
for index,fruits in enumerate(fruits):
print(index,fruits) #output : 0 apple
# 1 banana
# 2 mango
# 7.----------Sorted function---------
# it is sorts data in ascending or descending order
numbers=[3,2,5,1]
print(sorted(numbers)) #output : [1, 2, 3, 5]
print(sorted(numbers,reverse=True)) #output : [5, 3, 2, 1]
# 8.----------Any function------------
# it is returns true if at least one value is true
values=[False,False,True]
print(any(values)) #output : True
# 9.----------All function------------
#it is returns true only when every value is true
marks=[70,80,90]
print(all(mark>50 for mark in marks)) #output : True
# 10.---------isinstance function----------
#it is checks whether a variable belongs to a particular data type
age=21
print(isinstance(age,int)) #output : True