Basic Python - 1650699594076

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

4/4/2019

Basics of Python

Disclaimer: This material is protected under copyright act AnalytixLabs ©, 2011-2016. Unauthorized use and/ or duplication of this material or any part of this material
including data, in any form without explicit and written permission from AnalytixLabs is strictly prohibited. Any violation of this copyright will attract legal actions

Python for Data Science

1
4/4/2019

Python for Data Science


Note that we will focus on particularaspects of Python that would beimportant for someonewho wants to
 l oad in some data sets,
 perform some computations on them,
 a nd plotsome of theresults.
Therefore, we will mostl y be talking about Python’s built-in data structures and libraries from the perspecti ve of processing and
ma nipulating structured and unstructured data.

Why Python?
The practice of data scienceinvolves many interrelated but differentactivities,including
- a ccessing data,
- ma nipulating data,
- computing statistical summaries or business metrics,
- pl otting/gra phing/visualizing data,
- building predictiveand explanatory models,
- eva luating those models,and finally,
- i ntegrating models into productionsystems

One option for the da ta scientist is to learn several different softwa re packages that each specialize in one of these things, or to
us ea general-purpose,high-level programminglanguage that provideslibraries to do all these things.

Why Python?
Python is an excellent choice for this. It has a diverse range of open source libraries for just about
everything the data scientist w ill do. Some of its highlights include:

 Cross-platform - high performance python interpreters exist for running yourcode on almost any
operating system (Windows,Mac orLinux)or architecture.
 Free - Python and most of its libraries are both open source and free.
 Simple - It has efficient high-level data structures and a simple but effective approach to
object-oriented programming.
 Elegant syntax - w hich, together w ith its interpreted nature, makes it an ideal language for
scripting and rapid application development inmany areas on most platforms

2
4/4/2019

Python: Writing Pythonic Code


 You w ill often read questions on StackOverflow like,‘What is a more Pythonic way of doing X.’

 To know what that means,read The Zen o f Python. Simply runimport this on any Python interface.

 It is a description of its design principles,and code writtenusing these principlesis called ‘Pythonic.’

 While there are typically multiple ways to crack a given problem, w e w ill generally favor Pythonic
solutionsovershabby ones.

Python: Package Managers


Do read about pip and conda - bothof w hich w ill act asyourpackage/library managers.
To install libraries that aren’t part of the Anaconda distribution,youw ill be using commands such as

● pip install ggplot


● conda install ggplot

3
4/4/2019

What do you mean Python Basics?

Python Basic programming topics


 Basic Rules
 Declaring & Printing variables
 Objects, Methods,Attributes and Functions
 Using built-in functions
 Modules (Libraries)
 Data Types
 Ba s i c O per ators: Arithmetic using binary operators
 Dealing with Strings
 Control flow statements
 Control Flow with if, elif, else
 Loops
 Data Structures
 Working with Collections - List, Tuple, Set & Dictionary
 Functions – User defined functions
 Lambda functions
 Classes

4
4/4/2019

Python: Basic Rules


Comments:
Any text preceded by the hash mark (pound sign) # is ignored by the Python interp re ter.

Whitespace Formatting (Indentation):


Many languages like R, C++, Ja va, and Perl use curly braces to delimit blocks o f code. Python uses
wh itespace indentation to make code more readable and consistent. A colon denotes th e start o f an
indented code block a fter which all o f th e code must be indented by the same amount

One major reason th at whitespace ma tters is th at i t results in most Python code looking cosmetically
similar, which means less cogniti ve dissonance when you read a piece o f code tha t you didn’t wri te
yourself.

Python uses indenta tion for blocks , instead of curl y bra ces . Both tabs and spa ces a re supported, but the s tanda rd
i ndentation requires s tandard Python code to use four s paces. For example

Declaring & Printing variables


Variable are dynamically typed, so no need to mention the variable types. Python interpr eter can
automa tically infer the type when the variables are initialized. The simplest directive in Python is
the "print" directive - it simply prints out a line

There is di fference between Python 2 and 3 for the pri nt sta tement. In Python 2, the "print" s tatement is not a function,
and therefore i t is invoked wi thout pa rentheses. However, in Python 3, i t is a function, a nd mus t be invoked with
pa rentheses.

5
4/4/2019

Objects, Methods, Attributes and Functions

Everynumber, string,data structure, function, class,module,andsoonexists inthe Pythoninterpreter is referredto as a


Python object.

Eachobject has anassociated


 type (int,float,list,dict,str andsoon…)
 attachedfunctions, known as methods,
 these have access to the object’s internal data.
 They canbe calledusingthe syntax:obj.<method>(parameters)
 attributes whichcanbeaccessedwith the syntax:obj.attribute

“Functions are called using parentheses and passingzero or more arguments, optionallyassigningthe returned value to a
variable: result = f(x,y,z)”

Let’s discuss classes & objects later once we have done some basic topics in python

Built in functions

Functions comes with python base version, called built in


functions.
Example: round()

To invoke some functions that packaged need to be imported.

For example import a math function

6
4/4/2019

Modules (Libraries) - Packages


Certain functions in Python are not loadedby default.

These include both features included as part of the language as well as third-party features that y ou download
explicitly.In order to use these features, you’llneedto import the modules that contain them.

> In Py thon a module is simply a .py file containing function and variable definitions. You

c an import the module itself as:import pandas


But after this y ou’ll have to always access its functions by prefixing themwith the module name,

For example : pandas.Series()


Alternatively, we can provide an alias: import pandas as pd

This will save us some typing as wecan then write pd.Series() to refer to the same thing.
Another option is to import frequently used functions explicitly anduse themwithout any prefixes. For example,
from pandas import Series

Tip: Importing everything froma module is possible, but is considered bad practice as it might interfere with variable
names and function definitions in your working environment.
So avoid doing things like: from pandas import *

Modules (Libraries) - Packages


Exploring built-in modules:
 Two very i mportant functions come i n handy when exploring modules i n Python- the dir and help functions.
 We ca n l ook for which functions a re implemented i n each module by using thedir function
 When we find the function i n the module we want to use, we ca n read a bout it more using the help function, i nside the
Python i nterpreter

Writing modules
 Wri ti ng Python modules is very s imple. To create a module of your own, simply create a new .py file with the module
na me, and then i mport i t using the Python file name (without the .py extension) using the import command.

Writing packages
 Pa cka ges are namespaces which contain multiple packages and modules themselves. They a re simply directories, but
wi th a twist.
 Ea ch pa ckage i n Python is a directory which MUST contain a special file called __i nit__.py. This file can be empty, a nd i t
i ndicates that the directory i t contains is a Python package, so i t can be imported the same way a module can be
i mported.
 If we create a directory ca lled foo, which marks the package name, we can then create a module inside that package
ca l led bar. We a lso must not forget to a dd the __i nit__.py fi le inside the foo directory.

7
4/4/2019

Data Types
Py thon supports two ty pes of numbers - integers and floating point numbers. (It also supports c omplex numbers,
whic h will not be explained in this tutorial).

Py thon has a small set of built-in types for handling numerical data,strings, boolean (True or False) values, anddates and
time. These include

 None - The Python Null Value


 str, unicode - for strings
 int - signed integer whose maximum v alue is platformdependent.
 long - large ints are automatically converted to long
 float - 64-bit (double precision) floating point numbers
 bool - a True or False value

You c ould call the function type on an object to checkif it is an int or float or string etc.
Type Conversion can be achieved by using functions like int(), float(), str() on objects of other ty pes.

Basic Operators: Arithmetic using binary Operators


Just as any other programming languages, the addition, subtrac tion, multiplic ation, and div ision operators
c an be used with numbers. Most of the binary math operations and comparisons are as you might expect:
1 + 23; 5– 7; ‘This’ + ‘ That’
OperationDescription
a+b Add a and b
a-b Subtract b froma
a* b Multiply a by b
a/b Div ide a by b
a // b Floor-divide a by b,dropping any fractional remainder
a ** b Raise a to the b power
a&b True if both a and b are True.For integers,take the bitwise AND.
a| b True if either a or b is True.For integers,take the bitwise OR.
a^b For booleans,True if a or b is True, but not both.
For integers, take the bitwise EXCLUSIVE-OR.
a == b True if a equals b
a != b True if a is not equal to b
a <= b True if a is less than (less than or equal) to b
a<b a>b True if a is greater than (greater than or equal) to b
a >= b a is b True if a and b reference same Python object
a is not b True if a and b reference differentPy thon objects

8
4/4/2019

Arithmetic using Binary Operators

NOTE that Py thon 2.7 uses integ er division by default,so that 5 / 2 equals 2.Almost alway s this is not what we want,
so we have two options:
 Start y our files with from future__ import division
 Explic itly convert your denominator to a float as 5/float(2)
However, if for some reason you still want integer division,use the // operator.

Arithmetic using Binary Operators


Strings
“Many people use Py thon for its powerful and flexible built-in string processing c apabilities. You can write string literal
using either single quotes or double quotes, but multiline strings are defined with triple quotes. The difference between
the two is that using double quotes makes it easy to include apostrophes

a = 'one way of writing a string' b = "another way"


c = """This is a multiline string"""

Strings are
 sequences of c haracters, andso can be treated like other Py thon sequences (for iteration)
 immutable, y ou cannot modify themin place without creating a new string
 c an contain escape characters like \n or \t
 there’s a workaround if y ou want backslashes in your string: prefix it with r (for raw)
 concatenated by the + operator, try 'This' + ' and ' + 'That'

Here I will highlight a few cool string methods as a teaser to what y ou can do with Py thon

my_str = 'a, b, c, d, e'


my_str.replace('b', 'B')
my_str.split(',') '‐'.join(my_str.split(', '))

9
4/4/2019

Arithmetic using Binary Operators


Strings F ormatting:

Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a
"tuple" (a fix ed size list), together with a format string, which contain s normal text together with "argument specifiers", special symbols like
"%s" and "%d".

Let's say you have a variable called "name" with your user name in it, and you would then like to print(out a greeting to that user.)

# This prints out "Hello, ALabs!“


name = “ALabs“
print("Hello,%s!" % name)

To use two or more argument specifiers, use a tuple (parentheses):

# This prints out “ALabs is 4 years old.“


name = “ALabs“
age = 4
print("%s is %d years old." % (name,age))

Arithmetic using Binary Operators


Strings F ormatting:

Any object which is not a string can be formatted using the %s operator as well. The string which returns from the "repr" method of that
object is formatted as the string.

For example:

# This prints out:A list: [1,2, 3]


mylist = [1,2,3]
print("A list: %s" % mylist)

Here are some basic argument specifiers you should know:


%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)

10
4/4/2019

Dealing with strings - Examples

Control Flow with if, elif, else


Py thon uses boolean variables to evaluate conditions. The booleanv alues True and False are returned when an
expression is c ompared or evaluated.
“The if statement is one of the most well-known control flow statement ty pes. It c hecks a c ondition which,if True,
ev aluates the code in the block that follows:

if x < 0:
print 'It's negative'

An if statement can be optionally followed by one or more elif blocks and a catch-allelse block if all of the c onditions are
False:

if x < 0:
print 'It's negative' elif x == 0:
print 'Equal to zero' elif 0 < x < 5:
print 'Positive but smaller than 5' else:
print 'Positive and larger than or equal to 5'

If any of the conditions is True, no further elif or else blocks will be reached.

11
4/4/2019

Compound Logic
We can write compound logic using boolean operators like and, or. Remember that conditions are evaluated left-to-
right and will short circuit,i.e, if a True is found in an or statement, the remaining ones will not be tested.

if 5 < 10 or 8 > 9:
print ‘The second condition was ignored.’

You c an also write a ternary if-t hen-else on one line, which sometimes helps keep thingsc oncise, These statements called
as inline statements

parity = "even" if x % 2 == 0 else "odd”

Control flow statements - Loops


There are tw o ty pes of loops in Py thon, for and w hile .

for Loops : F or loops iterate over a given s equence .

These are meantfor iteration tasks over a collection (a Py thon data structure like a Tuple or List.)

Sy ntax:
for value in collection:
# do something with value

Example: Here we print out the squares of the first five natural numbers

for x in [1, 2, 3, 4, 5]: print x ** 2

- The c ontinue keywordadvances the for loop to the next iteration - skipping the remainder of the block.

For loops can iterate over a sequence of numbers using the "range" and "xrange" functions.
The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas
xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange).
Note that the range function is zero based.

12
4/4/2019

Control flow statements - Loops


Example: The following loopsums up v alues,ignoring instances of None

total = 0
for value in [1, 2, None, 4, None, 5]: if value is None:
continue total += value

- The break keyword is used to altogether exit the for loop. Example: This code sums elements of the list until a
5 is reached:

until_5 = 0
for value in [1, 4, 2, 0, 7, 5, 1, 4]: if value == 5:
break until_5 += value

Control flow statements - Loops


while Loops: While loops repeat as long as a certain boolean condition is met.

P y t h on h as a while loop a s well, which works a s ex pec ted.

x = 0
while x < 10:
print x, "is less than 10" x += 1

13
4/4/2019

Control flow statements - Loops


"break" and "continue" statements:
break is used to exit a for loop or a while loop, whereas continue is used to skip
the current block, and return to the "for" or "while" statement.

can we use "else" clause for loops?


we can use else for loops. When the loop condition of "for" or "while"
statement fails then code part in "else" is executed.

If break statement is executed inside for loop then the "else" part is skipped.
Note that "else" part is executed even if there is a continue statement.

Data Structures - Tuples


Python Data Structures are simple, but quite powerful. Understanding them well and mastering their use is critical
for a programmer to write effic ient code for doing data science. Here we will learn about Tuples, Lists and
Dic tionaries - eac h of which are characterized by how data is stored in them, and the use-cases they ’re most suitable
for.

TUPLES: A t uple is a sequence of Python objects that is


- one-dimensional,
- fixed-length,
- immutable.

Syntax: We can create tuples in two ways

-A c omma-separated sequenceof v alues assigned to a v ariable (optional: placed inside parentheses)


Calling tuple() on any sequence/iterator (eg. a list)

tup = 1, 2, 6
nested_tup = (1, 3, 5), (2, 8), [‘a’, ‘b’]

tuple([1, 2, 7]) tuple(‘forests’)

14
4/4/2019

Data Structures - Tuples


Subsetting: Elements can be accessedwith square brackets [],with indexes beginning with 0.

nested_tup[0]

Immutability: Once a tuple is created, it’s not possible to modify whichobject is stored at an index.

nested_tup[1] = (1, 3)
TypeError: 'tuple' object does not support item assignment

[Not e] The objects stores in the tuple (eg. a list) might be mutable, so they can be altered (but not mov ed.)

nested_tup[2].append(‘c’)

Concatenation: The ‘+’operator joins tuples to formlonger tuples.

(4,None,'foo')+(6,0)+('bar',)

Data Structures - Tuples


Tuple Unpacking: In an assignment statement, corresponding elements of a tuple will be assigned to respective
objects on the RHS (given that the number of objects is the same as length of the tuple.) This makes is very easy to
swap v ariables.

a, b, c = (1, 2, 3)
a, b = b, a

Tuple Methods: Press <tab>following a dot after the tuple object.Though there aren’t too many Tuple methods, count
is one of the useful ones.

nested_tuple.count()

15
4/4/2019

Data Structures - Lists


A Py thon list is simply an ordered c ollection of v alues or objects. It is similar to what in other languages might be called
an array,but with some added functionality.
Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also
be iterated over in a very simple manner.

Lists are
- one-dimensional
- c ontainers for collections of objects of any type
- variable-length
- mut able,ie, their contents can be modified

Syntax: They can be defined using

- square brackets [] or
- using the list() type function
- Py thon functions that producelists

int_list = [1, 2, 3]
mix_list = ["string", 0.1, True]
list_of_lists = [int_list, mix_list, [‘A’, ‘B’]] x = range(10)

Data Structures - Lists


Single elements can be accessed using their index or position

16
4/4/2019

Data Structures - Lists


1. Adding elements

a. append(), t o add single element s at the end of t he list . For example:


x.append(‘a’)

b. extend(), t o add mult iple element at the end of an exist ing list . For example:
x.extend([10, 11, 12])

[Not e] List s can be combined/concat enat ed using t he + operat or. For example:
[1, 2, 3] + [‘a’, ‘b’, ‘c’]

c. insert(), t o insert element s at a specific index (t his is an expensiv e operat ion) For example:
x.insert(3, ‘a’)

2. Removing elements
a. pop(), remov es and ret urns an element at a part icular index (default : fromt he end) For example:
x.pop(5)

b. remove(), t akes an element as input and remov es it s first occurrence


For example: x.remove(5)
3. Sorting
a. .sort()

Data Structures - Lists


List Functions
x = l i st(‘just a s tring’)

- l en(), returns the number of elements in thelist Forexample:


l en(x)

- i n, checks whether an element belongs toa listand returnsa boolean For example:
‘t’ i n x

- s orted(), returns a new list from the elements of given list,sortedin ascending order Forexample:
s orted(x)

- revers ed(), i terates over the elements of a sequencein reverse order Forexample:
revers ed(x)

List Unpacking works a l ot like tuple unpacking,where you can assign list elements to objects using an assignment
s tatement.
For example: a , b, c, d = [1, 2, 3, 4]

17
4/4/2019

Data Structures - Dictionary


Dictionary (or dict)

dict i s likely the mostimportant built-in Python data structure. Itisa flexibly-sized collection of key-value pairs,where
key a nd value are Pythonobjects.

W e frequently use dictionariesas a simple way to representstructured data.

doc_i nfo = {
"a uthor" : “cha ndra",
"ti tl e" : "Da ta Science i n Python", "chapters": 10,
"tags ": ["#da ta", "#science", "#datascience", "#python", "#a nalysis"]
}

Syntax: dicts a re created using curly braces {}and using colons to separate keys and values.

empty_dict = {}
a _di ct = {‘k1’:12, ‘k2’:36}
b_di ct = {'a ': 'a string', 'b' : [1, 2, 3, 4]}

Data Structures - Dictionary

Subs etting: W e can access or insert the value/object associated with a key by using the curly brackets

b_dict[a] # retrieves ‘a string’


b_dict[‘c’] = 3.142 # adds a new key‐value pair to the dict

[Note] We can check if a dict contains a key usingthe in keyword


‘d’ in b_dict

18
4/4/2019

Data Structures – Dictionary methods


1. Remov ing/Adding elements
a. .keys(), .values(), .items() ‐ w ill returnthe keys,values,pairsof the dict

For example: a_dict.keys()


a_dict.values()
a_dict.items()

b. .del() - Will remov e the key-value pair associated with passedkey For example: del b_dict[‘a’]

c. .pop() ‐ w orks inmuchthe same fashion For example: b_dict.pop(‘c’)

d. .update() ‐ will merge two given dictionaries

For example: b_dict.update(a_dict)

2. Finding elements
a. .get() ‐ behaves gracefully for missing keys. It is used to fetch a value from a dict. If the

key is found, it returns the associated value. It returns a default value if the key isn’t
contained in the dict. This ensures that an exception is not raised.
For example: b_dict.get(‘d’, ‘Nothing found’)

Data Structures – Sets


A set is an unordered collection of unique elements. Theycan be thought of being like
dic ts,but keys only, no values.

Syntax: A set c an be created in two ways:

- v ia the set function,


- using a set literal with curly braces:

set([2, 2, 2, 1, 3, 3]) # produces set([1, 2, 3])


{2, 2, 2, 1, 3, 3} # produces set([1, 2, 3])

S ets support mathematical set operations like union, intersection, difference, and symmetric difference.”

19
4/4/2019

User Defined functions in Python


Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it
more readable, reuse it and save some time. Also functions are a key way to define interfaces so
programmers can share their code.
Python makes use of blocks. Where a block line is more Python code (even another block), and the block
head is of the following format: block_keyword block_name(argument1,argument2, ...) Block keywords you
already know are "if", "for", and "while".
Functions in Python:
 Functions in python are defined using the block keyword "def", followed with the function's name as
the block's name
 Parameters types are not defined. The types are inferred from values passed to the function .
 Python functions overloading is implicit
 Functions may also receive arguments (variables passed from the caller to the function)
 Functions may return a value to the caller, using the keyword- 'return‘. Functions can return multiple
parameter
 If only one or few returned parameters need to be captured and other ignored
 Simply write the function's name followed by (), placing any required arguments within the brackets.

User Defined functions in Python


Python functions can be optional
The default value for the parameters can be defined in function
signatures.

20
4/4/2019

Lambda functions in Python


 Lambda functions in python are key features.
These are functions that can be passed as
parameters to another functions.
 The functions can be anonymous and defined
inline, while passing as a parameter.
 Primarily used to deal with collections, to apply a
function or operations on each individual
elements of python

Classes & Objects


 Objects are an encapsulation of variables and functions into a single entity. Objects get their variables
and functions from classes. Classes are essentially a template to create your objects.

Example:

We have a class defined for Student


with student details like name and
age. Create new student called
student1. Set student1 to be with
name as chandra as age as 33.
Return the results as

“chandra is 33 years old and


participating in python class”

21
4/4/2019

Resources to Learn Python

39

Python Resources
Workshop material
 Presentations
 Sample codes
 case studies
 Projects
https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-
Notebooks
http://stackoverflow.com/tags/python/info
http://learnpythonthehardway.org/book/
"Web Scraping with Python: Collecting Data from the Modern Web",
O'Reilly

22
4/4/2019

Contact us

Vi s it us on: http://www.analytixlabs.in/

For cours e registration, please visit: http://www.analytixlabs.co.in/course-registration/

For more i nformation, please contact us: http://www.analytixlabs.co.in/contact-us/


Or ema il: [email protected]
Ca l l us we would love to speak with you: (+91) 88021-73069

Joi n us on:
Twi tter - http://twitter.com/#!/AnalytixLabs
Fa cebook - http://www.facebook.com/analytixlabs
Li nkedIn - http://www.linkedin.com/in/analytixlabs
Bl og - http://www.analytixlabs.co.in/category/blog/

23

You might also like