Skip to content

Commit b6cddd0

Browse files
Basic of Python
1 parent b73cc74 commit b6cddd0

File tree

1 file changed

+354
-0
lines changed

1 file changed

+354
-0
lines changed

01_Python_Basics_Improved.ipynb

Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "bb15401b",
6+
"metadata": {},
7+
"source": [
8+
"# 🐍 Python Basics Notebook\n",
9+
"This notebook contains fundamental concepts of Python programming with examples and explanations."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"id": "c8d60193",
15+
"metadata": {},
16+
"source": [
17+
"## 1. Variables and Data Types\n",
18+
"Variables are containers for storing data values. Python has various data types like int, float, string, boolean, etc."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"id": "f62bfe24",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"name = \"Shivam\"\n",
29+
"age = 21\n",
30+
"height = 5.9\n",
31+
"is_student = True\n",
32+
"\n",
33+
"print(name, age, height, is_student)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"id": "92c7bca5",
39+
"metadata": {},
40+
"source": [
41+
"## 2. Type Casting\n",
42+
"You can convert values from one data type to another."
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"id": "805b5c2f",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"x = 5\n",
53+
"y = float(x)\n",
54+
"name = str(x)\n",
55+
"print(x, y, name)"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"id": "4f288f83",
61+
"metadata": {},
62+
"source": [
63+
"## 3. Input from User"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"id": "acf67901",
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"user_name = input(\"Enter your name: \")\n",
74+
"print(\"Welcome,\", user_name)"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"id": "018ff0ce",
80+
"metadata": {},
81+
"source": [
82+
"## 4. Conditional Statements\n",
83+
"Used to execute code based on conditions."
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"id": "75675993",
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"marks = 85\n",
94+
"if marks >= 90:\n",
95+
" print(\"Grade A\")\n",
96+
"elif marks >= 75:\n",
97+
" print(\"Grade B\")\n",
98+
"else:\n",
99+
" print(\"Grade C\")"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"id": "c92be02a",
105+
"metadata": {},
106+
"source": [
107+
"## 5. Loops\n",
108+
"Used for iteration."
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"id": "ca96278d",
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"# For loop\n",
119+
"for i in range(1, 6):\n",
120+
" print(i)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": null,
126+
"id": "35996e7d",
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"# While loop\n",
131+
"count = 0\n",
132+
"while count < 3:\n",
133+
" print(\"Counting:\", count)\n",
134+
" count += 1"
135+
]
136+
},
137+
{
138+
"cell_type": "markdown",
139+
"id": "74e01974",
140+
"metadata": {},
141+
"source": [
142+
"## 6. Functions\n",
143+
"Functions are reusable blocks of code."
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"id": "73782cf0",
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"def add(a, b):\n",
154+
" return a + b\n",
155+
"\n",
156+
"add(10, 5)"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"id": "78d3ebfc",
162+
"metadata": {},
163+
"source": [
164+
"## 7. Lists, Tuples, and Sets"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": null,
170+
"id": "a2adcccf",
171+
"metadata": {},
172+
"outputs": [],
173+
"source": [
174+
"# List\n",
175+
"fruits = [\"apple\", \"banana\", \"cherry\"]\n",
176+
"print(fruits[1])"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"id": "943383aa",
183+
"metadata": {},
184+
"outputs": [],
185+
"source": [
186+
"# Tuple\n",
187+
"colors = (\"red\", \"green\", \"blue\")\n",
188+
"print(colors[0])"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"id": "7e31f1c1",
195+
"metadata": {},
196+
"outputs": [],
197+
"source": [
198+
"# Set\n",
199+
"nums = {1, 2, 3, 2}\n",
200+
"print(nums)"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"id": "7fa80a3c",
206+
"metadata": {},
207+
"source": [
208+
"## 8. Dictionaries\n",
209+
"Key-value pairs to store data."
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": null,
215+
"id": "0d0c658f",
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"student = {\"name\": \"Shivam\", \"age\": 21, \"grade\": \"A\"}\n",
220+
"print(student[\"grade\"])"
221+
]
222+
},
223+
{
224+
"cell_type": "markdown",
225+
"id": "db65197c",
226+
"metadata": {},
227+
"source": [
228+
"## 9. Loops with Lists and Dictionaries"
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": null,
234+
"id": "6b660129",
235+
"metadata": {},
236+
"outputs": [],
237+
"source": [
238+
"# Loop through list\n",
239+
"for fruit in fruits:\n",
240+
" print(fruit)"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"id": "792cc111",
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"# Loop through dictionary\n",
251+
"for key, value in student.items():\n",
252+
" print(key, \"=\", value)"
253+
]
254+
},
255+
{
256+
"cell_type": "markdown",
257+
"id": "e32bb396",
258+
"metadata": {},
259+
"source": [
260+
"## 10. String Methods"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": null,
266+
"id": "215cde95",
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"message = \" Hello Python! \"\n",
271+
"print(message.strip())\n",
272+
"print(message.lower())\n",
273+
"print(message.upper())\n",
274+
"print(message.replace(\"Python\", \"World\"))"
275+
]
276+
},
277+
{
278+
"cell_type": "markdown",
279+
"id": "a1f0191f",
280+
"metadata": {},
281+
"source": [
282+
"## 11. File Handling"
283+
]
284+
},
285+
{
286+
"cell_type": "code",
287+
"execution_count": null,
288+
"id": "24cfb30e",
289+
"metadata": {},
290+
"outputs": [],
291+
"source": [
292+
"with open(\"sample.txt\", \"w\") as f:\n",
293+
" f.write(\"This is a test file.\")\n",
294+
"\n",
295+
"with open(\"sample.txt\", \"r\") as f:\n",
296+
" content = f.read()\n",
297+
" print(content)"
298+
]
299+
},
300+
{
301+
"cell_type": "markdown",
302+
"id": "c8853f74",
303+
"metadata": {},
304+
"source": [
305+
"## 12. Exception Handling"
306+
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": null,
311+
"id": "c7fde07c",
312+
"metadata": {},
313+
"outputs": [],
314+
"source": [
315+
"try:\n",
316+
" result = 10 / 0\n",
317+
"except ZeroDivisionError:\n",
318+
" print(\"You can't divide by zero!\")\n",
319+
"finally:\n",
320+
" print(\"This block always runs.\")"
321+
]
322+
},
323+
{
324+
"cell_type": "markdown",
325+
"id": "045a2989",
326+
"metadata": {},
327+
"source": [
328+
"## Summary\n",
329+
"We’ve now seen the basics of Python including variables, data types, conditions, loops, functions, collections, and more. Practice more to become confident!"
330+
]
331+
}
332+
],
333+
"metadata": {
334+
"kernelspec": {
335+
"display_name": "Python 3 (ipykernel)",
336+
"language": "python",
337+
"name": "python3"
338+
},
339+
"language_info": {
340+
"codemirror_mode": {
341+
"name": "ipython",
342+
"version": 3
343+
},
344+
"file_extension": ".py",
345+
"mimetype": "text/x-python",
346+
"name": "python",
347+
"nbconvert_exporter": "python",
348+
"pygments_lexer": "ipython3",
349+
"version": "3.11.7"
350+
}
351+
},
352+
"nbformat": 4,
353+
"nbformat_minor": 5
354+
}

0 commit comments

Comments
 (0)