3.1. Notes
3.1. Notes
We can't use a keyword as variable name, function name or any other identifier
In [1]:
#Get all keywords in python 3.6
import keyword
#print(keyword.kwlist)
"Total number of keywords ", keyword.kwlist
In [4]:
"Total number of keywords ", len(keyword.kwlist)
Identifiers
Identifier is the name given to entities like class, functions, variables etc. in Python. It helps
differentiating one entity from another.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
In [2]:
global = 1
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 [3]:
#Print Hello, world to console
print("Hello, world")
Hello, world
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 """.
DocString in python
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_
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