This document tells how to install Python and gives an overview of basics of Python Programming. Python Programming could be very powerful in doing data analysis and for data science in general.

0.0.1 Installing Anaconda Python

Lets get started with installing Python on your machine first. This also installs IPython Notebook (Jupyter) which is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, machine learning and much more.

Anaconda for Windows

  1. Go to Anaconda https://www.continuum.io/
  2. Choose Version: Python 2.7 or Python 3.4.
  3. Choose Windows 64-bit or 32-bit.
  4. Download the graphical Installer.
  5. Double-click the .exe file to install Anaconda and follow the instructions on the screen.

Anaconda for OS X

  1. Go to Anaconda https://www.continuum.io/
  2. Choose Version: Python 2.7 or Python 3.4
  3. Choose Graphical or Command-line installer.
  4. Download the installer.
  5. Graphical Installer :- Double click the .pkg file and follow the instructions on the screen. (Recommended)
  6. Command Line Installer :-
  1. Open a terminal
  2. cd Downloads
  3. Execute: bash Anaconda-2.3.0-MacOSX-x86_64.sh

Anaconda for Linux

  1. Go to Anaconda https://www.continuum.io/
  2. Choose Version: Python 2.7 or Python 3.4
  3. Download the Installer
  4. Command-lineInstaller:
  1. Open a Shell
  2. cd Downloads
  3. Execute: bash Anaconda-2.3.0-Linux-x86_64.sh

0.0.2 Installing IPython notebook (Jupyter)

Anaconda Python installation includes IPython Notebook installation, so usually you dont need to install Jupyter notebook. But in case if there are any issues and the Jupyter Notebook is not installed you can run these two commands in a shell/terminal to install it :

0.0.3 Starting an IPython notebook

  1. Take a shell/terminal window
  2. cd to your Working Directory (All IPython notebooks will be created here)
  3. Run: ipython3 notebook (That will open a notebook window in your browser)

0.0.4 Overview of Python Programming

0.0.5 Variable Assignment

The following code tells you how the variable assignment is done in python. You cannot start with a number or special character for a variable.

# Can not start with number or special characters
name_of_var = 2       # the variable name_of_var holds the value 2.
print(name_of_var)    # print() function is used to print the result
## 2
x = 2       # variable x has the value 2 assigned to it.
y = 3       # variable y has the value 3 assigned to it.
z = x + y   # variable z has the addition of x and y 

# Lets check the variables 
print(x)
print(y)
print(z)
## 2
## 3
## 5

0.1 Data types

0.1.1 Numbers, Arithmetic Operations

Some basic examples of using numbers and arithmetic operations in python :-

add = 1 + 1  # addition 
print(add)
## 2
mul = 1 * 3  # multiplication
print(mul)
## 3
div = 4 / 2  # division
print(div)
## 2
exp = 2 ** 4  # 2 to the power of 4
print(exp)
## 16
mod1 = 4 % 2   # modulus operation (return the remainder as an output)
mod2 = 5 % 2 

print(mod1)
print(mod2)
## 0
## 1
bodmas = (2+3) * (5+5)   # BODMAS rule
print(bodmas)
## 50
i = 100           # integer 
print(type(i))    # check the type of a variable by type() function

f = 100.5         # float
print(type(f))
## <type 'int'>
## <type 'float'>

Some basic arithmetic operations :-

print(min(10,20,30))    # returns the minimum value
print(max(10,20,30))    # returns the maximum value
print(abs(-100))        # returns the absolute value
## 10
## 30
## 100
import math
x = 100
print(math.sqrt(x))        # returns square root
print(math.factorial(5))   # returns factorial
## 10.0
## 120

0.1.2 Strings

You can initialize strings within single quotes or double quotes as shown by the following examples :-

s = 'Hello'
p = "I love Python"

print(s)
print(p)
## Hello
## I love Python

If you want to use quotes within the string you can use it by the following way :-

p1 = "I love 'Python'"       # the most straight forward way 
print(p1)
## I love 'Python'

You can append two strings as follows :-

s1 = "Hello"
s2 = " World"

print(s1+s2)  # notice in s2 I have used a blank space before World. If not the output would have been "HelloWorld". 
## Hello World

You can use * for reptition of a string as shown below :-

print('A' * 10)           # prints 'A' 10 times 
## AAAAAAAAAA
print('Hello' * 10)       # prints the word 'Hello' 10 times 
## HelloHelloHelloHelloHelloHelloHelloHelloHelloHello

0.1.3 Printing

Using .format() to print string :-

num = 12
name = 'Sam'

print('My number is: {one}, and my name is: {two}'.format(one=num,two=name)) 
## My number is: 12, and my name is: Sam

0.1.4 Lists

Lists are mutable objects which mean they are not fixed in size and are dynamic. In simple words you can add or remove elements from Lists.

x = [1,2,3,"John"]       # List can contain same data types or multiple data types.
print(x)                 # print the list  
print(type(x))           # confirm the type
## [1, 2, 3, 'John']
## <type 'list'>

List append :-

my_list = ['a','b','c']      # create a list 
my_list.append('d')          # append to the list using .append() function

print(my_list)
## ['a', 'b', 'c', 'd']

List Subsetting :-

my_list =  ['a','b','c','d']  # create the same list 
print(my_list)
print(my_list[0])             # select the first element from the list. In Python the List indexing begins from 0.
print(my_list[1])             # select the second item. 
print(my_list[1:])            # select all items from the 1st index and onwards. 
print(my_list[:1])            # select all items before the 1st index. 
## ['a', 'b', 'c', 'd']
## a
## b
## ['b', 'c', 'd']
## ['a']

0.1.5 Dictionaries

Dictionaries are special data types. They are stored as key:value pairs.

d = {'key1':'item1','key2':'item2'}    # create a dictionary
print(d)

print(d['key1'])                       # will return the value associated with the key 'key1' from the dictionary
## {'key2': 'item2', 'key1': 'item1'}
## item1

0.1.6 Booleans

Basic boolean operations :-

x = True             # initialize x as true 
print(type(x))       # confirm the type to be boolean
                     
y = False           # initialize y as false 
print(type(y))      # confirm the type to be boolean
## <type 'bool'>
## <type 'bool'>
x1 = 10 > 2          # will return true
print(x1)

y1 = 1 > 10          # will return false
print(y1)
## True
## False

0.1.7 Tuples

Unlike Lists Tuples are fixed size in nature. They are immutable which means you cannot add elements in a tuple.

t = (1,2,3,4)       # initialize a tuple t.
print(t)            # print the tuple
print(type(t))      # confirm the type

# t.append(5)       # You can't add elements to a tuple as it is immutable. So this will return an error. 
## (1, 2, 3, 4)
## <type 'tuple'>

0.1.8 Sets

A set contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics.

set1 = {1,2,3}                              # create a set
print(set1)                                 # print a set

set2 = {1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}  
print(set2)                                 # will only return the unique values 
## set([1, 2, 3])
## set([1, 2, 3])

0.1.9 Comparison Operators

Basic Comparison Operators :-

x = 10
y = 20

print(x < y)        # returns true
print(x > y)        # returns false 
print(x <= y)       # returns true
print(x >= y)       # returns false
print(x == y)       # returns false (check for equality)
print(x != y)       # returns true  (! is not operator)
## True
## False
## True
## False
## False
## True
x = 10
xx = 10.0
y = 10

print(x == xx)    # returns true
print(x is xx)    # returns false
print(x is y)     # returns true
## True
## False
## True
A = [10,20,30,40]        # create a list
print(A)

print(10 in A)           # returns true
print(50 in A)           # returns false

s = "Hello World"        # sample string
print(s)                 # print string

print('e' in s)          # returns true
print('k' in s)          # returns false
print('Hello' in s)      # returns true
## [10, 20, 30, 40]
## True
## False
## Hello World
## True
## False
## True

0.1.10 Logical Operators

Some examples of logical operators :-

print(1 > 2) and (2 < 3)                  # false and true will return false
print(1 > 2) or (2 < 3)                   # false or true will return true
print((1 == 2) or (2 == 3) or (4 == 4))   # false or false or true will return true
## False
## True
## True

0.1.11 Assignment Operators

Some examples of assignment operators :-

x = 10
print(x)

x = x + 5
print(x)

x +=10      # similar to x = x + 10
print(x)

x -=5      # similar to x = x - 5
print(x)

x *=2      # similar to x = x * 2
print(x)
## 10
## 15
## 25
## 20
## 40

0.1.12 if,elif, else Statements

Some examples of if, elif and else statements :-

if 1 < 2:
   print('Yep!')         # will print 'Yep!'
   
if 1 < 2:
   print('first')
else:
   print('last')         # will print 'first'
   
if 1 > 2:
   print('first')
else:
   print('last')         # will print 'last'
   
if 1 == 2:
   print('first')
elif 3 == 3:
   print('middle')
else:
   print('last')         # will print middle
## Yep!
## first
## last
## middle

0.1.13 for Loops

Some examples of for loops :-

seq = [1,2,3,4,5]        # create a list

for item in seq:
    print(item)          # will print all the contents of the list seq. 
## 1
## 2
## 3
## 4
## 5
seq = [1,2,3,4,5]  

for item in seq:
    print('Yep!')          # will print 'Yep!' 5 times
## Yep!
## Yep!
## Yep!
## Yep!
## Yep!
seq = [1,2,3,4,5] 

for item in seq:
    print(item+item)       # add each element in the list with itself
## 2
## 4
## 6
## 8
## 10

0.1.14 while Loops

Example of while Loop :-

i = 1
while i < 5:
      print('i is: {}'.format(i))
      i = i + 1
## i is: 1
## i is: 2
## i is: 3
## i is: 4

0.1.15 range()

range() function can be used to create a list. It has two paramets start and stop. Example is shown as follows :-

print(range(5))          # prints a list with 5 items from 0 to 4
print(range(5,20))       # prints a list from 5 to 19. 
## [0, 1, 2, 3, 4]
## [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

for i in range(5):         # you can also use a for loop with range
    print(i)
## 0
## 1
## 2
## 3
## 4

0.1.16 List comprehension

Example of List comprehension :-

x = [1,2,3,4]
out = []
 
for item in x:               # take each element from x, square it and append to out []
    out.append(item ** 2)
print(out)
## [1, 4, 9, 16]
x = [1,2,3,4]

print([item ** 2 for item in x])    # same output as above with one liner
## [1, 4, 9, 16]

0.1.17 functions

An example of a simple function in Python :-

def square(x):
    return x**2            # function takes an integer and returns its squared value

out = square(2)
print(out)                # returns 4
## 4

0.1.18 lambda expressions

def times2(var):
    return var*2          # takes an integer as an input and returns integer times 2 as output
out = times2(2) 
print(out)


l = lambda var: var*2        # same function using lambda which is one liner
print l(2)
## 4
## 4

0.1.19 map and filter

Example using map and filter along with lambda expressions :-

def times2(var):                                    # same times2 function as defined above
    return var*2 
   
seq = [1,2,3,4,5]                                   # create a list
print(list(map(times2,seq)))                        # use map to apply times2 function on each element of list seq.

print(list(map(lambda var: var*2,seq)))             # same as above but using lambda expression

print(list(filter(lambda item: item%2 == 0,seq)))   # filter and print those elements from list seq which are divisible by 2
## [2, 4, 6, 8, 10]
## [2, 4, 6, 8, 10]
## [2, 4]

0.1.20 methods

Some common methods which are used frequently :-

st = 'Hello My Name is Sam'
print(st)
print(st.lower())                    # makes the string lower case
print(st.upper())                    # makes the string upper case
print(st.split())                    # returns a list of each word on space

tweet = 'Go Sports! #Sports'
print(tweet)
print(tweet.split('#'))              # split on # (any special character)

lst = [1,2,3]
print(lst)
lst.pop()                            # returns the last item from the list
print(lst)
## Hello My Name is Sam
## hello my name is sam
## HELLO MY NAME IS SAM
## ['Hello', 'My', 'Name', 'is', 'Sam']
## Go Sports! #Sports
## ['Go Sports! ', 'Sports']
## [1, 2, 3]
## [1, 2]

I hope you liked this basic overview of Python Programming. I will keep on posting additional documents that focuses on data analysis and data science in Python programming.

Meanwhile you can also check some additional resources here :-

  1. Code Academy https://www.codecademy.com/learn/python
  2. Google Python Class https://developers.google.com/edu/python/
  3. Udacity https://www.udacity.com/course/programming-foundations-with-python--ud036
  4. Python Course List https://cisco.jiveon.com/docs/DOC-1742650

Great Job !