Skip to content

Commit cbd488d

Browse files
committed
added lesson 2 #data-types&conversion
1 parent 2f4f405 commit cbd488d

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed

Lesson-2.ipynb

+313
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "bdc4946b-43bb-4bf3-aff0-4473917363ae",
6+
"metadata": {},
7+
"source": [
8+
"Understanding Data Types in Python\n",
9+
"\n",
10+
"In Python, data types are the classification of data items. Python has several built-in data types, and each type has its own rules and properties. Here are the main data types you'll encounter:"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "3c517d1d-033b-4f36-b600-37a1e4227fd5",
16+
"metadata": {},
17+
"source": [
18+
"Integers (int): Whole numbers without a fractional part."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"id": "6f223503-048d-4567-b0ef-a9280921ff65",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
" a = 10\n",
29+
" b = -5"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"id": "b4691206-dac4-479d-8246-970988b794e6",
35+
"metadata": {},
36+
"source": [
37+
"Floating-Point Numbers (float): Numbers with a decimal point."
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "610b839e-2aad-4fe0-9a88-65e796302f8d",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"c = 3.14\n",
48+
"d = -2.718"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "71c65dee-bac3-4d5c-a6e9-3392a19a2e64",
54+
"metadata": {},
55+
"source": [
56+
"Strings (str): A sequence of characters enclosed in quotes (single or double)."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "11a02d7b-164e-4345-8c1d-df2103ef4127",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"e = \"hello\"\n",
67+
"f = 'world'"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"id": "11d6d922-27be-4b28-aa32-741ef5319290",
73+
"metadata": {},
74+
"source": [
75+
"Booleans (bool): Represents True or False."
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"id": "0cabcdce-064d-4f5e-9264-87c02861afbf",
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"g = True\n",
86+
"h = False"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"id": "703f383c-186f-47a0-821e-8b1562732a61",
92+
"metadata": {},
93+
"source": [
94+
"Lists (list): Ordered collection of items which can be of different types. Lists are mutable, meaning you can change their content."
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "6cf01916-c907-4246-a64b-9dcdaa865ba0",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"i = [1, 2, 3, \"four\", 5.0]"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"id": "b5073df7-02be-4188-b059-fa4ea0febfa6",
110+
"metadata": {},
111+
"source": [
112+
"Tuples (tuple): Ordered collection of items which can be of different types. Tuples are immutable, meaning once created, their content cannot be changed."
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"id": "0c0364f6-9657-4ee5-a05b-1d1da03ec914",
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"j = (1, 2, 3, \"four\", 5.0)"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"id": "2f1b384a-9f37-4633-93f5-1911dc108e84",
128+
"metadata": {},
129+
"source": [
130+
"Sets (set): Unordered collections of unique items, enclosed in curly braces."
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"id": "d9ea428d-c587-4522-be66-9e9f75710f53",
137+
"metadata": {},
138+
"outputs": [],
139+
"source": [
140+
"y = {1, 2, 3}\n",
141+
"z = {'apple', 'banana'}"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"id": "0eaebb7e-fc24-405e-bbc1-6b031e4af940",
147+
"metadata": {},
148+
"source": [
149+
"Dictionaries (dict): Collection of key-value pairs. Each key is unique and used to access its corresponding value."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"id": "70f54597-562e-4d91-a417-411ef1a5e4a3",
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"k = {\"name\": \"Alice\", \"age\": 25}"
160+
]
161+
},
162+
{
163+
"cell_type": "markdown",
164+
"id": "ee9a9d72-c6aa-4b1b-9912-70b9613b401f",
165+
"metadata": {},
166+
"source": [
167+
"Type Conversion in Python\n",
168+
"\n",
169+
"Type conversion is the process of converting one data type to another. This is also known as type casting. \n",
170+
"Python provides several built-in functions for type conversion:"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"id": "f097113d-212c-491a-abf7-6cfbc81f8c0c",
176+
"metadata": {},
177+
"source": [
178+
"Converting to an Integer (int):"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": null,
184+
"id": "fd68e335-07dd-4488-a81e-b76892bca674",
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"x = int(3.14) \n",
189+
"y = int(\"10\")"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"id": "4052458b-105f-46da-96f5-c3123e37bfe9",
195+
"metadata": {},
196+
"source": [
197+
"Converting to a Float (float):"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"id": "c1875e75-0380-47b2-81d6-33e31b62d2c1",
204+
"metadata": {},
205+
"outputs": [],
206+
"source": [
207+
" z = float(10) # Converts int to float, result is 10.0\n",
208+
" w = float(\"3.14\") # Converts string to float, result is 3.14"
209+
]
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"id": "e4a6b768-6418-4006-bf4f-f2c0ac4d0757",
214+
"metadata": {},
215+
"source": [
216+
"Converting to a String (str):"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"id": "2951a0a5-dd97-4fbd-a9a0-948158cdeeb1",
223+
"metadata": {},
224+
"outputs": [],
225+
"source": [
226+
"a = str(10) # Converts int to string, result is \"10\"\n",
227+
"b = str(3.14) # Converts float to string, result is \"3.14\""
228+
]
229+
},
230+
{
231+
"cell_type": "markdown",
232+
"id": "35a18d94-d299-4ebe-a12b-8d5d895e3532",
233+
"metadata": {},
234+
"source": [
235+
"Converting to a List (list):"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"id": "c2af13c9-f2f5-4bb8-be3d-ed0de39d8eda",
242+
"metadata": {},
243+
"outputs": [],
244+
"source": [
245+
"c = list((1, 2, 3)) # Converts tuple to list, result is [1, 2, 3]\n",
246+
"d = list(\"hello\") # Converts string to list, result is ['h', 'e', 'l', 'l', 'o']"
247+
]
248+
},
249+
{
250+
"cell_type": "markdown",
251+
"id": "12af8d1b-340d-4903-9920-ae614a67ef9f",
252+
"metadata": {},
253+
"source": [
254+
" Practical example to illustrate type conversion:"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"id": "c3eff578-1058-41b0-8a05-d3492ac36add",
261+
"metadata": {},
262+
"outputs": [],
263+
"source": [
264+
"# Let's say we have a string representing a number\n",
265+
"num_str = \"123\"\n",
266+
"\n",
267+
"# Convert the string to an integer\n",
268+
"num_int = int(num_str)\n",
269+
"\n",
270+
"# Now let's add 10 to this integer\n",
271+
"result = num_int + 10\n",
272+
"\n",
273+
"print(result) # Output will be 133\n",
274+
"\n",
275+
"# Convert the integer back to a string\n",
276+
"result_str = str(result)\n",
277+
"\n",
278+
"print(result_str) # Output will be \"133\""
279+
]
280+
},
281+
{
282+
"cell_type": "markdown",
283+
"id": "583246f8-ec99-4315-9445-f13162246a8a",
284+
"metadata": {},
285+
"source": [
286+
"- Python has various data types like integers, floats, strings, booleans, lists, tuples, and dictionaries.\n",
287+
"- Type conversion allows you to convert a value from one data type to another.\n",
288+
"- Use built-in functions like int(), float(), str(), list(), tuple(), and dict() for conversions."
289+
]
290+
}
291+
],
292+
"metadata": {
293+
"kernelspec": {
294+
"display_name": "Python 3 (ipykernel)",
295+
"language": "python",
296+
"name": "python3"
297+
},
298+
"language_info": {
299+
"codemirror_mode": {
300+
"name": "ipython",
301+
"version": 3
302+
},
303+
"file_extension": ".py",
304+
"mimetype": "text/x-python",
305+
"name": "python",
306+
"nbconvert_exporter": "python",
307+
"pygments_lexer": "ipython3",
308+
"version": "3.12.4"
309+
}
310+
},
311+
"nbformat": 4,
312+
"nbformat_minor": 5
313+
}

0 commit comments

Comments
 (0)