python and data analysis

25
For Data Analysis PRAVEEN NAIR blog.ninethsense.com/

Upload: praveen-nair

Post on 01-Jul-2015

250 views

Category:

Data & Analytics


1 download

DESCRIPTION

A session on Python programming language and Data Analysis.

TRANSCRIPT

Page 1: Python and Data Analysis

For Data Analysis

PRAVEEN NAIR

blog.ninethsense.com/

Page 2: Python and Data Analysis

HISTORY

• Late 1980

• Started using by 1989

• V1.0 – 1994-2000

• v2.0 – 2000-2010

• v3.0 – 2008-current

Page 3: Python and Data Analysis

Origin of name - Python

Page 4: Python and Data Analysis

Guido van Rossum

Page 5: Python and Data Analysis
Page 6: Python and Data Analysis

Highlights

• Simple and Easy to Learn

• Free and Open Source

• High Level Language

• Portable

• Object Oriented

• Embeddable

• Extensive Libraries

Page 7: Python and Data Analysis

Applications for Python

• Web and Internet Development

• Scientific and Numeric

• Education

• Desktop GUIs

• Software Development

Page 8: Python and Data Analysis

Who uses Python?

• Google

• Yahoo

• YouTube

• BitTorrent

• Maya

• Intel, Cisco, HP, Seagate, IBM etc.

• Pixar/Disney

• JPMorgan, UBS, Citadel

• NASA

• Redhat

• USA CIA

Page 9: Python and Data Analysis

INSTALL

Page 10: Python and Data Analysis

Python Interactive Shell

Page 11: Python and Data Analysis

interpreter OR compiler ?

Page 12: Python and Data Analysis

Types

• Dynamic Typing• Strong Typing

Page 13: Python and Data Analysis

Python programming basics

Program 1

Program 2

Page 14: Python and Data Analysis

help

>>> help(math)

>>> help(math.sin)

Help on built-in function sin in module math:

sin(...)

sin(x)

Return the sine of x (measured in radians).

Page 15: Python and Data Analysis

Lists

Page 16: Python and Data Analysis

Strings>>> s = "Hello World"

>>> s.upper()

'HELLO WORLD'

>>> s.lower()

'hello world'

>>> s.find("ll")

2

>>> s.split("o")

['Hell', ' W', 'rld']

>>> s[2:6]

'llo '

Page 17: Python and Data Analysis

Strings - cont

>>> a = "Hello"

>>> a

'Hello'

>>> a[0]

'H'

>>> a[0] = "t"

Traceback (most recent call last):

File "<pyshell#3>", line 1, in <module>

a[0] = "t"

TypeError: 'str' object does not support item assignment

>>> a = ['hello','what','abc']

>>> a.sort()

>>> a

['abc', 'hello', 'what']

>>> a.append("test")

>>> a

['abc', 'hello', 'what', 'test']

>>> a.append("test")

>>> a

['abc', 'hello', 'what', 'test']

>>> a.sort()

>>> a

['abc', 'hello', 'test', 'what']

>>> a.reverse()

>>> a

['what', 'test', 'hello', 'abc']

>>> a.insert(0,"blah")

>>> a

['blah', 'what', 'test', 'hello', 'abc']

>>>

Page 18: Python and Data Analysis

Dictionary

>>> a = {'c': 'ccc', 'a': 'aaa', 'b': 'bbb'}

>>> a

{'c': 'ccc', 'a': 'aaa', 'b': 'bbb'}

>>> a["b"]

'bbb'

>>> a.get("b")

'bbb'

>>> a.keys()

dict_keys(['c', 'a', 'b'])

>>> a.values()

dict_values(['ccc', 'aaa', 'bbb'])

>>> a.items()

dict_items([('c', 'ccc'), ('a', 'aaa'), ('b', 'bbb')])

>>> del a["a"]

>>> a

{'c': 'ccc', 'b': 'bbb'}

>>> a.update({"a":"aaaa"})

>>> a

{'c': 'ccc', 'a': 'aaaa', 'b': 'bbb'}

>>>

Page 19: Python and Data Analysis

Bool

>>> bool(1)

True

>>> bool(0)

False

>>> bool("")

False

>>> bool("test")

True

>>> bool(True)

True

>>> bool(False)

False

s = "Hello World"

if "ll" in s:

print("Found")

else:

print("Not Found")

if "o" not in s:

print("Found")

else:

print("Not Found“)

Page 20: Python and Data Analysis

Bool - cont.

a = "hello"

if a == "test":

b = "test"

elif a == "hello":

b = "hello"

elif a == "blah":

b = "blah"

print("Answer:“,b)

a = 30

b = 10

c = 20

if (a > b):

if (a > c):

print ("Big = ", a)

else:

print ("Big = ", c)

elif (b > c):

print ("Big = ", b)

else:

print ("Big = ", c)

a = 14

if (1 <= a <= 10):

print("Yes!")

else:

print("No!")

Page 21: Python and Data Analysis

For

>>> for l in "HELLO WORLD":

print(l)

H

E

L

L

O

W

O

R

L

D

>>> a = ["hello","world","kochi"]

>>> a

['hello', 'world', 'kochi']

>>> for x in a:

print(x)

hello

world

kochi

>>>

>>> for i in range(1,5):

print(i)

1

2

3

4

Page 22: Python and Data Analysis

Files – reading and writing

>>> f = open("d:\\test.txt")

>>> f.readline()

'Praveen\n'

>>> open("d:\\test.txt").readlines()

['Praveen\n', 'Hello\n', 'World']

>>> o = open("d:\\test2.txt","w")

>>> o.write("test")

>>> o.close()

Page 23: Python and Data Analysis

Modules

>>> import math

>>> math.pi

3.141592653589793

>>> from math import pi

>>> pi

3.141592653589793

Page 24: Python and Data Analysis

Data Analysis libraries for Python

plotly

Ref: https://wiki.python.org/moin/NumericAndScientific

Page 25: Python and Data Analysis

DEMO