Python Programming
Department of Computer Science & Engineering
CHAPTER-1
Basics
Basics
What is Python?
➢ High Level Programming Language
➢ Shorter Language( 3-5 lines than java,
5-10 lines than c++)
➢ Since 1991, consistently in top 10
most popular computing language
Image source : https://www.nicepng.com/
History
➢ Invented in the Netherlands,
early 90s by Guido van Rossum
➢ Named after Monty Python
➢ Open sourced from the
beginning
➢ Managed by Python Software
Foundation
Figure 1.1 Python Release
Easy to learn No type declaration –
Duck Typing
Open Source
Object Oriented
Why Python?
Broad Standard Library
GUI programming
Interactive + Scripting Mode Rapid Development Cycle
Image source : Google
Moral of the Python Story
Image source : https://hackernoon.com/
Moral of the Python Story
Image source : Google
Image source : Google
Reserved Words
➢ Reserved Words can not be used as identifiers name
Image source : http://python-nitol.blogspot.com/
Identifier
➢ A name given to identify variables, user defined function, class and object
➢ Rules
1. Must start with letter or
underscore
2. Must contain only letters, ❑ Valid : my_var _x var1
numbers or underscores
❑ Not Valid : my.var 2x var$
3. Case sensitive
❑ Different : My_var my_var x X
4. Keywords are not allowed
Figure 1.2 Identifier Example
Operators
➢ Required to perform an operation on variables
Arithmetic Operators Comparison Operators
Operators Name Operators Name
+ Addition == Equal
- Subtraction != Not equal
* Multiplication > Greater than
/ Division < Less than
** Power >= Greater than or equal to
% Reminder
<= Less than or equal to
// Integer division
Table 1.1 Arithmetic Operators Table 1.2 Comparison Operators
Operators
Special Operators
Logical Operators Operator Operation
Operator Operation is Returns true if identity of two
operands are same, else false
and (x and y) is true if both x is not Returns true if identity of two
and y are true operands are not same, else
false
or (x or y) is true if either x
in Returns True if a sequence with
or y is true
the specified value is present in
the object
not True value will become
false not in Returns True if a sequence with
the specified value is not
present in the object
Table 1.3 Logical Operators
Table 1.4 Special Operators
Python As Interactive Shell
➢ Get immediate output after typing one python instruction
➢ Python Installation:
➢ Get latest python release(3.0 OR above) from official website
http://www.python.org/download
Python As Script
➢Write sequence of statements into file and tell python to execute it
➢Python file uses extension as ‘.py’
➢Execute your python file from command prompt with command :
‘python hello_world.py’
➢Create a script:
➢ Open a file from your python idle 3.0
➢ Write python statements
➢ Execute file by choosing ‘Run Module’ or press F5
Code Flow in Python
➢Python code is sequence of statements to perform task
➢Some statements can be selective based on conditions
➢Some statements can be iterative
➢Some group of statements may be used again at different places
Image source : https://www.researchgate.net/
Indentation in Python
➢Line indentation used to identify block of code
While
Block
Comments in Python
➢ Statements ignored by python
➢ Use ‘#’ sign to write comment
➢ Used to provide documentation to python statements
Python comment
www.paruluniversity.ac.in