Python -
Introduction
Dr. M. Ramakrishnan
• Created by Guido Van Rossum at 1991
• Popular Scripting Language
• AI & Data Analysis
• Web Development
• Python works on different platforms
• Windows
Intro • Linux
• Mac
• Simple syntax
• Programmer can achieve the objective with
fewer lines of code
• Interpreter based language
• Python 2
• More existing software package uses this.
• No update will be available.
• Nearing its EOL
Versions • Python 3
• Released on December 2008
• Backward incompatible
• https://www.python.org
/downloads/
• To install packages –
Python pip install
Packagename
Installation
• https://www.anaconda.c
om/products/individual
• Conda install
packagename
Python - • Indentation – Spaces at the beginning of
the code line
features • Python variables are not declared
• Variable is declared when you assign
value for it.
• x = 5 # x is declared as integer
• Y = 5.23 #here Y is declared as float
• Casting
• X = str(3) # X value is ‘3’
• Case Sensitive
• x=5
• X = “John
• Variable name can start with a alphabet
letter and underscore
• A variable name can not start with a number
• A Variable name can contain alpha numeric
characters and underscore
Naming a • Variable names are case sensitive
variable • 2myvar = "John"
my-var = "John"
my var = "John“
• Can assign multiple values to variables in one
line
• X, y = 5, 10
• Print(“x value is” + x)
•
+ for •
x = “IoT is”
Y = “Internet of Things”
Concatenation • Z=x+Y
• Print(Z)
+ Addition
- Subtraction
* Multiplication
Python / Division
Operators % Modulus Operator
Ex: x = 5%2 - Here 5 will be divided by 2
and the reminder (1) will be assigned to x.
• Equals: a == b
• Not Equals: a != b
Relational • Less than: a < b
Operators • Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Syntax
if condition:
if block statement 1
if block statement 2
….
….
If Statement if block statement N
When the condition is true, then the if
block statements 1 to N are getting
executed. Here the block is defined by the
indentation. Typically, in C, the block is
written inside the curly brace open and
close
•x = 5 •Here there are three blocks
If Statement •y = 10 and each block has one
(Example) •if x < y:
statement.
• print(“x is less than y”) •If x < y then “x less than y”
•elif x > y: will be printed.
•Else if x>y then “x greater
• print(“ x is greater than y”)
than y” will be printed
•else •Else “x is equal to y” will be
• print (“x is equal to y”) printed.
• Typical Syntax
variable Initialization
while condition with variable:
Loop statement 1
Loop statement 2
….
While loop ….
Loop statement N
Variable manipulation
As long as the condition is true, the while loop gets
executed. When the condition becomes false the
control comes out of while loop.
While loop
example
• Used to iterate over a
sequence
• Example:
For loop
For loop
Here, genre is a list with 3 elements.
• We can iterate through the list
with index in the for loop Len(genre) will give 3
using range() function range(len(genre)) will generate the numbers 0, 1,2
• range(5) will generate 5 So in the for loop, i gets the value of 0, 1 and 2 for each
numbers from 0 to 4. iteration.
• Example: In the first iteration genre[0] will be printed and in the next
iteration genre[1] will be printed. And then genre[2] will be
printed.