Python Notes
Sandeep Agrawal
if you are intrusted in one on one classes of Python
Contect me in WhatsAp: +916386469316 ¶
What is Python
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for: web development (server-side), software development, mathematics, system scripting.
What can Python do?
Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software development.
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is written.
This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Python Getting Started
Python Install
Many PCs and Macs will have python already installed.
To check if you have python installed on a Windows PC, search in the start bar for Python or run the
following on the Command Line (cmd.exe):
, 1 C:\Users\Your Name>python --version
To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac
open the Terminal and type:
In [ ]: 1 python --version
If you find that you do not have Python installed on your computer, then you can download it for free from
the following website: https://www.python.org/ (https://www.python.org/)
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write Python (.py)
files in a text editor and then put those files into the python interpreter to be executed.
The way to run a python file is like this on the command line:
1 C:\Users\Your Name>python helloworld.py
Where "helloworld.py" is the name of your python file.
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
In [1]: 1 # helloworld.py
2 print("Hello, World!")
Hello, World!
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in
Python is very important.
Python uses indentation to indicate a block of code.
In [2]: 1 if 5 > 2:
2 print("Five is greater than two!")
Five is greater than two!
In [3]: 1 # Syntax Error:
2 if 5 > 2:
3 print("Five is greater than two!")
File "<ipython-input-3-a314491c53bb>", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block
,In [4]: 1 # Example
2 if 5 > 2:
3 print("Five is greater than two!")
4 if 5 > 2:
5 print("Five is greater than two!")
Five is greater than two!
Five is greater than two!
In [5]: 1 # You have to use the same number of spaces in the same block of code, otherwise P
2 # Example
3 #Syntax Error:
4 if 5 > 2:
5 print("Five is greater than two!")
6 print("Five is greater than two!")
File "<ipython-input-5-fe2edd88874a>", line 6
print("Five is greater than two!")
^
IndentationError: unexpected indent
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 [6]: 1 #Get all keywords in python 3.6
2
3 import keyword
4
5 print(keyword.kwlist)
6
7 print("Total number of keywords ", len(keyword.kwlist))
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'globa
l', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
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 [7]: 1 global = 1
File "<ipython-input-7-3d177345d6e4>", line 1
global = 1
^
SyntaxError: invalid syntax
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
In [8]: 1 a@ = 10 #can't use special symbols as an identifier
File "<ipython-input-8-a615a2f3f728>", line 1
a@ = 10 #can't use special symbols as an identifier
^
SyntaxError: invalid syntax
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 [9]: 1 #Print Hello, world to console
2 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 [10]: 1 #This is a long comment
2 #and it extends
3 #Multiple lines
Another way of doing this is to use triple quotes, either ''' or """.
,In [11]: 1 """This is also a
2 perfect example of
3 multi-line comments"""
Out[11]: '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 [13]: 1 def double(num):
2 """
3 function to double the number
4 """
5 return 2 * num
6
7 print (double(10))
20
In [14]: 1 print (double.__doc__) #Docstring is available to us as the attribute __doc__ of t
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 [15]: 1 for i in range(10):
2 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 [16]: 1 if True:
2 print ("Learning")
3 c = "AAIC"
Learning
In [17]: 1 if True: print( "Learning"); c = "AAIC"
Learning
Python Statement
Instructions that a Python interpreter can execute are called statements.
Examples:
In [19]: 1 a = 1 #single statement
Multi-Line Statement
In Python, end of a statement is marked by a newline character. But we can make a statement extend
over multiple lines with the line continuation character ().
In [20]: 1 a = 1 + 2 + 3 + \
2 4 + 5 + 6 + \
3 7 + 8
In [21]: 1 print (a)
36
In [22]: 1 #another way is
2 a = (1 + 2 + 3 +
3 4 + 5 + 6 +
4 7 + 8)
5 print (a)
36
In [23]: 1 a = 10; b = 20; c = 30 #put multiple statements in a single line using ;
Variables
A variable is a location in memory used to store some data (value).
They are given unique names to differentiate between different memory locations. The rules for writing a
variable name is same as the rules for writing identifiers in Python.
Sandeep Agrawal
if you are intrusted in one on one classes of Python
Contect me in WhatsAp: +916386469316 ¶
What is Python
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for: web development (server-side), software development, mathematics, system scripting.
What can Python do?
Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software development.
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is written.
This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Python Getting Started
Python Install
Many PCs and Macs will have python already installed.
To check if you have python installed on a Windows PC, search in the start bar for Python or run the
following on the Command Line (cmd.exe):
, 1 C:\Users\Your Name>python --version
To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac
open the Terminal and type:
In [ ]: 1 python --version
If you find that you do not have Python installed on your computer, then you can download it for free from
the following website: https://www.python.org/ (https://www.python.org/)
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write Python (.py)
files in a text editor and then put those files into the python interpreter to be executed.
The way to run a python file is like this on the command line:
1 C:\Users\Your Name>python helloworld.py
Where "helloworld.py" is the name of your python file.
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
In [1]: 1 # helloworld.py
2 print("Hello, World!")
Hello, World!
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in
Python is very important.
Python uses indentation to indicate a block of code.
In [2]: 1 if 5 > 2:
2 print("Five is greater than two!")
Five is greater than two!
In [3]: 1 # Syntax Error:
2 if 5 > 2:
3 print("Five is greater than two!")
File "<ipython-input-3-a314491c53bb>", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block
,In [4]: 1 # Example
2 if 5 > 2:
3 print("Five is greater than two!")
4 if 5 > 2:
5 print("Five is greater than two!")
Five is greater than two!
Five is greater than two!
In [5]: 1 # You have to use the same number of spaces in the same block of code, otherwise P
2 # Example
3 #Syntax Error:
4 if 5 > 2:
5 print("Five is greater than two!")
6 print("Five is greater than two!")
File "<ipython-input-5-fe2edd88874a>", line 6
print("Five is greater than two!")
^
IndentationError: unexpected indent
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 [6]: 1 #Get all keywords in python 3.6
2
3 import keyword
4
5 print(keyword.kwlist)
6
7 print("Total number of keywords ", len(keyword.kwlist))
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'globa
l', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
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 [7]: 1 global = 1
File "<ipython-input-7-3d177345d6e4>", line 1
global = 1
^
SyntaxError: invalid syntax
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
In [8]: 1 a@ = 10 #can't use special symbols as an identifier
File "<ipython-input-8-a615a2f3f728>", line 1
a@ = 10 #can't use special symbols as an identifier
^
SyntaxError: invalid syntax
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 [9]: 1 #Print Hello, world to console
2 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 [10]: 1 #This is a long comment
2 #and it extends
3 #Multiple lines
Another way of doing this is to use triple quotes, either ''' or """.
,In [11]: 1 """This is also a
2 perfect example of
3 multi-line comments"""
Out[11]: '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 [13]: 1 def double(num):
2 """
3 function to double the number
4 """
5 return 2 * num
6
7 print (double(10))
20
In [14]: 1 print (double.__doc__) #Docstring is available to us as the attribute __doc__ of t
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 [15]: 1 for i in range(10):
2 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 [16]: 1 if True:
2 print ("Learning")
3 c = "AAIC"
Learning
In [17]: 1 if True: print( "Learning"); c = "AAIC"
Learning
Python Statement
Instructions that a Python interpreter can execute are called statements.
Examples:
In [19]: 1 a = 1 #single statement
Multi-Line Statement
In Python, end of a statement is marked by a newline character. But we can make a statement extend
over multiple lines with the line continuation character ().
In [20]: 1 a = 1 + 2 + 3 + \
2 4 + 5 + 6 + \
3 7 + 8
In [21]: 1 print (a)
36
In [22]: 1 #another way is
2 a = (1 + 2 + 3 +
3 4 + 5 + 6 +
4 7 + 8)
5 print (a)
36
In [23]: 1 a = 10; b = 20; c = 30 #put multiple statements in a single line using ;
Variables
A variable is a location in memory used to store some data (value).
They are given unique names to differentiate between different memory locations. The rules for writing a
variable name is same as the rules for writing identifiers in Python.