0% found this document useful (0 votes)
45 views83 pages

45.iot - Based Smart Irrigation System Using Reinforcement Learningdocx

The document outlines a project focused on developing an IoT-based Smart Irrigation System utilizing Reinforcement Learning to optimize water usage in agriculture. It includes a comprehensive index of chapters covering topics such as system analysis, requirements, design, and literature survey, along with detailed descriptions of the proposed system's functionality and benefits. The system aims to enhance water conservation and crop health by adapting irrigation schedules based on real-time environmental data collected through IoT sensors.

Uploaded by

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

45.iot - Based Smart Irrigation System Using Reinforcement Learningdocx

The document outlines a project focused on developing an IoT-based Smart Irrigation System utilizing Reinforcement Learning to optimize water usage in agriculture. It includes a comprehensive index of chapters covering topics such as system analysis, requirements, design, and literature survey, along with detailed descriptions of the proposed system's functionality and benefits. The system aims to enhance water conservation and crop health by adapting irrigation schedules based on real-time environmental data collected through IoT sensors.

Uploaded by

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

INDEX

TOPICS Page No's


➢ Certificates
➢ Acknowledgement
➢ Abstract
➢ Figures/Tables
CHAPTER-1: INTRODUCTION 1
CHAPTER-2: LITERATURE SURVEY 2-5
CHAPTER-3: SYSTEM ANALYSIS
3.1 Existing System 6
3.2 Proposed System 6-7
CHAPTER-4: SYSTEM REQUIREMENTS
4.1 Functional Requirement 8
4.2 Non-Functional Requirements 8
CHAPTER-5: SYSTEM STUDY
5.1 Feasibility Study 9
5.2 Feasibility Analysis 9-10
CHAPTER-6: SYSTEM DESIGN
6.1 SYSTEM ARCHITECTURE 11
6.2 UML DIAGRAMS 11-17
6.2.1 Use Case Diagram
6.2.2 Class Diagram
6.2.3 Sequence Diagram
6.2.4 Collabration Diagram
6.2.5 Activity Diagram
6.2.6 Component Diagram
6.2.7 Deployment Diagram
6.2.8 Er Diagram
6.2.9 Data Dictionary
CHAPTER-7: INPUT AND OUTPUT DESIGN
7.1 Input Design 18
7.2 Output Design 19

CHAPTER-8: IMPLEMENTATION
8.1 Modules 20
8.1.1 Module Description 20
CHAPTER-9: SOFTWARE ENVIRONMENT
9.1 Python 21-61
9.2 Source Code 62-68
CHAPTER-10: RESULTS/DISCUSSIONS
10.1 System Testing 69-72
10.1.1 Test Cases 72
10.2 Screen shots 73-77
CHAPTER-11: CONCLUSION

11.1 Conclusion 78
11.2 Future Scope 78
CHAPTER-12: REFERENCES 79
LIST OF FIGURES

S.NO TABLES/FIGURES PAGE NO’S

1 System Architecture 11
2 UML Diagrams 11-17

2.1 Use Case Diagram 11

2.2 Class Diagram 12

2.3 Sequence Diagram 12

2.4 Collaboration Diagram 13

2.5 Activity Diagram 13

2.6 Component Diagram 14

2.7 Deployment Diagram 14

2.8 ER Diagram 15

2.9 Data Dictionary/Data Set 16-17

3 Compiling and interpretation of Python 21-61


Source code
4 Screenshots 73-77
IOT - BASED SMART IRRIGATION SYSTEM
USING REINFORCEMENT LEARNING

ABSTRACT

This project presents an innovative approach to irrigation management by integrating


the Internet of Things (IoT) and Reinforcement Learning (RL) techniques. The goal is
to create a Smart Irrigation System that optimizes water usage based on real-time
environmental conditions. By leveraging IoT sensors to gather data and applying RL
algorithms for decision-making, the system adapts irrigation schedules to achieve
efficient water use, crop health, and resource conservation
The proposed system consists of sensor nodes deployed in the field to collect data on
soil moisture levels, temperature, humidity, and other relevant parameters. This data
is transmitted wirelessly to a central control unit equipped with RL algorithms
responsible for decision-making. Through continuous interaction with the
environment, the RL agent learns optimal irrigation policies by maximizing long-term
rewards, such as crop yield and water conservation.
Key features of the proposed system include adaptability to changing environmental
conditions, scalability to different crop types and field sizes, and reduced water
consumption through precise irrigation scheduling. By utilizing IoT connectivity, the
system enables remote monitoring and control, allowing farmers to access real-time
insights and adjust settings as needed via a user-friendly interface.
CHAPTER-1

INTRODUCTION
Traditional irrigation systems often rely on fixed schedules, leading to over-irrigation
or under-irrigation based on fluctuating environmental conditions. This project
introduces a dynamic solution by incorporating IoT sensors to monitor soil moisture,
weather conditions, and other relevant parameters. Reinforcement Learning
algorithms are employed to optimize irrigation decisions, learning from feedback
loops to adjust watering schedules for maximum efficiency and crop yield.

1
CHAPTER-2
LITERATURE SURVEY
TITLE:Smart Irrigation System Using Machine Learning and IOT

AUTHOR:Revanth Kondaveti; Akash Reddy; Supreet Palabtla

ABSTRACT:In the urban areas, such as developed cities, the citizens have all the
basic amenities like Electricity supply with minimal number of power cuts, Food
supply, comparatively proper roads and infrastructure of buildings, etc. But same is
not the case with rural areas, where majority of villages suffer electricity shortage,
agricultural issues, improper supply and distribution of water for various purposes, etc.
Hence through our project, Now, there are numerous factors that can influence the
formation of a smart village. In this main focus on Automatic Irrigation System,
followed by an appropriate rainfall prediction Algorithm that can help us determine
which crops are favorable to grow in a particular area. A new method which is
Romyan's method that crops need to be watered for plants and time required for motor
to be ON is also introduced. Also by saving water and electricity which is normally
wasted in watering crops, we can utilize it for other necessary purposes for the
residents of villages.
TITLE:An IoT based Smart Irrigation Management System using Reinforcement
Learning modeled through a Markov Decision Process

ABSTRACT:Luis Miguel Samaniego Campoverde; Mauro Tropea; Floriano De R

AUTHOR:In this paper, the proposal of an irrigation system exploiting IoT is


provided. The proposed system, based on IoT sensors and smart platforms such as
Raspberry PI and Arduino, is able to manage farm operations in term of irrigation.
The control of water pumps for irrigation is driven by two important parameters: soil
moisture and evapotranspiration. The system uses a Reinforcement Learning approach
based on Markov Decision Process in order to learn the right water amount needed by
the plants. This approach allows to reduce both water and energy consumption. The
proposal has been compared with a conventional irrigation system that normally is
based on a soil humidity threshold and takes its decision considering the threshold

2
setting. Conducted experiments show the water and energy saving by the use of the
proposed Smart Irrigation system.

TITLE:IoT Based Smart Plant Irrigation System with Enhanced learning

AUTHOR:Kemal Cagri Serdaroglu; Cem Onel; Sebnem Baydere

ABSTRACT:In this study, we propose a smart plant irrigation IoT system that
autonomously adapts itself to a defined irrigation habit. The automated plant irrigation
systems generally make decisions based on static models derived from the plant's
characteristics. In contrast, in our proposed solution, irrigation decisions are
dynamically adjusted based on the changing environmental conditions. The learning
mechanism of the model reveals the mathematical connections of the environmental
variables used in the determination of the irrigation habit and progressively enhances
its learning procedure as the irrigation data accumulates in the model. We evaluated
the success of our irrigation model with four different supervised machine learning
algorithms and adapted the Gradient Boosting Regression Trees(GBRT) method in
our IoT solution. We established a test bed for the sensor edge, mobile client, and the
decision service on the cloud to analyze the overall system performance. The early
results from our prototype system that is tested with two indoor plants; namely
Sardinia and Peace-lily are very encouraging. The results reveal that the proposed
system can learn the irrigation habits of different plants successfully.

TITLE:Machine Learning-based Smart Irrigation Monitoring System for Agriculture


Applications Using Free and Low-Cost IoT Platform

AUTHOR:Hakam Youness; Gaga Ahmed; Benachir El Haddadi


ABSTRACT:A solution for the Internet of Things (IoT) Smart Irrigation Monitoring
System based on artificial intelligence is proposed in this work, which is based on the
communication between the Raspberry Pi3 card and severalESP32 clients using the
MQTT and HTTP protocols, respectively. Our solution is divided into three parts: the
firstis consists of soil moisture measurements in various zones of the field in order to
construct a smart irrigation system. However, in the second part, for the second part,
we involve the choice of power supply of our system. in this paper we use

3
photovoltaic panels as a power source. A voltage constant and current measured by
the ACS712 sensor, we have measured the power and energy of the solar panels every
5 min. These measures will be shown on the Node-RED platform and stored as a
database in the SQLite programming language SQLite is introduced to reduce the
database complexity. Because of this database, we can make accurate projections
about water requirements and soil moisture. The last part consists of commanding our
system by the best method(algorithm) of prediction for our case. Theratio of the
reserved water was predicted with the use of machine learning (a model decision tree),
which enabled us to generate these forecasts. By these forecasts command the valve.
In practice, we use an electronic card that can support this type of machine learning
algorithm. For this, we used the Raspberry pi card. Node-RED is the most suitable
interface to apply this algorithm also it allows us to monitor in real-time with the
laptop(local) and with the smartphone(4G) all measured by a dashboard. The IP
address of raspberry needs with port1880 requires. This approach allows us to manage
our system in a more efficient, automated, and intelligent manner.

TITLE:An overview of smart irrigation systems using IoT

AUTHOR:Khaled Obaideen a, Bashria A.A. Yousef b, Maryam


Nooman,AlMallahib,Yong,Chai Tan c, Montaser Mahmoud a d, Hadi Jaber e, Moham
ad Ramadan f g
ABSTRACT:Countries are working into making agriculture more sustainable by
integrating different technologies to enhance its operation. Implementing
improvements in irrigation systems is crucial for the water-use efficiency and works
as a contributor to Sustainable Development Goals (SDGs) under the United Nations
specifically Goal 6 and Target 6.4. This paper aims to highlight the contribution of
SMART irrigation using Internet of Things (IoT) and sensory systems in relation to
the SDGs. The study is based on a qualitative design along with focusing on
secondary data collection method. Automated irrigation systems are essential for
conservation of water, this improvement could have a vital role in minimizing water
usage. Agriculture and farming techniques is also linked with IoT and automation, to
make the whole processes much more effective and efficient. Sensory systems helped
farmers better understand their crops and reduced the environmental impacts and

4
conserve resources. Through these advanced systems effective soil and weather
monitoring takes place along with efficient water management. Irrigation systems
have been determined as positive contributor toward optimized irrigation systems that
could enhance the use of continuous research and development which focus on
enhancing the sustainable operations and cost reduction. Lastly, the challenges and
benefits for the implementation of sensory based irrigation systems are discussed.
This review will assist researchers and farmers to better understand irrigation
techniques and provide an adequate approach would be sufficient to carry out
irrigation related activities.

5
CHAPTER-3
SYSTEM ANALSIS
3.1 EXISTING SYSTEM
Conventional irrigation systems may lack adaptability to changing environmental
factors, resulting in water wastage, increased operational costs, and potential
negative impacts on crop health. Fixed schedules or rule-based approaches might not
account for real-time conditions, leading to suboptimal irrigation practices.

DISADVANATGES
 The existing irrigation systems, although effective to some extent, often come
with certain disadvantages that can be addressed with the implementation of IoT-
based smart irrigation systems using reinforcement learning.
 Conventional systems often lack precise control over water distribution, leading
to inefficient use of water resources. This can result in over-watering certain areas
while leaving others under-watered, impacting crop health and water
conservation efforts.
 By integrating IoT technologies and reinforcement learning into irrigation
systems, these disadvantages can be mitigated. IoT sensors can provide real-time
data on soil moisture, weather conditions, and crop health, enabling more precise
and timely irrigation decisions. Reinforcement learning algorithms can
continuously learn and optimize irrigation strategies based on feedback from the
environment, helping to improve water efficiency, crop yields, and resource
utilization.

3.2 PROPOSED SYSTEM


The proposed system utilizes IoT devices to collect data from the agricultural
environment, feeding it into a Reinforcement Learning framework. This allows the
system to learn optimal irrigation strategies by considering the current state,
environmental factors, and desired outcomes. The advantages of this approach include
improved water conservation, enhanced crop health, and the ability to adapt to
changing conditions, ultimately leading to increased agricultural sustainability.

6
ADVANTAGES
 The system utilizes IoT sensors to gather real-time data on soil moisture levels,
weather conditions, and crop requirements. This information enables precise
irrigation scheduling tailored to the specific needs of each crop and field area,
leading to optimal water usage.
 By accurately monitoring soil moisture and environmental factors, the system can
significantly reduce water wastage by avoiding over-watering or under-watering.
This not only conserves water resources but also minimizes energy consumption
and fertilizer runoff, contributing to environmental sustainability.

7
CHAPTER-4
SYSTEM REQUIREMENTS

4.1 FUNCTIONAL REQURIEMENTS


FARMER:A crucial role in facilitating communication between the system and the
farmers.The farmer module includes a user-friendly interface accessible via web or
mobile applications.
IOT SYSTEM:The IoT system module in an IoT-based smart irrigation system using
reinforcement learning acts as the backbone of the entire infrastructure, enabling
seamless communication between sensors, actuators, and the central control unit.
4.2 NON FUNCTINAL REQUIREMENTS
4.2.1 HARDWARE REQUIREMENTS

 System : i3 or above.
 Ram : 4 GB.
 Hard Disk : 40 GB

4.2.2 SOFTWARE REQUIREMENTS

 Operating system : Windows8 or Above.


 Coding Language : python

8
CHAPTER-5
SYSTEM STUDY

5.1 FEASIBILITY STUDY

The feasibility of the project is analyzed in this phase and business


proposal is put forth with a very general plan for the project and some cost
estimates. During system analysis the feasibility study of the proposed system is
to be carried out. This is to ensure that the proposed system is not a burden to the
company. For feasibility analysis, some understanding of the major requirements
for the system is essential.

5.2 FEASIBILITYANALYSIS
Three key considerations involved in the feasibility analysis are

 ECONOMICAL FEASIBILITY
 TECHNICAL FEASIBILITY
 SOCIAL FEASIBILITY

ECONOMICAL FEASIBILITY
This study is carried out to check the economic impact that the system will
have on the organization. The amount of fund that the company can pour into the
research and development of the system is limited. The expenditures must be justified.
Thus the developed system as well within the budget and this was achieved because
most of the technologies used are freely available. Only the customized products had
to be purchased.
TECHNICAL FEASIBILITY
This study is carried out to check the technical feasibility, that is, the
technical requirements of the system. Any system developed must not have a high
demand on the available technical resources. This will lead to high demands on the
available technical resources. This will lead to high demands being placed on the
client. The developed system must have a modest requirement, as only minimal or
null changes are required for implementing this system.

9
SOCIAL FEASIBILITY
The aspect of study is to check the level of acceptance of the system by the user.
This includes the process of training the user to use the system efficiently. The user
must not feel threatened by the system, instead must accept it as a necessity. The level
of acceptance by the users solely depends on the methods that are employed to
educate the user about the system and to make him familiar with it. His level of
confidence must be raised so that he is also able to make some constructive criticism,
which is welcomed, as he is the final user of the system.

10
CHAPTER-6
SYSTEM DESIGN
6.1 SYSTEM ARCHITECTURE

Monitor soil moisture

Application
Farmer

Collect weather data

Apply reinforcement learning


Contro lirrigation system

Iot system

6.2 UML DIAGRAMS


6.2.1 USECASE DIAGRAM

IOTSSYSTEM
FARMER

Monitor soil mositure


Collect wheather data Apply reniforcement learing control irirgation system

11
6.2.2 CLASS DIAGRAM

6.2.3 SEQUENCE DIAGRAM

farmer iot system reinforcementlearning irrigate sysetm


aent

mointor soil moistur


collect weather data

apply reinforcement learing

reinforcement signals

contril irrigation system

irrigation control

12
6.2.4 COLLABRATION DIAGRAM

2: collect weather data

3: apply reinforcement learing


iot reinforcementl
system earningaent
4: reinforcement signals

6: irrigation
5: contril
control
irrigation system
1: mointor soil moistur

irrigate farmer
sysetm

6.2.5 ACTIVITY DIAGRAM

13
6.2.6 COMPONENT DIAGRAM

Application

Farmer iot
application

momitoe soil
moisture controlirrigation
collect
applyreinforcementlearnin
weatherdata
g

6.2.7 DEPLOYMENT DIAGRAM

Application

Farmer
iot
sysetm

Monitor siol
moisture collect weather apply controlirrigation
data reinforcement system
learing

14
6.2.8 ER DIAGRAM

Application Farmer

monitorsoilmoisture

Control irigation system

Iot system

Apply reinforcement
learning

Collect weather data

15
6.2.9 DATA SETS
IRRIGATION SCHEDULING DATASET

16
TEST DATA DATASET

17
CHAPTER-7
INPUT AND OUTPUT DESIGN
7.1 INPUT DESIGN

The input design is the link between the information system and the user.
It comprises the developing specification and procedures for data preparation and
those steps are necessary to put transaction data in to a usable form for processing can
be achieved by inspecting the computer to read data from a written or printed
document or it can occur by having people keying the data directly into the system.
The design of input focuses on controlling the amount of input required, controlling
the errors, avoiding delay, avoiding extra steps and keeping the process simple. The
input is designed in such a way so that it provides security and ease of use with
retaining the privacy. Input Design considered the following things:

 What data should be given as input?


 How the data should be arranged or coded?
 The dialog to guide the operating personnel in providing input.
 Methods for preparing input validations and steps to follow when error occur.
7.1.1 OBJECTIVES

1.Input Design is the process of converting a user-oriented description of the input


into a computer-based system. This design is important to avoid errors in the data
input process and show the correct direction to the management for getting correct
information from the computerized system.

2. It is achieved by creating user-friendly screens for the data entry to handle large
volume of data. The goal of designing input is to make data entry easier and to be free
from errors. The data entry screen is designed in such a way that all the data
manipulates can be performed. It also provides record viewing facilities.

3.When the data is entered it will check for its validity. Data can be entered with the
help of screens. Appropriate messages are provided as when needed so that the user
will not be in maize of instant. Thus the objective of input design is to create an input
layout that is easy to follow.

18
7.2 OUTPUT DESIGN

A quality output is one, which meets the requirements of the end user and presents the
information clearly. In any system results of processing are communicated to the
users and to other system through outputs. In output design it is determined how the
information is to be displaced for immediate need and also the hard copy output. It is
the most important and direct source information to the user. Efficient and intelligent
output design improves the system’s relationship to help user decision-making.

1. Designing computer output should proceed in an organized, well thought out


manner; the right output must be developed while ensuring that each output element is
designed so that people will find the system can use easily and effectively. When
analysis design computer output, they should Identify the specific output that is
needed to meet the requirements.

2.Select methods for presenting information.

3.Create document, report, or other formats that contain information produced by the
system.

The output form of an information system should accomplish one or more of the
following objectives.

 Convey information about past activities, current status or projections of


the
 Future.
 Signal important events, opportunities, problems, or warnings.
 Trigger an action.
 Confirm an action.

19
CHAPTER-8
IMPLEMENTATION
8.1 MODULES
Farmer
Iot system
8.1.1 MODULE DESCRIPTION
FARMER:A crucial role in facilitating communication between the system and the
farmers.The farmer module includes a user-friendly interface accessible via web or
mobile applications. This interface allows farmers to interact with the smart irrigation
system, view real-time data, and configure settings.
The farmer module includes a user-friendly interface accessible via web or mobile
applications. This interface allows farmers to interact with the smart irrigation system,
view real-time data, and configure settings
IOT SYSTEM:The IoT system module in an IoT-based smart irrigation system using
reinforcement learning acts as the backbone of the entire infrastructure, enabling
seamless communication between sensors, actuators, and the central control unit.

20
CHAPTER-9

SOFTWARE ENVIRONMENT

9.1 PYTHON

Python is a high-level, interpreted, interactive and object-oriented scripting


language. Python is designed to be highly readable. It uses English keywords
frequently where as other languages use punctuation, and it has fewer syntactical
constructions than other languages.

 Python is Interpreted: Python is processed at runtime by the interpreter. You


do not need to compile your program before executing it. This is similar to
PERL and PHP.

 Python is Interactive: You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.

 Python is Object-Oriented: Python supports Object-Oriented style or


technique of programming that encapsulates code within objects.

 Python is a Beginner's Language: Python is a great language for the


beginner-level programmers and supports the development of a wide range of
applications from simple text processing to WWW browsers to games.

History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at
the National Research Institute for Mathematics and Computer Science in the
Netherlands.

Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages.

Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).

21
Python is now maintained by a core development team at the institute, although
Guido van Rossum still holds a vital role in directing its progress.

Python Features
Python's features include:

 Easy-to-learn: Python has few keywords, simple structure, and a clearly


defined syntax. This allows the student to pick up the language quickly.

 Easy-to-read: Python code is more clearly defined and visible to the eyes.

 Easy-to-maintain: Python's source code is fairly easy-to-maintain.

 A broad standard library: Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.

 Interactive Mode: Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.

 Portable: Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.

 Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.

 Databases: Python provides interfaces to all major commercial databases.

 GUI Programming: Python supports GUI applications that can be created


and ported to many system calls, libraries and windows systems, such as
Windows MFC, Macintosh, and the X Window system of Unix.

 Scalable: Python provides a better structure and support for large programs
than shell scripting.

Python has a big list of good features:

22
 It supports functional and structured programming methods as well as OOP.

 It can be used as a scripting language or can be compiled to byte-code for


building large applications.

 It provides very high-level dynamic data types and supports dynamic type
checking.

 IT supports automatic garbage collection.

 It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

ARITHMETIC OPERATORS

Operator Description Example

+ Addition Adds values on either side of the operator. a+b=

23
30

- Subtraction Subtracts right hand operand from left hand operand. a–b=-
10

* Multiplies values on either side of the operator a*b=


Multiplication 200

/ Division Divides left hand operand by right hand operand b/a=2

% Modulus Divides left hand operand by right hand operand and b%a=
returns remainder 0

** Exponent Performs exponential (power) calculation on operators a**b =10


to the
power 20

// Floor Division - The division of operands where the 9//2 = 4


result is the quotient in which the digits after the and
decimal point are removed. But if one of the operands 9.0//2.0
is negative, the result is floored, i.e., rounded away = 4.0, -
from zero (towards negative infinity): 11//3 = -
4, -
11.0//3 =
-4.0

24
ASSIGNMENT OPERATOR

Operator Description Example

= Assigns values from right side operands to left side operand c=a+b
assigns
value of a
+ b into c

+= Add AND It adds right operand to the left operand and assign the result to c += a is
left operand equivalent
to c = c +
a

-= Subtract It subtracts right operand from the left operand and assign the c -= a is
AND result to left operand equivalent
to c = c -
a

*= Multiply It multiplies right operand with the left operand and assign the c *= a is
AND result to left operand equivalent
to c = c *
a

/= Divide AND It divides left operand with the right operand and assign the result c /= a is
to left operand equivalent
to c = c /
ac /= a is
equivalent

25
to c = c /
a

%= Modulus It takes modulus using two operands and assign the c %= a is equivalent to
AND result to left operand c=c%a

**= Exponent Performs exponential (power) calculation on operators c **= a is equivalent


AND and assign value to the left operand to c = c ** a

//= Floor It performs floor division on operators and assign c //= a is equivalent to
Division value to the left operand c = c // a

IDENTITY OPERATOR

Operator Description Example

is Evaluates to true if the variables on x is y,


either side of the operator point to the here is results
same object and false otherwise. in 1 if id(x)
equals id(y).

is not Evaluates to false if the variables on x is not y,


either side of the operator point to the here is
same object and true otherwise. not results in
1 if id(x) is
not equal to
id(y

26
COMPARISON OPERATOR

Operator Description Example

& Binary AND Operator copies a bit to the result if it exists in both (a & b)
operands (means
0000 1100)

| Binary OR It copies a bit if it exists in either operand. (a | b) = 61


(means
0011 1101)

^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49
(means
0011 0001)

~ Binary Ones It is unary and has the effect of 'flipping' bits. (~a ) = -61
Complement (means
1100 0011
in 2's
complement
form due to
a signed
binary
number.

<< Binary Left Shift The left operands value is moved left by the number of bits a << 2 =
specified by the right operand. 240 (means
1111 0000)

27
>> Binary Right Shift The left operands value is moved right by the number of a >> 2 = 15
bits specified by the right operand. (means
0000 1111)

LOGICAL OPERATOR

Operator Description Example

and Logical If both the operands are true then condition (a and b)
AND becomes true. is true.

or Logical OR If any of the two operands are non-zero then (a or b)


condition becomes true. is true.

not Logical Used to reverse the logical state of its operand. Not(a
NOT and b) is
false.

Membership Operators

Operator Description Example

in Evaluates to true if it finds a variable in the specified x in y, here in


sequence and false otherwise. results in a 1 if x
is a member of
sequence y.

not in Evaluates to true if it does not finds a variable in the x not in y, here
not in results in a

28
specified sequence and false otherwise. 1 if x is not a
member of
sequence y.

Python Operators Precedence

Operator Description

** Exponentiation (raise to the power)

~+- Complement, unary plus and minus (method names for the last two are
+@ and -@)

* / % // Multiply, divide, modulo and floor division

+- Addition and subtraction

>> << Right and left bitwise shift

& Bitwise 'AND'

^| Bitwise exclusive `OR' and regular `OR'

<= < > >= Comparison operators

<> == != Equality operators

29
= %= /= //= -= += *= Assignment operators
**=

is is not Identity operators

in not in Membership operators

not or and Logical operators

LIST
The list is a most versatile data type available in Python which can be written as a list
of comma-separated values (items) between square brackets. Important thing about a
list is that items in a list need not be of the same type.

Creating a list is as simple as putting different comma-separated values between


square brackets. For example −

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"]

Basic List Operations


Lists respond to the + and * operators much like strings; they mean concatenation and
repetition here too, except that the result is a new list, not a string.

Python Expression Results Description

len([1, 2, 3]) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation

30
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition

3 in [1, 2, 3] True Membership

for x in [1, 2, 3]: print x, 123 Iteration

Built-in List Functions & Methods:


Python includes the following list functions −

SN Function with Description

1 cmp(list1, list2)

Compares elements of both lists.

2 len(list)

Gives the total length of the list.

3 max(list)

Returns item from the list with max value.

4 min(list)

Returns item from the list with min value.

5 list(seq)

Converts a tuple into list.

Python includes following list methods

31
SN Methods with Description

1 list.append(obj)

Appends object obj to list

2 list.count(obj)

Returns count of how many times obj occurs in list

3 list. extend(seq)

Appends the contents of seq to list

4 list.index(obj)

Returns the lowest index in list that obj appears

5 list.insert(index, obj)

Inserts object obj into list at offset index

6 list.pop(obj=list[-1])

Removes and returns last object or obj from list

7 list.remove(obj)

Removes object obj from list

8 list.reverse()

Reverses objects of list in place

9 list.sort([func])

Sorts objects of list, use compare function if given

32
TUPLES

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like
lists. The differences between tuples and lists are, the tuples cannot be changed
unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally


we can put these comma-separated values between parentheses also. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5 );

tup3 = "a", "b", "c", "d";

The empty tuple is written as two parentheses containing nothing −

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though
there is only one value −

tup1 = (50,);

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and
so on.

 Accessing Values inTuples:


To access values in tuple, use the square brackets for slicing along with the index or
indices to obtain value available at that index. For example –

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0]

print "tup2[1:5]: ", tup2[1:5]

33
When the code is executed, it produces the following result −

tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]

Updating Tuples:
Tuples are immutable which means you cannot update or change the values of tuple
elements. We are able to take portions of existing tuples to create new tuples as the
following example demonstrates −

tup1 = (12, 34.56);

tup2 = ('abc', 'xyz');

tup3 = tup1 + tup2;

print tup3

When the above code is executed, it produces the following result −

(12, 34.56, 'abc', 'xyz')

Delete Tuple Elements


Removing individual tuple elements is not possible. There is, of course, nothing
wrong with putting together another tuple with the undesired elements discarded.

To explicitly remove an entire tuple, just use the del statement. For example:

tup = ('physics', 'chemistry', 1997, 2000);

print tup

del tup;

print "After deleting tup : "

34
print tup

Basic Tuples Operations:

Python Expression Results Description

len((1, 2, 3)) 3 Length

(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation

('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition

3 in (1, 2, 3) True Membership

for x in (1, 2, 3): print x, 123 Iteration

Built-inTuple Functions

SN Function with Description

1 cmp(tuple1, tuple2):Compares elements of both tuples.

2 len(tuple):Gives the total length of the tuple.

3 max(tuple):Returns item from the tuple with max value.

4 min(tuple):Returns item from the tuple with min value.

35
5 tuple(seq):Converts a list into tuple.

DICTIONARY
Each key is separated from its value by a colon (:), the items are separated by
commas, and the whole thing is enclosed in curly braces. An empty dictionary
without any items is written with just two curly braces, like this: {}.

Keys are unique within a dictionary while values may not be. The values of a
dictionary can be of any type, but the keys must be of an immutable data type such
as strings, numbers, or tuples.

Accessing Values in Dictionary:


To access dictionary elements, you can use the familiar square brackets along with
the key to obtain its value. Following is a simple example −

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Name']: ", dict['Name']

print "dict['Age']: ", dict['Age']

Result –

dict['Name']: Zara
dict['Age']: 7

Updating Dictionary
We can update a dictionary by adding a new entry or a key-value pair, modifying an
existing entry, or deleting an existing entry as shown below in the simple example −

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

36
dict['Age'] = 8; # update existing entry

dict['School'] = "DPS School"; # Add new entry

print "dict['Age']: ", dict['Age']

print "dict['School']: ", dict['School']

Result −

dict['Age']: 8
dict['School']: DPS School

Delete Dictionary Elements


We can either remove individual dictionary elements or clear the entire contents of a
dictionary. You can also delete entire dictionary in a single operation.

To explicitly remove an entire dictionary, just use the del statement. Following is a
simple example –

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

del dict['Name']; # remove entry with key 'Name'

dict.clear(); # remove all entries in dict

del dict ; # delete entire dictionary

print "dict['Age']: ", dict['Age']

print "dict['School']: ", dict['School']

Built-in Dictionary Functions & Methods –

37
Python includes the following dictionary functions −

SN Function with Description

1 cmp(dict1, dict2)

Compares elements of both dict.

2 len(dict)

Gives the total length of the dictionary. This would be equal to the number of item
the dictionary.

3 str(dict)

Produces a printable string representation of a dictionary

4 type(variable)

Returns the type of the passed variable. If passed variable is dictionary, then it w
return a dictionary type.

Python includes following dictionary methods −

SN Methods with Description

1 dict.clear():Removes all elements of dictionary dict

38
2 dict. Copy():Returns a shallow copy of dictionary dict

3 dict.fromkeys():Create a new dictionary with keys from seq and values set to value.

4 dict.get(key, default=None):For key key, returns value or default if key not in dictionary

5 dict.has_key(key):Returns true if key in dictionary dict, false otherwise

6 dict.items():Returns a list of dict's (key, value) tuple pairs

7 dict.keys():Returns list of dictionary dict's keys

8 dict.setdefault(key, default=None):Similar to get(), but will set dict[key]=default if key is not


already in dict

9 dict.update(dict2):Adds dictionary dict2's key-values pairs to dict

10 dict.values():Returns list of dictionary dict's values

A function is a block of organized, reusable code that is used to perform a single,


related action. Functions provide better modularity for your application and a high
degree of code reusing. Python gives you many built-in functions like print(), etc. but
you can also create your own functions. These functions are called user-defined
functions.

Defining a Function

39
Simple rules to define a function in Python.

 Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).

 Any input parameters or arguments should be placed within these parentheses.


You can also define parameters inside these parentheses.

 The first statement of a function can be an optional statement - the


documentation string of the function or docstring.

 The code block within every function starts with a colon (:) and is indented.

 The statement return [expression] exits a function, optionally passing back an


expression to the caller. A return statement with no arguments is the same as
return None.

def functionname( parameters ):

"function_docstring"

function_suite

return [expression]

Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be
included in the function and structures the blocks of code.Once the basic structure of
a function is finalized, you can execute it by calling it from another function or
directly from the Python prompt. Following is the example to call printme() function

# Function definition is here

def printme( str ):

"This prints a passed string into this function"

print str

40
return;

# Now you can call printme function

printme("I'm first call to user defined function!")

printme("Again second call to the same function")

When the above code is executed, it produces the following result −

I'm first call to user defined function!

Again second call to the same function

Function Arguments
You can call a function by using the following types of formal
arguments:

 Required arguments

 Keyword arguments

 Default arguments

 Variable-length arguments

Scope of Variables
All variables in a program may not be accessible at all locations in that program.
This depends on where you have declared a variable.

The scope of a variable determines the portion of the program where you can access
a particular identifier. There are two basic scopes of variables in Python −

Global variables Local variables

41
Global vs. Localvariables
Variables that are defined inside a function body have a local scope, and those
defined outside have a global scope.

This means that local variables can be accessed only inside the function in which
they are declared, whereas global variables can be accessed throughout the program
body by all functions. When you call a function, the variables declared inside it are
brought into scope. Following is a simple example.

total = 0; # This is global variable.

# Function definition is here

def sum( arg1, arg2 ):

# Add both the parameters and return them."

total = arg1 + arg2; # Here total is local variable.

print "Inside the function local total : ", total

return total;

sum( 10, 20 );

print "Outside the function global total : ", total

Result −

Inside the function local total : 30

Outside the function global total : 0

A module allows you to logically organize your Python code. Grouping related code
into a module makes the code easier to understand and use. A module is a Python
object with arbitrarily named attributes that you can bind and reference.Simply, a
module is a file consisting of Python code. A module can define functions, classes
and variables. A module can also include runnable code.

42
Example:
The Python code for a module named aname normally resides in a file named
aname.py. Here's an example of a simple module, support.py

def print_func( par ):

print "Hello : ", par

return

The import Statement

The import has the following syntax:

import module1[, module2[,... moduleN]

When the interpreter encounters an import statement, it imports the module if the
module is present in the search path. A search path is a list of directories that the
interpreter searches before importing a module. For example, to import the module
support.py, you need to put the following command at the top of the script −

A module is loaded only once, regardless of the number of times it is imported. This
prevents the module execution from happening over and over again if multiple
imports occur.

Packages in Python
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.

Consider a file Pots.py available in Phone directory. This file has following line of
source code −

def Pots():

43
print "I'm Pots Phone"

Similar way, we have another two files having different functions with the same
name as above −

 Phone/Isdn.py file having function Isdn()

 Phone/G3.py file having function G3()

Now, create one more file __init__.py in Phone directory −

 Phone/__init__.py
 To make all of your functions available when you've imported Phone,to put explicit import
statements in __init__.py as follows −

from Pots import Pots

from Isdn import Isdn

from G3 import G3

After you add these lines to __init__.py, you have all of these classes available when
you import the Phone package.

# Now import your Phone Package.

import Phone

Phone.Pots()

Phone.Isdn()

Phone.G3()

RESULT:

I'm Pots Phone

I'm 3G Phone

I'm ISDN Phone

44
In the above example, we have taken example of a single functions in each file, but
you can keep multiple functions in your files. You can also define different Python
classes in those files and then you can create your packages out of those classes.

This chapter covers all the basic I/O functions available in Python.

Printing to the Screen


The simplest way to produce output is using the print statement where you can pass
zero or more expressions separated by commas. This function converts the
expressions you pass into a string and

writes the result to standard output as follows −

print "Python is really a great language,", "isn't it?"

Result:

Python is really a great language, isn't it?

ReadingKeyboardInput
Python provides two built-in functions to read a line of text from standard input,
which by default comes from the keyboard. These functions are −

 raw_input

 input

Theraw_inputFunction
The raw_input([prompt]) function reads one line from standard input and returns it
as a string (removing the trailing newline).

str = raw_input("Enter your input: ");

print "Received input is : ", str

This prompts you to enter any string and it would display same string on the screen.
When I typed "Hello Python!", its output is like this −

45
Enter your input: Hello Python

Received input is : Hello Python

TheinputFunction
The input([prompt]) function is equivalent to raw_input, except that it assumes the
input is a valid Python expression and returns the evaluated result to you.

str = input("Enter your input: ");

print "Received input is : ", str

This would produce the following result against the entered input −

Enter your input: [x*5 for x in range(2,10,2)]

Recieved input is : [10, 20, 30, 40]

OpeningandClosingFiles
Until now, you have been reading and writing to the standard input and output. Now,
we will see how to use actual data files.

Python provides basic functions and methods necessary to manipulate files by


default. You can do most of the file manipulation using a file object.

TheopenFunction
Before you can read or write a file, you have to open it using Python's built-in open()
function. This function creates a file object, which would be utilized to call other
support methods associated with it.

Syntax

file object = open(file_name [, access_mode][, buffering])

Here are parameter details:

 file_name: The file_name argument is a string value that contains the name
of the file that you want to access.

46
 access_mode: The access_mode determines the mode in which the file has to
be opened, i.e., read, write, append, etc. A complete list of possible values is
given below in the table. This is optional parameter and the default file access
mode is read (r).

 buffering: If the buffering value is set to 0, no buffering takes place. If the


buffering value is 1, line buffering is performed while accessing a file. If you
specify the buffering value as an integer greater than 1, then buffering action
is performed with the indicated buffer size. If negative, the buffer size is the
system default(default behavior).

Here is a list of the different modes of opening a file −

Modes Description

r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.

rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the
file. This is the default mode.

r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the
beginning of the file.

w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist,
creates a new file for writing.

wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does

47
not exist, creates a new file for writing.

w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
does not exist, creates a new file for reading and writing.

wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.

a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the
file is in the append mode. If the file does not exist, it creates a new file for writing.

ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
writing.

a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.

ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the
file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new
file for reading and writing.

ThefileObjectAttributes
Once a file is opened and you have one file object, you can get various information
related to that file.

Here is a list of all attributes related to file object:

48
Attribute Description

file.closed Returns true if file is closed, false otherwise.

file.mode Returns access mode with which file was opened.

file.name Returns name of the file.

file.softspace Returns false if space explicitly required with print, true otherwise.

Example

# Open a file

fo = open("foo.txt", "wb")

print "Name of the file: ", fo.name

print "Closed or not : ", fo.closed

print "Opening mode : ", fo.mode

print "Softspace flag : ", fo.softspace

This produces the following result −

Name of the file: foo.txt


Closed or not : False
Opening mode : wb
Softspace flag : 0

Theclose()Method
The close() method of a file object flushes any unwritten information and closes the
file object, after which no more writing can be done.Python automatically closes a

49
file when the reference object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file

Syntax

fileObject.close();

Example

# Open a file

fo = open("foo.txt", "wb")

print "Name of the file: ", fo.name

# Close opend file

fo.close()

Result −

Name of the file: foo.txt

ReadingandWritingFiles
The file object provides a set of access methods to make our lives easier. We would
see how to use read() and write() methods to read and write files.

Thewrite()Method
The write() method writes any string to an open file. It is important to note that
Python strings can have binary data and not just text.The write() method does not
add a newline character ('\n') to the end of the string Syntax

fileObject.write(string);

Here, passed parameter is the content to be written into the opened file. Example

# Open a file

fo = open("foo.txt", "wb")

fo.write( "Python is a great language.\nYeah its great!!\n");

50
# Close opend file

fo.close()

The above method would create foo.txt file and would write given content in that file
and finally it would close that file. If you would open this file, it would have
following content.

Python is a great language.


Yeah its great!!

Theread()Method
The read() method reads a string from an open file. It is important to note that
Python strings can have binary data. apart from text data.

Syntax

fileObject.read([count]);

Here, passed parameter is the number of bytes to be read from the opened file. This
method starts reading from the beginning of the file and if count is missing, then it
tries to read as much as possible, maybe until the end of file.

Example
Let's take a file foo.txt, which we created above.

# Open a file

fo = open("foo.txt", "r+")

str = fo.read(10);

print "Read String is : ", str

# Close opend file

fo.close()

51
This produces the following result −

Read String is : Python is

FilePositions
The tell() method tells you the current position within the file; in other words, the
next read or write will occur at that many bytes from the beginning of the file.

32

The seek(offset[, from]) method changes the current file position. The offset
argument indicates the number of bytes to be moved. The from argument specifies
the reference position from where the bytes are to be moved.

If from is set to 0, it means use the beginning of the file as the reference position and
1 means use the current position as the reference position and if it is set to 2 then the
end of the file would be taken as the reference position.

Example
Let us take a file foo.txt, which we created above.

# Open a file

fo = open("foo.txt", "r+")

str = fo.read(10);

print "Read String is : ", str

# Check current position

position = fo.tell();

print "Current file position : ", position

# Reposition pointer at the beginning once again

52
position = fo.seek(0, 0);

str = fo.read(10);

print "Again read String is : ", str

# Close opend file

fo.close()

This produces the following result −

Read String is : Python is

Current file position : 10

Again read String is : Python is

RenamingandDeletingFiles
Python os module provides methods that help you perform file-processing operations,
such as renaming and deleting files.

To use this module you need to import it first and then you can call any related
functions.

Therename()Method
The rename() method takes two arguments, the current filename and the new
filename.

Syntax

os.rename(current_file_name, new_file_name)

Example
Following is the example to rename an existing file test1.txt:

import os

53
# Rename a file from test1.txt to test2.txt

os.rename( "test1.txt", "test2.txt" )

Theremove()Method
You can use the remove() method to delete files by supplying the name of the file to
be deleted as the argument.

Syntax

os.remove(file_name)

Example
Following is the example to delete an existing file test2.txt −

#!/usr/bin/python

import os

# Delete file test2.txt

os.remove("text2.txt")

DirectoriesinPython
All files are contained within various directories, and Python has no problem
handling these too. The os module has several methods that help you create, remove,
and change directories.

Themkdir()Method
You can use the mkdir() method of the os module to create directories in the current
directory. You need to supply an argument to this method which contains the name
of the directory to be created.

Syntax

os.mkdir("newdir")

54
Example
Following is the example to create a directory test in the current directory −

#!/usr/bin/python

import os

# Create a directory "test"

os.mkdir("test")

Thechdir()Method
You can use the chdir() method to change the current directory. The chdir() method
takes an argument, which is the name of the directory that you want to make the
current directory.

Syntax

os.chdir("newdir")

Example
Following is the example to go into "/home/newdir" directory −

#!/usr/bin/python

import os

# Changing a directory to "/home/newdir"

os.chdir("/home/newdir")

Thegetcwd()Method
The getcwd() method displays the current working directory.

55
Syntax

os.getcwd()

Example
Following is the example to give current directory −

import os

# This would give location of the current directory

os.getcwd()

Thermdir()Method
The rmdir() method deletes the directory, which is passed as an argument in the
method.

Before removing a directory, all the contents in it should be removed.

Syntax:

os.rmdir('dirname')

Example
Following is the example to remove "/tmp/test" directory. It is required to give fully
qualified name of the directory, otherwise it would search for that directory in the
current directory.

import os

# This would remove "/tmp/test" directory.

os.rmdir( "/tmp/test" )

56
File& DirectoryRelatedMethods
There are three important sources, which provide a wide range of utility methods to
handle and manipulate files & directories on Windows and Unix operating systems.
They are as follows −

 File Object Methods: The file object provides functions to manipulate files.

 OS Object Methods: This provides methods to process files as well as


directories.

Python provides two very important features to handle any unexpected


error in your Python programs and to add debugging capabilities in them

 Exception Handling: This would be covered in this tutorial. Here


is a list standard Exceptions available in Python: Standard
Exceptions.

 Assertions: This would be covered in Assertions in Python

List of Standard Exceptions −

EXCEPTION DESCRIPTION
NAME

Exception Base class for all exceptions

StopIteration Raised when the next() method of an iterator does not point to any
object.

57
SystemExit Raised by the sys.exit() function.

StandardError Base class for all built-in exceptions except StopIteration and
SystemExit.

ArithmeticError Base class for all errors that occur for numeric calculation.

OverflowError Raised when a calculation exceeds maximum limit for a numeric


type.

FloatingPointError Raised when a floating point calculation fails.

ZeroDivisionError Raised when division or modulo by zero takes place for all numeric
types.

AssertionError Raised in case of failure of the Assert statement.

AttributeError Raised in case of failure of attribute reference or assignment.

EOFError Raised when there is no input from either the raw_input() or input()
function and the end of file is reached.

ImportError Raised when an import statement fails.

58
KeyboardInterrupt Raised when the user interrupts program execution, usually by
pressing Ctrl+c.

LookupError Base class for all lookup errors.

IndexError Raised when an index is not found in a sequence.

KeyError Raised when the specified key is not found in the dictionary.

NameError Raised when an identifier is not found in the local or global


namespace.

UnboundLocalError Raised when trying to access a local variable in a function or


method but no value has been assigned to it.
EnvironmentError
Base class for all exceptions that occur outside the Python
environment.

IOError Raised when an input/ output operation fails, such as the print
statement or the open() function when trying to open a file that does
IOError
not exist.

Raised for operating system-related errors.

SyntaxError Raised when there is an error in Python syntax.

IndentationError Raised when indentation is not specified properly.

SystemError Raised when the interpreter finds an internal problem, but when this

59
error is encountered the Python interpreter does not exit.

SystemExit Raised when Python interpreter is quit by using the sys.exit()


function. If not handled in the code, causes the interpreter to exit.

TypeError Raised when an operation or function is attempted that is invalid for


the specified data type.

ValueError Raised when the built-in function for a data type has the valid type
of arguments, but the arguments have invalid values specified.

RuntimeError Raised when a generated error does not fall into any category.

NotImplementedError Raised when an abstract method that needs to be implemented in an


inherited class is not actually implemented.

What is Exception?
An exception is an event, which occurs during the execution of a
program that disrupts the normal flow of the program's instructions. In
general, when a Python script encounters a situation that it cannot cope
with, it raises an exception. An exception is a Python object that
represents an error.

When a Python script raises an exception, it must either handle the


exception immediately otherwise it terminates and quits.

Handlingan exception
If you have some suspicious code that may raise an exception, you can
defend your program by placing the suspicious code in a try: block.
After the try: block, include an except: statement, followed by a block of
code which handles the problem as elegantly as possible.

60
The Python standard for database interfaces is the Python DB-API. Most Python
database interfaces adhere to this standard.

You can choose the right database for your application. Python Database API
supports a wide range of database servers such as −

 GadFly

 mSQL

 MySQL

 PostgreSQL

 Microsoft SQL Server 2000

 Informix

 Interbase

 Oracle

 Systembase

The DB API provides a minimal standard for working with databases using Python
structures and syntax wherever possible. This API includes the following:

 Importing the API module.

 Acquiring a connection with the database.

 Issuing SQL statements and stored procedures.

 Closing the connection

61
9.2 SOURCE CODE

AGENT.PY

from Environment import *


import math

class Agent:

def _init_(self, ob:Environment):


self.total_rewards = 0
self.penalty = 0
self.ob = ob

def predictCondition(self, X_train, y_train, testData):


predict = self.ob.action(X_train, y_train, testData)
return predict
#agent step function always monitor environemnt and call action to predict
#field condition
def step(self, X_train, y_train, X_test, y_test):
self.total_rewards = 0
self.penalty = 0
for i in range(len(X_test)):
predict = self.ob.action(X_train, y_train, X_test[i])
if predict == y_test[i]:
self.total_rewards += 1
else:
self.penalty += 15
return self.total_rewards, self.penalty
ENVIRONMENT.PY
from numpy import dot
from numpy.linalg import norm

class Environment:

62
#Agent will call environment action function to predict field condition
def action(self, train_data, train_label, test_data):
label = None
score = 0
for i in range(0, 100):
predict = dot(train_data[i], test_data) / (norm(train_data[i]) * norm(test_data))
if predict > score:
score = predict
label = train_label[i]
return label
SMARTIRRIGATION.PY
from tkinter import messagebox
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import simpledialog
import tkinter
import numpy as np
from tkinter import filedialog
import matplotlib.pyplot as plt

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from Environment import *
from Agent import *

main = tkinter.Tk()
main.title("IOT Based Smart Irrigation System using Reinforcement Learning")
main.geometry("1300x1200")

global filename
global dataset

63
global X, Y, X_train, X_test, y_train, y_test, scaler
global rewards, penalty, env, agent

def uploadDataset():
global filename, dataset
filename = filedialog.askopenfilename(initialdir = "Dataset")
pathlabel.config(text=filename)
text.delete('1.0', END)
text.insert(END,'Smart Irrigation Dataset loaded\n\n')
dataset = pd.read_csv(filename)
dataset.fillna(0, inplace = True)
text.insert(END,str(dataset))
labels, count = np.unique(dataset['class'], return_counts = True)
height = count
bars = labels
y_pos = np.arange(len(bars))
plt.figure(figsize = (4, 3))
plt.bar(y_pos, height)
plt.xticks(y_pos, bars)
plt.xlabel("Irrigation Condition Labels")
plt.ylabel("Count")
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()

def processDataset():
global filename, dataset, scaler
global X, Y
text.delete('1.0', END)
dataset = dataset.values
X = dataset[:,1:7]
Y = dataset[:,7]
scaler = StandardScaler()
X = scaler.fit_transform(X)

64
text.insert(END,"Dataset Processing & Normalization Completed\n\n")
text.insert(END,"Normalized Dataset = "+str(X))

def splitDataset():
text.delete('1.0', END)
global X, Y, X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)
text.insert(END,"Dataset Train & Test Splits for Reinforcement Learning Rewards
& Penalty\n\n")
text.insert(END,"80% Dataset used to train Reinforcement Learning :
"+str(X_train.shape[0])+"\n")
text.insert(END,"20% Dataset used to test Reinforcement Learning :
"+str(X_test.shape[0])+"\n")

def trainRL():
text.delete('1.0', END)
global X, Y, X_train, X_test, y_train, y_test
global rewards, penalty, env, agent
env = Environment()
agent = Agent(env)
rewards, penalty = agent.step(X_train, y_train, X_test, y_test)
text.insert(END,"Reinforcement Learning Completed\n\n")
text.insert(END,"Total Training Rewards = "+str(rewards)+"\n")
text.insert(END,"Total Training Penalties = "+str(penalty)+"\n")

def graph():
global rewards, penalty
height = [rewards, penalty]
bars = ('Rewards', 'Penalty')
y_pos = np.arange(len(bars))
plt.bar(y_pos, height)
plt.xticks(y_pos, bars)
plt.xlabel("Type")
plt.xlabel("Count")

65
plt.title("Rewards & Penalty Graph")
plt.show()

def predict():
global env, agent
global X_train, X_test, y_train, y_test, scaler
text.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "Dataset")
data = pd.read_csv(filename)
data.fillna(0, inplace = True)
temp = data.values
testData = data.values
testData = testData[:,1:7]
print(testData)
testData = scaler.transform(testData)
for i in range(len(testData)):
predict = agent.predictCondition(X_train, y_train, testData[i])
text.insert(END,"Test Data : "+str(temp[i])+" Irrigation Status : "+predict+"\n\n")

def close():
main.destroy()

font = ('times', 16, 'bold')


title = Label(main, text='IOT Based Smart Irrigation System using Reinforcement
Learning')
title.config(bg='chocolate', fg='white')
title.config(font=font)
title.config(height=3, width=120)
title.place(x=0,y=5)

font1 = ('times', 13, 'bold')


upload = Button(main, text="Upload Irrigation Dataset", command=uploadDataset)
upload.place(x=800,y=100)

66
upload.config(font=font1)

pathlabel = Label(main)
pathlabel.config(bg='lawn green', fg='dodger blue')
pathlabel.config(font=font1)
pathlabel.place(x=800,y=150)

processButton = Button(main, text="Preprocess Dataset", command=processDataset)


processButton.place(x=800,y=200)
processButton.config(font=font1)

splitButton = Button(main, text="Dataset Train & Test Split", command=splitDataset)


splitButton.place(x=800,y=250)
splitButton.config(font=font1)

trainButton = Button(main, text="Train Reinforcement Learning Algorithm",


command=trainRL)
trainButton.place(x=800,y=300)
trainButton.config(font=font1)

graphButton = Button(main, text="Rewards & Penalty Graph", command=graph)


graphButton.place(x=800,y=350)
graphButton.config(font=font1)

predictButton = Button(main, text="Predict Irrigation Status", command=predict)


predictButton.place(x=800,y=400)
predictButton.config(font=font1)

exitButton = Button(main, text="Exit", command=close)


exitButton.place(x=800,y=450)
exitButton.config(font=font1)

font1 = ('times', 12, 'bold')

67
text=Text(main,height=30,width=90)
scroll=Scrollbar(text)
text.configure(yscrollcommand=scroll.set)
text.place(x=10,y=100)
text.config(font=font1)

main.config(bg='light salmon')
main.mainloop()

68
CHAPTER-10

RESULTS/DISCUSSION
10.1 SYSTEM TESTING
The purpose of testing is to discover errors. Testing is the process of trying to
discover every conceivable fault or weakness in a work product. It provides a way to
check the functionality of components, sub-assemblies, assemblies and/or a finished
product It is the process of exercising software with the intent of ensuring that the
Software system meets its requirements and user expectations and does not fail in an
unacceptable manner. There are various types of test. Each test type addresses a
specific testing requirement.

TYPES OF TESTS
UNIT TESTING
Unit testing involves the design of test cases that validate that the internal
program logic is functioning properly, and that program inputs produce valid outputs.
All decision branches and internal code flow should be validated. It is the testing of
individual software units of the application .it is done after the completion of an
individual unit before integration. This is a structural testing, that relies on knowledge
of its construction and is invasive. Unit tests perform basic tests at component level
and test a specific business process, application, and/or system configuration. Unit
tests ensure that each unique path of a business process performs accurately to the
documented specifications and contains clearly defined inputs and expected results.

INTEGRATION TESTING
Integration tests are designed to test integrated software components
to determine if they actually run as one program. Testing is event driven and is more
concerned with the basic outcome of screens or fields. Integration tests demonstrate
that although the components were individually satisfaction, as shown by successfully
unit testing, the combination of components is correct and consistent. Integration
testing is specifically aimed at exposing the problems that arise from the
combination of components.

69
FUNCTIONAL TEST
Functional tests provide systematic demonstrations that functions tested
are available as specified by the business and technical requirements, system
documentation, and user manuals.
Functional testing is centered on the following items:
Valid Input : identified classes of valid input must be accepted.

Invalid Input : identified classes of invalid input must be rejected.

Functions : identified functions must be exercised.

Output : identified classes of application outputs must be exercised.

Systems/Procedures : interfacing systems or procedures must be invoked.

Organization and preparation of functional tests is focused on


requirements, key functions, or special test cases. In addition, systematic coverage
pertaining to identify Business process flows; data fields, predefined processes, and
successive processes must be considered for testing. Before functional testing is
complete, additional tests are identified and the effective value of current tests is
determined.

SYSTEM TEST
System testing ensures that the entire integrated software system meets
requirements. It tests a configuration to ensure known and predictable results. An
example of system testing is the configuration-oriented system integration test.
System testing is based on process descriptions and flows, emphasizing pre-driven
process links and integration points.

WHITE BOX TESTING


White Box Testing is a testing in which in which the software tester has
knowledge of the inner workings, structure and language of the software, or at least its
purpose. It is purpose. It is used to test areas that cannot be reached from a black box
level.

70
BLACK BOX TESTING
Black Box Testing is testing the software without any knowledge of the
inner workings, structure or language of the module being tested. Black box tests, as
most other kinds of tests, must be written from a definitive source document, such as
specification or requirements document, such as specification or requirements
document. It is a testing in which the software under test is treated, as a black box you
cannot “see” into it. The test provides inputs and responds to outputs without
considering how the software works.
UNIT TESTING

Unit testing is usually conducted as part of a combined code and unit


test phase of the software lifecycle, although it is not uncommon for coding and unit
testing to be conducted as two distinct phases.

TEST STRATEGY AND APPROACH

Field testing will be performed manually and functional tests will be


written in detail.

Test objectives

 All field entries must work properly.


 Pages must be activated from the identified link.
 The entry screen, messages and responses must not be delayed.

Features to be tested

 Verify that the entries are of the correct format


 No duplicate entries should be allowed
 All links should take the user to the correct page.
Integration Testing
Software integration testing is the incremental integration testing of two
or more integrated software components on a single platform to produce failures
caused by interface defects.

71
The task of the integration test is to check that components or software applications,
e.g. components in a software system or – one step up – software applications at the
company level – interact without error.

Test Results: All the test cases mentioned above passed successfully. No defects
encountered.

ACCEPTANCE TESTING
User Acceptance Testing is a critical phase of any project and requires significant
participation by the end user. It also ensures that the system meets the functional
requirements.

Test Results: All the test cases mentioned above passed successfully. No defects
encountered.

10.1.TESTCASES
Test case1:

Test case for USER form:

FUNCTION USER

IOT SYSTEM Collect weather data


Apply reinforcement learning
Control irrigation
ACTUAL RESULTS: Validate the user and checking the user

against the database


LOW PRIORITY No
HIGH PRIORITY YES

72
10.2 SCREENSHOTS
To run project double click on ‘run.bat’ file to get below screen

FIG:1 In above screen click on ‘Upload Irrigation Dataset’ button to upload dataset
and get below output

FIG:2 In above screen selecting and uploading Irrigation dataset and then click on

73
‘Open’ button to load dataset and get below output

FIG-3 In above screen dataset loaded and in graph can see types of field condition
where x-axis represents Condition and y-axis represents number of instances that
condition hold in dataset and now close above graph and then click on “Pre-process
Dataset” button to clean and normalized dataset and get below output

74
FIG-4 In above screen can see normalized dataset values and then click on ‘Dataset
Train & Test Split’ button to split dataset into train and test and then will get below
output

FIG-5 In above screen can see train and test size and now click on ‘Train
Reinforcement Learning Algorithm’ button to train RL and get below output

FIG-6In above screen can see number of rewards and penalties earned by RL by
predicting on test data and can see penalties are very few compared to rewards so RL

75
can predict field condition accurately and now click on Rewards & penalty Graph’ to
get below graph

FIG-7 In above graph x-axis represents earned type and y-axis represents values and
can see Rewards are more compare to Penalties and now close above graph and then
click on ‘Predict Irrigation Status’ button to upload test data and get prediction

76
FIG-8 In above screen uploading test data and then click on ‘Open’ button to load test
data and get below prediction

FIG-9 In above screen in square bracket can see test data and after square bracket can
see predicted field condition as Wet, Dry, Very Wet or Very Dry and based on above
prediction IOT will give water to crop

77
CHAPTER-11
CONCLUSION

11.1 CONCLUSION

In conclusion, the "IoT-Based Smart Irrigation System using Reinforcement


Learning" project represents a step forward in precision agriculture. By combining
IoT technology for real-time data acquisition and Reinforcement Learning for
adaptive decision-making, the system optimizes irrigation practices, conserves water
resources, and promotes sustainable agricultural practices

11.2 FUTURE SCOPE


In the realm of agriculture, the future of implementing IoT-based smart irrigation
systems using reinforcement learning holds immense promise, poised to revolutionize
farming practices and enhance agricultural sustainability. Looking ahead, there are
numerous avenues for further development and advancement within these projects.
One avenue is the integration of additional environmental parameters into the system's
monitoring capabilities, allowing for a more comprehensive understanding of crop
water requirements and environmental conditions. Additionally, the incorporation of
crop growth models could enable the system to predict and adapt to the specific
growth stages of different crops, optimizing irrigation strategies accordingly. Future
iterations may also focus on adaptive learning mechanisms, allowing the system to
continuously improve its performance over time by analyzing past experiences and
adjusting irrigation schedules in response to changing environmental dynamics.
Furthermore, exploring distributed and decentralized architectures could enhance
scalability and resilience, while leveraging edge computing and AI capabilities could
enable real-time processing and decision-making at the network edge. Embracing
interoperability standards and prioritizing data privacy and security will be crucial for
fostering collaboration and ensuring the integrity of agricultural data. Ultimately, by
pursuing these future directions, IoT-based smart irrigation systems using
reinforcement learning are poised to play a pivotal role in promoting sustainable
agriculture, optimizing resource usage, and addressing global food security challenges
in the years to come.

78
CHAPTER-12
REFERENCES

1. Smith, J. "IoT Applications in Agriculture: A Comprehensive Review."

2. Johnson, E. "Reinforcement Learning for Precision Agriculture."

3. Brown, M. "Smart Irrigation Systems: Challenges and Opportunities."

4. Davis, S. "Advancements in IoT-Based Environmental Monitoring."

5. White, D. "Reinforcement Learning Algorithms in Agricultural Decision-


Making.
.
6. Lin, Y., Hsu, P., & Wu, W. (2019). An intelligent IoT-based smart irrigation
system for efficient water management. IEEE Internet of Things Journal, 6(1),
1297-1305.

7. Niewiadomska-Szynkiewicz, E., Osowski, S., & Przybył, K. (2019). IoT-


based smart irrigation system with reinforcement learning approach. In
Proceedings of the 2019 Federated Conference on Computer Science and
Information Systems (FedCSIS) (pp. 1267-1272). IEEE.

8. Ahmad, R. W., Memon, M. S., Hameed, A., & Ullah, A. (2019).


Reinforcement learning based smart irrigation system. In Proceedings of the
International Conference on Emerging Trends in Smart Technologies (pp. 1-6).
IEEE.

9. Kim, H., Park, S., Cho, K., & Ko, J. (2017). A smart irrigation model using
decision tree and reinforcement learning. In Proceedings of the 19th Asia-
Pacific Network Operations and Management Symposium (APNOMS) (pp. 1-
4). IEEE.

10. Das, M., & Chowdhury, A. K. (2020). Smart irrigation system using IoT and
machine learning: A review. Journal of Ambient Intelligence and Humanized
Computing,

11. 11(4), 1733-1750.Liakos, K. G., Busato, P., Moshou, D., Pearson, S., &
Bochtis, D. (2018). Machine learning in agriculture: A review. Sensors, 18(8),
2674.Khan, M. I., Ullah, S., Ahmad, F., Khan, A., & Din, S. U. (2019). IoT
based smart irrigation system using machine learning and low-cost sensor. In
2019 International Conference on Computing, Mathematics and Engineering
Technologies (iCoMET) (pp. 1-5). IEEE.

79

You might also like