Skip to content

Commit 4a289bf

Browse files
authored
function exercise
1 parent a3b3f86 commit 4a289bf

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

function_exercise.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "3c1f7893-37e6-405b-8476-324a0befe5f0",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"## filter() function construct an iterator from elements of an iterable for which a function returns true.\n",
11+
"## it is used to filter out items from a list(or any other iterable) based on a condition.\n",
12+
"def even(num):\n",
13+
" if num%2==0:\n",
14+
" return True"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"id": "0ad00a0c-5e63-4818-918d-6b2c3731ecb6",
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"data": {
25+
"text/plain": [
26+
"True"
27+
]
28+
},
29+
"execution_count": 2,
30+
"metadata": {},
31+
"output_type": "execute_result"
32+
}
33+
],
34+
"source": [
35+
"even(28)"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"id": "5a541e47-3d9a-427f-bee4-4ad3e741f1a4",
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"[2, 4, 6, 8, 10]"
48+
]
49+
},
50+
"execution_count": 3,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"lst=[1,2,3,4,5,6,7,8,9,10]\n",
57+
"list(filter(even,lst))"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 4,
63+
"id": "904646b4-ae5f-4cf5-aac5-5f554ccba103",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"name": "stdout",
68+
"output_type": "stream",
69+
"text": [
70+
"[7, 8, 9, 10]\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"## filter with lambda function \n",
76+
"num =[1,2,3,4,5,7,8,9,10]\n",
77+
"greater_than_five =list(filter(lambda x : x >5,num))\n",
78+
"print(greater_than_five)"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 5,
84+
"id": "4c926ed6-523a-4ccb-b0fa-856dbad653eb",
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"name": "stdout",
89+
"output_type": "stream",
90+
"text": [
91+
"[6, 8, 10]\n"
92+
]
93+
}
94+
],
95+
"source": [
96+
"## filter with lambda function and multiple conditions\n",
97+
"number=[1,2,3,4,5,6,7,8,9,10]\n",
98+
"even_greater_than_five=list(filter(lambda x : x >5 and x%2==0, number))\n",
99+
"print(even_greater_than_five)"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 6,
105+
"id": "6e77166b-992f-4677-8a80-8e6c52d0ee75",
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"data": {
110+
"text/plain": [
111+
"[{'name': 'sunil', 'age': 41}, {'name': 'rahul', 'age': 35}]"
112+
]
113+
},
114+
"execution_count": 6,
115+
"metadata": {},
116+
"output_type": "execute_result"
117+
}
118+
],
119+
"source": [
120+
"## filter to check if the age is greater than 25 in dictionaries\n",
121+
"people =[\n",
122+
" {'name':'sunil','age':41},\n",
123+
" {'name':'rahul','age':35},\n",
124+
" {'name':'santosh','age':25} \n",
125+
" ]\n",
126+
"\n",
127+
"def age_greater_than_25(person):\n",
128+
" return person['age'] > 25\n",
129+
"list(filter(age_greater_than_25,people))"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"id": "317b519c-3bad-4acb-a671-3d7a96fd66fa",
136+
"metadata": {},
137+
"outputs": [],
138+
"source": []
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3 (ipykernel)",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 3
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython3",
157+
"version": "3.12.7"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

0 commit comments

Comments
 (0)