F-IoT Unit-3
F-IoT Unit-3
Presented By:
G. Kiran Kumari
Assistant Professor
Department of ECE
Anurag College of Engineering
Syllabus
UNIT – III
4
Anurag College of Engineering
Multi-paradigm programming language:
• Python supports more than one programming
paradigm including object-oriented programming and
structured programming.
Interpreted language:
• Python is an interpreted language and does not require an
explicit compilation step.
• The Python interpreter executes the program source code
directly, statement by statement, as a processor or
scripting engine does.
Interactive language:
• Python provides an interactive mode in which the user
can submit commands at the Python prompt and interact
with the interpreter directly.
5
Anurag College of Engineering
Benefits of Python
The key benefits of Python are:
• Easy-to-learn, read and maintain
• Object and Procedure Oriented
• Extendable
• Scalable
• Portable
• Broad library support
7
Anurag College of Engineering
Object and procedure oriented :
• Python supports both procedure-oriented programming
and object-oriented programming.
• Procedure oriented paradigm allows programs to be
written around procedures or functions that allow reuse
of code .
• Object oriented paradigm allows programs to be written
around objects that include both data and functionality.
8
Anurag College of Engineering
Extendable:
• Python is an extendable language and allows integration
of low-level modules written in languages such as
C/C++. This is useful to speedup a critical portion of a
program.
Scalable:
• Due to the minimalistic nature of Python, it provides a
manageable structure for large programs.
9
Anurag College of Engineering
Portable:
• Python programs can be directly executed from source
code and copied from one machine to other without
worrying about portability.
• Since Python is an interpreted language , programmers
do not have to worry about compilation , linking and
loading of programs.
• The Python interpreter converts the source code to an
immediate form called byte codes and then translates this
into the native language of the system and then runs it.
10
Anurag College of Engineering
Broad Library Support:
11
Anurag College of Engineering
Python Data Types and Data Structures
Numbers
12
Anurag College of Engineering
Working with Numbers in Python
Integer:
>>>a=5
>>>type(a)
<type ‘int’>
Floating Point:
>>>b=2.5
>>>type(b)
<type ‘float’>
13
Anurag College of Engineering
Long:
>>>x=9898878787676L
>>>type(x)
<type ‘long’>
Complex:
>>>y=2+5j
>>>y
(2+5j)
>>>type(y)
<type ‘complex’>
>>>y.real
2
>>>y.imag
5
14
Anurag College of Engineering
Addition:
>>>c=a+b
>>>c
7.5
>>>type(c)
<type ‘float’>
Subtraction:
>>>d=a-b
>>>d
2.5
>>>type(d)
<type ‘float’>
15
Anurag College of Engineering
Multiplication:
>>>e=a*b
>>>e
12.5
>>>type(e)
<type ‘float’>
Division:
>>>f=b/a
>>>f
0.5
>>>type(f)
<type ‘float’>
16
Anurag College of Engineering
Power:
>>>g=a**2
>>>g
25
17
Anurag College of Engineering
Strings
18
Anurag College of Engineering
Working with Strings in Python
Create string:
>>> s=“Hello World!”
>>>type(s)
<type ‘str’>
String concatenation:
>>> s=“Hello World!”
>>>type(s)
<type ‘str’>
>>>t= “ This is a sample program.”
>>>type(t)
<type ‘str’>
>>>r= s+t
>>>r
Hello World! This is a sample program. 19
Anurag College of Engineering
Get Length of String:
>>> s=“Hello World!”
>>>len (s)
12
Formatting output:
>>>print “The string (%s) has %d characters” % (s, len(s))
The string (Hello World!) has 12 characters
22
Anurag College of Engineering
Strip:
Strip returns a copy of the string with the leading and
trailing characters removed.
s=“Hello World!”
>>>s.strip (“!”)
‘Hello World’
Accessing sub-strings:
s=“Hello World!”
>>>s[0]
‘H’
>>>s[6:]
‘World!’
>>>s[6: -1]
‘World’
23
Anurag College of Engineering
Lists
24
Anurag College of Engineering
Working with Lists in Python
>>>fruits=[‘apple’ , ‘orange’ , ‘banana’ , ‘mango’]
>>>type(fruits)
<type ‘list’>
>>>len (fruits)
4
>>>fruits[1]
‘orange’
>>>fruits[1:3]
[‘orange’ , ‘banana’]
>>>fruits[1:]
[‘orange’ , ‘banana’ , ‘mango’]
25
Anurag College of Engineering
Appending an item to a list:
>>>fruits . append (‘pear’)
>>>fruits
[‘apple’ , ‘orange’ , ‘banana’ , ‘mango’ , ‘pear’]
Removing an item from a list:
>>>fruits . remove (‘mango’)
>>>fruits
[‘apple’ , ‘orange’ , ‘banana’ , ‘pear’]
Inserting an item to a list:
>>>fruits . insert (1,‘mango’)
>>>fruits
[‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’]
26
Anurag College of Engineering
Combining lists:
>>>fruits=[‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’]
>>>fruits
‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’
>>>vegetables=[‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ ,
‘radish’]
>>>vegetables
‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ , ‘radish’
>>>eatables=fruits+vegetables
>>>eatables
[‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’ ,
‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ , ‘radish’]
27
Anurag College of Engineering
Lists can be nested:
>>>fruits=[‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’]
>>>fruits
‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’
>>>vegetables=[‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ ,
‘radish’]
>>>vegetables
‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ , ‘radish’
>>>nested=[fruits,vegetables]
>>>nested
[[‘apple’ , ‘mango’ , ‘orange’ , ‘banana’ , ‘pear’ ],
[‘potato’ , ‘carrot’ , ‘onion’ , ‘beans’ , ‘radish’]]
28
Anurag College of Engineering
Tuples
29
Anurag College of Engineering
Working with Tuples in Python
>>>fruits=[“apple” , “mango” , “banana” , “pineapple”]
>>>fruits
(‘apple’ , ‘mango’ , ‘banana’ , ‘pineapple’)
>>>type(fruits)
<type ‘tuple’>
30
Anurag College of Engineering
Get an element from a tuple:
>>>fruits=[“apple” , “mango” , “banana” , “pineapple”]
>>>fruits[0]
‘apple’
>>>fruits[:2]
[‘apple’ , ‘mango’]
31
Anurag College of Engineering
Combining tuples:
>>>fruits =[“apple” , “mango” , “banana” , “pineapple”]
>>>fruits
(‘apple’ , ‘mango’ , ‘banana’ , ‘pineapple’)
>>>vegetables=[“potato’”, “carrot”, “onion’”, “radish”]
>>>vegetables
(‘potato’ , ‘carrot’ , ‘onion’ , ‘radish’)
>>>eatables=fruits+vegetables
>>>eatables
(‘apple’ , ‘mango’ , ‘banana’ , ‘pineapple’ ,
‘potato’ , ‘carrot’ , ‘onion’ , ‘radish’)
32
Anurag College of Engineering
Dictionaries
33
Anurag College of Engineering
Working with Dictionaries in Python
>>>student={‘name’: ‘Mary’ , ‘id’ : ‘8776’, ‘major’: ‘cs’}
>>>student
{‘major’: ‘cs’ , ‘name’: ‘Mary’ , ‘id’ : ‘8776’ }
>>>type(student)
<type ‘dict’>
More
40
Anurag College of Engineering
if statement
>>>a = 25**5
>>>if a>10000:
if a<1000000:
print “Between 10k and 100k”
else:
print “More than 100k”
elif a==10000:
print “Equal to 10k”
else:
print “Less than 10k”
42
Anurag College of Engineering
Looping over items in a list:
fruits=[‘apple’ , ‘orange’ , ‘banana’ , ‘mango’]
i=0
for item in fruits:
print “Fruit-%d: %s” % {i, item}
i=i+1
Looping over keys in a dictionary:
student= ‘name’: ‘Mary’ , ‘id’ : ‘8776’, ‘major’: ‘cs’
for key in student:
print “%s: %s % (key, student[key])
43
Anurag College of Engineering
while statement
• The while statement in Python executes the statements
within the while loop as long as the while condition is
true.
4
32
384
46
Anurag College of Engineering
break/continue statement
Continue statement:
>>>fruits=[‘apple’ , ‘orange’ , ‘banana’ , ‘mango’]
>>>for item in fruits:
if item == “banana”:
continue
else:
print item
apple
orange
mango
47
Anurag College of Engineering
pass statement
• The pass statement in Python is a null operation.
• The pass statement is used when a statement is required
syntactically but do not want any command or code to
execute.
>>>fruits=[‘apple’ , ‘orange’ , ‘banana’ , ‘mango’]
>>>for item in fruits:
if item == “banana”:
pass
else:
print item
apple
orange
48
Anurag College of Engineering
Functions
• A function is a block of code that takes information in (in
the form of parameters), does some computation and
returns a new piece of information based on parameter
information.
• A function in Python is a block of code that begins with the
keyword def followed by the function name and
parentheses.
• The function parameters are enclosed within the
parenthesis.
• The code block within a function begins after a colon that
comes after the parenthesis enclosing the parameters.
• The first statement of the function body can optionally be a
documentation string or docstring.
49
Anurag College of Engineering
Example- Function that computes the average grade
given a dictionary containing student records:
Students = { ‘1’ : {‘name’ : ‘Bob’ , ‘grade’ : 2.5},
‘2’ : {‘name’ : ‘Mary’ , ‘grade’ : 3.5},
‘3’ : {‘name’ : ‘David’ , ‘grade’ : 4.2},
‘4’ : {‘name’ : ‘John’ , ‘grade’ : 4.1},
‘5’ : {‘name’ : ‘Alex’ , ‘grade’ : 3.8}}
def averageGrade(students) :
“ This function computes the average grade”
sum ~ 0.0
for key in students:
sum = sum + students[key] [‘grade’]
average = sum/len(students)
return average
avg ~ averageGrade(students)
Print “The average grade is: %0.2f” % (avg) 50
Anurag College of Engineering
• Functions can have default values of the parameters.
• If a function with default values is called with fewer parameters
or without any parameter, the default values of the parameters are
used.
Example- Function with default arguments
>>>def displayFruits{fruits=[‘apple’ , ‘orange’]}:
print “There are %d fruits in the list” % {len{fruits}}
for item in fruits:
print item
Using default arguments
>>>displayFruits{}
apple
orange
>>>fruits=[‘banana’ , ‘pear’ , ‘mango’]
>>>displayFruits{fruits}
banana
pear
mango
51
Anurag College of Engineering
• All parameters in the Python functions are passed by
reference.
• If a parameter is changed within a function the change also reflected back
in the calling function.
Example- Passing by reference
>>> displayFruits{fruits}:
print “There are %d fruits in the list” % {len{fruits}}
for item in fruits:
print item
print “ Adding one more fruit”
fruits.append{‘mango’}
55
Anurag College of Engineering
Example- Importing the student that contains two
functions and using it
def averageGrade(students) :
sum = 0.0
for key in students:
sum = sum + students[key] [‘grade’]
average = sum/len(students)
return average
def printRecords(students):
print “There are %d students” %(len(students))
i=1
for key in students:
print “Student-%d: “ % (i)
print “Name: “ + students[key][‘name’]
print “Grade: “ + str{students[key][‘grade’]}
56
Anurag College of Engineering
Example- Using student module
57
Anurag College of Engineering
Example- Importing a specific function from a
module
58
Anurag College of Engineering
Example- Listing all names defined in a
module
59
Anurag College of Engineering
Packages
60
Anurag College of Engineering
skimage package listing:
61
Anurag College of Engineering
• Fig. shows the listing of the skimage package that
provides image processing algorithms.
• The package is organized into a root directory (skimage)
with sub-directories (color, draw, etc) which are sub-
packages within the skimage package.
• Each directory contains a special file named _init_.py
which tells Python to treat directories as packages.
• This file can either be an empty file or contain some
initialization code for the package.
62
Anurag College of Engineering
File Handling
• Python allows reading and writing to files using the file
object.
• The open(filename, mode) function is used to get a file
object.
• The mode can be read (r), write (w), append (a), read and
write (r+ or w+), read-binary (rb), write-binary (wb), etc.
63
Anurag College of Engineering
Reading an entire file:
Fig. shows reading an entire file with read function.
After the file contents have been read the close
function is called which closes the file object.
64
Anurag College of Engineering
Reading line by line from a file using the readline
function:
65
Anurag College of Engineering
Reading lines in a loop using the readlines
function:
66
Anurag College of Engineering
Reading a certain number of bytes from a file
using the read(size) function:
67
Anurag College of Engineering
Getting the current position of read using the
tell function:
68
Anurag College of Engineering
Seeking to a certain position using seek function :
69
Anurag College of Engineering
Writing a file using the write function :
70
Anurag College of Engineering
Date/Time Operations
71
Anurag College of Engineering
Example-Manipulating with date:
72
Anurag College of Engineering
Example-Manipulating with time:
73
Anurag College of Engineering
Classes
• Python is an Object-Oriented Programming
(OOP) language.
• Python provides all the standard features of
Object-Oriented Programming such as classes,
class variables, class methods, inheritance,
function loading and operator overloading.
74
Anurag College of Engineering
Class:
A class is simply a representation of a type of object and
user-defined prototype for an object that is composed of
three things: a name, attributes and operations/ methods.
Instance/Object:
Object is an instance of the data structure defined by a
class.
Inheritance:
Inheritance is the process of forming a new class from an
existing class or base class.
Function overloading:
Function overloading is a form of polymorphism that
allows a function to have different meanings, depending
on its context.
75
Anurag College of Engineering
Operator overloading:
Operator overloading is a form of polymorphism that
allows assignment of more than one function to a
particular operator.
Function overriding :
Function overriding allows a child class to provide a
specific implementation of a function that is already
provided by the base class. Child class implementation of
the overridden function has the same name, parameters
and return type as the function in the base class.
76
Anurag College of Engineering
Python Packages for IoT
77
Anurag College of Engineering
JavaScript Object Notation (JSON)
78
Anurag College of Engineering
Extensible Markup Language (XML)
79
Anurag College of Engineering
HTTPLib and URLLib
80
Anurag College of Engineering
SMTPLib
• Simple Mail Transfer Protocol (SMTP) is a protocol
which handles sending email and routing e-mail between
mail servers.
• The Python smtplib module provides an SMTP client
session object that can be used to send email.
81
Anurag College of Engineering
Introduction to Raspberry Pi
82
Anurag College of Engineering
Raspberry Pi Board:
83
Anurag College of Engineering
Raspberry Pi board with various
components/peripherals labeled:
84
Anurag College of Engineering
Raspberry Pi board with various
components/peripherals labeled:
85
Anurag College of Engineering
About the Raspberry Pi Board
Processor and RAM:
• Raspberry Pi is based on an ARM processor.
• The Raspberry Pi (Model B, Revision 2) comes with
700MHz Low Power ARM11 76JZ-F processor and 512
MB SDRAM.
USB Ports:
• Raspberry Pi comes with two USB 2.0 ports.
• The USB ports on Raspberry Pi can provide a current
upto 100mA.
• For connecting devices that draw current more than
100mA, an external USB powered hub is required.
86
Anurag College of Engineering
About the Raspberry Pi Board
Ethernet Ports:
• Raspberry Pi comes with standard RJ45 Ethernet port.
• An Ethernet cable or a USB Wi-Fi adapter can be
connected to provide Internet connectivity.
HDMI Output:
• High-Definition Multimedia Interface (HDMI) port on
Raspberry Pi provides both video and audio output.
• Raspberry Pi can connected to a monitor using HDMI
cable.
• For monitors having a DVI (Digital Visual Interface) port
but no HDMI port, we can use HDMI to DVI
adapter/cable.
87
Anurag College of Engineering
About the Raspberry Pi Board
Composite Video Output:
• Raspberry Pi comes with a composite video output with
an RCA (Radio Corporation of America) jack that
supports both PAL (Phase Alternating Line ) and NTSC
(National Television Standards Committee) video output.
• The RCA jack can be used to connect old television that
have an RCA input only.
Audio Output:
• Raspberry Pi has a 3.5mm audio output jack.
• This audio jack is used for providing audio output to old
televisions along with the RCA jack for video.
• The audio quality from this jack is inferior to the HDMI
output.
88
Anurag College of Engineering
About the Raspberry Pi Board
GPIO Pins:
89
Anurag College of Engineering
Raspberry Pi GPIO Header Pins
90
Anurag College of Engineering
About the Raspberry Pi Board
Display Serial Interface (DSI):
• The DSI interface can be used to connect an LCD panel to
Raspberry Pi.
Camera Serial Interface (CSI):
• The CSI interface can be used to connect a camera module to
Raspberry Pi.
Power Input:
• Raspberry Pi has a micro-USB connector for power input.
SD card Slot:
• Raspberry Pi does not have a built in operating system and
storage. We can plug in an SD card loaded with a Linux
image to the SD card slot. An 8GB SD card is required for
setting up New Out-of-the-Box Software (NOOBS). 91
Anurag College of Engineering
About the Raspberry Pi Board
Status LEDs:
• Raspberry Pi has five status LEDs.
• Table below shows the Raspberry Pi status LEDs and
their functions.
92
Anurag College of Engineering
Anurag College of Engineering 93
Anurag College of Engineering 94