0% found this document useful (0 votes)
59 views57 pages

Unit - Ii: Mr. Babu Illuri

This document provides an overview of Python programming concepts including: - Data types like numbers, strings, lists, tuples, and dictionaries - Control flow structures like if/else statements, for loops, while loops - Functions, modules, packages - File handling and reading/writing XML and JSON files - Classes and object-oriented programming fundamentals It also introduces the minidom and ElementTree modules for parsing and manipulating XML data in Python.

Uploaded by

Babu I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views57 pages

Unit - Ii: Mr. Babu Illuri

This document provides an overview of Python programming concepts including: - Data types like numbers, strings, lists, tuples, and dictionaries - Control flow structures like if/else statements, for loops, while loops - Functions, modules, packages - File handling and reading/writing XML and JSON files - Classes and object-oriented programming fundamentals It also introduces the minidom and ElementTree modules for parsing and manipulating XML data in Python.

Uploaded by

Babu I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

UNIT -II

Mr. Babu Illuri


B.Tech, M.Tech, (Ph.D)
Email:[email protected]

VARDHAMAN COLLEGE OF ENGINEERING


Shamshabad, Hyderabad – 501218, India.
Contents
Language features of Python
Data types& data structures
Control flow
 Functions
 Modules
 Packages
 File Handling
 Data/Time operations
 Classes
 Python packages of interest for IoT(JSON,XML)
Language features of Python
Python is a general-purpose high level programming language
The main characteristics of Python are:
1. Multi-paradigm programming language: Supports more than one
Programming including object-oriented programming and Structured
programming.
2. Interpreted Language: It does not require an explicit compilation
step.Phyton Interpreter executes the program source code directly
3. Interactive Language: It provides an interactive mode in which user can
submit the commands
4. Easy to Learn, read and maintain
5. Supports both object and procedure oriented programming. procedure
oriented programming allows programs to be written around procedures or
functions that allow reuse of code.
6. Extendable: It is an extendable language that allows integration of low level
modules written in C/C++
7. Portable: Python programs can be copied from one machine to another
without worrying about portability.
1.
Data types& data structures
Numbers: This data type is used to store numeric values
2. Strings
A String is simply a list of characters in order. There is no limit
for number of characters in a string. A string with zero
characters is an empty string
3. Lists
List is a compound data type used to group together other values.
List items need not all have the same type. A List contains items separated
by commas and enclosed within square brackets.
Lists Contd..
Tuples
A tuple is a Sequence data type that is similar to that to the list.
Unlike lists, the elements of tuples cannot be changed, so tuples can
be thought of as read-only lists.
Dictionaries
Dictionary is a mapping data type or a kind of hash table that maps keys to values.
Keys in a dictionary can be of any data type, though numbers and strings are
commonly used for keys
Contd..
Control Flow
1. if:
The if statement in python is similar to the if statement of other languages.
2. for
Syntax: For iterating_var in sequence:
Execute Statements
3. While
While(condition is true):
Statements…
4. range
The range statement in Python generates a list of numbers in
arithmetic progression
5. Break/Continue
The Break statement breaks out for/while loop where as continue
statement continues with next iteration
Continue
Pass
The pass statement is used when statement is required syntactically but
you do not want any command or code to execute.
Functions
A Function is a block of code that takes information in the
form of parameters, does some computation and returns a new
piece of information based on the parameter information.
A Function in python is a block of code that begins with the
keyword def followed by function name and parenthesis. The
function parameters are enclosed with these parenthesis.
Syntax : def function name(Function parameters)
statements……….
Modules
Python allows organizing of the program code into different modules which improves
the code readability and management.
A module in python file defines some functionality in the form of functions or classes.
Modules can be imported using the import keyword.
Package
A Directory that contains __init__.py is a package.
A Package is a Hierarchical file directory structure that defines
a single python application environment that consists of
modules and sub packages and sub sub packages and soon.
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 binary(rb),write
binary(wb),etc.,
After the file contents have been read the close () is called which closes
the file object
Classes
Python is an object-oriented programming (oop)
language.
Python provides all the standard features of Object
Oriented Programming such as
classes
class variable
class methods
inheritance
function overloading and operator overloading
The simplest form of class definition looks like this:
Class Objects
Inheritance
IF
These conditions can be used in several ways, most commonly in "if
statements" and loops.
An "if statement" is written by using the if keyword.
a = 33
b = 200
if b > a:
  print("b is greater than a")

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
you can have if statements inside if statements, this is
called nested if statements.
x = 41
if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")
Python has two primitive loop commands:
while loops
for loops

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
  print(i)
  i += 1
Note: remember to increment i, or else the loop will
continue forever.
With the break statement we can stop the loop even if the while condition is true:

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
JSON
JSON or JavaScript Object Notation is a lightweight
text-based open standard designed for human-readable
data interchange.
The JSON format was originally specified by Douglas
Crockford,.
The official Internet media type for JSON is
application/json. The JSON filename extension is
.json.
JSON stands for JavaScript Object Notation.
The format was specified by Douglas Crockford.
It was designed for human-readable data interchange.
It has been extended from the JavaScript scripting
language.
The filename extension is .json.
JSON Internet Media type is application/json.
The Uniform Type Identifier is public.json.
Uses of JSON
It is used while writing JavaScript based applications
that includes browser extensions and websites.
JSON format is used for serializing and
transmitting structured data over network
connection.
It is primarily used to transmit data between a server
and web applications.
Web services and APIs use JSON format to provide
public data.
It can be used with modern programming languages.
Simple Example in JSON

 The following example shows how to use JSON to store information related to books
based on their topic and edition.

"
 The process of encoding JSON is usually called serialization. This

term refers to the transformation of data into a series of


bytes (hence serial) to be stored or transmitted across a network. You
may also hear the term marshaling, but that’s a whole other discussion.
Naturally, deserialization is the reciprocal process of decoding data that
has been stored or delivered in the JSON standard.
n? It needs to take a data dump. Accordingly, the json library exposes the dump() method for writing data to files. There is also a dumps() metho
A Simple Serialization Example
It is critical that you save this information to disk, so
your mission is to write it to a file.
Using Python’s context manager, you can create a file
called data_file.json and open it in write mode. (JSON
files conveniently end in a .json extension.)
Reading and Writing XML Files in Python

XML, or Extensible Markup Language, is a markup-


language that is commonly used to structure, store, and
transfer data between systems.
With Python being a popular language for the web and
data analysis, it's likely you'll need to read or write
XML data at some point.
Throughout this Class look at the ElementTree module
for reading, writing, and modifying XML data. We'll
also compare it with the older minidom module in the
first few sections so you can get a good comparison of
the two.
The XML Modules

The minidom, or Minimal DOM Implementation, is a


simplified implementation of the Document Object
Model (DOM). The DOM is an application
programming interface that treats XML as a tree
structure, where each node in the tree is an object.
Thus, the use of this module requires that we are
familiar with its functionality.
The ElementTree module provides a more "Pythonic"
interface to handling XMl and is a good option for
those not familiar with the DOM.
XML File Example
In the examples below, we will be using the following XML file, which we will save as "items.xml":

As you can see, it's a fairly simple XML example, only


containing a few nested objects and one attribute. 
Reading XML Documents

Using minidom
In order to parse an XML document using minidom,
we must first import it from the xml.dom module. This
module uses the parse function to create a DOM object
from our XML file. The parse function has the
following syntax:
 xml.dom.minidom.parse (filename_or_file[,parser[, bufsize]])
Here the file name can be a string containing the file
path or a file-type object. The function returns a
document, which can be handled as an XML type.
Thus, we can use the
function getElementByTagName() to find a specific
tag.
If we wanted to use an already-opened file, can just pass our
file object to parse like so:
Using ElementTree

ElementTree presents us with an very simple way to


process XML files. As always, in order to use it we
must first import the module. In our code we use
the import command with the as keyword, which
allows us to use a simplified name (ET in this case) for
the module in the code.
Following the import, we create a tree structure with
the parse function, and we obtain its root element.
Once we have access to the root node we can easily
traverse around the tree, because a tree is a connected
graph.
Flask Package
Import flask from Flask
Flask: Web Framework
Building Web Applications
HTTP Requests
(__name__): Special Variable in Python. In all other
programming languages you will have main(),but in python
we don't. So by declaring in (__name__) python will
understand that this is main() and starts executing from it.
@(Decorator)-Sending request to webpage
“/” Indicates the default Server

You might also like