Python With Data Science
Python With Data Science
Assistant Professor
DATA SCIENCE MES-AIMAT Marampilly
BRIEF HISTORY OF
PYTHON
Invented in the Netherlands, early 90s by Guido van Rossum
Increasingly popular
INTRODUCTION
Python is a
high-level
Interpreted
interactive
Easy-to-learn
Easy-to-read
Interactive Mode
Portable
Databases
APPLICATIONS OF PYTHON
Web Applications
• The variable name is case sensitive: ‘val’ is not the same as ‘Val’
>>> "GATTACA".count("T")
>>> "GATTACA".lower()
'gattaca'
>>> "gattaca".upper()
'GATTACA'
'UATTACA‘
'GATTAUA'
'G**TACA'
>>> "GATTACA".startswith("G")
True
>>> "GATTACA".startswith("g")
False
STRINGS ARE IMMUTABLE
>>> s = "GATTACA"
>>> s[3] = "C"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> s = s[:3] + "C" + s[4:]
>>> s
'GATCACA'
>>> s = s.replace("G","U")
>>> s
'UATCACA'
STRINGS ARE IMMUTABLE
• String methods do not modify the string; they return a new string.
Methods:
S.upper()
S.lower()
S.count(substring)
S.replace(old,new)
S.find(substring)
S.startswith(substring), S. endswith(substring)
Printing:
strng="count # of u"
index=0
count=0
while index < len(strng):
if strng[index] == "u“ or strng[index] == “U":
count+=1
index+=1
Try it out
How would we traverse backwards?
STRING OPERATIONS: FIND
find() searches for a string within a string
To use it, insert this at the top of your code:
import string
Sequence Types
List
Tuple
Mapping Types
Dictionary
LIST
List - list of comma-separated values (items) between square brackets
Lists might contain items of different types, but usually the items all have the same type.
All slice operations return a new list containing the requested elements. This
means that the following slice returns a new (shallow) copy of the list:
Concatenation
Add new items at the end of the list, by using the append() method
Assignment to slices is possible
clear the list by replacing all the elements with an empty list
Length of string
To write a tuple containing a single value you have to include a comma, even though
there is only one value .
ACCESSING VALUES IN TUPLES
To access values in tuple, use the square brackets for slicing along with the
index or indices to obtain value available at that index.
UPDATING TUPLES
Tuples are immutable which means you cannot update or change the values
of tuple elements.
print tup3
DELETE TUPLE ELEMENTS
Undesired elements that are discarded can be put together into another tuple.
>>> d
{1: 'hello', 'two': 42, 'blah': [1, 2, 3]}
>>> d['two'] = 99
>>> d
{1: 'hello', 'two': 99, 'blah': [1, 2, 3]}
>>> d[7] = 'new entry'
>>> d
{1: 'hello', 'two': 99, 'blah': [1, 2, 3], 7: 'new
entry'}
USER-DEFINED INDEXES AS MOTIVATION FOR
DICTIONARIES
>>> employee = {
'864-20-9753': ['Anna', 'Karenina'],
'987-65-4321': ['Yu', 'Tsun'],
'100-01-0010': ['Hans', 'Castorp']}
>>> employee['987-65-4321']
['Yu', 'Tsun']
>>> employee['864-20-9753']
['Anna', 'Karenina']
Write a function birthState() that takes as input the full name of a recent
U.S. president (as a string) and returns his birth state. You should use this
dictionary to store the birth state for each recent president:
>>>h.has_key(‘nyckel’)
True
>>> h.values() # all values in a list; unordered
['word', 12]
>>> phonebook1
{'123-45-67', '234-56-78', '345-67-89'}
>>> type(phonebook1)
<class 'set'>
>>> phonebook1
{'123-45-67', '234-56-78', '345-67-89'}
SET
Create two sets s1 and s2 consisting of elements s1= rose, jasmine, lily, lotus and s2= rose, daisy, lily, iris,
poppy and perform all the set operations.
i) Intersection
ii) Union
iii) Difference
s1=set(['rose','jasmine','lily','lotus'])
s2=set(['rose','daisy','lily','iris','poppy'])
Suppose we have a list with duplicates, such as this list of ages of students in a class:
>>> ages = [23, 19, 18, 21, 18, 20, 21, 23, 22, 23, 19, 20]
To remove duplicates from this list, we can convert the list to a set, using the set
constructor. The set constructor will eliminate all duplicates because a set is not supposed
to have them.
>>> ages
[18, 19, 20, 21, 22, 23]
EMPTY SETS
>>> phonebook2 = {}
>>> type(phonebook2)
<class 'dict'>
The problem here is that curly braces ({}) are used to define dictionaries as well, and {} represents
an empty dictionary.
>>> phonebook2
set()
>>> type(phonebook2)
<class 'set'>
SET OPERATORS
>>> phonebook1
True
False
True
>>> len(phonebook1)
3
SET METHODS
add() is used to add an item to a set:
>>> phonebook3.add('123-45-67')
>>> phonebook3
>>> phonebook3.remove('123-45-67')
>>> phonebook3
{'345-67-89', '456-78-90'}
>>> phonebook3.clear()
>>> phonebook3
set()
Implement function sync() that takes a list of phone books (where each
phone book is a set of phone numbers) as input and returns a phone
book (as a set) containing the union of all the phone books.
>>> sync(phonebooks)
{'234-56-78', '456-78-90', '123-45-67', '345-67-89'}
Python operators
Types of Operator
Python language supports the following
types of operators.
•Arithmetic Operators
•Comparison (Relational) Operators
•Assignment Operators
•Logical Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
ARITHMETIC OPERATORS
A=10,b=20
COMPARISON(RELATIONAL) OPERATOR
ASSIGNMENT OPERATORS
BITWISE OPERATOR
var1 = 100
if var1==100:
print var1
if expression:
statement(s)
else:
statement(s)
if var1<150:
print var1
else:
print var2
DECISION MAKING[ CONTINUED ..]
for
Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a comma.
When the function is called, we pass along a first name, which is used inside the function to print
the full name:
DEFAULT PARAMETER VALUE