3/25/2021 Python - Jupyter Notebook
Prepared by Asif Bhat
Python Tutorial
In [103]: import sys
import keyword
import operator
from datetime import datetime
import os
Keywords
Keywords are the reserved words in Python and can't be used as an identifier
In [3]: print(keyword.kwlist) # List all Python Keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'cl
ass', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'fr
om', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
In [4]: len(keyword.kwlist) # Python contains 35 keywords
Out[4]: 35
Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.
In [13]: 1var = 10 # Identifier can't start with a digit
File "<ipython-input-13-37e58aaf2d3b>", line 1
1var = 10 # Identifier can't start with a digit
^
SyntaxError: invalid syntax
In [14]: val2@ = 35 # Identifier can't use special symbols
File "<ipython-input-14-cfbf60736601>", line 1
val2@ = 35 # Identifier can't use special symbols
^
SyntaxError: invalid syntax
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 1/118
,3/25/2021 Python - Jupyter Notebook
In [15]: import = 125 # Keywords can't be used as identifiers
File "<ipython-input-15-f7061d4fc9ba>", line 1
import = 125 # Keywords can't be used as identifiers
^
SyntaxError: invalid syntax
In [16]: """
Correct way of defining an identifier
(Identifiers can be a combination of letters in lowercase (a to z) or uppercase (
"""
val2 = 10
In [17]: val_ = 99
Comments in Python
Comments can be used to explain the code for more readabilty.
In [18]: # Single line comment
val1 = 10
In [19]: # Multiple
# line
# comment
val1 = 10
In [20]: '''
Multiple
line
comment
'''
val1 = 10
In [21]: """
Multiple
line
comment
"""
val1 = 10
Statements
Instructions that a Python interpreter can execute.
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 2/118
,3/25/2021 Python - Jupyter Notebook
In [27]: # Single line statement
p1 = 10 + 20
p1
Out[27]: 30
In [28]: # Single line statement
p2 = ['a' , 'b' , 'c' , 'd']
p2
Out[28]: ['a', 'b', 'c', 'd']
In [26]: # Multiple line statement
p1 = 20 + 30 \
+ 40 + 50 +\
+ 70 + 80
p1
Out[26]: 290
In [29]: # Multiple line statement
p2 = ['a' ,
'b' ,
'c' ,
'd'
]
p2
Out[29]: ['a', 'b', 'c', 'd']
Indentation
Indentation refers to the spaces at the beginning of a code line. It is very important as Python uses
indentation to indicate a block of code.If the indentation is not correct we will endup with
IndentationError error.
In [37]: p = 10
if p == 10:
print ('P is equal to 10') # correct indentation
P is equal to 10
In [38]: # if indentation is skipped we will encounter "IndentationError: expected an inde
p = 10
if p == 10:
print ('P is equal to 10')
File "<ipython-input-38-d7879ffaae93>", line 3
print ('P is equal to 10')
^
IndentationError: expected an indented block
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 3/118
, 3/25/2021 Python - Jupyter Notebook
In [39]: for i in range(0,5):
print(i) # correct indentation
0
1
2
3
4
In [43]: # if indentation is skipped we will encounter "IndentationError: expected an inde
for i in range(0,5):
print(i)
File "<ipython-input-43-4a6de03bf63e>", line 2
print(i)
^
IndentationError: expected an indented block
In [45]: for i in range(0,5): print(i) # correct indentation but less readable
0
1
2
3
4
In [48]: j=20
for i in range(0,5):
print(i) # inside the for loop
print(j) # outside the for loop
0
1
2
3
4
20
Docstrings
1) Docstrings provide a convenient way of associating documentation with functions, classes,
methods or modules.
2) They appear right after the definition of a function, method, class, or module.
In [49]: def square(num):
'''Square Function :- This function will return the square of a number'''
return num**2
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 4/118
Prepared by Asif Bhat
Python Tutorial
In [103]: import sys
import keyword
import operator
from datetime import datetime
import os
Keywords
Keywords are the reserved words in Python and can't be used as an identifier
In [3]: print(keyword.kwlist) # List all Python Keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'cl
ass', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'fr
om', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
In [4]: len(keyword.kwlist) # Python contains 35 keywords
Out[4]: 35
Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.
In [13]: 1var = 10 # Identifier can't start with a digit
File "<ipython-input-13-37e58aaf2d3b>", line 1
1var = 10 # Identifier can't start with a digit
^
SyntaxError: invalid syntax
In [14]: val2@ = 35 # Identifier can't use special symbols
File "<ipython-input-14-cfbf60736601>", line 1
val2@ = 35 # Identifier can't use special symbols
^
SyntaxError: invalid syntax
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 1/118
,3/25/2021 Python - Jupyter Notebook
In [15]: import = 125 # Keywords can't be used as identifiers
File "<ipython-input-15-f7061d4fc9ba>", line 1
import = 125 # Keywords can't be used as identifiers
^
SyntaxError: invalid syntax
In [16]: """
Correct way of defining an identifier
(Identifiers can be a combination of letters in lowercase (a to z) or uppercase (
"""
val2 = 10
In [17]: val_ = 99
Comments in Python
Comments can be used to explain the code for more readabilty.
In [18]: # Single line comment
val1 = 10
In [19]: # Multiple
# line
# comment
val1 = 10
In [20]: '''
Multiple
line
comment
'''
val1 = 10
In [21]: """
Multiple
line
comment
"""
val1 = 10
Statements
Instructions that a Python interpreter can execute.
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 2/118
,3/25/2021 Python - Jupyter Notebook
In [27]: # Single line statement
p1 = 10 + 20
p1
Out[27]: 30
In [28]: # Single line statement
p2 = ['a' , 'b' , 'c' , 'd']
p2
Out[28]: ['a', 'b', 'c', 'd']
In [26]: # Multiple line statement
p1 = 20 + 30 \
+ 40 + 50 +\
+ 70 + 80
p1
Out[26]: 290
In [29]: # Multiple line statement
p2 = ['a' ,
'b' ,
'c' ,
'd'
]
p2
Out[29]: ['a', 'b', 'c', 'd']
Indentation
Indentation refers to the spaces at the beginning of a code line. It is very important as Python uses
indentation to indicate a block of code.If the indentation is not correct we will endup with
IndentationError error.
In [37]: p = 10
if p == 10:
print ('P is equal to 10') # correct indentation
P is equal to 10
In [38]: # if indentation is skipped we will encounter "IndentationError: expected an inde
p = 10
if p == 10:
print ('P is equal to 10')
File "<ipython-input-38-d7879ffaae93>", line 3
print ('P is equal to 10')
^
IndentationError: expected an indented block
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 3/118
, 3/25/2021 Python - Jupyter Notebook
In [39]: for i in range(0,5):
print(i) # correct indentation
0
1
2
3
4
In [43]: # if indentation is skipped we will encounter "IndentationError: expected an inde
for i in range(0,5):
print(i)
File "<ipython-input-43-4a6de03bf63e>", line 2
print(i)
^
IndentationError: expected an indented block
In [45]: for i in range(0,5): print(i) # correct indentation but less readable
0
1
2
3
4
In [48]: j=20
for i in range(0,5):
print(i) # inside the for loop
print(j) # outside the for loop
0
1
2
3
4
20
Docstrings
1) Docstrings provide a convenient way of associating documentation with functions, classes,
methods or modules.
2) They appear right after the definition of a function, method, class, or module.
In [49]: def square(num):
'''Square Function :- This function will return the square of a number'''
return num**2
localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 4/118