# Introduction to Python# Learning Objectives # Learn the basic features of Python Language to get started with
Data Science and Machine Learning# Introduction to Python # Declaring Variables # Conditional Statements #
Generating Sequence Numbers # Control Flow Statements # Working with Collections # Dealing with Strings #
Functional Programming # Modules and Packages
In [1]:
a=3
In [2]:
print(a)
In [3]:
a=5
In [4]:
print(a)
# Means comment in Python# Declaring Variables # int # float # bool # str
In [5]:
# int - Integer Type
var1 = 2
# float - Floarting Point Numbers
var2 = 5.0
# bool - True or False
var3 = True
# str - Textual data
var4 = "Data Science and Machine Learning"
# Use print function to get console output
In [6]:
print("value of var 1 :", var1)
print("value of var 2 :", var2)
print("value of var 3 :", var3)
print("value of var 4 :", var4)
value of var 1 : 2
value of var 2 : 5.0
value of var 3 : True
value of var 4 : Data Science and Machine Learning
In [7]:
# Check the data type of a variable
type(var1)
Out[7]:
int
In [8]:
type(var2)
Out[8]:
float
In [9]:
type(var3)
Out[9]:
bool
In [10]:
type(var4)
Out[10]:
str
In [11]:
print(type(var1))
print(type(var2))
print(type(var3))
print(type(var4))
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
# Conditional Statements # Style - if - elif - else
In [12]:
if var1 >1:
print("Bigger than 1")
Bigger than 1
In [13]:
x=10
y=12
# if x is greater than y
if x > y:
print("x is greater than y")
# if x is lesser than y
elif x < y:
print("x is lesser than y")
# if x is equarl to y
else:
print("x = y")
x is lesser than y
# Ternary operator
In [14]:
# Intialize x
x = 5
In [15]:
# True if x is more than 10 or assign False using ternary operator
isGreater = True if x > 10 else False
print(isGreater)
False
# Generating Sequence Numbers # Three parameters to be specified # Start number # Stop number (last
number is not included) # Step number (optional value, default step is 1)
In [16]:
numbers = range(1,10)
# Use control flow statements to print numbers # For loop
In [17]:
for i in numbers:
print(i)
1
2
3
4
5
6
7
8
9
In [18]:
numbers = range(1,20,2)
# Use control flow statements to print numbers # For loop
In [19]:
for i in numbers:
print(i)
1
3
5
7
9
11
13
15
17
19
# Use While loop to execute a loop untill condition is turned false
In [20]:
#initialize i
i = 1
In [21]:
#checking value of i to control if loop is to be executed or not
while i<5:
print (i)
#increment value of i.
i = i + 1
# print after the value of i
print('Done')
1
2
3
4
Done
# Functions # Functions are created using "def" keyword # Function signature should contain the function name
followed by input parameters and end with : # Function ends with a return statement # Function may also not
end with a return statement
In [22]:
def addElements(a,b):
return a+b
In [23]:
result = addElements(2,3)
print(result)
In [24]:
result = addElements(2.5,3.5)
print(result)
6.0
In [25]:
result = addElements("DataScience","MachineLearning")
print(result)
DataScienceMachineLearning
# Parameters may be initialized in a function
In [26]:
def addElements(a,b=4):
return a+b
In [27]:
print(addElements(2))
# Value of initialized parameter may be overridden
In [28]:
print(addElements(2,5))
# Working with Collections Collections are useful containers or data structures to store and manipulate list of
homogeneous or heterogeneous elements. Popular collections are 1. List 2. Tuple 3. Set 4. Dictionary# List # A
list can contrin heterogeneous items
In [29]:
batsmen = ['Rohit','Dhawan','Kohli','Rahane','Rayadu','Dhoni']
In [30]:
type(batsmen)
Out[30]:
list
In [31]:
batsmen[0]
Out[31]:
'Rohit'
In [32]:
batsmen[0:2]
Out[32]:
['Rohit', 'Dhawan']
In [33]:
batsmen[-1]
Out[33]:
'Dhoni'
In [34]:
batsmen[-2]
Out[34]:
'Rayadu'
In [35]:
#Returns the length of the array
len(batsmen)
Out[35]:
In [36]:
bowlers = ['Bumrah','Shami','Bhuvi','Kuldeep','Chahal']
In [37]:
all_players = batsmen + bowlers
In [38]:
print(all_players)
['Rohit', 'Dhawan', 'Kohli', 'Rahane', 'Rayadu', 'Dhoni', 'Bumrah',
'Shami', 'Bhuvi', 'Kuldeep', 'Chahal']
In [39]:
# Check if a value exists in a list
'Bumrah' in bowlers
Out[39]:
True
In [40]:
# Return the index of a value from a list
all_players.index('Dhoni')
Out[40]:
In [41]:
#Reversing a List
all_players.reverse()
print(all_players)
['Chahal', 'Kuldeep', 'Bhuvi', 'Shami', 'Bumrah', 'Dhoni', 'Rayadu',
'Rahane', 'Kohli', 'Dhawan', 'Rohit']
In [42]:
#Element of a list if replaceable
batsmen[0]='Mayank'
# Tuples Tuple is also a list, but is immutable. Once a tuple is created it cannot be modified. For,example a tuple
containing ODI player and his/her debut year.
In [43]:
odiDebut = ('Kohli', 2008)
In [44]:
type(odiDebut)
Out[44]:
tuple
In [45]:
odiDebut[0]
Out[45]:
'Kohli'
In [46]:
# Try odiDebut[1]=2009, it will not be allowed
In [47]:
# Typecasting a list into a Tuple
players = tuple(all_players)
In [48]:
type(all_players)
Out[48]:
list
In [49]:
type(players)
Out[49]:
tuple
# Set # A set is a collection of unique elements (elements cannot repeat unlike a tuple or a list)
In [50]:
wc2011 = {'Dhoni','Sehwag','Tendulkar','Gambhir','Kohli','Raina','Yuvraj','Yusu
f'}
In [51]:
wc2015 = {'Dhoni','Dhawan','Rohit','Kohli','Vijay','Jadeja','Rahane'}
In [52]:
type(wc2011)
Out[52]:
set
In [53]:
# union of two sets
print(wc2011.union(wc2015))
{'Kohli', 'Yusuf', 'Tendulkar', 'Raina', 'Vijay', 'Rohit', 'Gambhi
r', 'Rahane', 'Yuvraj', 'Dhoni', 'Jadeja', 'Dhawan', 'Sehwag'}
In [54]:
# intersection of two sets
print(wc2011.intersection(wc2015))
{'Kohli', 'Dhoni'}
In [55]:
# difference of two sets
print(wc2011.difference(wc2015))
{'Yusuf', 'Raina', 'Tendulkar', 'Gambhir', 'Yuvraj', 'Sehwag'}
# Dictionary # Dictionary is a list of keys and values. All the keys in a dictionary are unique.
In [56]:
wcWinners = { 1975: "West Indies",
1979: "West Indies",
1983: "India",
1987: "Australia",
1991: "Pakistan",
1996: "Srilanka",
1999: "Australia",
2003: "Australia",
2007: "Australia",
2011: "India"}
In [57]:
wcWinners[1983]
Out[57]:
'India'
In [58]:
wcWinners.values()
Out[58]:
dict_values(['West Indies', 'West Indies', 'India', 'Australia', 'Pa
kistan', 'Srilanka', 'Australia', 'Australia', 'Australia', 'Indi
a'])
In [59]:
set(wcWinners.values())
Out[59]:
{'Australia', 'India', 'Pakistan', 'Srilanka', 'West Indies'}
In [60]:
wcWinners[2015] = 'Australia'
In [61]:
wcWinners
Out[61]:
{1975: 'West Indies',
1979: 'West Indies',
1983: 'India',
1987: 'Australia',
1991: 'Pakistan',
1996: 'Srilanka',
1999: 'Australia',
2003: 'Australia',
2007: 'Australia',
2011: 'India',
2015: 'Australia'}
# Dealing with Strings
In [62]:
# Declaring Strings
string0 = 'python'
string1 = "machine learning"
string2 = """This is a
multiline string"""
In [63]:
print(string0)
print(string1)
print(string2)
python
machine learning
This is a
multiline string
In [64]:
# Strings - change into upper case
string0.upper()
Out[64]:
'PYTHON'
In [65]:
# Strings - change into lower case
string0.lower()
Out[65]:
'python'
In [66]:
# Split Strings
tokens = string1.split(' ')
tokens
Out[66]:
['machine', 'learning']
# Functional Programming # A concept where a function is passed as a paramter to another function# Map
function
In [67]:
# Create function for squaring elements of a list
def square_me(x):
return x*x
In [68]:
intList = [1,2,3,4,5,6,7,8,9]
In [69]:
# Pass square_me function as a parameter to map function
squareList = map(square_me,intList)
In [70]:
type(squareList)
Out[70]:
map
In [71]:
list(squareList)
Out[71]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
In [72]:
# Normal way
squareList = []
for x in intList:
squareList.append(pow(x,2))
squareList
Out[72]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
# Anonymous function # Anonymous function is a function without a name and is defined using lambda keyword
In [73]:
squareList = map(lambda x: x*x, intList)
In [74]:
list(squareList)
Out[74]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
# Filter Function
In [75]:
evenInts = filter(lambda x: x%2 == 0, intList)
In [76]:
type(evenInts)
Out[76]:
filter
In [77]:
list(evenInts)
Out[77]:
[2, 4, 6, 8]
# Modules and Packages # A Module is a file that consists of functions, classes and variables. A set of modules
become a package. A module can be imported into another module (code).
In [78]:
#sqrt(16)
#will not work as module containing sqrt is not imported.
In [79]:
import math
math.sqrt(16)
Out[79]:
4.0
In [80]:
from random import sample
In [81]:
sample(range(0,11),3)
Out[81]:
[5, 3, 4]
In [82]:
randomList = sample(range(0,100),20)
randomList
Out[82]:
[86, 18, 89, 56, 53, 35, 28, 26, 98, 7, 54, 23, 94, 81, 8, 92, 36,
9, 49, 31]
In [83]:
from statistics import mean, median
In [84]:
def getMeanMedian(listNum):
return mean(listNum), median(listNum)
In [85]:
mean, median = getMeanMedian(randomList)
In [86]:
print("Mean:", mean, "Median:", median)
Mean: 48.65 Median: 42.5
In [87]:
# Difference between array and list
In [88]:
# array stores only elements of similar type
# list stores elements of various types
In [89]:
# create list
list_1 = [1,2,3,4]
list_2 = [1,2,3,4,'a','b','c']
print(type(list_1))
print(type(list_2))
print("")
print("_____________________")
print("")
print(list_1)
print(list_2)
<class 'list'>
<class 'list'>
_____________________
[1, 2, 3, 4]
[1, 2, 3, 4, 'a', 'b', 'c']
In [90]:
# create array
from numpy import array
array_1 = array([1,2,3,4])
array_2 = array([1,2,3,4,'a','b','c'])
print(type(array_1))
print(type(array_2))
print("")
print("_____________________")
print("")
print(array_1)
print(array_2)
# What difference do you observe in list and array after printing ?
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
_____________________
[1 2 3 4]
['1' '2' '3' '4' 'a' 'b' 'c']