Python Keywords
Keywords are the reserved words in python
We can't use a keyword as variable name, function name or any other identifier
Keywords are case sentive
In [1]:
#Get all keywords in python 3.6
import keyword
#print(keyword.kwlist)
"Total number of keywords ", keyword.kwlist
Out [1]: ('Total number of keywords ',
['False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield'])
In [4]:
"Total number of keywords ", len(keyword.kwlist)
Out [4]: ('Total number of keywords ', 35)
Identifiers
Identifier is the name given to entities like class, functions, variables etc. in Python. It helps
differentiating one entity from another.
Rules for Writing Identifiers:
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0
to 9) or an underscore (_).
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
3. Keywords cannot be used as identifiers.
In [2]:
global = 1
File "<ipython-input-2-3d177345d6e4>", line 1
global = 1
^
SyntaxError: invalid syntax
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@ = 10 #can't use special symbols as an identifier
Python Comments
Comments are lines that exist in computer programs that are ignored by compilers and interpreters.
Including comments in programs makes code more readable for humans as it provides some
information or explanation about what each part of a program is doing.
In general, it is a good idea to write comments while you are writing or updating a program as it is easy
to forget your thought process later on, and comments written later may be less useful in the long
term.
In Python, we use the hash (#) symbol to start writing a comment.
In [3]:
#Print Hello, world to console
print("Hello, world")
Hello, world
Multi Line Comments
If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning
of each line.
In [6]:
#This is a long comment
#and it extends
#Multiple lines
Another way of doing this is to use triple quotes, either ''' or """.
In [7]: """This is also a
perfect example of
multi-line comments"""
Out [7]: 'This is also a\nperfect example of\nmulti-line comments'
DocString in python
Docstring is short for documentation string.
It is a string that occurs as the first statement in a module, function, class, or method definition.
In [9]:
def double(num):
"""
function to double the number
"""
return 2 * num
print (double(10))
20
In [10]:
print(double.__doc__) #Docstring is available to us as the attribute __doc_
function to double the number
Python Indentation
1. Most of the programming languages like C, C++, Java use braces { } to define a block of code.
Python uses indentation.
2. A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line. The amount of indentation is up to you, but it must be consistent throughout
that block.
3. Generally four whitespaces are used for indentation and is preferred over tabs.
In [12]:
for i in range(10):
print (i)
0
1
2
3
4
5
6
7
8
9
Indentation can be ignored in line continuation. But it's a good idea to always indent. It makes the code
more readable.
In [14]:
if True:
print("Machine Learning")
c = "AAIC"
Machine Learning
In [15]:
if True: print()"Machine Learning"; c = "AAIC"
Machine Learning
Python Statement