Crash Course in Python

  • This tutorial is a crash course for developers or data scientists, who wish to learn how to write spark or sklearn programs in python. The tutorial is designed keeping in mind to introduce only those basic features of python, that would be required mostly to get started with spark or sklearn programs.
  • What you will learn here
    • Declaring variables
    • Arithmatic and logical operations
    • Using built-in functions
    • Conditional Statements
    • Control flow statements
    • Working with Collections - List, Tuple, Set & Dictionary
    • Dealing with String
    • Functions
    • Lambda functions
    • Classes

Declaring Variables

  • Variable are dynamically typed, so no need to mention the variable types. Python interpreter can automatically infer the type when the variables are initialized.
In [1]:
var1 = 2
var2 = 5.0
In [2]:
var1
Out[2]:
2
In [3]:
type( var1 )
Out[3]:
int
In [4]:
type( var2 )
Out[4]:
float

Printing Variable Values

In [5]:
print( var1 )
2
In [6]:
mystring = 'This is python'
print( mystring )
This is python
In [7]:
print( var1, var2, mystring )
2 5.0 This is python

Operations on variables .. Arithmatic or logical

In [8]:
var1 + var2
Out[8]:
7.0
In [9]:
var1 * var2
Out[9]:
10.0
In [10]:
var1 == 2
Out[10]:
True
In [11]:
var1 == var2
Out[11]:
False

Built-in functions

In [12]:
round( 1.234 )
Out[12]:
1

Round upto a number of decimal values

In [13]:
round( 1.234, 2 )
Out[13]:
1.23

To invoke some functions tha packaged need to be imported. For example import a math function

In [14]:
import math
In [15]:
math.ceil( 1.2 )
Out[15]:
2
In [16]:
math.floor( 1.2 )
Out[16]:
1
In [17]:
abs( -1.2 )
Out[17]:
1.2
In [18]:
# Get the variable type
type( var1 )
Out[18]:
int
In [19]:
pow( var1 , 2 )
Out[19]:
4

Generate a sequence number

In [20]:
numbers = range( 1, 10 )
In [21]:
numbers
Out[21]:
range(1, 10)
In [22]:
type( numbers )
Out[22]:
range
In [23]:
for i in numbers:
   print( i )
1
2
3
4
5
6
7
8
9
In [24]:
len( numbers )
Out[24]:
9
In [25]:
for i in numbers:
   print(i , end = " ")
1 2 3 4 5 6 7 8 9 

Conditional Statements

In [26]:
if var1 > 1:
  print( "Bigger" )
Bigger
In [27]:
if var1 > 5:
  print( "Bigger" )
else:
  print( "Smaller" )
Smaller
In [28]:
x = 10
y = 12
if x > y:
  print ("x>y")
elif x < y:
  print ("x<y")
else:
  print ("x=y")
x<y

Inline conditional statements

In [29]:
a = 0 if x > 10 else 1
In [30]:
a
Out[30]:
1

Control Flow Statements

In [31]:
for i in range(5):
  print (i)
0
1
2
3
4
In [32]:
i = 1
while i < 5:
  print(i)
  i = i+1
print('Bye')
1
2
3
4
Bye
In [33]:
i = 1
while i < 5:
  print(i)
  i = i+1
  if i == 4:
      break
print('Bye')
1
2
3
Bye
In [34]:
i = 1
while i < 5:
  i = i+1
  if i == 3:
      continue
  print(i)
print('Bye')
2
4
5
Bye

Working with Collections

List - Collection of elements

  • List can contain heterogeneous types of elements
  • Element values can repeat
In [35]:
## Create an empty list
a = []
In [36]:
fruits = ['apple', 'orange', 'banana', 'papaya']
In [37]:
fruits[0]
Out[37]:
'apple'
In [38]:
## Slicing an list
fruits[1:3]
Out[38]:
['orange', 'banana']
In [39]:
## Accessing the last element
fruits[-1]
Out[39]:
'papaya'
In [40]:
# how many elements in the list
len( fruits )
Out[40]:
4
In [41]:
seasonal_fruits = ['mango', 'cherry', 'watermelon']
In [42]:
all_fruits = fruits + seasonal_fruits
In [43]:
all_fruits
Out[43]:
['apple', 'orange', 'banana', 'papaya', 'mango', 'cherry', 'watermelon']
In [44]:
'banana' in all_fruits
Out[44]:
True
In [45]:
'grapes' in fruits
Out[45]:
False
In [46]:
all_fruits.index( 'banana' )
Out[46]:
2
In [47]:
all_fruits.append( 'grapes' )
In [48]:
all_fruits
Out[48]:
['apple',
'orange',
'banana',
'papaya',
'mango',
'cherry',
'watermelon',
'grapes']
In [49]:
a = [1,1,2,4,5,6,7]
In [50]:
a
Out[50]:
[1, 1, 2, 4, 5, 6, 7]
In [51]:
min( a )
Out[51]:
1
In [52]:
max( a )
Out[52]:
7
In [53]:
## How many times an element exists in a list
a.count( 1 )
Out[53]:
2
In [54]:
a.insert( 3, 3 )
In [55]:
a
Out[55]:
[1, 1, 2, 3, 4, 5, 6, 7]
In [56]:
a.reverse()
In [57]:
a
Out[57]:
[7, 6, 5, 4, 3, 2, 1, 1]
In [58]:
a.sort()
In [59]:
a
Out[59]:
[1, 1, 2, 3, 4, 5, 6, 7]

Tuples - Immutable List

  • Tuple is also a list but once created it can not be modified.
In [60]:
tup1 = ( 1, 3, 'orange' )
In [61]:
tup1
Out[61]:
(1, 3, 'orange')

It is not allowed to change the tuple elements..

In [62]:
tup1[1] = 'a'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-3939eb89a174> in <module>()
----> 1 tup1[1] = 'a'

TypeError: 'tuple' object does not support item assignment
In [63]:
tupa = tuple( a )
In [64]:
tupa
Out[64]:
(1, 1, 2, 3, 4, 5, 6, 7)

Set - Order list of non-repeating items

  • Items can not repeat in set
  • Elements are ordered
In [65]:
b = set( [6,1,1,2,4,5] )
In [66]:
b
Out[66]:
{1, 2, 4, 5, 6}
In [67]:
b.add( 3 )
In [68]:
b
Out[68]:
{1, 2, 3, 4, 5, 6}
In [69]:
c = set( [2,4,6,7] )
In [70]:
c.union( b )
Out[70]:
{1, 2, 3, 4, 5, 6, 7}
In [71]:
b.intersection( c )
Out[71]:
{2, 4, 6}
In [72]:
c.difference( b )
Out[72]:
{7}
In [73]:
b.remove
Out[73]:
<function set.remove>
In [74]:
b
Out[74]:
{1, 2, 3, 4, 5, 6}
In [75]:
b.clear()
In [76]:
b
Out[76]:
set()

Iterating through the elements in list or set

In [77]:
for i in a:
  print( i * 2 )
2
2
4
6
8
10
12
14
In [78]:
for i in b:
  print( i )

Using a Dictionary

In [79]:
d0 = {}
d1 = dict( { 'One': 1, 'Two':2 } )
d1
Out[79]:
{'One': 1, 'Two': 2}
In [80]:
d0['One'] = 1
d0['OneTwo'] = 12
print( d0 )
{'One': 1, 'OneTwo': 12}
In [81]:
d0['One']
Out[81]:
1
In [82]:
# Join two lists and create an dictionary... 
names = ['One', 'Two', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]
In [83]:
d2 = dict( zip(names,numbers) )
In [84]:
print( d2 )
{'Three': 3, 'One': 1, 'Two': 2, 'Five': 5, 'Four': 4}
In [85]:
d2.keys()
Out[85]:
dict_keys(['Three', 'One', 'Two', 'Five', 'Four'])
In [86]:
d2.values()
Out[86]:
dict_values([3, 1, 2, 5, 4])
In [87]:
d2['six'] = 6
In [88]:
d2
Out[88]:
{'Five': 5, 'Four': 4, 'One': 1, 'Three': 3, 'Two': 2, 'six': 6}
In [89]:
# Remove an element and return it
d2.pop( 'six' )
Out[89]:
6
In [90]:
d2
Out[90]:
{'Five': 5, 'Four': 4, 'One': 1, 'Three': 3, 'Two': 2}

Dealing with Strings

In [91]:
string0 = 'python'
string1 = "Data Science"
string2 = '''This is Data science 
        workshop
        using Python'''
In [92]:
print( string0, string1, string2)
python Data Science This is Data science
      workshop
      using Python
In [93]:
string2.find( "Python" )
Out[93]:
53
In [94]:
string0.capitalize()
Out[94]:
'Python'
In [95]:
string0.upper()
Out[95]:
'PYTHON'
In [96]:
len( string2 )
Out[96]:
59
In [97]:
string2.split()
Out[97]:
['This', 'is', 'Data', 'science', 'workshop', 'using', 'Python']
In [98]:
string2.replace( 'Python', 'R')
Out[98]:
'This is Data science \n        workshop\n        using R'

Functions in Python

  • Parameters types are not defined. The types are inferred from values passed to the function.
  • Python functions overloading is implicit
In [99]:
def addElements( a, b ):
  return a + b
In [100]:
addElements( 2, 3 )
Out[100]:
5
In [101]:
addElements( 2.3, 4.5 )
Out[101]:
6.8
In [102]:
addElements( "python", "workshop" )
Out[102]:
'pythonworkshop'

Functions can return multiple parameters

In [103]:
def addElements( a, b ):
  return a, b, a + b
In [104]:
x, y, z = addElements( 2, 3 )
In [105]:
addElements( 2.3, 4.5 )
Out[105]:
(2.3, 4.5, 6.8)

If only one or few returned parameters need to be captured and other igonred.

In [106]:
_, _, z = addElements( 4, 5 )
In [107]:
x
Out[107]:
2

Python functions can be optional.

  • The default value for the parameters can be defined in function signatures.
In [108]:
def addElements( a, b = 4 ):
  return a + b
In [109]:
addElements( 2 )
Out[109]:
6
In [110]:
addElements( 2, 5 )
Out[110]:
7
In [111]:
def add_n(*args):
  sum = 0
  for arg in args:
      sum = sum + arg
  return sum
In [112]:
add_n( 1, 2, 3 )
Out[112]:
6
In [113]:
add_n( 1, 2, 3, 4, 5, 6 )
Out[113]:
21
In [114]:
add_n()
Out[114]:
0

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.
  • Primariliy used to deal with collections, to apply a function or operations on each individual elements of python
In [115]:
a = lambda x: x * x
In [116]:
a( 2 )
Out[116]:
4
In [117]:
a( 2 )  *  a( 2 )
Out[117]:
16
In [118]:
mylist = [1,2,3,4,5,6,7,8,9]
In [119]:
xsquare = []

for x in mylist:
  xsquare.append( pow( x, 2 ) )

print( xsquare )
[1, 4, 9, 16, 25, 36, 49, 64, 81]
In [120]:
map( lambda x: pow( x, 2 ), mylist)
Out[120]:
<map at 0x45c9fd0>
In [121]:
xsquare1 = list( map( lambda x: pow( x, 2 ), mylist) )
In [122]:
print( xsquare1 )
[1, 4, 9, 16, 25, 36, 49, 64, 81]
In [123]:
mylist1 = [1,2,3,4,5,6,7,8,9]
In [124]:
listprods = list( map( lambda x, y: x * y, mylist, mylist1 ) )
In [125]:
listprods
Out[125]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
In [126]:
list( filter( lambda x : x < 5, listprods ) )
Out[126]:
[1, 4]

Classes and Objects

In [127]:
class Student:
  workshop = 'python'
  def __init__(self,name,age):
      self.name = name
      self.age = age
  def describe( self ):
      print( self.name, " is ",
            self.age,
            " years old and participating in ",
            Student.workshop,
            " class ")
      return
In [128]:
student1 = Student( "manaranjan", 39 )
In [129]:
student1.name
Out[129]:
'manaranjan'
In [130]:
student1.describe()
manaranjan  is  39  years old and participating in  python  class
In [131]:
Student.workshop = "python"
In [132]:
student1.workshop
Out[132]:
'python'