Iot Unit 3
Iot Unit 3
Introduction to python:
Python Features
Python provides many useful features which make it popular and valuable
from the other programming languages. It supports object-oriented
programming, procedural programming approaches and provides dynamic
memory allocation. few essential features.
2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple
example, the hello world program you simply type print("Hello World"). It
will take only one line to execute, while Java or C takes multiple lines.
3) Interpreted Language
Python is an interpreted language; it means the Python program is executed
one line at a time. It makes debugging easy and portable.
4) Portable Language
Python can run equally on different platforms such as Windows, Linux, UNIX,
and Macintosh, etc. So, we can say that Python is a portable language.once
written can run anywhere.
5) Object-Oriented Language
6) Extensible
It converts the program into byte code, and any platform can use that byte
code.
It provides a vast range of libraries for the various fields such as machine
learning, web developer, and also for the scripting. Ex: Pandas,
Numpy,matplotlib etc. Django, flask are the popular framework for Python
web development.
8) Integrated
Python runs code line by line like C,C++ Java. It makes easy to debug the
code.
Data types
Data structures
Built in:
List:
A list is a data structure in Python that is a mutable, or changeable, ordered
sequence of elements.
Ex:same from datatypes
Sets:
Dose not maintain insertion order,immutable(cant modify),dose not allow
duplicates
Ex: same from datatypes
Tuples:
Maintains insertion order,immutable(cant modify),allows duplicates
Ex: same from datatypes
Dictionary:
Unordered,unique,mutable(can modify)
Ex: same from datatypes
User defined:
1. Array:
An array is a special variable, which can hold more than one value at a time.
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
2. Stack:
A stack is a linear data structure where data is arranged objects on over
another.
It stores the data in LIFO (Last in First Out) manner.
We can perform the two operations in the stack - PUSH and POP.
The PUSH operation is when we add an element
POP operation is when we remove an element from the stack.
Methods of Stack
1. empty() - It returns true, it the stack is empty. The time complexity is O(1).
2. size() - It returns the length of the stack. The time complexity is O(1).
3. top() - This method returns an address of the last element of the stack.
The time complexity is O(1).
4. push(g) - This method adds the element 'g' at the end of the stack - The
time complexity is O(1).
5. pop() - This method removes the topmost element of the stack. The time
complexity is O(1).
3.Queue:
A queue is a linear type of data structure used to store the data in a
sequentially.
The concept of queue is based on the FIFO, which means "First in First
Out".
It is also known as "first come first severed".
The queue has the two ends front and rear.
The next element is inserted from the rear end and removed from
the front end.
https://www.scaler.com/topics/types-of-functions-in-python/
Modules:
Modules work like code library
It is file containing set of functions you want to include in your application
A module allows you to logically organize your code
A module can define functions ,classes and variables
A module can also include a runnable code
Ex:
//ex1.py
def sample(name):
print(“welcome”+name)
import ex1
ex1.sample(“to home”)
Variables module:
# This is variables module
## this is a function in module
def factorial(n):
if n == 1 || n == 0:
return 1
else:
return n * factorial(n-1)
fact_of_6 = variables.factorial(6)
power_of_6 = variables.power[6]
alphabet_2 = variables.alphabets[1]
Renaming a Module
Python provides an easy and flexible way to rename our module with a
specific name and then use that name to call our module resources. For
renaming a module, we use the as keyword. The syntax is as follows:
Code:
import math as m
import numpy as np
import random as r
print(m.pi)
print(r.randint(0,10))
print(np.__version__)
Output:
3.141592653589793
10
1.22.0
File handling:
A file is a container in computer storage devices used for storing
data.
1. Open a file
2. Read or write (perform operation)
3. Close the file
Date time operations:
There are six main object classes with their respective components in the
datetime module mentioned below:
1. datetime.date
2. datetime.time
3. datetime.datetime
4. datetime.tzinfo
5. datetime.timedelta
6. datetime.timezone
1.datetime.date
from datetime import date
current = date.today()
print("Current Day is :", current.day)
print("Current Month is :", current.month)
print("Current Year is :", current.year)
2.datetime.time:
from datetime import time
defaultTime = time(9,14,59,12)
print("default_hour =", defaultTime.hour)
print("default_minute =", defaultTime.minute)
print("default_second =", defaultTime.second)
print("default_microsecond =", defaultTime.microsecond)
3.datetime.datetime:
from datetime import datetime
current = datetime.now()
date_time = current.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:", date_time)
manual = datetime(2021, 3, 28, 23, 55, 59, 342380)
print("year =", manual.year)
print("month =", manual.month)
print("day =", manual.day)
print("hour =", manual.hour)
print("minute =", manual.minute)
print("sec =", manual.second)
Exception handling:
In Python, a class is a user-defined data type that contains both the data
itself and the methods that may be used to manipulate it.
They provide the characteristics and operations that the objects will
employ.
Syntax
class ClassName:
Example:
Code:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
Name and age are the two properties of the Person class. Additionally, it has a
function called greet that prints a greeting.
Packages:
JSON:
JSON (JavaScript Object Notation) is the most widely used data format
for data interchange on the web.
JSON is a lightweight text-based, data-interchange format and it is
completely language-independent.
It is based on a subset of the JavaScript programming language and it is
easy to understand and generate.
JSON supports mainly 6 data types:
1. String
2. Number
3. Boolean
4. Null
5. Object
6. Array
Note: string, number, boolean, null are simple data types or
primitives data types whereas object and array are referred as
complex data types.
HTTP lib:
SMTP lib:
Simple Mail Transfer Protocol (SMTP) is used as a protocol to handle the email
transfer using Python. It is an application layer protocol which allows to users
to send mail to another. The receiver retrieves email using the
protocols POP(Post Office Protocol) and IMAP(Internet Message Access
Protocol).
The SMTP object is used for the email transfer. The following syntax is used to
create the smtplib object.
Syntax:
import smtplib
smtpObj = smtplib.SMTP(host, port, local_hostname)