-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFunctions_basics.py
134 lines (106 loc) · 3.4 KB
/
Functions_basics.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 9 22:02:25 2020
@author: Abhinav
"""
#abs(): Returns the absolute value of a number.
# integer number
integer = -20
print('Absolute value of -20 is:', abs(integer))
# floating number
floating = -20.83
print('Absolute value of -20.83 is:', abs(floating))
#all(): It returns true if all items passed in iterable object are true. Otherwise, it returns False.
#This fxn accepts an iterable object (such as list, dictionary, etc.).
# all values true
k = [1, 3, 4, 6]
print(all(k))
# all values false
k = [0, False]
print(all(k))
# one false value
k = [1, 3, 7, 0]
print(all(k))
# empty iterable
k = []
print(all(k))
#bool(): Converts a value to boolean(True or False)
test1 = []
print(test1,'is',bool(test1))
test1 = [0]
print(test1,'is',bool(test1))
test1 = None
print(test1,'is',bool(test1))
test1 = 'Easy string'
print(test1,'is',bool(test1))
#sum(): Used to get the sum of numbers of an iterable, i.e., list.
list_1 = [1,2,4]
s = sum(list_1)
print(s)
s = sum(list_1, 10)
print(s)
#len(): Returns the length (the number of items) of an object.
strA = 'Python'
print(len(strA))
#list() creates a list in python.
# empty list
print(list())
# string
String = 'abcde'
print(list(String))
#divmod(): Used to get quotient and remainder of two numbers.
#This function takes two numeric arguments and returns a tuple.
#Both arguments are required and numeric
# Calling function
result = divmod(10,2)
# Displaying result
print(result)
#dict(): Its a constructor which creates a dictionary.
# Calling function
result = dict() # returns an empty dictionary
result2 = dict(a=1,b=2)
# Displaying result
print(result)
print(result2)
#set(): It is used to create a new set using elements passed during the call.
#It takes an iterable object as an argument and returns a new set object.
# Calling function
result = set() # empty set
result2 = set('12')
result3 = set('javatpoint')
# Displaying result
print(result)
print(result2)
print(result3)
#pow(): Used to compute the power of a number.
# positive x, positive y (x**y)
print(pow(4, 2))
# negative x, positive y
print(pow(-4, 2))
#tuple(): Used to create a tuple object.
t1 = tuple()
print('t1=', t1)
# creating a tuple from a list
l = [1, 6, 9]
t2 = tuple(l)
print('t2=', t2)
# creating a tuple from a string
t1 = tuple('Java')
print('t1=',t1)
#lambda()- Helps creating anonymous functions.
#Lambda functions can accept any number of arguments,
#but they can return only one value in the form of expression.
#Multiple arguments to Lambda function
x = lambda a,b:a+b
# a and b are the arguments and a+b is the expression which gets evaluated and returned.
print("sum = ",x(20,10))
#program to filter out the list which contains numbers divisible by 3.
List = [1,2,3,4,10,123,22]
Oddlist = list(filter(lambda x:(x%3 == 0),List))
# the list contains all the items of the list for which the lambda function evaluates to true
print(Oddlist)
#program to triple each number of the list using map
List = [1,2,3,4,10,123,22]
new_list = list(map(lambda x:x*3,List))
# this will return the triple of each item of the list and add it to new_list
print(new_list)