Skip to content

Commit 05bde79

Browse files
authored
Python_Day7
Solved some coding questions
1 parent 46b97c3 commit 05bde79

File tree

1 file changed

+393
-0
lines changed

1 file changed

+393
-0
lines changed

Python_Day7.ipynb

+393
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,393 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b4a06900-34c6-4557-9c03-f50b5be347c4",
6+
"metadata": {},
7+
"source": [
8+
"#### 1.Write a Python program to convert the distance (in feet) to inches, yards, and miles."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "6a360e14-e3c0-4354-ba30-a5aa00d548a7",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Converted 100 feet distance into:\n",
22+
"\n",
23+
"Inch:1200\n",
24+
"Yards:33.33\n",
25+
"miles:0.02\n"
26+
]
27+
}
28+
],
29+
"source": [
30+
"def distance_converter(feet):\n",
31+
" inch = feet * 12\n",
32+
" yards = feet / 3\n",
33+
" miles = feet / 5280\n",
34+
" return round(inch, 2), round(yards, 2), round(miles, 2)\n",
35+
"\n",
36+
"print('Converted 100 feet distance into:\\n\\nInch:{}\\nYards:{}\\nmiles:{}'.format(distance_converter(100)[0], \n",
37+
" distance_converter(100)[1],distance_converter(100)[2]))"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"id": "02ef6e45-5e83-4c0a-bace-125f3d8a7662",
43+
"metadata": {},
44+
"source": [
45+
"#### 2.Write a Python program to convert all units of time into seconds."
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"id": "31ec1051-a3d0-43ae-8f37-54b0eadbd3c9",
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"Total time converted into seconds: 364810\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"def time_convert(days, hours, minutes, seconds):\n",
64+
" days = days * 24 * 60 * 60\n",
65+
" hours = hours * 60 * 60\n",
66+
" minutes = minutes * 60\n",
67+
" seconds = seconds\n",
68+
" \n",
69+
" total_seconds = days + hours + minutes + seconds\n",
70+
" return total_seconds\n",
71+
"\n",
72+
"print('Total time converted into seconds: {}'.format(time_convert(4, 5, 20, 10)))"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"id": "31093e27-4d0e-4aac-95e8-992a7a85d816",
78+
"metadata": {},
79+
"source": [
80+
"#### 3.Write a Python program to get an absolute file path."
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 3,
86+
"id": "519d46eb-43f9-4cbb-9151-ececf734c287",
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"text/plain": [
92+
"'D:\\\\Jupyter_Notebook\\\\file_41.txt'"
93+
]
94+
},
95+
"execution_count": 3,
96+
"metadata": {},
97+
"output_type": "execute_result"
98+
}
99+
],
100+
"source": [
101+
"def abs_file_path(file_name):\n",
102+
" from os import path\n",
103+
" return path.abspath('file_41.txt')\n",
104+
"\n",
105+
"abs_file_path('file1.txt')"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "973fe314-1f58-41d7-8462-85fd90635e54",
111+
"metadata": {},
112+
"source": [
113+
"#### 4.Write a Python program to get file creation and modification date/times."
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 4,
119+
"id": "6ec4f1fc-3fdb-48c3-980c-1aa23394b15e",
120+
"metadata": {},
121+
"outputs": [
122+
{
123+
"name": "stdout",
124+
"output_type": "stream",
125+
"text": [
126+
"Last modified:\n",
127+
"Thu Jan 30 23:18:43 2025\n",
128+
"\n",
129+
"Time Created:\n",
130+
"Thu Jan 30 23:17:03 2025\n"
131+
]
132+
}
133+
],
134+
"source": [
135+
"import os.path, time\n",
136+
"\n",
137+
"print('Last modified:')\n",
138+
"print(time.ctime(os.path.getmtime('file1.txt')))\n",
139+
"\n",
140+
"print('\\nTime Created:')\n",
141+
"print(time.ctime(os.path.getctime('file1.txt')))"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"id": "cf40fdbe-d7e1-4168-b900-283f21965506",
147+
"metadata": {},
148+
"source": [
149+
"#### 5.Write a Python program to convert seconds to day, hour, minutes and seconds."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 6,
155+
"id": "d3ae46f7-2948-4a33-a86d-3e19f28befdc",
156+
"metadata": {},
157+
"outputs": [
158+
{
159+
"name": "stdout",
160+
"output_type": "stream",
161+
"text": [
162+
"Input second: 123456789\n",
163+
"\n",
164+
"Output in =\n",
165+
"Day: 1428\n",
166+
"Hour: 21\n",
167+
"Minutes: 33\n",
168+
"Seconds: 9\n"
169+
]
170+
}
171+
],
172+
"source": [
173+
"def second_converter(sec):\n",
174+
" day = sec // (24 * 60 * 60)\n",
175+
" remaining_sec_after_day_conversion = sec % (24 * 60 * 60)\n",
176+
" hour = remaining_sec_after_day_conversion // 3600\n",
177+
" \n",
178+
" remaining_sec_after_hour_conversion = sec % 3600\n",
179+
" minute = remaining_sec_after_hour_conversion // 60\n",
180+
" \n",
181+
" second = sec % 60\n",
182+
" \n",
183+
" return day, hour, minute, second\n",
184+
"sec = 123456789\n",
185+
"print('Input second: {}'.format(sec))\n",
186+
"day = second_converter(sec)[0]\n",
187+
"hour = second_converter(sec)[1]\n",
188+
"minutes = second_converter(sec)[2]\n",
189+
"second = second_converter(sec)[3]\n",
190+
"\n",
191+
"print('\\nOutput in =\\nDay: {}\\nHour: {}\\nMinutes: {}\\nSeconds: {}'.format(day, hour, minutes, second))"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"id": "acddf7bc-97f1-4f88-9aea-04bd8699d173",
197+
"metadata": {},
198+
"source": [
199+
"#### 6.Write a Python program to calculate body mass index."
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 7,
205+
"id": "362703b4-07d8-4b99-b1f1-ae32a278b4a8",
206+
"metadata": {},
207+
"outputs": [
208+
{
209+
"data": {
210+
"text/plain": [
211+
"2.83"
212+
]
213+
},
214+
"execution_count": 7,
215+
"metadata": {},
216+
"output_type": "execute_result"
217+
}
218+
],
219+
"source": [
220+
"def BMI(weight, height_in_meter):\n",
221+
" return round(weight / (height_in_meter**2), 2) # weight/(height_in_meter)^2\n",
222+
"\n",
223+
"BMI(92, 5.7)"
224+
]
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"id": "d01b8da5-1a4a-49c7-b1e4-2a991c6a85b7",
229+
"metadata": {},
230+
"source": [
231+
"#### 7.Write a Python program to convert pressure in kilopascals to pounds per square inch, a millimeter of mercury (mmHg) and atmosphere pressure."
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": 8,
237+
"id": "fd469345-01e4-43cd-b474-6f1e25ed90a1",
238+
"metadata": {},
239+
"outputs": [
240+
{
241+
"name": "stdout",
242+
"output_type": "stream",
243+
"text": [
244+
"Pressure in pounds per square inch: 1.79\n",
245+
"Pressure in millimeter of mercury (mmHg): 92.64\n",
246+
"Pressure in atm: 0.12\n"
247+
]
248+
}
249+
],
250+
"source": [
251+
"def pressure_converter(kpa):\n",
252+
" ppsi = kpa / 6.89475729 #pounds per square inch. 1 kpa = pressure in kpa / 6.895\n",
253+
" mmhg = kpa * 7.501 # 1 mmhg = kpa * 7.501\n",
254+
" atm = kpa / 101.325 # 1 atm = kpa / 101.325\n",
255+
" \n",
256+
" print('Pressure in pounds per square inch: %.2f' %(ppsi)) \n",
257+
" print('Pressure in millimeter of mercury (mmHg): %.2f' %(mmhg))\n",
258+
" print('Pressure in atm: %.2f' %(atm))\n",
259+
" \n",
260+
"pressure_converter(12.35)"
261+
]
262+
},
263+
{
264+
"cell_type": "markdown",
265+
"id": "81ae46ae-9aff-49be-96cd-b1c366f6dc32",
266+
"metadata": {},
267+
"source": [
268+
"### 8.Write a Python program to calculate the sum of the digits in an integer."
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": 9,
274+
"id": "2edda33d-19e9-461f-9455-d9e5dbd6e6c7",
275+
"metadata": {},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"15"
281+
]
282+
},
283+
"execution_count": 9,
284+
"metadata": {},
285+
"output_type": "execute_result"
286+
}
287+
],
288+
"source": [
289+
"def sum_of_digits(n):\n",
290+
" s = 0\n",
291+
" while(n != 0):\n",
292+
" rem = n % 10\n",
293+
" s = s + rem\n",
294+
" n = n // 10\n",
295+
" return s\n",
296+
" \n",
297+
"sum_of_digits(12345)"
298+
]
299+
},
300+
{
301+
"cell_type": "markdown",
302+
"id": "78c9b714-10f2-45f4-8ace-8f2fb2236635",
303+
"metadata": {},
304+
"source": [
305+
"#### 9. Write a Python program to sort three integers without using conditional statements and loops."
306+
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": 10,
311+
"id": "a93554d5-819a-4aaa-8b04-583535a0e425",
312+
"metadata": {},
313+
"outputs": [
314+
{
315+
"name": "stdout",
316+
"output_type": "stream",
317+
"text": [
318+
"Integers after sorting in ascending order: (2, 5, 9)\n"
319+
]
320+
}
321+
],
322+
"source": [
323+
"def sort_three_integer(a, b, c):\n",
324+
" maximum = max(a, b, c)\n",
325+
" minimum = min(a, b, c)\n",
326+
" middle = (a + b + c) - (maximum + minimum)\n",
327+
" return minimum, middle, maximum\n",
328+
"\n",
329+
"print('Integers after sorting in ascending order:', sort_three_integer(5, 2, 9))"
330+
]
331+
},
332+
{
333+
"cell_type": "markdown",
334+
"id": "71a641db-897a-45b8-b7ef-374c63b6108c",
335+
"metadata": {},
336+
"source": [
337+
"#### 10.Write a Python program to sort files by date."
338+
]
339+
},
340+
{
341+
"cell_type": "code",
342+
"execution_count": 11,
343+
"id": "408e05f4-c7e2-4be5-b0bc-ec9ff9c5f813",
344+
"metadata": {},
345+
"outputs": [
346+
{
347+
"name": "stdout",
348+
"output_type": "stream",
349+
"text": [
350+
"file1.txt\n"
351+
]
352+
}
353+
],
354+
"source": [
355+
"import glob\n",
356+
"import os\n",
357+
"\n",
358+
"files = glob.glob(\"*.txt\")\n",
359+
"files.sort(key=os.path.getmtime)\n",
360+
"print(\"\\n\".join(files))"
361+
]
362+
},
363+
{
364+
"cell_type": "code",
365+
"execution_count": null,
366+
"id": "1ca5e5a3-0285-4eae-b33a-573bf4397960",
367+
"metadata": {},
368+
"outputs": [],
369+
"source": []
370+
}
371+
],
372+
"metadata": {
373+
"kernelspec": {
374+
"display_name": "Python 3 (ipykernel)",
375+
"language": "python",
376+
"name": "python3"
377+
},
378+
"language_info": {
379+
"codemirror_mode": {
380+
"name": "ipython",
381+
"version": 3
382+
},
383+
"file_extension": ".py",
384+
"mimetype": "text/x-python",
385+
"name": "python",
386+
"nbconvert_exporter": "python",
387+
"pygments_lexer": "ipython3",
388+
"version": "3.12.6"
389+
}
390+
},
391+
"nbformat": 4,
392+
"nbformat_minor": 5
393+
}

0 commit comments

Comments
 (0)