Introduction To Python
Introduction To Python
PYTHON
The language that meets all requirements
And the language which all requirements meet
Why python?
print(hello world)
The obligatory
hello world
program
#
#
#
#
Basics
Datatypes
Conditional Statements
Loops
Operators
Lists
Functions
Datatypes
What you must have learnt in C:
1)
2)
3)
4)
Datatypes
Code
1)
2)
3)
4)
#integer
a = 21
#float
b = 3.14159
#complex
c = 3 + 4j
#string
g = hello world
x,y = 20,30
a = b = c = 1
Datatypes:
name = harambe
print(type(x))
print(type(name))
Conditional Statements
1) if statement
2) if-else statement
3) if-elif-else statement
Code
a,b,c = 4,1,2
if a>3:
print(a is greater than 3)
if b<3:
print(b is less than 3)
else:
print(b is not less than 3)
Conditional
Statements
a = 4
if a>5:
print(a is greater than 5)
elif a<5:
print(a is less than 5)
else:
print(a equals 5)
Loops
1) For loop
2) While loop
Code
#for loop
for i in range(10):
print(i)
#while loop
i = 0
while(i<10):
print(i)
i = i + 1
Example Code
for i in range(0,20,2):
for j in range(1,10,1):
print(i,j)
points = [0,4,5,10]
for i in points:
print(i)
Operators - Arithmetic
1)
2)
3)
4)
5)
6)
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus(%)
Exponent(**)
Code
a = 5
b = 6
#operations in order
a+b
b-2
a*4
b/3
a%b
a**2
Operators - Comparison
1)
2)
3)
4)
5)
6)
Code
a = 5
b = 6
#operations in order
a==b
b!=2
a>4
b<3
a>=b
a<=2
Operators - Logical
Those C / C++ pain days of || and &&
and ! are gone!!
1) and
2) or
3) not
Code
a = 5
b = 6
if(a==b and b>5):
print(something)
if(a==5 or b==2):
print(some other thing)
if(not b==9):
print(b is not equal to 9)
Lists
Lists
Example Code:
a = [A ,C, C, G, C, C, G, C, T, T ,A]
a.count(G)
a.insert(3,A)
"helloworld"
"hellohellohello"
"h"
"o"
"ell"
5
1
1
# concatenation
# repetition
# indexing
# (from end)
# slicing
# size
# comparison
# search
Code
Functions
A function is a block of organized,
reusable code that is used to perform a
single, related action.
def isDivisibleBy(dividend,divisor):
if(dividend%divisor==0):
return 1
else:
Return 0
print(isDivisibleBy(4,2))
Another Example:
#Recursive Factorial Function
def fact(n):
if(n<=1):
return 1
else:
return n*fact(n-1)
Intermediate topics
Dictionaries
Opening files
Regular Expressions
Numpy arrays
Searching with Scipy
Matplotlib
Dictionaries
Ever wanted to access items in an array the other way round? i.e give the name
and find the index?
Dictionaries are meant just for that. Its an associative list where your index
can be a string
Dictionaries
Example Code:
data['a']=1
# OR
data.update({'a':1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)
Inverting
Dictionary
Opening files
f = open('workfile','r+')
for line in f:
a=line.split(' ')
Numpy Arrays
Code
import numpy as np
np.array()
np.zeros((100,100,3),np.uint8)
#gives an array of size 100x100x3
#of type 8bit int
import numpy as np
Numpy Arrays
The basics
Scipy
Scipy has good searching options
For example, if you want only elements
greater than 0
Code
import scipy
i,j = scipy.where(A>0)
B = A[i,j]
#where A is 2D
#Similarly u can do it across arrays
i = scipy.where(A[:,0]>A[:,1])
import numpy as np
from scipy import *
Numpy Arrays
Thresholding
a=np.arange(30,dtype=np.uint8)
b=a.reshape(5,6)
i,j=where(b>15)
#i,j are lists containing indices of
#all those elements which are >15
#Set them all to 100
b[i,j]=100
#This can also be done in one line as
b=a.reshape(5,6)
b[b>15]=100
import numpy as np
from matplotlib import pyplot as
plt
Matplotlib
x=np.arange(1,10,.1)
y=x**2
plt.plot(x,y)
plt.scatter(x,y)
plt.show()
3D Plots
Turtle library
Regular Expressions
Plotting capabilities of matplotlib
Python packaging index (pypi)