Skip to content

Commit 213edb8

Browse files
authored
Python_Day-5
Solved some coding question
1 parent 11c5764 commit 213edb8

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

Python_Day5.ipynb

+345
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "cd90bbdc-17b0-4816-a994-b6c557629f31",
6+
"metadata": {},
7+
"source": [
8+
"#### 1.Write a Python program to check whether a file exists."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "b7c17718-3ab6-4c07-9724-6dd40358baae",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"data": {
19+
"text/plain": [
20+
"True"
21+
]
22+
},
23+
"execution_count": 1,
24+
"metadata": {},
25+
"output_type": "execute_result"
26+
}
27+
],
28+
"source": [
29+
"import os.path\n",
30+
"open('file1.txt','w')\n",
31+
"os.path.isfile('file1.txt')"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"id": "a0b0c783-504e-4ff6-969f-f8fcb7c4f17a",
37+
"metadata": {},
38+
"source": [
39+
"#### 2.Write a Python program to determine if a Python shell is executing in 32bit or 64bit mode on OS."
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 2,
45+
"id": "46a67718-5d4b-4e7e-990e-fedf50d03b20",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"data": {
50+
"text/plain": [
51+
"64"
52+
]
53+
},
54+
"execution_count": 2,
55+
"metadata": {},
56+
"output_type": "execute_result"
57+
}
58+
],
59+
"source": [
60+
"import struct\n",
61+
"struct.calcsize('P') * 8"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "741413a7-dd62-4fe2-b57e-e37aa501a307",
67+
"metadata": {},
68+
"source": [
69+
"Why char 'P'?\n",
70+
"struct is a module for packing and unpacking data to and from C representations. P represents void * (a generic pointer). On 32-bit systems a pointer is 4 bytes, and on a 64-bit system a pointer requires 8 bytes.\n",
71+
"\n",
72+
"struct.calcsize('P') calculates the number of bytes required to store a single pointer --> returning 4 on a 32-bit system and 8 on a 64-bit system."
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"id": "3049631b-f209-4153-99ae-a3a7070b203c",
78+
"metadata": {},
79+
"source": [
80+
"#### 3.Write a Python program to get OS name, platform and release information."
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 3,
86+
"id": "063eb65f-87f4-4e65-ba8f-b35ba11d6909",
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"name": "stdout",
91+
"output_type": "stream",
92+
"text": [
93+
"nt\n",
94+
"Windows\n",
95+
"11\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"import os, platform\n",
101+
"\n",
102+
"print(os.name)\n",
103+
"print(platform.system())\n",
104+
"print(platform.release())"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"id": "43174ef8-ee86-41cd-8f2a-577174f3dab6",
110+
"metadata": {},
111+
"source": [
112+
"### 4. Write a Python program to locate Python site-packages."
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 4,
118+
"id": "ffd96848-0ee9-4051-a1a4-d9177a5f4b09",
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"data": {
123+
"text/plain": [
124+
"['C:\\\\Users\\\\Sunil Zambare\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312',\n",
125+
" 'C:\\\\Users\\\\Sunil Zambare\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python312\\\\Lib\\\\site-packages']"
126+
]
127+
},
128+
"execution_count": 4,
129+
"metadata": {},
130+
"output_type": "execute_result"
131+
}
132+
],
133+
"source": [
134+
"import site\n",
135+
"\n",
136+
"site.getsitepackages()"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "29921156-283b-4940-aead-dcb1d83cf541",
142+
"metadata": {},
143+
"source": [
144+
"### 5. Write a python program to call an external command in Python."
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 6,
150+
"id": "5e9c740f-14bf-44ca-bed9-915fc04a7749",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"##from subprocess import call\n",
155+
"\n",
156+
"##call(['ls', '-l'])"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"id": "d094371a-3180-428f-8b0d-ef9ecb66b3c3",
162+
"metadata": {},
163+
"source": [
164+
"#### 6.Write a python program to get the path and name of the file that is currently executing."
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 7,
170+
"id": "920e9845-11d2-46a1-a529-e3d7ac28b826",
171+
"metadata": {},
172+
"outputs": [
173+
{
174+
"data": {
175+
"text/plain": [
176+
"'D:\\\\Jupyter_Notebook\\\\file1.txt'"
177+
]
178+
},
179+
"execution_count": 7,
180+
"metadata": {},
181+
"output_type": "execute_result"
182+
}
183+
],
184+
"source": [
185+
"import os\n",
186+
"\n",
187+
"os.path.realpath('file1.txt')"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"id": "689ff7c0-a1ee-429e-877b-6f91d24159ee",
193+
"metadata": {},
194+
"source": [
195+
"#### 7. Write a Python program to find out the number of CPUs using."
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 8,
201+
"id": "9fec5944-e6e4-49ba-8ec5-7fd769a58a34",
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"data": {
206+
"text/plain": [
207+
"8"
208+
]
209+
},
210+
"execution_count": 8,
211+
"metadata": {},
212+
"output_type": "execute_result"
213+
}
214+
],
215+
"source": [
216+
"import multiprocessing\n",
217+
"\n",
218+
"multiprocessing.cpu_count()"
219+
]
220+
},
221+
{
222+
"cell_type": "markdown",
223+
"id": "b386e284-ae8c-4e4d-a5b7-ad5be45b705c",
224+
"metadata": {},
225+
"source": [
226+
"#### 8. Write a Python program to parse a string to Float or Integer."
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 9,
232+
"id": "94657ded-1ac3-4d04-9ec1-b9af349b615f",
233+
"metadata": {},
234+
"outputs": [
235+
{
236+
"name": "stdout",
237+
"output_type": "stream",
238+
"text": [
239+
"<class 'float'>\n",
240+
"<class 'int'>\n"
241+
]
242+
}
243+
],
244+
"source": [
245+
"s = '3.14'\n",
246+
"\n",
247+
"str_to_float = float(s)\n",
248+
"# str_to_int = int(s) # We need to convert the string into float first. Otherwise it will raise 'ValueError'\n",
249+
"str_to_int = int(float(s))\n",
250+
"\n",
251+
"print(type(str_to_float))\n",
252+
"print(type(str_to_int))"
253+
]
254+
},
255+
{
256+
"cell_type": "markdown",
257+
"id": "5b245949-c085-4901-8134-2e335f5726ce",
258+
"metadata": {},
259+
"source": [
260+
"#### 9. Write a Python program to list all files in a directory in Python."
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 10,
266+
"id": "7aecdc4b-c57d-4ca4-8787-bd0211cd8841",
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stderr",
271+
"output_type": "stream",
272+
"text": [
273+
"<>:4: SyntaxWarning: invalid escape sequence '\\J'\n",
274+
"<>:4: SyntaxWarning: invalid escape sequence '\\J'\n",
275+
"C:\\Users\\Sunil Zambare\\AppData\\Local\\Temp\\ipykernel_6436\\77662623.py:4: SyntaxWarning: invalid escape sequence '\\J'\n",
276+
" files_list = [f for f in listdir('D:\\Jupyter_Notebook\\') if isfile(join('D:\\Jupyter_Notebook\\', f))]\n"
277+
]
278+
},
279+
{
280+
"ename": "SyntaxError",
281+
"evalue": "invalid syntax. Perhaps you forgot a comma? (77662623.py, line 4)",
282+
"output_type": "error",
283+
"traceback": [
284+
"\u001b[1;36m Cell \u001b[1;32mIn[10], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m files_list = [f for f in listdir('D:\\Jupyter_Notebook\\') if isfile(join('D:\\Jupyter_Notebook\\', f))]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n"
285+
]
286+
}
287+
],
288+
"source": [
289+
"from os import listdir\n",
290+
"from os.path import isfile, join\n",
291+
"\n",
292+
"files_list = [f for f in listdir('D:\\Jupyter_Notebook\\') if isfile(join('D:\\Jupyter_Notebook\\', f))]\n",
293+
"print(files_list)"
294+
]
295+
},
296+
{
297+
"cell_type": "markdown",
298+
"id": "5597a932-c89c-472d-b1fa-12217a4f114e",
299+
"metadata": {},
300+
"source": [
301+
"#### 10.Write a Python program to print without newline or space."
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": 12,
307+
"id": "75b5cf02-c941-4169-8863-85b98748fc62",
308+
"metadata": {},
309+
"outputs": [
310+
{
311+
"name": "stdout",
312+
"output_type": "stream",
313+
"text": [
314+
"# # # # # # # # # # "
315+
]
316+
}
317+
],
318+
"source": [
319+
"for i in range(10):\n",
320+
" print('#', end= ' ')"
321+
]
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python 3 (ipykernel)",
327+
"language": "python",
328+
"name": "python3"
329+
},
330+
"language_info": {
331+
"codemirror_mode": {
332+
"name": "ipython",
333+
"version": 3
334+
},
335+
"file_extension": ".py",
336+
"mimetype": "text/x-python",
337+
"name": "python",
338+
"nbconvert_exporter": "python",
339+
"pygments_lexer": "ipython3",
340+
"version": "3.12.6"
341+
}
342+
},
343+
"nbformat": 4,
344+
"nbformat_minor": 5
345+
}

0 commit comments

Comments
 (0)