Monday, February 22, 2016

Python Data Analysis 2 - Introduction to the Python's World

Chap 2 Introduction to the Python's World


The Python interpreter is simply a program that reads and interprets the commands passed to the prompt. You have seen that the interpreter can accept either a single command at a time or entire files of Python code. However the approach by which it performs this is always the same.

Each time you press the Enter key, the interpreter begins to scan the code written (either a row or a full file of code) token by token (tokenization). These tokens are fragments of text which the interpreter will arrange in a tree structure. The tree obtained is the logical structure of the program which is then converted to bytecode (.pyc or .pyo). The process chain ends with the bytecode which will be executed by a Python virtual machine (PVM).

Using Python

myname = raw_input("What is your name? ")
print "Hi " + myname + ", Iím glad to say: Hello world!"

Wrinting Python Code

1 + 2
(1.045 * 3)/4
4 ** 2
((4 + 5j) * (2 + 3j))
4 < (2*3)
a = 12 * 3.4
a

import math
math.sin(a)
from math import *
sin(a)
from math import sin

Data Structure

list, set, strings, tuples, dictionary, deque, heap

# dictionary
dict = {ínameí:íWilliamí, íageí:25, ícityí:íLondoní}
dict["name"]

for key, value in dict.items():
 print(key,value)

list = [1,2,3,4]
list
list[2]
list[1:3]
list[-1]
items = [1,2,3,4,5]
for item in items:
 item + 1

# Functional Programming (Only for Python 3.4)

map(function, list)
filter(function, list)
reduce(function, list)
lambda
list comprehension

items = [1,2,3,4,5]
def inc(x): return x+1
list(map(inc,items))
list(map((lambda x: x+1),items))
list(filter((lambda x: x < 4), items))
from functools import reduce
reduce((lambda x,y: x/y), items)

S = [x**2 for x in range(5)] 
S

# Indentation

a = 4
if a > 3:
 if a < 5:
         print("Iím four")
 else:
  print("Iím a little number")

if a > 3:
 if a < 5:
  print("Iím four")
 else:
  print("Iím a big number")

IPython

IPython is a further development of Python that includes a number of tools: IPython shell, a powerful interactive shell resulting in a greatly enhanced Python terminal; a QtConsole, which is a hybrid between a shell and a GUI, allowing in this way to display graphics inside the console instead of in separate windows; and finally the IPython Notebook, which is a web interface that allows you to mix text, executable code, graphics, and formulas in a single representation.

In [1]: print "Hello World!"
Hello World! 

In [2]: 3/2
Out[2]: 1

In [3]: 5.0/2
Out[3]: 2.5

In [4]: In
Out[4]: [íí, uíprint "Hello World!"í, uí3/2í, uí5.0/2í, uí_i2í, uíIní]

In [5]: In[3]
Out[5]: uí5.0/2í

{2: 1,
 3: 2.5,
 4: [íí,
  uíprint "Hello World!"í,
  uí3/2í,
  uí5.0/2í,
  uí_i2í,
  uíIní,
  uíIn[3]í,
  uíOutí], 
 5: uí5.0/2í}

SciPy

SciPy (pronounced ìSigh Pieî) is a set of open-source Python libraries specialized for scientific computing. Many of these libraries will be the protagonists of many chapters of the book, given that their knowledge is critical to the data analysis. Together they constitute a set of tools for calculating and displaying data that has little to envy from other specialized environments for calculation and data analysis (such as R or Matlab). 

 NumPy is the foundation library for scientific computing
 Pandas provides complex data structures and functions specifically designed to make the work on them easy, fast, and effective.
 matplotlib is currently most popular for producing plots and other data visualizations in 2D.

No comments:

Post a Comment

Blog Archive