Skip to content

Commit b652e82

Browse files
authored
Merge pull request #32 from nurdtechie98/master
Inbuilt functions . close #27
2 parents b314f3f + 4da308d commit b652e82

File tree

3 files changed

+162
-8
lines changed

3 files changed

+162
-8
lines changed

python/09-Inbuilt_functions.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<h1 align="center"><a href="#"> Strings </a></h1>
2+
3+
* The Python interpreter has a number of functions and types built into it that are always available.
4+
* There is no need to import modules extra in order to use them.
5+
* A few functions that we will be covering here are-
6+
* Map
7+
* Filter
8+
* Sort
9+
* Max/Min
10+
* Reduce
11+
12+
## Map
13+
* It iterates through all the elements of a iterable and applies some specific funtion to each element of that iterable.
14+
* With multiple iterables, the iterator stops when the shortest iterable is exhausted
15+
16+
### syntax
17+
```python
18+
r = map(func, seq)
19+
#takes each element in seq
20+
#applies func function to them
21+
#inserts them to r
22+
23+
```
24+
### implementation
25+
```python
26+
27+
#for a single list involved
28+
a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']
29+
dt=list(map(str.split, a)) #split function is applied to each element and it returns a list corresponding to each element
30+
print(dt)
31+
32+
#for multiple lists involved
33+
a = [1,2,3,4]
34+
b = [17,12,11,10]
35+
print(list(map(lambda x,y:x+y, a,b)))
36+
#goes through each element of a,b
37+
#adds them correspondingly till one of the lists end
38+
39+
```
40+
Output
41+
```
42+
[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']]
43+
[18, 14, 14, 14]
44+
```
45+
## Filter
46+
* As the name justifies,filter creates a list of elements for which a function returns true.
47+
48+
### Syntax
49+
```python
50+
filter(filter_fnc,list)
51+
```
52+
### implementation
53+
```python
54+
def fn(x):
55+
return (x<0) #(x<0) is condition automatically returns true or false
56+
number_list = range(-10, 10)
57+
less_than_zero = list(filter(fn, number_list))
58+
print(less_than_zero)
59+
```
60+
Output
61+
```
62+
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
63+
```
64+
65+
## Sort/Sorted
66+
* Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable
67+
* There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals describing them
68+
* The key difference between both is that sorted returns a new list whereas sort chsnges the original list itself.
69+
70+
### syntax
71+
```python
72+
list.sort()
73+
sorted(container)#sort in ascending order
74+
sorted(list,reverse=True)#sort in descending order
75+
sorted(list,key=<insert the key>)#sort in ascending order of the key set
76+
```
77+
### implementation
78+
```python
79+
80+
a = [5, 2, 3, 1, 4]
81+
a.sort()# .sort() function is specific to lists
82+
print(a)
83+
84+
chir={2: 'D', 1: 'B', 4: 'B', 3: 'E', 5: 'A'}# declaring a dictionary
85+
for ind in sorted(chir): #sorted is general approach, although return type is a list
86+
print("{}:{}".format(ind,chir[ind]),end=" ")
87+
print()
88+
for ind in sorted(chir,reverse=True): #note the order is descending of the keys
89+
print("{}:{}".format(ind,chir[ind]),end=" ")
90+
def gv(key):
91+
return chir[key];
92+
print()
93+
for ind in sorted(chir,key=gv,reverse=True): #note the order is descending of the values of each key
94+
print("{}:{}".format(ind,chir[ind]),end=" ")
95+
96+
```
97+
Output
98+
```
99+
[1, 2, 3, 4, 5]
100+
1:B 2:D 3:E 4:B 5:A
101+
5:A 4:B 3:E 2:D 1:B
102+
3:E 2:D 1:B 4:B 5:A
103+
104+
```
105+
## max/min
106+
* Return the greatets/smallest item in an iterable or the smallest of two or more arguments.
107+
* It can also be made to make use of key argument as used in sort function
108+
### implementation
109+
```python
110+
list = [1, 4, 3, 5,9,2]
111+
print(max(list)) #returns maximum value of list
112+
print(min(list)) #returns minimum value in the list
113+
print(max(list[2:-2])) #returns max value compined with slicing
114+
print(min(list[4:])
115+
print(max(4,2,3,5))
116+
print(min(7,2,1,8,4))
117+
```
118+
Output
119+
```
120+
9
121+
1
122+
5
123+
9
124+
125+
```
126+
127+
## Reduce
128+
* Reduce is a really useful function for performing some computation on a list and returning the result.
129+
* It applies a rolling computation to sequential pairs of values in a list
130+
>note this not a inbuilt function as such because it needs to be imported from func tools but is pretty useful.
131+
132+
### implementation
133+
```python
134+
#Normal way to find product of elements in a list
135+
product = 1
136+
list = [1, 2, 3, 4]
137+
for num in list:
138+
product = product * num
139+
140+
#Using reduce function to do so
141+
from functools import reduce
142+
def func(x,y):
143+
return x*y
144+
product = reduce(func, [1, 2, 3, 4])
145+
146+
```
147+
Output
148+
```
149+
24
150+
24
151+
```
152+
153+
154+

python/10-user_defined_functions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ After defining the function name and arguments(s) a block of program statement(s
88
Here is the syntax of a user defined function.
99

1010
## Syntax
11-
```
11+
```python
1212
def function_name(argument1, argument2, ...) :
1313
statement_1
1414
statement_2
@@ -19,11 +19,11 @@ def function_name(argument1, argument2, ...) :
1919
Calling a function in Python is similar to other programming languages, using the function name, parenthesis (opening and closing) and parameter(s). See the syntax, followed by an example.
2020

2121
### Syntax
22-
```
22+
```python
2323
function_name(arg1, arg2)
2424
```
2525
### Example :
26-
```
26+
```python
2727
def avg_number(x, y):
2828
print("Average of ",x," and ",y, " is ",(x+y)/2)
2929
avg_number(3, 4)
@@ -42,15 +42,15 @@ Average of 3 and 4 is 3.5
4242
## Function without arguments :
4343

4444
The following function has no arguments.
45-
```
45+
```python
4646
def function_name() :
4747
statement_1
4848
statement_2
4949
....
5050
```
5151
### Example :
5252

53-
```
53+
```python
5454
def printt():
5555
print("I Love CodeCell")
5656
print("I Love CodeCell")
@@ -73,7 +73,7 @@ I Love CodeCell
7373
## The Return statement in function
7474

7575
In Python the return statement (the word return followed by an expression.) is used to return a value from a function, return statement without an expression argument returns none. See the syntax.
76-
```
76+
```python
7777
def function_name(argument1, argument2, ...) :
7878
statement_1
7979
statement_2
@@ -85,7 +85,7 @@ function_name(arg1, arg2)
8585
### Example :
8686

8787
The following function returns the square of the sum of two numbers.
88-
```
88+
```python
8989
def nsquare(x, y):
9090
return (x*x + 2*x*y + y*y)
9191
print("The square of the sum of 2 and 3 is : ", nsquare(2, 3))

python/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
6. [Arrays/Lists](./06-Lists.md)
1212
7. [Dictionaries](./07-Dictionaries.md)
1313
8. [Sets](./08-Sets.md)
14-
9. [In build functions]()
14+
9. [In build functions](./09-Inbuilt_functions.md)
1515
10. [User defined functions](./10-user_defined_functions.md)
1616
11. [Lambda](./11-Lambda.md)
1717
12. [Modules](./12-Modules.md)

0 commit comments

Comments
 (0)