Skip to content

Commit f387f93

Browse files
authored
List data type program and pickle file
1 parent 00b0812 commit f387f93

File tree

1 file changed

+394
-0
lines changed

1 file changed

+394
-0
lines changed

List_data_type.ipynb

+394
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,394 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "5f415a44",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"# create a list of integers\n",
11+
"my_list = [1, 2, 3, 4, 5]"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"id": "65f31300",
18+
"metadata": {},
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"[1, 2, 3, 4, 5]\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"# print the list\n",
30+
"print(my_list)"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 3,
36+
"id": "bfc368b1",
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"1\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"# access an element in the list\n",
49+
"print(my_list[0]) # prints 1"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 4,
55+
"id": "f0794d3d",
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"[1, 6, 3, 4, 5]\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"# change an element in the list\n",
68+
"my_list[1] = 6\n",
69+
"print(my_list) # prints [1, 6, 3, 4, 5]"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 5,
75+
"id": "fe1ec520",
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"[1, 6, 3, 4, 5, 6]\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"# add an element to the end of the list\n",
88+
"my_list.append(6)\n",
89+
"print(my_list) # prints [1, 6, 3, 4, 5, 6]"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 6,
95+
"id": "681f2fd8",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"[1, 6, 4, 5, 6]\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"# remove an element from the list\n",
108+
"my_list.remove(3)\n",
109+
"print(my_list) # prints [1, 6, 4, 5, 6]"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 7,
115+
"id": "bdf120bb",
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"5\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"# get the length of the list\n",
128+
"print(len(my_list)) # prints 5"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 8,
134+
"id": "e74c822d",
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"# create a list of mixed data types\n",
139+
"mixed_list = [\"apple\", 1, \"banana\", 2]"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 9,
145+
"id": "fc6ed793",
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"['apple', 1, 'banana', 2]\n"
153+
]
154+
}
155+
],
156+
"source": [
157+
"# print the list\n",
158+
"print(mixed_list) # prints [\"apple\", 1, \"banana\", 2]"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 10,
164+
"id": "35d8d2d2",
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"apple\n"
172+
]
173+
}
174+
],
175+
"source": [
176+
"# access an element in the list\n",
177+
"print(mixed_list[0]) # prints \"apple\""
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 11,
183+
"id": "32e03d22",
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"name": "stdout",
188+
"output_type": "stream",
189+
"text": [
190+
"['apple', 'orange', 'banana', 2]\n"
191+
]
192+
}
193+
],
194+
"source": [
195+
"# change an element in the list\n",
196+
"mixed_list[1] = \"orange\"\n",
197+
"print(mixed_list) # prints [\"apple\", \"orange\", \"banana\", 2]"
198+
]
199+
},
200+
{
201+
"cell_type": "markdown",
202+
"id": "24e2e981",
203+
"metadata": {},
204+
"source": [
205+
"## Write a Python function that takes a list of integers as input and returns the sum of all the even numbers in the list."
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 12,
211+
"id": "33974f61",
212+
"metadata": {},
213+
"outputs": [
214+
{
215+
"name": "stdout",
216+
"output_type": "stream",
217+
"text": [
218+
"The sum of all even number in a list:30\n"
219+
]
220+
}
221+
],
222+
"source": [
223+
"def sum_even_num(number):\n",
224+
" sum = 0 # initialize a variable to hold the sum of even numbers \n",
225+
" \n",
226+
" # iterate over the numbers in the list\n",
227+
" for num in number:\n",
228+
" # check if the number is even\n",
229+
" if num % 2 == 0:\n",
230+
" # add the even number to the sum\n",
231+
" sum += num\n",
232+
" \n",
233+
" # return the sum of even numbers\n",
234+
" return sum\n",
235+
"\n",
236+
"number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
237+
"\n",
238+
"result = sum_even_num(number)\n",
239+
"\n",
240+
"print(f\"The sum of all even number in a list:{result}\") # prints 30, which is the sum of 2 + 4 + 6 + 8 + 10"
241+
]
242+
},
243+
{
244+
"cell_type": "markdown",
245+
"id": "a7e46446",
246+
"metadata": {},
247+
"source": [
248+
"## Write a python code for sum of all the even numbers in the list by using filter, lambda and reduce function."
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": 13,
254+
"id": "def0b891",
255+
"metadata": {},
256+
"outputs": [
257+
{
258+
"name": "stdout",
259+
"output_type": "stream",
260+
"text": [
261+
"The sum of all even number in a list:30\n"
262+
]
263+
}
264+
],
265+
"source": [
266+
"from functools import reduce\n",
267+
"\n",
268+
"number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
269+
"\n",
270+
"even_numbers = filter(lambda x: x % 2 == 0, number) # use a lambda function to filter out even numbers from the list.\n",
271+
"\n",
272+
"sum_even_num = reduce(lambda x, y: x + y, even_numbers) # use a lambda function to find the sum of the filtered even numbers.\n",
273+
"\n",
274+
"print(f\"The sum of all even number in a list:{sum_even_num}\") # prints 30, which is the sum of 2 + 4 + 6 + 8 + 10"
275+
]
276+
},
277+
{
278+
"cell_type": "markdown",
279+
"id": "a10e3112",
280+
"metadata": {},
281+
"source": [
282+
"## Write a python code for sum of all the even numbers in the list by using List Comprehension."
283+
]
284+
},
285+
{
286+
"cell_type": "code",
287+
"execution_count": 14,
288+
"id": "799e6080",
289+
"metadata": {},
290+
"outputs": [
291+
{
292+
"name": "stdout",
293+
"output_type": "stream",
294+
"text": [
295+
"The sum of all even number in a list:30\n"
296+
]
297+
}
298+
],
299+
"source": [
300+
"number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
301+
"\n",
302+
"even_numbers = [num for num in number if num % 2 == 0] # use list comprehension to filter out even numbers from the list\n",
303+
"\n",
304+
"sum_even_numbers = sum(even_numbers) # use the built-in sum() function to calculate the sum of the filtered even numbers\n",
305+
"\n",
306+
"print(f\"The sum of all even number in a list:{sum_even_numbers}\") # prints 30, which is the sum of 2 + 4 + 6 + 8 + 10"
307+
]
308+
},
309+
{
310+
"cell_type": "markdown",
311+
"id": "40276043",
312+
"metadata": {},
313+
"source": [
314+
"## Create a pickle file "
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": 15,
320+
"id": "9d26cfda",
321+
"metadata": {},
322+
"outputs": [],
323+
"source": [
324+
"import pickle\n",
325+
"# create a pickle file with the result\n",
326+
"with open('result.pickle', 'wb') as file:\n",
327+
" pickle.dump(result, file)"
328+
]
329+
},
330+
{
331+
"cell_type": "markdown",
332+
"id": "c66e8b62",
333+
"metadata": {},
334+
"source": [
335+
"* Finally, we create a pickle file named result.pickle to store the result of the function call.\n",
336+
"* We open the file in binary mode using the 'wb' mode flag, \n",
337+
"* We use the pickle.dump() function to write the result to the file."
338+
]
339+
},
340+
{
341+
"cell_type": "code",
342+
"execution_count": 16,
343+
"id": "ba668092",
344+
"metadata": {},
345+
"outputs": [
346+
{
347+
"name": "stdout",
348+
"output_type": "stream",
349+
"text": [
350+
"30\n"
351+
]
352+
}
353+
],
354+
"source": [
355+
"# read the result from the pickle file\n",
356+
"with open('result.pickle', 'rb') as file:\n",
357+
" result = pickle.load(file)\n",
358+
"\n",
359+
"print(result) # prints 30, which is the sum of 2 + 4 + 6 + 8 + 10"
360+
]
361+
},
362+
{
363+
"cell_type": "markdown",
364+
"id": "4821b858",
365+
"metadata": {},
366+
"source": [
367+
"* We open the result.pickle file in binary mode using the 'rb' mode flag.\n",
368+
"* We use the pickle.load() function to read the result from the file. \n",
369+
"* The result is then printed, which should be the same as the result obtained from calling the sum_even_numbers function."
370+
]
371+
}
372+
],
373+
"metadata": {
374+
"kernelspec": {
375+
"display_name": "Python 3 (ipykernel)",
376+
"language": "python",
377+
"name": "python3"
378+
},
379+
"language_info": {
380+
"codemirror_mode": {
381+
"name": "ipython",
382+
"version": 3
383+
},
384+
"file_extension": ".py",
385+
"mimetype": "text/x-python",
386+
"name": "python",
387+
"nbconvert_exporter": "python",
388+
"pygments_lexer": "ipython3",
389+
"version": "3.9.12"
390+
}
391+
},
392+
"nbformat": 4,
393+
"nbformat_minor": 5
394+
}

0 commit comments

Comments
 (0)