Python Coding Part-1
In [103]: import keyword
Keywords
Keywords are the reserved words in Python and can't be used as an identifier
In [3]: print([Link]) # 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([Link]) # 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
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]: '''
Multipl
e line
commen
t '''
val1 = 10
In [21]: """
Multipl
e line
commen
t """
val1 = 10
Statements
Instructions that a Python interpreter can execute.
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 [Link] 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 ind
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
In [39]: for i in
range(0,5): # correct
print(i) indentation
0
1
2
3
4
In [43]: # if indentation is skipped we will encounter "IndentationError:
expected an ind
for i in
range(0,5):
File "<ipython-input-43-4a6de03bf63e>", line 2
print(i)
^
IndentationError: expected an indented block
for i in range(0,5): print(i) # correct indentation but less
In [45]:
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