Skip to content

Commit 88d04c2

Browse files
committed
004 - Basic Syntax - Learn Python Full Course For Beginner. Source code used in above video.
1 parent 07fc471 commit 88d04c2

File tree

1 file changed

+380
-0
lines changed

1 file changed

+380
-0
lines changed
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Python - Basic Syntax"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Assignments"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 7,
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"5\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"# The following is a statement\n",
32+
"a = 5 # is an assignment\n",
33+
"print(a)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Lines & Indentation"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 8,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"True\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"# If Statement below is with proper indentation. Works perfectly fine\n",
58+
"if True:\n",
59+
" print(\"True\")\n",
60+
"else:\n",
61+
" print(\"False\")"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 9,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"ename": "IndentationError",
71+
"evalue": "expected an indented block (<ipython-input-9-8a6ab3e1e73f>, line 3)",
72+
"output_type": "error",
73+
"traceback": [
74+
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-9-8a6ab3e1e73f>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m print(\"True\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"# If statement below without indentation. Will throw error.\n",
80+
"if True:\n",
81+
"print(\"True\")\n",
82+
"else:\n",
83+
"print(\"False\")"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"**_The above statement executed with error. Error says that after the `if True:` in line 2, it expected indented block in the next line which is missing. Hence the error_**"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 10,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"ename": "SyntaxError",
100+
"evalue": "invalid syntax (<ipython-input-10-b90599039ce6>, line 5)",
101+
"output_type": "error",
102+
"traceback": [
103+
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-10-b90599039ce6>\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m else:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"# If Statement with multiple statements within the blocks without indentation. Will throw error\n",
109+
"if True:\n",
110+
" print(\"Hello its\")\n",
111+
"print(\"True\")\n",
112+
"else:\n",
113+
" print(\"Hello its\")\n",
114+
"print(\"False\")"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"**_in the above code block, the third line `print(\"True\")` should be indented which is missing. \n",
122+
"Hence python has considered this line as outside the block for `if True:`. \n",
123+
"So the `else:` is been seen as a else without a corresponding if condition. Hence the error -** `invalid syntax`\n",
124+
"\n",
125+
"The above code block with correct indentation is given below, and you can see it works fine."
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 12,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"Hello its\n",
138+
"True\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"# If Statement with multiple statements within the blocks without indentation. Will throw error\n",
144+
"if True:\n",
145+
" print(\"Hello its\")\n",
146+
" print(\"True\")\n",
147+
"else:\n",
148+
" print(\"Hello its\")\n",
149+
" print(\"False\")"
150+
]
151+
},
152+
{
153+
"attachments": {},
154+
"cell_type": "markdown",
155+
"metadata": {},
156+
"source": [
157+
"## Quotations in Python\n",
158+
"\n",
159+
"Python accepts single `'`, double `\"` and triple `'''` or `\"\"\"` quotes to denote string literals, as long as the same type of quote starts and ends the string."
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 16,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"sunday\n",
172+
"\n",
173+
"Today is Sunday.\n",
174+
"\n",
175+
"Hello everyone. This para\n",
176+
"spans multiple\n",
177+
"lines.\n"
178+
]
179+
}
180+
],
181+
"source": [
182+
"# String literal with single quote\n",
183+
"single_word = 'sunday\\n'\n",
184+
"print(single_word)\n",
185+
"\n",
186+
"# String literal using double quotes\n",
187+
"sentence = \"Today is Sunday.\\n\"\n",
188+
"print(sentence)\n",
189+
"\n",
190+
"# String literal using 3 double quotes. Here you can notice that the sentance spans multiple lines, but its still valid.\n",
191+
"paragraph = \"\"\"Hello everyone. This para\n",
192+
"spans multiple\n",
193+
"lines.\"\"\"\n",
194+
"print(paragraph)"
195+
]
196+
},
197+
{
198+
"attachments": {},
199+
"cell_type": "markdown",
200+
"metadata": {},
201+
"source": [
202+
"## Comments in Python\n",
203+
"\n",
204+
"Comments can be used to explain Python code and also to make the code more readable.<br>\n",
205+
"Comments can also be used to prevent execution when testing code.\n",
206+
"\n",
207+
"A hash sign `#` that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.\n",
208+
"\n",
209+
"**_In the statement below, notice how the first line and the string after # in the second line are ignored._**"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 17,
215+
"metadata": {},
216+
"outputs": [
217+
{
218+
"name": "stdout",
219+
"output_type": "stream",
220+
"text": [
221+
"Hello Python World!\n"
222+
]
223+
}
224+
],
225+
"source": [
226+
"# First comment\n",
227+
"print(\"Hello Python World!\") # second comment"
228+
]
229+
},
230+
{
231+
"attachments": {},
232+
"cell_type": "markdown",
233+
"metadata": {},
234+
"source": [
235+
"Comments does not have to be text to explain the code, it can also be used to prevent Python from executing code.\n",
236+
"\n",
237+
"**_In the code below, the first line is ignored_**"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": 18,
243+
"metadata": {},
244+
"outputs": [
245+
{
246+
"name": "stdout",
247+
"output_type": "stream",
248+
"text": [
249+
"Hello Python World!\n"
250+
]
251+
}
252+
],
253+
"source": [
254+
"# print(\"Hello Python World!\")\n",
255+
"print(\"Hello Python World!\")"
256+
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {},
261+
"source": [
262+
"Python does not really have a syntax for multi-line comments. To add a multiline comment you could insert a `#` for each line."
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": 20,
268+
"metadata": {},
269+
"outputs": [
270+
{
271+
"name": "stdout",
272+
"output_type": "stream",
273+
"text": [
274+
"Hello Python World!\n"
275+
]
276+
}
277+
],
278+
"source": [
279+
"# This is a comment.\n",
280+
"# Another comment.\n",
281+
"# Yet another comment.\n",
282+
"print(\"Hello Python World!\")"
283+
]
284+
},
285+
{
286+
"cell_type": "markdown",
287+
"metadata": {},
288+
"source": [
289+
"Not the same as comment, you can use a multiline string.\n",
290+
"Python ignores string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it."
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": 21,
296+
"metadata": {},
297+
"outputs": [
298+
{
299+
"name": "stdout",
300+
"output_type": "stream",
301+
"text": [
302+
"Hello Python World!\n"
303+
]
304+
}
305+
],
306+
"source": [
307+
"'''\n",
308+
"This is a comment.\n",
309+
"Another comment.\n",
310+
"Yet another comment.\n",
311+
"'''\n",
312+
"print(\"Hello Python World!\")"
313+
]
314+
},
315+
{
316+
"cell_type": "markdown",
317+
"metadata": {},
318+
"source": [
319+
"**_In the above code, notice that the first 5 lines are ignored by python, just like it ignores comments._**"
320+
]
321+
},
322+
{
323+
"attachments": {},
324+
"cell_type": "markdown",
325+
"metadata": {},
326+
"source": [
327+
"## Multiple Statements on a Single Line\n",
328+
"\n",
329+
"The semicolon ( `;` ) allows multiple statements on the single line. However, neither statement should start a new code block."
330+
]
331+
},
332+
{
333+
"cell_type": "code",
334+
"execution_count": 27,
335+
"metadata": {},
336+
"outputs": [
337+
{
338+
"name": "stdout",
339+
"output_type": "stream",
340+
"text": [
341+
"Hello\n",
342+
"another statement\n",
343+
"yet another statement\n"
344+
]
345+
}
346+
],
347+
"source": [
348+
"print(\"Hello\"); print(\"another statement\"); print(\"yet another statement\");"
349+
]
350+
},
351+
{
352+
"cell_type": "code",
353+
"execution_count": null,
354+
"metadata": {},
355+
"outputs": [],
356+
"source": []
357+
}
358+
],
359+
"metadata": {
360+
"kernelspec": {
361+
"display_name": "Python 3",
362+
"language": "python",
363+
"name": "python3"
364+
},
365+
"language_info": {
366+
"codemirror_mode": {
367+
"name": "ipython",
368+
"version": 3
369+
},
370+
"file_extension": ".py",
371+
"mimetype": "text/x-python",
372+
"name": "python",
373+
"nbconvert_exporter": "python",
374+
"pygments_lexer": "ipython3",
375+
"version": "3.7.0"
376+
}
377+
},
378+
"nbformat": 4,
379+
"nbformat_minor": 2
380+
}

0 commit comments

Comments
 (0)