Basic Python - 1650699594076
Basic Python - 1650699594076
Basic Python - 1650699594076
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
1
4/4/2019
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
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.
3
4/4/2019
4
4/4/2019
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
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
“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
6
4/4/2019
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
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 *
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
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.
8
4/4/2019
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.
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
9
4/4/2019
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.)
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:
10
4/4/2019
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
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
- 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
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
x = 0
while x < 10:
print x, "is less than 10" x += 1
13
4/4/2019
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.
tup = 1, 2, 6
nested_tup = (1, 3, 5), (2, 8), [‘a’, ‘b’]
14
4/4/2019
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’)
(4,None,'foo')+(6,0)+('bar',)
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
Lists are
- one-dimensional
- c ontainers for collections of objects of any type
- variable-length
- mut able,ie, their contents can be modified
- 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)
16
4/4/2019
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)
- 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
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.
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]}
Subs etting: W e can access or insert the value/object associated with a key by using the curly brackets
18
4/4/2019
b. .del() - Will remov e the key-value pair associated with passedkey For example: del b_dict[‘a’]
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’)
S ets support mathematical set operations like union, intersection, difference, and symmetric difference.”
19
4/4/2019
20
4/4/2019
Example:
21
4/4/2019
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/
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