0% found this document useful (0 votes)
88 views200 pages

Class 12th - LT - Junior Software Developer

The document outlines the job role of a Junior Software Developer, detailing the curriculum for Class XII students in Information Technology. It covers essential topics such as Relational Database Management Systems (RDBMS), Advanced Python Programming, Software Engineering, and Emerging Trends in Technology. Additionally, it discusses the advantages and limitations of Database Management Systems (DBMS) compared to traditional file systems.

Uploaded by

airdeep2006
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)
88 views200 pages

Class 12th - LT - Junior Software Developer

The document outlines the job role of a Junior Software Developer, detailing the curriculum for Class XII students in Information Technology. It covers essential topics such as Relational Database Management Systems (RDBMS), Advanced Python Programming, Software Engineering, and Emerging Trends in Technology. Additionally, it discusses the advantages and limitations of Database Management Systems (DBMS) compared to traditional file systems.

Uploaded by

airdeep2006
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/ 200

Junior Software Development

Developer
(Job Role)

Qualification Pack: Ref. Id. SSC/Q0508)


Sector: Information Technology- Information Technology
Enables Services (IT-ITeS)
Textbook for Class XII

PSS CENTRAL INSTITUTE OF VOCATIONAL EDUCATION


Shyamla Hills, Bhopal – 462 002, M.P., India
www.psscive.ac.in
Page 2 of 200
TEXTBOOK DEVELOPMENT COMMITTEE

MEMBERS
1. Akshay Sharam, Govt. Hss Girls Narsinghgarh, Rajgarh, AISECT.
2. Aakrati Verma, Govt. Subhash Excellence, Bhopal, AISECT.
3. Amit Sondhiya, Govt. HSS Garh, Rewa, ICA.
4. Prakash Deshmukh, Govt. Hss Dharnakala, Seoni, Satyam.
5. Shashank Dhuper, Govt. HSS Girls PT. Ravishankar Sagar, Satyam.
Job Role – Junior Software Development for Class-XII. There are 05 Vocational Trainers from
SAMGRA SIKSHA ABHIYAN (Secondary Education) Bhopal M.P. attached to PSSCIVE Bhopal for
05 days between 5 June 2023 to 9 June 2023.
GUIDELINE MEMBER
Prerna Kumari, Assistant Professor (Contractual), Department of Engineering and Technology,
PSSCIVE, NCERT, Bhopal.

Page 3 of 200
CONTENTS

Unit1. Relational Database Management System

Chapter 1. RDBMS Concepts

Chapter 2. Structured Query Language (SQL)

Chapter 3. FUNCTIONS IN SQL

Unit 2. Advanced Python Programming

Chapter 4. File Handling in Python

Chapter 5. Exception Handling in Python

Chapter 6. Implement data structure using python

Unit 3. Software Engineering

Chapter 7. Software Engineering Concepts

Chapter 8. Software Testing

Chapter 9. Software project using python

Chapter 10. Contact Management System

Unit 4. Emerging Trends and Social Impact

Chapter 11. Emerging Trends and New Technology

Chapter 12. Social impact on technology

Page 4 of 200
Unit1. Relational Database Management System
Chapter 1. RDBMS Concepts
Kushal was playing with his father’s mobile phone. Accidentally all the contacts on the phone were
deleted by him. To recover these contacts his father consulted a technician. The technician recovered
all the contacts available using the Google contacts as these contacts are linked with the Google
account. Thus, it is possible to save and manage the contacts in a smartphone. This is possible with
the use of database applications available with Google. The data can be saved and managed through
database application.
In this session, you will understand the concept of the database and how data is organised in the
database. Data models and the various concepts associated with database application such as
constraints, primary key, foreign key is also discussed. There are various database management
software available in the market. We will discuss MySQL which is a popular Relational Database
Management System (RDBMS).
1.1 INTRODUCTION TO DATABASE SYSTEMS
The word data is taken from "Datum", which means raw facts. Datum is a single piece of factual
information of interest to us. Data, the plural of datum, is a collection of information. Data is the
name given to basic raw facts and entities such as names, numbers and quantity. Its data can be
defined as a collection of facts and records. Data, representing facts, figures, and ideas, are commonly
used in everyday life. it needs to be managed to use it effectively.
Data items are organised or processed to produce the information. It can be used for processing some
useful information from it. The examples of data are weights, prices, costs, numbers of items sold,
employee names, product names, addresses, tax codes, registration fees, obtained marks, reservation
details, images, sounds, multimedia and animated data. Data can exist in the form of text, graphics,
sound, video that represents every kind of information. The data items can be stored manually in a
diary. But it is difficult to retrieve and process the data items when there are a large number of data
items. Through the relational database systems, users can access a view of data called relations.
With Relational Database Management Systems (RDBMS), programmers can perform database
operations without knowing data storage details.
It is observed that the schools are maintaining the student data and that is stored in the register.
After several years of leaving the school, the student can get the duplicate of the school leaving
certificate. For issuing such certificates the office staff check the student data from the registers
which is maintained year-wise and class-wise. By checking the student record, the office staff can
easily give the certificate to the student. This is how the schools are maintaining the student data in
the register.
The office staff also manually maintain student details who are presently learning. Their Admission
number, Name, Date of Birth, Address, Contact Number are stored in the school register.
There are two major types of databases – Relational and Non-relational. Relational databases are the
most commonly used databases today. The following are several types of databases in use: -
Flat file databases – Stores data in permanent files that mostly are in text form;
Hierarchical databases – Arranges data in a tree-like structure;
Network database – Arranges data in network-like structure;
Relational databases – Contains a set of tables in which data are related;
Object databases – Represents information in the form of objects as used in object-oriented
programming.
1.2 FILE SYSTEM
Now you must have noticed that maintaining such types of records manually does not allow you to
correct, modify or delete the data in the register. Also, searching the details of the student is difficult.

Page 5 of 200
To overcome the hassles faced in manual record keeping, this data can be stored in a computer. The
student details are stored in the computer in the form of a separate file.
In computers, any contents are stored in the form of a file, which is opened and viewed in the
respective software. In a computer, a file is a container to store data or information. These files are
stored on the storage device of the computer, such as a hard disk drive or pen drive.
The student data can be stored in the document file or spreadsheet file. These files stored on the
computer can be accessed quickly. To process or manipulate this data, it is required to write the
program in computer programming languages. Various operations can be performed through
computer programming. It includes searching, sorting, computing the percentage of marks, number
of days attendance and retrieving the data.
1.2.1 Limitations of a File System
There are certain limitations to maintain and manipulate such types of data when there are several
hundreds or thousands of students. It also becomes difficult to maintain the number of files as it
increases the volume when data grows. There are certain limitations of the file system to maintain
such type of data. The limitations of file system are:
Difficulty in Access – Files themselves do not provide any mechanism to retrieve data. Data
maintained in a file system is accessed through application programs. While writing such programs,
the developer may not anticipate all the possible ways in which data may be accessed. So, sometimes
it is difficult to access data in the required format and one has to write an application program to
access data.
Data Redundancy – Redundancy means the same data is duplicated in different files. For example,
if we are maintaining student’s data for various purposes then data such as student names are
maintained in different files. The common data in all such files are required to be maintained a
number of times. This may cause data redundancy which is difficult to avoid in a file system.
Redundancy leads to excess storage use and may cause data inconsistency also.
Data Inconsistency – Data inconsistency occurs when the same data maintained in different places
do not match. If the student wants to change the spelling of their name, it needs to be changed in
the number of files where it appears. Likewise, if a student leaves school, the details need to be
deleted from these files. As the files are being maintained by different people, the changes may not
happen in one of the files. In that case, the student name will be different (inconsistent) in both the
files.
Data Isolation – Although these files are maintained for the students of the same class, there is no
link or mapping between these files. The school will have to write separate programs to access these
files. This is because data mapping is not supported in the file system. In a more complex system
where data files are generated by different people at different times, files being created in isolation
may be of different formats. In such cases, it is difficult to write new application programs to retrieve
data from different files.
Data Dependence – Data is stored through specific format or structure in a file. If the structure or
format itself is changed, all the existing application programs accessing that file also need to be
changed. Otherwise, the programs may not work correctly. This is data dependency. Hence, updating
the structure of a data file requires modification in all the application programs accessing that file.
Controlled Data Sharing – There can be different categories of users like teachers, office staff and
parents. Ideally, not every user should be able to access all the data. It means different types of users
should be given different types of access, such as read only. It is very difficult to enforce this kind of
access control in a file system while accessing files through application programs.
1.3 DATABASE MANAGEMENT SYSTEM
Limitations faced in file systems can be overcome by storing the data in a database where data are
logically related. The Database Management Systems (DBMSs) are used as an interface to manage
databases.

Page 6 of 200
A database is an organised collection of data, generally stored and accessed electronically from a
computer system. It supports the storage and manipulation of data. In other words, databases are
used by an organisation as a method of storing, managing and retrieving information. It is possible
to store and organise related data in a database so that it can be managed in an efficient and easy
way.
A DBMS is a collection of software components designed to create and maintain databases and
control all access to them. It used to create a database, store, manage, update/modify and retrieve
data from that database by users or application programs. It is used to provide an effective method
of performing database operations, troubleshooting database issues, and restricting data access.
Relational Database Management System (RDBMS), which is still popular today, is an advanced
version of a DBMS system. Dr. E. F. Codd defined the criteria to determine whether a DBMS is a
relational database management system or not. These criteria are known as Codd’s twelve rules. (E.
F. Codd, 1985).
Some examples of open source and commercial DBMS include MySQL, Oracle, PostgreSQL, SQL
Server, Microsoft Access, MongoDB etc. as presented in Table 1.1.
Table 1.1 Popular DBMS
DBMS Primary Database Model Licence
Oracle RDBMS Commercial (restricted free
version is available)
MySQL RDBMS Open Source
Microsoft SQL Server RDBMS Commercial (restricted free
version is available)
PostgreSQL RDBMS Open Source
MangoDB Document store Open Source

Fig: 1.1 Different types of DBMS/RDBMS available in market

Some database management systems include a graphical user interface for users to create and
manage databases. Other database systems use a command line interface that requires users to
use programming commands to create and manage databases.

A database management system hides certain details about how data are actually stored and
maintained. Thus, it provides users with an abstract view of the data. A database system has a set
of programs through which users or other programs can access, modify and retrieve the stored data.
The DBMS serves as an interface between the database and end users or application programs.
Retrieving data from a database through a special type of commands is called Querying the database.
In addition, users can modify the structure of the database itself through a DBMS.
Databases are widely used in various fields. Some applications are given in Table 1.2.

Page 7 of 200
Table 1.2 Use of Database in Real-life Applications

Application Database to maintain data about

Banking Customer information, account details, loan details,


transaction details etc.

Crop Loan Kisan credit card data, farmer’s personal data, land area
and cultivation data, loan history, repayment data etc.

Inventory Management of product details, customer information,


order details, delivery data etc.

Organisation Resource Management Employee records, salary details, information, branch


locations etc.

Online Shopping Items description, user login details, users


preferences, details etc.

1.3.1 Limitations of DBMS


Increased Complexity – Use of DBMS increases the complexity of maintaining functionalities like
security, consistency, sharing and integrity.
Increased data vulnerability – As data are stored centrally, it increases the chances of loss of data
due to any failure of hardware or software. It can bring all operations to a halt for all the users.
1.3.2 Application of the DBMS
following are important applications of the DBMS:
● Student Admission System, School Examination System, Library Management System
● Payroll, HR, Sales and Personnel Management System
● Accounting System, Hotel Reservation System and Airline Reservation System
● It is used in the Banking system for Customer information, account activities, Payments,
deposits, loans etc.
● Insurance management system.
● DBMS is also used by universities to keep all records.
● Financial purposes like storing information about, sales, and purchases of financial instruments
like stocks and bonds.
1.3.2 Advantages of DBMS
The advantages of DBMS are:
● It offers a variety of techniques to store and retrieve data.
● Uniform administration procedures for data storage and retrieval.
● Application programmers are never exposed to details of data representation and Storages.
● A DBMS uses various powerful functions to store and retrieve data efficiently.
● Offers Data independence, Data Integrity and Data Security and reduces data redundancy.
● The DBMS implies integrity constraints to get a high level of protection against prohibited access
to data.
● Reduced application development Time and occupies less space.
1.3.4 Disadvantages of the DBMS
The disadvantages of DBMS are:

Page 8 of 200
● Cost of Hardware and Software of a DBMS is quite high, which increases the budget of your
organisation.
● Most database management systems are often complex systems, so the training for users to use
the DBMS is required.
● The use of the same program at a time by many users sometimes lead to the loss of some data.
● DBMS can't perform sophisticated calculations.
● Data-sets begin to grow large as it provides a more predictable query response time.
● It required a processor with a high speed of data processing.
● The database can fail because of power failure or the whole system stops.
● The cost of DBMS is dependent on the environment, function, or recurrent annual maintenance
cost.
1.3.5 Comparison of Database Management System (DBMS) with File System
The comparative points of DBMS with File System are given in Table 1.3.
Table 1.3: Comparison of DBMS with File System
File System DBMS

A file system is a software that manages and DBMS or Database Management System is a
organises the files in a storage medium. It software application. It is used for accessing,
controls how data is stored and retrieved. creating, and managing databases.

The file system provides the details of data DBMS gives an abstract view of data that hides
representation and storage of data. the details.

Storing and retrieving of data can't be done DBMS is efficient to use as there are a wide
efficiently in a file system. variety of methods to store and retrieve data.

It does not offer data recovery processes. There is a backup recovery for data in DBMS.

The file system doesn't have a crash recovery DBMS provides a crash recovery mechanism
mechanism.

Protecting a file system is very difficult. DBMS offers a good protection mechanism.

In a file management system, the redundancy of The redundancy of data is low in the DBMS
data is greater. system.

Data inconsistency is higher in the file system. Data inconsistency is low in a database
management system.

The file system offers lesser security. Database Management System offers high
security.

File System allows you to store the data as Database Management System stores data as
isolated data files and entities. well as defined constraints and interrelation.

It does not provide support for complicated Easy to implement complicated transactions.
transactions.

The centralization process is hard in the File Centralization is easy to achieve in the DBMS
Management System. system.

It doesn't offer backup and recovery of data if it DBMS systems provide backup and recovery of
is lost. data even if it is lost.

There is no efficient query processing in the file You can easily query data in a database using

Page 9 of 200
system. the SQL language.

This system doesn't offer concurrency. DBMS system provides a concurrency facility.
1.4 Key Concepts in DBMS
It is important to understand the following concepts to efficiently manage data using a DBMS.
(A) Database schema
A database schema is a set of schemas for a database's relations. It consists of tables with all
attributes with their data types and constraints if any. It also represents the relationships among the
tables. It is used to visualise the logical architecture of a database and how the data are organised in
a database. The schema of a relation may not change, but the relation, which is a variable, changes
over time.

Fig: 1.2 Database schema for STUDENTATTENDANCE system


(B) Data Constraint
Sometimes, it is required to put certain restrictions or limitations on the type of data to be inserted
in the columns of a table. This is done by specifying constraints on that column(s) while creating the
tables. For example, the constraint that the column mobile number can only have non-negative integer
values of exactly 10 digits. Since each student shall have one unique roll number, we can put the
NOT NULL and UNIQUE constraints on the Roll Number column. Constraints are used to ensure
accuracy and reliability of data in the database
(C) Meta-data or Data Dictionary
The database schema along with various constraints on the data is stored by DBMS in a database
catalogue or dictionary, called meta-data. A meta-data is data about the data.
(D) Database Instance
When we define a database structure or schema, the state of the database is empty. After loading
data, the state or snapshot of the database at any given time is the database instance. We may then
retrieve data through queries or manipulate data through updating, modification or deletion. Thus,
the state of the database can change, a database schema can have many instances at different times.
(E) Query
A query is a request to a database for obtaining information in a desired way. Query can be made to
get data from one table or from a combination of tables. For example, “find names of all those students
present today” is a query to the database. To retrieve or manipulate data, the user needs to write a
query using a query language called Structured Query Language (SQL).
(F) Data Manipulation
Modification of database consists of three operations viz. Insertion, Deletion or Updating. Suppose
Rivaan joins as a new student in the class then the student details need to be added in
StudentRecord as well as in ParentRecord files of the STUDENTATTENDANCE database. This is

Page 10 of 200
called an Insertion operation on the database. In case a student leaves the school, then student as
well as parent data need to be removed from StudentRecord, ParentRecord and AttendanceRecord
tables, respectively. This is called Deletion operation on the database. Suppose Rivaan’s Parent has
changed his mobile number, his Par_Phone should be updated in the ParentRecord file. This is called
an Update operation on the database.
(G) Database Engine
Database engine is the underlying component or set of programs used by a DBMS to create a
database and handle various queries for data retrieval and manipulation.
1.5 RELATIONAL DATA MODEL
A data model describes the structure of the database and represents data. It defines and represents
relationships among relations. In database design, first the conceptual data model is designed for
non-technical users. Then, based on the conceptual data models, the logical data models are designed
by the technical users. It represents how to store and retrieve data logically. Finally, the logical design
models are converted into physical data models that show all table structures. Relational data models
are the most commonly used data model. So, in this topic we will focus on relational data models.
1.5.1 Key terms in Relational Data Model
In relational data models, tables are called relations that store data for different entities. Each relation
in a relational model represents a specific type of entity. An entity is an object and we store data
about the object. In other words, a relation is a two-dimensional table used to store data.
Let us consider the relational database SCHOOLRECORD along with the three relations (tables)
StudentRecord, AttendanceRecord and ParentRecord, as shown in Figure 1.3.

Fig. 1.3 Representation SCHOOLRECORD database using Relational Data Model


Assume that, a relation AttendanceRecord has attribute Stu_RollNo which links it with
corresponding student record in relation StudentRecord. Similarly, attribute Par_ID is placed with
the StudentRecord table for extracting parent details of a particular student. If linking attributes
are not there in appropriate relations, it will not be possible to keep the database in the correct state
and retrieve valid information from the database.
Table 1.4 Relation schema along with its description of Student Attendance database
Relation Schema Description of attributes
StudentRecord Stu_RollNo : unique id of the student
(Stu_RollNo, Stu_FName : First name of the student
Stu_FName, Stu_LName : Last name of the student
Stu_LName, Stu_DOB, Stu_DOB : Student’s date of birth
Stu_Address,
Stu_Address : Home address of the student
Par_ID )
Par_ID : unique id of the parent of the student

Page 11 of 200
AttendanceRecord Att_Date : date on which attendance is taken
(Att_Date, Stu_RollNo : roll number of the student
Att_Status : Either P (for present) or A (for absent)
Stu_RollNo,
Note: Combination of Att_Date and Stu_RollNo will be
Att_Status)
unique in each record of the table
ParentRecord Par_ID : unique id of the parent
(Par_ID, Par_Name : Name of the parent
Par_Phone : Contact number of the parent
Par_Name,
Par_Address : Address of the parent
Par_Phone, Par_Email : Email id of the parent
Par_Address,
Par_Email)
Each tuple (row) in a relation (table) corresponds to data of a real-world entity as in StudentRecord,
ParentRecord, and AttendanceRecord. In the ParentRecord relation (Table 1.4), each row
represents the facts about the parent and each column name in the ParentRecord table is used to
interpret the meaning of data stored under that column. A database that is modelled on relational
data model concept is called Relational Database. Figure 1.5 shows relation ParentRecord with some
populated data.
Let us now understand the commonly used terminologies in relational data models using Figure 1.4.

Fig 1.4 Relation ParentRecord with its attributes (Columns) and tuples (Rows)
Attribute – Characteristic or parameters for which data are to be stored in a relation. Simply stated,
the columns of a relation are the attributes which are also referred to as fields. For example, Par_ID,
Par_Name, Par_Phone and Par_Address are attributes of relation ParentRecord.
Tuple – Each row of data in a relation (table) is called a tuple. In a table with ‘n’ columns, a tuple is
a relationship between the ‘n’ related values.

Page 12 of 200
Domain – It is a set of values from which an attribute can take a value in each row. Usually, a data
type is used to specify a domain for an attribute. For example, in StudentRecord relation, the
attribute Stu_RollNo takes integer values and hence, its domain is a set of integer values. Similarly,
the set of character strings constitutes the domain of the attribute Stu_Fname.
Degree – The number of attributes in a relation is called the ‘Degree’ of the relation. For example,
relation ParentRecord with four attributes is a relation of degree five.
Cardinality – The number of tuples in a relation is called the Cardinality of the relation. For example,
the cardinality of relation ParentRecord is 10 as there are 10 tuples in the table.
1.5.2 Three Important Properties of a Relation
In relational data model, following three properties are observed with respect to a relation which
makes a relation different from a data file or a simple table.
Property 1: Imposes following rules on an attribute of the relation:
a) Each attribute in a relation has a unique name.
b) Sequence of attributes in a relation is immaterial.
Property 2: Governs following rules on a tuple of a relation:
• Each tuple in a relation is distinct. For example, data values in no two tuples of relation
AttendanceRecord can be identical for all the attributes. Thus, each tuple of a relation must be
uniquely identified by its contents.
• Sequence of tuples in a relation is immaterial. The tuples are not considered to be ordered, even
though they appear to be in tabular form.
Property 3: Imposes following rules on the state of a relation:
• All data values in an attribute must be from the same domain (same data type).
• Each data value associated with an attribute must be atomic (cannot be further divisible into
meaningful subparts). For example, Par_Phone of relation ParentRecord has ten digits numbers
which are indivisible.
• No attribute can have many data values in one tuple. For example, any Parent cannot specify
multiple contact numbers under the Par_Phone attribute.
• A special value “NULL” is used to represent values that are unknown or non-applicable to certain
attributes. For example, if a parent does not share his or her contact number with the school
authorities, then Par_Phone is set to NULL (data unknown).
1.6 KEYS IN A RELATIONAL DATABASE
The tuples within a relation must be distinct. It means no two tuples in a table should have the same
value for all attributes. That is, there should be at least one attribute in which data are distinct
(unique) and not NULL. That way, we can uniquely distinguish each tuple of a relation. So, relational
data models impose some restrictions or constraints on the values of the attributes and how the
contents of one relation can be referred through another relation. These restrictions are specified at
the time of defining the database through different types of keys as given below:
1.6.1 Candidate Key
A relation can have one or more attributes that take distinct values. Any of these attributes can be
used to uniquely identify the tuples in the relation. Such attributes are called ‘candidate keys’ as
each of them are candidates for the primary key.
As shown in Figure 1.5, the relation ParentRecord has five attributes out of which Par_ID and
Par_Phone always take unique values. No two parents will have the same phone number or same
Par_ID. Hence, these two attributes are the candidate keys as they both are candidates for primary
key.

Page 13 of 200
1.6.2 Primary Key
Out of one or more candidate keys, the attribute chosen by the database designer to uniquely identify
the tuples in a relation is called the primary key of that relation. The remaining attributes in the list
of candidate keys are called the ‘alternate keys.
In the relation ParentRecord, suppose Par_ID is chosen as primary key, then Par_Phone will be
called the alternate key.
1.6.3 Composite Primary Key
If no single attribute in a relation is able to uniquely distinguish the tuples, then more than one
attribute is taken together as the primary key. Such a primary key consisting of more than one
attribute is called ‘Composite Primary key’. In relation to AttendanceRecord, Roll Number cannot
be used as primary key as roll number of same students will appear in another row for a different
date. Similarly, in relation to AttendanceRecord, ‘Att_Date’ cannot be used as the primary key
because the same date is repeated for each roll number.
However, combination of these two attributes Stu_RollNo and Att_Date together would always have
unique value in the AttendanceRecord table as on any working day, a student would be marked
attendance only once. Hence {Stu_RollNo, Att_Date} will combine to make the AttendanceRecord
relation composite primary key.
1.6.4 Foreign Key
A foreign key is used to represent the relationship between two relations. A foreign key is an attribute
whose value is derived from the primary key of another relation. This means that any attribute of a
relation (referencing), which is used to refer contents from another (referenced) relation, becomes
foreign key if it refers to the primary key of the referenced relation. The referencing relation is called
‘Foreign Relation’. In some cases, foreign keys can take NULL value if it is not the part of the primary
key of the foreign table.
The relation in which the referenced primary key is defined is called ‘primary relation’ or ‘master
relation’. In Figure 1.5, two foreign keys in STUDENTATTENDANCE database are shown using a
schema diagram where the foreign key is displayed as a directed arc (arrow) originating from it and
ending at the corresponding attribute of the primary key of the referenced table. The underlined
attributes make the primary key of that table.

Fig. 1.5 StudentAttendance Database with the Primary and Foreign keys

Page 14 of 200
Chapter 2. Structured Query Language (SQL)
Once the result date was declared, Shyam was eager to see the result on the website. He opened the
website to enter his Roll number to see the result. After entering the Roll number, he pressed the OK
button. Immediately the score card of Shyam got displayed on the screen and passed with first
division marks. He was very happy and also surprised, how a computer searches the Roll number so
fast among approximately 5 lacs students records. Later on, Shyam understood that it was possible
because of the database query language which is also known as Structured Query Language (SQL).
SQL is used to search, store, modify records in database management systems. In this chapter, you
will understand how to create database objects, insert data in the database and various types of
commands used to retrieve the required data from the database.

Fig. 2.1 Checking result online


2.1 Structured Query Language (SQL)
In the file system it is required to write programs to access data. However, in DBMS there exists a
Structured Query Language (SQL), which is a special kind of query language used to access and
manipulate data from the database. It is the most popular query language used by major relational
database management systems (RDBMS), such as MySQL, Oracle, Informix, PostGreSQL, SQL server,
MS Access, and Sybase.
SQL is easy to learn as the statements comprise descriptive English words. It is possible to interact
with a database using SQL very easily. It is simply required to specify what is to be retrieved rather
than how to retrieve data from the database. SQL provides statements for defining the structure of
data, manipulating data in the database, declaring constraints and retrieving data from the database
in various ways, depending on requirement.
2.1.1 Installing MySQL
MySQL is an open source RDBMS software which can be easily downloaded from its official website
https://dev.mysql.com/downloads. After installing MySQL, start MySQL service. If the screen will so
mysql> prompt, then MySQL is ready to accept SQL statements on this prompt.

Fig. 2.2: MySQL Shell


Following are some important points to be kept in mind while using SQL:

Page 15 of 200
● SQL is not case insensitive. For example, the column names ‘salary’ and ‘SALARY’ are the same
for SQL.
● SQL statements terminate with a semicolon (;). In multi-line SQL statements, the “;” is not required
after the first line. Just press the ‘Enter key’ to continue on the next line. The prompt mysql> then
changes to “->”, indicating that statement is continued to the next line. Only at the end of the SQL
statement, put “;” and press Enter.
2.2 Data Types and Constraints in MySQL
We know that a database consists of one or more relations and each relation (table) is made up of
attributes (column). Each attribute has a data type. It is also possible to specify constraints for each
attribute of a relation.
2.2.1 Data type of Attribute
Data type of an attribute indicates the type of data value that an attribute can have. It also decides
the operations that can be performed on the data of that attribute. For example, arithmetic operations
can be performed on numeric data but not on character data. Commonly used data types in MySQL
are numeric types, date and time types, and string types as shown in Table 2.1.
Table 2.1 Commonly used data types in MySQL
Data Type Description

CHAR (n) Specifies character type data of length ‘n’ where ‘n’ could be any value from 0 to
255. ‘CHAR’ is of fixed length, which means, declaring CHAR (10) implies to reserve
spaces for 10 characters. If data does not have 10 characters (for example, ‘city’
has four characters), MySQL fills the remaining 6 characters with spaces padded
on the right.

VARCHAR (n) Specifies character type data of length ‘n’ where n could be any value from 0 to
65535. But unlike CHAR, VARCHAR is a variable-length data type. That is,
declaring VARCHAR (30) means a maximum of 30 characters can be stored but
the actual allocated bytes will depend on the length of the entered string. So ‘city’
in VARCHAR (30) will occupy the space needed to store 4 characters only.

INT ‘INT’ specifies an integer value. Each INT value occupies 4 bytes of storage. The
range of values allowed in integer type are -2147483648 to 2147483647. For values
larger than that, we have to use BIGINT, which occupies 8 bytes.

FLOAT Holds numbers with decimal points. Each FLOAT value occupies 4 bytes.

DATE The DATE type is used for dates in 'YYYY-MM-DD' format. YYYY is the 4 digits year,
MM is the 2 digits month and DD are the 2 digits date. The supported range is
'1000-01-01' to '9999-12-31'.
2.2.2 Constraints
Constraints are the certain types of restrictions on the data values that an attribute can have. Table
2.2 lists some of the commonly used constraints in SQL. They are used to ensure correctness of data.
However, it is not mandatory to define constraints for each attribute of a table.
Table 2.2 Commonly used SQL Constraints

Constraint Description

Ensures that a column cannot have NULL values where NULL means missing/
NOT NULL
unknown/not applicable value.

UNIQUE Ensures that all the values in a column are distinct/unique.

Page 16 of 200
DEFAULT A default value specified for the column if no value is provided.

PRIMARY KEY The column which can uniquely identify each row or record in a table.

FOREIGN The column which refers to the value of an attribute defined as the primary key in
KEY another table.
2.2.3 Types of Structured Query Language (SQL)
SQL is a standardised language used for making communication with relational databases and
performing various operations on it. According to ANSI (American National Standards Institute), it is
the standard language for relational database management systems. SQL statements are used to
perform tasks such as insert, update and delete data in any database. On the basis of different types
of operation, SQL commands are divided into five categories:
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Query Language (DQL)
4. Transaction Control Language (TCL)
5. Data Control Language (DCL)

Fig. 2.3: Types of SQL command


2.3 SQL – Data Definition Language (DDL)
It is first necessary to define the relation schema to store data in the database. Defining a schema
includes creating a relation and giving name to a relation, identifying the attributes in a relation,
deciding upon the datatype for each attribute and also specifying the constraints as per the
requirements. Sometimes, it may require making changes to the relation schema also. SQL provides
commands for defining the relation schema, modifying relation schema and deleting relations. These
are called Data Definition Language (DDL).
As you know that the data are stored in relations or tables in a database. Database is a collection of
database objects such as tables, queries and views. The CREATE statement is used to create a
database and its tables (relations). Before creating a database, it should be clear about the number
of tables in the database, the columns (attributes) in each table along with the data type of each
column. This is how we decide the relation schema. This category of SQL provides a set of commands
to create the database structure or schema.
2.3.1 CREATE Database
This SQL command is used to create various database objects. The syntax and example for creating
a database is given below.

Page 17 of 200
Syntax:
CREATE DATABASE databasename;
Example 2.1: The following command is used to create a database with the name “SchoolRecord”.
mysql> CREATE DATABASE SchoolRecord;
After successful execution of the command a message “Query OK” is displayed on the sql prompt. It
is also possible to see the newly created database by using the “show” command. The show command
displays the newly created database along with some default databases of MySQL as shown in Figure
2.4.

Note: In any RDBMS, it is possible to manage multiple databases on a single computer. USE
command is used to select the specific database. After selecting the database, it is possible to create
tables or querying data from this database.

To select the database SchoolRecord, issue the “USE” command followed by the database name.
Note: In LINUX OS environment, names for database and tables are case-sensitive whereas in
WINDOWS OS, there is no such differentiation. However, as a good practice, it is suggested to write
database or table names in the same letter cases that were used at the time of their creation.
2.3.2 CREATE Table
After creating database SchoolRecord, it is required to define relations (create tables) in this
database. In each relation specify attribute (column name) for each attribute with their required data
types. The syntax for the CREATE TABLE statement is as follows.
Syntax:
CREATE TABLE tablename (
Col_name1 datatype constraint,
Col_name2 datatype constraint,
:

Page 18 of 200
Col_nameN datatype constraint);
Let us understand how to choose attribute names and their respected data types. First identify data
types of the attributes in table “StudentRecord” along with their constraint, if any. Let us assume
that there is a total of 100 students in a class and values of Roll number are in a sequence from 1 to
100. Since the data values of attribute “Stu_RollNo” is stored in digits, the data type integer (INT) is
appropriate for this attribute. In the same way, the total number of characters in student First name
and Last name can be up to 20 characters. Since the number of characters can vary for different
students, the data type VARCHAR is used for these columns. The data type VARCHAR is used for
student addresses up to 50 characters in length. The specific data type DATE is used for specifying
any type of date. So, DATE data type is used for attribute “Date of Birth”. For student's parent ID,
Aadhaar number is used which is a 12-digit number. Since the Aadhaar number is of fixed length
and it is not required to perform any mathematical operation, the character data type with fixed
length of 12-character, CHAR (12) is used for this attribute.
Table 2.3 Data types and constraints for the attributes of relation StudentRecord
Attribute Data expected to be stored Data type Constraint

Stu_RollNo Numeric value consisting of maximum 3 digits Int Primary Key

Stu_FName Variable length string of maximum 20 characters Varchar (20) Not Null

Stu_LName Variable length string of maximum 20 characters Varchar (20) Not Null

Stu_DOB Date value Date Not Null

Stu_Address Variable length string of maximum 50 characters Varchar (50) Not Null

Fixed length string of 12 digits for Aadhaar


Par_ID Char (12) Foreign Key
Number
Table 2.4 Data types and constraints for the attributes of relation ParentRecord
Attribute Data expected to be stored Data type Constraint

Par_ID Fixed length string of 12 digits Aadhaar number Char (12) Primary Key

Par_Name Variable length string of maximum 20 characters Varchar (20) Not Null

Par_Phone Numeric value consisting of 10 digits Char (10) Null Unique

Par_Address Variable length string of size 30 characters Varchar (30) Not Null

Par_Email Variable length string of size 30 characters Varchar (30)


Table 2.5 Data types and constraints for the attributes of relation AttendanceRecord
Attribute Data expected to be stored Data type Constraint

Att_Date Date value Date Primary Key*

Primary Key*
Stu_RollNo Numeric value consisting of maximum 3 digits Int
Foreign Key

Att_Status ‘P’ for present and ‘A’ for absent Char(1) Not Null
Table 2.3, 2.4 and 2.5 show the chosen data type and constraint for each attribute of the relations
StudentRecord, ParentRecord and AttendanceRecord respectively.
Example 2.2: The following command is used to create table StudentRecord. To create the table in
the SchoolRecord database, first open the database with USE SchoolRecord command. Then create
the table under the StudentRecord database by using the CREATE TABLE command.

Page 19 of 200
Note: “,” is used to separate two attributes and each statement terminates with a semicolon (;). The
arrow (->) is an interactive continuation prompt. If we enter an unfinished statement, the SQL shell
will wait for us to enter the rest of the statement.
Example 2.3: The following command is used to Create table ParentRecord.

Example 2.4: The following command is used to Create table AttendanceRecord.

2.3.3 DESCRIBE Table


DESCRIBE or DESC command is used to view the structure of an already created table.
Syntax:
DESCRIBE tablename;
Example 2.5: The following SQL command is used to show the structure of the StudentRecord table.

Page 20 of 200
The SHOW TABLES statement is used to display all the tables in the database. We have created three
tables in the database SchoolRecord.
Example 2.6: The following SQL command is used to display the tables created in the database
SchoolRecord. It shows all the three tables created so far.

2.3.4 ALTER Table


After creating a table, it is possible to add or remove an attribute or modify the datatype of existing
attribute or to add constraint in attribute. ALTER statement is used to change or alter the structure
of the table.
Syntax:
ALTER TABLE tablename Add/Modify/Drop attribute1, attribute2,..
(a) Add primary key to a relation
Example 2.7: The following SQL command is used to add a primary key to the relation

“ParentRecord”
A composite primary key is made up of two attributes. The primary key to the “AttendanceRecord”
relation will be the composite primary key of two attributes. “AttendanceDate” and “Stu_RollNo”.
Example 2.8: The following SQL command is used to add the composite primary key to the relation
“AttendanceRecord”.

(b) Add foreign key to a relation


It is also possible to add foreign keys to the relation, if any. A relation may have multiple foreign keys
and each foreign key is defined on a single attribute. Note the following points while adding foreign
keys to a relation.
• The referenced relation must be already created.
• The referenced attribute must be a part of the primary key of the referenced relation.
• Data types and size of referenced and referencing attributes must be the same.

Page 21 of 200
Syntax:
ALTER TABLE table_name ADD FOREIGN KEY (attribute name)
REFERENCES referenced_table_name (attribute name);
Let us now add foreign key to the table StudentRecord.
In table “StudentRecord”, the attribute Par_ID (the referencing attribute) is a foreign key and it
refers to attribute Par_ID (the referenced attribute) of table “ParentRecord”. Hence, “StudentRecord”
is the referencing table and “ParentRecord” is the referenced table.
Example 2.9: The following SQL command is used to add the foreign key. The ALTER statement
changes the table StudentRecord.

(c) Add constraint unique to an existing attribute


In the “ParentRecord” table, attribute “Par_Phone” has a constraint UNIQUE, meaning no two values
in that column should be the same.
Syntax:
ALTER TABLE table_name ADD UNIQUE (attributename);
Example 2.10: The following SQL command is used to add the constraint UNIQUE with attribute
“Par_Phone” of the table “ParentRecord”.

(d) Add an attribute to an existing table


Sometimes, it is required to add an additional attribute in a table. The syntax for this is.
Syntax:
ALTER TABLE table_name ADD attribute_name DATATYPE;
Suppose the Principal of the school has decided to award scholarships to some needy students for
which the income of the parents must be known. But the school has not maintained an income
attribute with table “ParentRecord” so far.
Example 2.11: The following command is used to add a new attribute income of data type INT in the
table “ParentRecord”.

Page 22 of 200
The newly added attribute “income” with data type INT in the table “ParentRecord” can be viewed
using the DESC command as follows.

(e) Modify datatype of an attribute


It is possible to modify the data types of the existing attributes of a table using the following
statement.
Syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE;
Suppose, to change the size of attribute “Par_Address” from VARCHAR (30) to VARCHAR (40) of the
“ParentRecord” table.
Example 2.12: The following command is used to change the size of attribute “Par_Address” in
the“ParentRecord” table.

(f) Modify constraint of an attribute


When creating a table, by default each attribute takes null value except for the attribute defined as
primary key. It is possible to change an attribute’s constraint from NULL to NOT NULL using the
ALTER statement.
Syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE NOT NULL;
Note: It is required to specify the data type of the attribute along with constraint NOT NULL while
using MODIFY.
Example 2.13: The following command is used to associate NOT NULL constraint with attribute
“Stu_FName” of table “StudentRecord”.

(g) Add default value to an attribute


The syntax to specify the default value for an attribute is,

Page 23 of 200
Syntax:
ALTER TABLE table_name MODIFY attribute
DATATYPE DEFAULT default_value;
To set the default value of “Stu_DOB” of “StudentRecord” to 15th May 2000, write the following
statement.

Note: It is required to specify the data type of the attribute along with DEFAULT while using MODIFY.
(h) Remove an attribute
It is possible to remove attributes from a table using ALTER.
Syntax:
ALTER TABLE table_name DROP attribute;
Example 2.14: The following command is used to remove the attribute income from the table
“ParentRecord”.

(i) Remove primary key from the table


Sometimes it may be required to remove the primary key constraint from the table. In such a case,
the syntax for ALTER TABLE command is.
Syntax:
ALTER TABLE table_name DROP PRIMARY KEY;
Example 2.15: The following command is used to remove primary key of table “ParentRecord”

Note: The primary key is dropped from the StudentRecord table, but each table should have a
primary key to maintain uniqueness. Hence, to use ADD command to specify primary key for the
StudentRecord table as shown in earlier examples
2.3.5 DROP TABLE Command
Sometimes it may require removing a table in a database or the database itself. DROP statement is
used to remove a database or a table permanently from the system. Since this command will delete
the table or database permanently, you have to be cautious while using this statement as it cannot
be undone. Let us assume that you have created a table with the name “ParantRecord” instead of
“ParentRecord”. DROP command can be used to delete the table created with the wrong name.

Page 24 of 200
Syntax:
DROP TABLE table_name;
It is also possible to drop the entire database.
Syntax:
DROP DATABASE database_name;
Example 2.16: The following command is used to delete the table name “ParantRecord” from the
current database.

Cautions:
1. Using the Drop statement to remove a database will ultimately remove all the tables within
it.
2. DROP statements will remove the tables or databases created by you. Hence you may apply
the DROP statement at the end of the chapter.

2.3.6 TRUNCATE TABLE Command


It is possible to remove all records from a table using the TRUNCATE command. Later on, you can
insert new records in the same table. This command will delete all records from the table but table
structure will exist in the database. While using DROP command, all the records with table structure
will be deleted from the database. So, care should be taken while using both TRUNCATE and DROP
commands in SQL.
Syntax:
Truncate Table Table_Name;
Example 2.17: The following command is used to Truncate the table “StudentRecordBackup”.

Page 25 of 200
2.3.7 CREATE TABLE from Existing Table
If you want to create a new table from an existing table with partial or additional fields, then you can
use the CREATE table command with the SELECT statement. The new table is created with the result
of the SELECT statement with results provided by it.
Syntax:
mysql> Create table NewTableName AS
(Select Field 1, Field 2, Field 3, …Field N from Old_Table_Name)
Example 2.18: The following command is used to Create table “NewStudentRecord” from the existing
table “StudentRecord”.

It will create a new table named “NewStudentRecord” with only 5 attributes and all the records which
were inserted in this table earlier.

Page 26 of 200
It is possible to create a new table with all attributes and all records available in the existing table.
Example 2.19: The following command is used to create a new table “StudentRecord1” with all
attributes and all records available in the existing table “StudentRecord”.

2.3.8 RENAME TABLE command


Sometimes it may be required to change the name of an existing table. It is possible to do so by using
the RENAME or ALTER command.
Syntax:
RENAME TABLE old_table_name TO new_table_name;
Example 2.20: The following command is used to rename the table “NewStudentRecord” to
“StudentRecord1”
mysql> RENAME TABLE NewStudentRecord TO StudentRecord1;
It is possible to rename multiple tables using a single command as under.
Syntax:
RENAME TABLE Old_tableA TO New_tableA, Old_tableB TO New_tableB, Old_tableC TO
New_tableC;
It is also possible to use the ALTER command to rename the table as given below.
Syntax:
ALTER TABLE Old_table_name RENAME TO new_table_name;
Example 2.21: The following is the command to alter the table “StudentRecord1” to “StudentRecord2”
mysql> ALTER TABLE StudentRecord1 RENAME TO StudentRecord2;

Page 27 of 200
2.3.9 CREATE VIEW command
Like table, view is another database object. It is a special kind of virtual table. It does not hold its
own data. A view can have rows and columns just like in a table. It is possible to create a view using
the CREATE VIEW command, by selecting fields from one or more tables present in the database. A
View can either have all the rows of a table (s) or specific rows based on certain criteria. The syntax
to create a view is as under.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2 .... columnN
FROM table_name WHERE condition;
Example 2.22: The following is the command to create a view from a single table.

Now the view named EMP_VIEW will be created with only those employee records who have a salary
more than 10000. You can use this view similar to the Employee table to see all records using the
SELECT command. To see all records from EMP_VEW, use the SELECT command as under.

Practical Activity 2.1 – Create the table “Employee” and “Department” in MySQL with the
following attributes specification:
Employee Table
Attribute Data expected to be stored Data type Constraint

empno Numeric value consisting of 4 digits Int Primary Key

ename Variable length string of max 30 characters Varchar (30) Not Null

job Variable length string of max 15 characters Varchar (15) Not Null

mgr Numeric value consisting of 4 digits Int Not Null

hiredate Date of joining the company Date Not Null

sal Numeric value consisting of 6 digits Int Not Null

Page 28 of 200
comm Numeric value consisting of 4 digits Int Not Null

Dept no which is Numeric type consisting of


Deptno Int
maximum 2 digits
Department Table
Attribute Data expected to be stored Data type Constraint

deptno Numeric value consisting of 4 digits Int Primary Key

dname Variant length string of max 20 characters Varchar (20) Not Null

loc Variant length string of max 25 characters Varchar (25) Not Null

2.4 SQL FOR DATA MANIPULATION LANGUAGE (DML)


In the previous session, we created the database SchoolRecord with three relations (or tables) i.e.
StudentRecord, ParentRecord and AttendanceRecord. Creating a table, creates its structure only. It is
required to manipulate the data in the table by entering, deleting and updating the data records. The
commands or statements used to insert, delete and update the records come under SQL Data
Manipulation Language (DML).
Data manipulation means either retrieval (access) of existing data, insertion of new data, removal of
existing data or modification of existing data in the database. Updation and deletion of data records
are also important in SQL. These data manipulation methods are discussed in the following section.
2.4.1 INSERTION of Records
INSERT INTO statement is used to insert new records in any table or relation.
Syntax:
INSERT INTO tablename VALUES (value 1, value 2,....);
Here, value 1 corresponds to attribute 1, value 2 corresponds to attribute 2 and so on. It is required
to specify attribute names in the INSERT statement if there are exactly the same number of values
in the INSERT statement as the total number of attributes in the table.

Caution: While populating records in a table with foreign key, ensure that records in referenced
tables are already populated.

Let us insert some records in the SchoolRecord database. First insert the records in the
ParentRecord table first as it does not have any foreign key. A set of sample records for the
ParentRecord table is shown in Table 2.6.
Table 2.6 Records to be inserted into the ParentRecord Table
Par_ID Par_Nam Par_Phone Par_Address Par_Email
e
452695874564 Manu P 9834567890 203, Khandari, Agra, [email protected]
Singh UP
252154687451 Ashok K 9845678910 144 Gr Kailash, New [email protected]
Sharma Delhi
362115264625 Ashutos 9856789120 JP Greens, Noida, UP [email protected]
h Gaur
602125125261 Sachin 9812389120 Kanda, Bagheshwar, [email protected]
Agrawal UK
225423344657 Chandra 9891201238 Fortune Somya, [email protected]
Roy Bhopal, MP
268953264578 Dinesh Lajpat Nagar, [email protected]
Dixit Mathura, UP

Page 29 of 200
485466192343 Rizwan 9255614563 Deep Nagar, Sahrsa, [email protected]
Alam Bihar
521556651761 Ashish 8544556978 T Nagar, Hyderabad, [email protected]
Gupta Telangana m
686113652987 Gurmeet 9635214789 Shahid Nagar, [email protected]
Singh Amritsar, PB
954891122475 Michal 8554658958 Guindy, Chennai, TN [email protected]
DeSousa
Example 2.23: The following command is used to insert the record in the “ParentRecord” table.

We can use the SQL statement “SELECT * from table_ name;” to view the inserted record after any
statement to see the current changes in table.

It is also possible to provide values only for some of the attributes in a table by just specifying the
attribute name alongside each data value as per the following syntax.
Syntax:
INSERT INTO tablename (column1, column2, ...)
VALUES (value1, value2, ...);
Suppose to insert the sixth record in the “ParentRecord” table (Table 2.6) keeping the value of
“Par_Phone” to NULL. Then it is required to insert the values for the other four fields. In this case,
specify the names of attributes in which the values are to be inserted. The values must be given in
the same order in which attributes are written in the INSERT command.
Example 2.24: The following command is used to insert the record in the “ParentRecord” table by
specifying the field name and corresponding values.

Now observe that all the four values have been inserted in the table ParentRecord except “Par_Phone”
which is being set to NULL at the time of creating a table.

Page 30 of 200
Note: Text and date values must be enclosed in ‘ ’ (single quotes).
Practical Activity 2.2 – Insert the records in the ParentRecord table using INSERT command and
check the records inserted in ParentRecord as below.

Practical Activity 2.3 – Insert the records in StudentRecord table (Table 2.7).
Table 2.7 Records to be inserted into the StudentRecord table
Stu_
Stu_FName Stu_LName Stu_DOB Stu_Address Par_ID
RollNo
1 Rajvardhan Singh 5/15/2003 203, Khandari, Agra UP 452695874564
2 Trilok Sharma 8/15/2004 144 Gr Kailash, New Delhi 252154687451
3 Aditi Gaur 4/6/2005 JP Greens, Noida, UP 362115264625
4 Anshika Agrawal 5/17/2003 Kanda, Bagheshwar, UK 602125125261
5 Nandini Roy 12/29/2003 Fortune Somya, Bhopal, MP 225423344657
6 Pawani Dixit 11/12/2004 Lajpat Nagar, Mathura, UP 268953264578
7 Hiba Rizwan 12/3/2006 Deep Nagar, Sahrsa, Bihar 485466192343
8 Riddhi Gupta 1/11/2005 T Nagar, Hyderabad, Telangana 521556651761
9 Manpreet Singh 9/8/2005 Shahid Nagar, Amritsar, Punjab 686113652987
10 John DeSousa 8/17/2005 Guindy, Chennai, TN 954891122475
Example 2.25: The following command is used to insert the first record in table “StudentRecord”.

Page 31 of 200
When column names are not mentioned in the INSERT command, then it is necessary to mention
the values for all the columns. So, if there is no “ParentID” for Trilok, then mention the NULL value
for the “Par_ID”.
Example 2.26: The following command inserts the second record with “Par_ID” value as NULL.
mysql>INSERT INTO StudentRecord VALUES (2,'Trilok','Sharma','8/15/2004', '144 Gr
Kailash','New Delhi' NULL);

Note/Tip: Please be careful while entering the date in the INSERT command. Use the ‘YYYY-MM-DD’
format to write dates.

Practical Activity 2.3.1 – Use INSERT command


Insert the records in the employee table using the INSERT command and display it after inserting
all records using the SELECT statement.

Insert the records in the Department table using INSERT command and display it after inserting
all record using SELECT statement.

Page 32 of 200
2.4.2 UPDATION of Records using UPDATE and DELETE Command
UPDATE and DELETE are also part of SQL Data Manipulation Language (DML). UPDATE command
is used to make changes in existing data value(s) of one or more columns of existing records in a
table. For example, we may require some changes in address, phone number or spelling of name.
Syntax:
UPDATE table_name
SET attribute1 = value1, attribute2 = value2, …
WHERE condition;
In the ParentRecord table, Phone number is not available for Parent Name Dinesh Dixit. So, it is
required to update Phone number of Dinesh Dixit, update the table ParentRecord and use the
command.
Example 2.27: The following command is used to update the Phone number of Dinesh Dixit in the
ParentRecord table

.
The updated data can be verified using the statement.
SELECT * FROM ParentRecord.
It is also possible to update values for more than one column using the UPDATE statement.
Suppose, the ParentRecord with Par_ID 485466192343 has requested to change Address to 'WZ - 68,
Azad Avenue, Boriwali, Mumbai’ and Phone number to '9988776644'.
Example 2.28: The following SQL statement will update this record.
mysql> UPDATE ParentRecord SET Par_Address = 'WZ - 68, Azad Avenue, Boriwali, Mumbai’,
Par_Phone = 9988776644 WHERE Par_ID = 485466192343;
The changes affected can be verified by using the SELECT statement as below.

Page 33 of 200
2.4.3 DELETION of Records using DELETE
DELETE statement is used to delete or remove one or more records from a table.
Syntax:
DELETE FROM table_name WHERE condition;
Suppose the student with roll number 2 has left the school.
Example 2.29: The following SQL statement is used to delete that record from the STUDENT table.

The changes affected can be verified by using the SELECT statement as below.

Caution: The WHERE clause should be used in the UPDATE and DELETE statement, otherwise it
will apply on all the records.

2.5 SQL FOR DATA QUERY LANGUAGE (DQL)


So far, we have learned to create a database, store and manipulate data in the database tables. The
data stored in a database can be retrieved using a mechanism called Query. SQL provides efficient
mechanisms to retrieve data stored in multiple tables in a MySQL database (or any other RDBMS).
The SQL statement SELECT is used to retrieve data from the tables in a database and is also called
a query statement. One of the most commonly used DQL is the SELECT statement.

Page 34 of 200
2.5.1 SELECT Statement
In SQL, the SELECT statement is used to retrieve data from tables in a database and output is
displayed as per the specified parameter on successful execution of the statement.
Syntax:
SELECT attribute1, attribute2,... attribute N
FROM table_name
WHERE condition;
Here, attribute1, attribute2, ... attributeN are the names of columns of the table table_name from
which data is to be retrieved. The FROM clause is always written with the SELECT clause as it
specifies the name of the table from which data has to be retrieved. The WHERE clause is optional
and is used to retrieve data to meet any specified condition(s).
To select all the columns and rows available in a table, use the following select statement.
SELECT * FROM table_name;
Here * is used to retrieve all columns/attributes available in the table.
Let us use a SELECT statement to retrieve names of the students whose names start with the
alphabet “D”.
Example 2.30: The following SQL query statement is used to retrieve the name and date of birth of a
student whose roll number is 1.

In the above query, observe that the Student Roll Number and Date of birth of the student whose roll
number is 1 is retrieved using WHERE clause.
2.5.2 Querying using database OFFICE
Let us consider an EMP table of employee databases with the following fields. The “empno” is a
primary key and “deptno” as foreign key. Table 2.8 shows the data entered in the Emp table.
Table 2.8 Records available in EMP table
empno ename job mgr hiredate sal comm deptno

7019 Smita Clerk 7552 12/14/1994 8800 NULL 20

7049 Alam Salesman 7348 02/17/1995 9600 1800 30

7171 Wasim Salesman 7348 02/19/1995 9250 2000 30

7216 Jawahar Manager 7489 03/30/1995 10975 NULL 20

7304 Manoj Salesman 7348 09/25/1995 9250 2900 30

7348 Balwinder Manager 7489 04/28/1995 10850 NULL 30

7432 Chetana Manager 7489 06/06/1995 10450 NULL 10

7438 Sachin Analyst 7216 12/05/1996 11000 NULL 20

Page 35 of 200
7489 Kushaal President NULL 11/14/1995 13000 NULL 10

7494 Tarun Salesman 7348 09/05/1995 9500 0 30

7526 Amar Clerk 7438 01/08/1997 9100 NULL 20

7550 Jyoti Clerk 7348 11/30/1995 8950 NULL 30

7552 Farhan Analyst 7216 10/27/1995 11000 NULL 20

7584 Mohan Clerk 7432 01/20/1996 9300 NULL 10

7984 Lalitha Clerk 7432 05/23/1998 10300 NULL 10


Now if you wish to retrieve the desired data from the table, let us see how to apply the SELECT clause
to retrieve the data.
(a) Retrieve selected columns – It is possible to retrieve the data of one column of a table.
Example 2.31: The following SQL query statement is used to retrieve employee numbers of all
employees in the table.

Observe that the above query retrieves the empno of all the employees from the Emp table as only
one column is specified to retrieve.
Let us see another query that selects two columns such as emp no and corresponding employee name.
Modify the same query by specifying two fields of table as “empno” and “ename”. and observe the
desired output as below.
Example 2.32: The following SQL query statement will retrieve the data of employee number and
name in two columns.

Page 36 of 200
(b) Renaming of columns – There are specific naming conventions of the fields in the table. It is
possible to rename any column while displaying the output by using the alias 'AS'.
Example 2.33: The following SQL query statement selects Employee name as “Name” in the output
for all the employees.

Example 2.34: The following SQL query statement will calculate and to display the annual salary of
employees. Annual salary is calculated as “sal*12”.

Page 37 of 200
Now it doesn’t look nice to display the caption as “sal*12” in the table. It is possible to display it with
new caption as “Annual Salary” for “sal*12”. The revised query and its output are given below.

Observe that “ename” is shown with the caption as “Name” and “sal*12” is shown with the caption
as “Annual Income”.
Note – Annual Income is just the caption to display. It will not add as a new column in the database
table. It is just for displaying the output of the query. If an aliased column name has space as in the
case of Annual Income, it should be enclosed in quotes as ‘Annual Income’.
(c) Distinct Clause – The SELECT clause retrieves all the data through query as output. There may
be a chance of duplicate values such as two persons with the same name working in the department.
The DISTINCT clause has provision to retrieve the unique records by omitting the duplicate records.
The DISTINCT clause is used for this purpose.
Example 2.35: The following SQL query statement shows the different departments available in the
“emp” table.

Page 38 of 200
Let us understand how to retrieve different types of jobs available using the DISTINCT clause in the
following example.
Example 2.36: The following SQL query statement will use a DISTINCT clause to retrieve different
types of jobs available in the “emp” table.

Observe that there are five different job titles although more records exist.
(d) WHERE Clause – It retrieves data that meet some specified conditions. In our OFFICE database,
more than one employee can have the same salary.
Example 2.37: The following SQL query statement will give distinct salaries of the employees working
in the department number 10.

Observe in the output that all the records of employees working in dept no. 10 and having the distinct
salary are retrieved.
In the above example, = operator is used in the WHERE clause. Other relational operators like (<, <=,
>, >=, !=) can also be used to specify conditions as per your requirement. The logical operators AND,
OR, and NOT are used to combine multiple conditions.
Let us see how to compare columns/fields value/s to specific required records or columns.

Page 39 of 200
Example 2.38: The following SQL query statement will display all the details of those employees of 30
departments who earn more than 5000.

Note: Observe the output, two different conditions are being tested separately. First condition tested
for Salary is greater than 5000 and second condition for department number is 10. AND operator
used to join both conditions.
Let us make a comparison of salaries like who is getting more than 8000 and less than 11000.
Example 2.39: The following SQL query statement will select the name and department number of all
those employees who are earning salary between 8000 and 11000 inclusive of both values.

The query in example 2.39 defines a range of salary between 8000 and 11000 that can also be
achieved using a comparison operator BETWEEN, in the query as below. The output of this query
will be the same as above.

Page 40 of 200
Note: The BETWEEN operator defines the range of values in which the column value must fall into,
to make the condition true.
Example 2.40: The following SQL query statement will select details of all the employees who work in
any of the department number 10, 20, or 40.

(E) Membership operator IN


The IN operator compares a value with a set of values and returns as true if the value belongs to that
set. The query given in Example 2.40 can be rewritten using the IN operator as below.
mysql> SELECT * FROM emp WHERE deptno IN (10, 20, 40);
It will give the same output as above.
Example 2.41: The following SQL query statement selects details of all the employees except those
working in department number 10 or 20.

Page 41 of 200
Note: Here NOT operator is used in combination with IN to retrieve all records except with deptno 10
and 20.
(F) ORDER BY Clause – It is used to display data in an ordered form with respect to a specified
column. By default, ORDER BY displays records in ascending order of the specified column values.
The DESC keyword is used to display the records in descending order.
Let us arrange the records in ascending or descending order using the ORDER BY clause with DESC
clause example 2.42.
Example 2.42: The following SQL query statement selects details of all the employees in ascending
order of their salaries.

Observe that the records are displayed in ascending order of salary of each employee. To arrange
records in descending order, use DESC clause with ORDER BY as in example 2.43.
Example 2.43: The following SQL query statement selects details of all the employees in descending
order of their salaries.

Page 42 of 200
Note: DESC clause used after the column name on which the records to be displayed in descending
order.
(G) Handling NULL Values – SQL supports a special value called NULL to represent a missing or
unknown value. For example, the “Par_Phone” column in the table “ParentRecord” can have missing
values for certain records. Hence, NULL is used to represent such unknown values. It is important
to note that NULL is different from value 0 (zero). Also, any arithmetic operation performed with NULL
value gives NULL. For example, 5 + NULL = NULL because NULL is unknown hence the result is also
unknown. In order to check for NULL value in a column, use IS NULL operator in particular
statements. Example 2.44 illustrates the use of NULL clauses.
Example 2.44: The following SQL query statement selects details of all employees who have not been
given a bonus. This implies that the bonus column will be blank.

Observe the output and see column mgr and comm where NULL is present.

Page 43 of 200
It is also possible to join a NULL statement with any other condition. Example 3.11 shows how to
use it in a statement.
Example 2.45: The following SQL query statement selects emp number, employee names and job of
all those employees who have been given a comm (i.e., comm is not null) and works in the department
30.

(H) Having clause – It is used in a SELECT statement to make a group with certain conditions as a
result of query.
Syntax:
SELECT expression1, expression2, ... expression_n,
aggregate_function (expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING condition;
Example 2.45 shows how to use Group by and Having clauses jointly. The HAVING clause must
follow the GROUP BY clause in any SELECT query and must also be preceded by ORDER BY clause
if used.
Example 2.45: The following SQL query statement selects jobs, number of employees in that job, their
total salary and department number wise list where minimum 3 employees of the same type of job
are working.

Page 44 of 200
(I) Substring pattern matching – Many times it may require that the query should not retrieve that
exact text or value, rather it should retrieve the matching of a few characters or values. For example,
to find names starting with “M” or to find pin codes starting with “11”, is called substring pattern
matching. Such patterns cannot match using = operator. SQL provides a LIKE operator that can be
used with the WHERE clause to search for specific patterns in a column.
The LIKE operator makes use of the following two wild card characters - (%) and (-). The percent (%)
is used to represent zero, one, or multiple characters. The underscore (_) is used to represent exactly
a single character.
There are several situations when we search data records for some pattern matching. A very common
situation when you search for any contacts in your smartphone, you just start typing the first few
characters of the name, then immediately a list appears with these characters and you tap on the
required name to call. Example 3.46 to 3.51 demonstrates such situations to search some patterns
in text values of records using LIKE clauses.
Example 2.46: The following SQL query statement selects details of all those employees whose name
starts with 'K'.

Example 2.47: The following SQL query statement selects details of all those employees whose name
ends with 'a', and receives a salary more than 8500.

Example 2.48: The following SQL query statement selects details of all those employees whose name
consists of exactly five letters and starts with any letter but has ‘mita’ after that.

You can also match a particular character or string in between the text simply by using wildcard
characters as shown in example 2.49.

Page 45 of 200
Example 2.49: The following SQL query statement selects all columns of all employees containing
'ma' as a substring in name.

Example 2.50: The following SQL query statement selects all columns of employees containing 'a' as
the second character in their names.

Example 2.51: The following SQL query statement selects records of all the employees except Alam.

2.6 SQL FOR DATA CONTROL LANGUAGE (DCL)


Data Control Language (DCL) is the part of SQL, which has commands to manage users for their
work permission. The user will be able to work as per the permissions granted to them by DBA
(Database Administrator). DCL includes the commands GRANT and REVOKE, which are used to
provide rights and permissions to users.

Page 46 of 200
GRANT statement – The GRANT statement is used to give access privileges to a specific user to work
with any selected database only.
Syntax:
GRANT SELECT, UPDATE ON Test_Table TO NewUser1, NewUser2;
Example:
GRANT SELECT, UPDATE, DELETE ON carshowroom TO 'WebUser';
Here the user 'WebUser' will be able to use only three SELECT, UPDATE and DELETE SQL statements
when working on carshowroom database.
REVOKE statement – The REVOKE statement is used to withdraw privileges from a specific user so
that specific user could not use a specific statement on a selected database. In other words, it is
useful to take back the given permission/s from the user.
Syntax:
REVOKE Privilege_Name ON Object_Name FROM User_Name.
Example:
REVOKE DELETE ON carshowroom FROM WebUser;
2.7 SQL FOR TRANSACTION CONTROL LANGUAGE (TCL)
Transaction control language (TCL) is the part of SQL commands that allows to permanently change
the databases or undo the database transactions. It is similar to saving the database or undoing the
current changes. The COMMIT, ROLLBACK and SAVEPOINT statements come under this category.
COMMIT – Commit command is used to save all the transactions to the database. After completing
any operation or SQL statement, you can simply write COMMIT as the next statement to permanently
save data in the database.
Syntax:
Commit;
Example:
DELETE FROM ClassStudents WHERE RollNo =25;
Commit;
Here, after the DELETE statement, the COMMIT statement is used. It means the student record
whose RollNo is 25 is permanently deleted. Now after the COMMIT statement, it is not possible to
rollback the record of that student.
ROLLBACK – ROLLBACK command allows to undo transactions that have not already been saved to
the database. This statement is useful to restore the database to the state where the last commit
statement was used. Rollback statement is also used with SAVEPOINT to jump to specific Savepoint
in the database transactions.
Syntax:
ROLLBACK;
SAVEPOINT – This command helps to set a Savepoint within a transaction. Basically, the
SAVEPOINT statement is used to save a transaction temporarily so that the user can rollback to that
point as and when required.
Syntax: SAVEPOINT Savepoint_Name;

Page 47 of 200
CHAPTER 3. FUNCTIONS IN SQL

Introduction to Function in SQL


There are various readily available functions in SQL that can be used in queries. It includes single
row functions, multiple row functions, group records based on some criteria, and working on multiple
tables using SQL. A function is used to perform some particular tasks and it returns zero or more
values as a result. Functions are useful while writing SQL queries also. Functions can be applied to
work on single or multiple records (rows) of a table.
3.1 SQL functions
SQL functions are categorised as Single Row functions and Aggregate functions, depending on their
application in one or multiple rows.
Single Row Functions are also known as Scalar functions. Single row functions are applied on a
single value and return a single value. These are used in SELECT, WHERE, and ORDER BY clauses.
MATH, STRING and DATE functions are examples of single row functions.
Aggregate functions are also called Multiple Row functions. These functions work on a set of
records as a whole and return a single value for each column of the records on which the function is
applied. These are used with SELECT clauses only. MAX ( ), MIN ( ), AVG ( ), SUM ( ), COUNT ( ) and
COUNT (*) are examples of multiple row functions.
To demonstrate the use of SQL function, let us create a database called CARSHOWROOM having the
schema with four relations as shown in Figure 3.1.

Fig 3.1: CARSHOWROOM database schema


Inventory – Stores Car id, Car Name, Price, Model, Year of manufacturing and fuel type for each car
in inventory of the showroom.
Table 3.1: Attribute specification of “Inventory” table
Attribute Data expected to be stored Data type Constraint
CarID Alpha-Numeric value consisting of Varchar (4) Primary Key
maximum 4 digits
CarName Variant length string of maximum 20 Varchar (20) Not Null
characters
Price Numeric value consisting of car price. Int Not Null
Model Variable length string of maximum 4 Varchar (10) Not Null
characters

Page 48 of 200
YearManufacturer Variable length string of maximum 4 Varchar (4) Not Null
characters
FuelType Variable length string of max 10 characters Varchar (10) Not Null
Customer – Stores Customer id, name, address, phone number and email for each customer.
Table 3.2: Attribute specification of “Customer” table
Attribute Data expected to be stored Data type Constraint

Alphanumeric value consisting of characters and


CustID Varchar (5) Primary Key
digits, max 5 chars

CustName Variable length string of max 30 characters Varchar (30) Not Null

CustAdd Variable length string of max 50 characters Varchar (50) Not Null

Phone Numeric value consisting of 10 digits Char (10) Not Null

Email Variable length string of max 50 characters Varchar (20) Not Null
Sale – Stores the invoice number, car id, customer id, sale date, mode of payment, sales person’s
employee id and selling price of the car sold,
Table 3.3: Attribute specification of “Sale” table
Attribute Data expected to be stored Data type Constraint

InvoiceNo Alpha-Numeric value consisting of Characters


Varchar (6) Primary Key
and digits, max 6 chars

Alpha-Numeric value consisting of maximum 4


CarID Varchar (4) Foreign Key
digits

Alpha-Numeric value consisting of Characters


CustID Varchar (5) Foreign Key
and digits, max 5 chars

SaleDate Date value Date Not Null

PaymentMode Variant length string of max 20 characters Varchar (20) Not Null

Alpha-Numeric value consisting of maximum 4


EmpID Varchar (4) Foreign Key
chars only

SalePrice Car price will be as Numeric Value Int Not Null


Employee – Stores employee id, name, date of birth, date of joining, designation and salary of each
employee in the showroom.
Table 3.4: Attribute specification of “Employee” table
Attribute Data expected to be stored Data type Constraint

Alpha-Numeric value consisting of maximum 4


EmpID Varchar (4) Primary Key
chars only

EmpName String of max 20 characters Varchar (20) Not Null

DOB Date value Date Not Null

DOJ Date value Date Not Null

Designation String of max 20 characters Varchar (20) Not Null

Salary Numeric value Int Not Null

Page 49 of 200
To proceed further, create database CARSHOWROOM and create all four tables as per the above
specification.
Insert the records in tables Inventory, Customer, Sale and Employee using INSERT command. The
records of these four relations can be viewed using the SELECT command.
Execute the following query to view the records of the “inventory” table. After successful execution of
the query, the records entered in the “inventory” table will be displayed.

Execute the following query to view the records of the “customer” table. After successful execution of
the query, the records entered in the “customer” table will be displayed.

Execute the following query to view the records of the “sale” table. After successful execution of the
query, the records entered in the “sale” table will be displayed.

Page 50 of 200
Execute the following query to view the records of the “employee” table. After successful execution of
the query, the records entered in the “employee” table will be displayed.

3.2 Single Row Functions


Figure 3.2 lists different single row functions under three categories — Numeric (Math), String, Date
and Time.
1. Math Functions accept numeric values as input and return a numeric value as a result.
2. String Functions accept character value as input and return either character or numeric values
as output.
3. Date and Time functions accept date and time value as input and return numeric or string or Date
and Time as output.

Fig 3.2: Categories of single row functions in SQL


3.2.1 Math Functions
Three commonly used numeric functions are POWER (), ROUND () and MOD (). Their usage
along with syntax is given below.
1. POWER (X, Y) or POW (X, Y) – calculates X to the power Y

Page 51 of 200
2. ROUND (N, D) – Rounds off number N to D number of decimal places. If D=0, then it rounds off
the number to the nearest integer.

3. MOD (A, B) – Returns the remainder after dividing number A by number B.

Practical Activity 3.1 – Demonstrate to use math function ROUND


In order to increase sales, suppose the car dealer offers the customers to pay the total amount in
10 easy EMIs (Equal Monthly Instalments). Assume that EMIs are required in multiples of 10000.
For that, the dealer wants to list the CarID and Price along with the following data from the
Inventory table.
Step 1. Calculate GST as 12 percent of Price and apply ROUND function to it. Execute the query
to round off the GST to one decimal place and display the records with the fields CarID, CarName
and GST.

Step 2. Add a new column “FinalPrice” to the table “inventory”. Update the table “inventory” with
“FinalPrice” as the sum of Price and 12 percent of the GST. Apply the ROUND function to round
off the GST to one decimal place. Execute the following query to do this.

Page 52 of 200
Display the values of “FinalPrice” for all the records by using the SELECT command.

Step 3. Calculate and display the amount to be paid each month in multiples of 1000 which is
calculated after dividing the FinalPrice of the car into 10 instalments. After dividing the amount
into EMIs, find out the remaining amount to be paid immediately, by performing modular division.
Use the SELECT command to display the result. Execute the following query to do this.

Step 4. Execute the following query to display the “InvoiceNo” and “Commission” value rounded off
to zero decimal places.

Step 5. Execute the following query to display the details of “sale” table where payment mode is
credit card.

Page 53 of 200
Step 6. Execute the query to add a new column “Commission” with total length of 7 with 2 decimal
places to the “sale” table.
Step 7. Execute the query to calculate commission for sales agents as 12% of “SalePrice”.

Step 8. Execute the following query to insert the values to the newly added column “Commission”
and then display all records of the “sale” table where Commission > 73000.

Step 9. Execute the following query to display InvoiceNo, EmpID, SalePrice and Commission such
that commission value is rounded off to 0.

3.2.2 String Functions


String functions can perform various operations on alphanumeric data which are stored in a table.
They can be used to change the case such as uppercase to lowercase or vice-versa, extract a
substring, calculate the length of a string and so on. Some of the string functions with examples are
given below.

Page 54 of 200
1. UCASE (string) OR UPPER (string) – converts string into uppercase.

2. LCASE (string) OR LOWER (string) – converts string into lowercase.

3. MID (string, pos, n) OR SUBSTRING (string, pos, n) OR SUBSTR (string, pos, n) – Returns a
substring of size n starting from the specified position (pos) of the string. If n is not specified, it
returns the substring from the position pos till end of the string.

4. LENGTH (string) – Return the number of characters in the specified string.

Page 55 of 200
5. LEFT (string, N) – Returns N number of characters from the left side of the string.

6. RIGHT (string, N) – Returns N number of characters from the right side of the string.

7. INSTR (string, substring) – Returns the position of the first occurrence of the substring in the given
string. Returns 0, if the substring is not present in the string.

8. LTRIM (string) – Returns the given string after removing leading white space characters.

9. RTRIM (string) – Returns the given string after removing trailing white space characters.

Page 56 of 200
10. TRIM (string) – Returns the given string after removing both leading and trailing white space
characters.

Practical Activity 3.2 – Demonstrate to use string function


Let us use Customer relation to understand the working of various string functions.
Step 1. Execute the followings query to display customer name in lower case and customer email
in upper case from “customer” table.

Step 2. Execute the following query to display the length of email and part of the email from the
email id before the character ‘@’.

The function INSTR will return the position of “@” in the email address. So, to print email id without
“@” position -1 is used.
Let us assume that four-digit area code is reflected in the mobile number starting from position
number 3. For example, 1851 is the area code of mobile number 9818511338.
Step 3. Execute the following query to display the area code of the customer living in Rohini.

Page 57 of 200
Step 4. Execute the following query to display emails after removing the domain name extension
“.com” from emails of the customers.

Step 5. Execute the following query to display details of all the customers having yahoo emails
only.

Now let us use the table “inventory” from CARSHOWROOM database, write SQL queries for the
following:
Step 6. Execute the following query to convert the “CarMake” to uppercase if its value starts with
the letter ‘B’.

Step 7. If the length of the car model is greater than 4 then Execute the following query to fetch
the substring starting from position 3 till the end from attribute Model.

Page 58 of 200
3.2.3 Date and Time Functions
There are various functions that are used to perform operations on date and time data. Some of the
operations include displaying the current date, extracting each element of a date (day, month and
year), displaying day of the week and so on. Some of the date and time functions with examples are
given below.
1. NOW() – It returns the current system date and time.

2. DATE() – It returns the date part from the given date/time expression.

3. MONTH(date) – It returns the month in numeric form from the date.

4. MONTHNAME(date) – It returns the month name from the specified date.

5. YEAR(date) – It returns the year from the date.

6. DAY(date) – It returns the day part from the date.

Page 59 of 200
7. DAYNAME(date) – It returns the name of the day from the date.

Practical Activity 3.3 – Demonstrate to use DATE and Time function


Let us use the “emp” table of CARSHOWROOM database to illustrate the working of some of the
date and time functions.
Step 1. Execute the following query to select the day, month number and year of joining of all
employees.

Step 1. Execute the following query to display the date in the format "Wednesday, 26, November,
1979", if the date of joining is not Sunday.

Page 60 of 200
Step 2. Execute the following query to list the Employee Name, date of birth and Salary for all
employees whose salary is more than 25000, in “emp” table.

Step 3. Execute the following query to list the invoice number, customer id and date of sale. Those
payments are done using bank finance in the “Sale” table.

Step 4. Execute the following query to list all the employees without a peon whose salary is more
than 30000 in the “emp” table.

Step 5. Execute the following query to list all the records without LXI and VXI models in the table
“inventory”.

Page 61 of 200
3.3 Aggregate Functions
In aggregate functions the column must be of numeric type. Some of the aggregate functions are
given below.
1. MAX (column) – Returns the largest value from the specified column.

2. MIN (column) – Returns the smallest value from the specified column.

3. AVG (column) – Returns the average of the values in the specified column.

4. SUM (column) – Returns the sum of the values for the specified column.

5. COUNT (*) – Returns the number of records in a table. COUNT (*) is used with the WHERE clause
to display the number of records that match a particular criterion in the table.

Page 62 of 200
Practical Activity 3.4 – Demonstrate to use aggregate functions in SQL
Let us explore how we can use various aggregate functions in SQL statements to fulfil various
requirements of real-world situations.
Step 1. Execute the following SQL query to display the total number of records from table
“inventory” having a model as VXI.

Step 2. Execute the following SQL query to display the total number of different types of Models
available from table “inventory”.

Step 3. Execute the following SQL query to display the average price of all the cars with Model LXI
from table “inventory”.

3.3 GROUP BY CLAUSE IN SQL


Sometimes it may require to fetch a group of rows on the basis of common values in a column.
GROUP BY clause is a special clause in SQL to do this. It groups the rows together that contain the
same values in a specified column. The aggregate functions (COUNT, MAX, MIN, AVG and SUM) can
be used with GROUP BY clauses. HAVING Clause in SQL is used to specify conditions on the rows
with GROUP BY clause.

Practical Activity 3.5 – Demonstrate to use GROUP BY and HAVING clause in SQL
Consider the “sale” table from the CARSHOWROOM database. Display the number of records in
the “sale” table using the following SQL statement.

Page 63 of 200
In these records, it is observed that the columns, CarID, CustID, SaleDate, PaymentMode, EmpID,
SalePrice can have rows with the same values in it. So, GROUP BY clauses can be used in these
columns to find the number of records of a particular type (column), or to calculate the sum of the
price of each car type.
Step 1. Execute the following SQL query to display the number of Cars purchased by each
Customer from the SALE table.

Step 2. Execute the following SQL query to display the Customer Id and number of cars purchased
if the customer purchased more than 1 car from SALE table.

Step 3. Execute the following SQL query to display the number of people in each category of
payment mode from the table SALE.

Step 4. Execute the following SQL query to display the PaymentMode and number of payments
made using that mode more than once.

Page 64 of 200
3.4 OPERATIONS ON RELATIONS
It is possible to perform certain operations on relations like Union, Intersection and Set Difference to
merge the tuples of two tables. These three operations are binary operations as they work upon two
tables. Note here that these operations can only be applied if both the relations have the same number
of attributes and corresponding attributes in both tables have the same domain.
3.4.1 Union (∪)
This operation is used to combine the selected rows of two tables at a time. If some rows are the same
in both the tables, then the result of the Union operation will show those rows only once. Figure 3.3
shows the union of two sets.

Fig 3.3: Union of two sets


Let us consider two relations DANCE and MUSIC shown in Tables 3.9 and 3.10 respectively.
Table 3.9 DANCE
Sno Name Class

1 Astha 7A

2 Pawani 6A

3 Mohit 7B

4 Vibhanshu 7A
Execute the following query to view the records of the “dance” table. After successful execution of the
query, the records entered in the “dance” table will be displayed.

Table 3.10 MUSIC

Page 65 of 200
Sno Name Class

1 Mahak 8A

2 Pawani 6A

3 Lavanya 7A

4 Vibhanshu 7A

5 Abhay 8A
Execute the following query to view the records of “music” table. After successful execution of the
query, the records entered in the “music” table will be displayed.

Step 1. Execute the following SQL query to find the list of students participating in either of events
by using UNION operation on relations DANCE and MUSIC. After execution it will display the union
of DANCE and MUSIC relations.

3.4.2 Intersect (∩) Intersect operation is used to get the common tuples from two tables and is
represented by symbol ∩. Figure 3.9 shows intersection of two sets.

Fig 3.9: Intersection of two sets

Page 66 of 200
Suppose, we have to display the list of students who are participating in both the events (DANCE and
MUSIC), then intersection operation is to be applied on these two tables. The output of INTERSECT
operation is shown in Table 3.11
Table 3.11 DANCE ∩ MUSIC
Sno Name Class
2 Pawani 6A
4 Vibhanshu 7A
3.4.3 Minus (-)
This operation is used to get tuples/rows which are in the first table but not in the second table and
the operation is represented by the symbol - (minus). Figure 3.10 shows difference operation between
two sets.

Fig 3.10: Difference of two sets


To find out the list of students who are only participating in MUSIC and not in DANCE events, use
the MINUS operation. The output of MINUS operation is given in Table 3.12
Table 3.12 DANCE – MUSIC

Sno Name Class

1 Mahak 8A

3 Lavanya 7A

5 Abhay 8A
3.4.4 Cartesian Product (×)
Cartesian product operation combines tuples from two relations. It results in all pairs of rows from
the two input relations, regardless of whether or not they have the same values on common
attributes. It is denoted as ‘×’.
The degree of the resulting relation is calculated as the sum of the degrees of both the relations under
consideration. The cardinality of the resulting relation is calculated as the product of the cardinality
of relations on which the Cartesian product is applied. Let us use the relations DANCE and MUSIC
to show the output of Cartesian products. Note that both relations are of degree 3. The cardinality of
DANCE and MUSIC is 4 and 5 respectively.
Applying Cartesian product on these two relations will result in a relation of degree 6 and cardinality
20, as shown in the output of the following query.

Page 67 of 200
3.5 USING TWO RELATIONS IN A QUERY
Till now we have written queries in SQL using a single relation only. Now let us see how to write
queries using two relations.
3.5.1 JOIN on two tables
JOIN operation combines tuples from two tables on specified conditions. This is unlike Cartesian
products which make all possible combinations of tuples. While using the JOIN clause of SQL, specify
conditions on the related attributes of two tables within the FROM clause. Usually, such an attribute
is the primary key in one table and foreign key in another table.
Let us create two tables UNIFORM (UCode, UName, UColor) and COST (UCode, Size, Price) in the
SchoolUniform database. “UCode” is the primary key in table UNIFORM. “UCode” and “Size” are the
composite keys in table COST. Therefore, UCode is a common attribute between the two tables which
can be used to fetch the common data from both tables. Define UCode as foreign key in the “Cost”
table while creating this table. Enter the records in these tables as shown in Table 3.13 and 3.14.
Table 3.13 Uniform table
UCode Uname UColor

1 Shirt White

2 Pant Grey

3 Tie Blue
Table 3.14 Cost table
UCode Size Price

1 L 580

1 M 500

2 L 890

1 M 810

Practical Activity 3.6 – Demonstrate to join two tables in SQL

Page 68 of 200
Let us consider two tables created, UNIFORM and COST to demonstrate the joining of two tables.
The joining of two tables can be done in three different ways – using WHERE clause, JOIN clause
and NATURAL JOIN clause
Step 1. Execute the following query to join the two tables using WHERE clause.

As the attribute “UCode” appears in both “uniform” and “cost” tables. Hence alias is used to remove
ambiguity by specifying qualifier U with attribute UCode in SELECT and FROM clauses to indicate
its scope.
Step 2. Execute the following query to join the two tables using JOIN clause.

The output of the query is the same as that of step 1. In this query the JOIN clause is used explicitly
along with the condition in FROM clause. Hence no condition is required in the WHERE clause.
The output of queries in step 1 and 2 has a repetitive column UCode having exactly the same
values. This redundant column provides no additional information. SQL provides the extension of
JOIN operation called NATURAL JOIN, which works similar to JOIN clause in SQL to remove the
redundant attribute. This operator can be used to join the contents of two tables if there is one
common attribute in both the tables.
Step 3. Execute the following query to join the two tables using the NATURAL JOIN clause.

It is clear from the output that the result of this query is the same as above in step 1 and 2, except
that the attribute UCode appears only once.
It is important to note the following points while applying JOIN operations on two or more relations.
● If two tables are to be joined on equal condition on the common attribute, then one may
use JOIN with ON clause or NATURAL JOIN in FROM clause. If three tables are to be joined
on equal condition, then two JOIN or NATURAL JOIN are required.
● In general, N-1 joins are needed to combine N tables on equality conditions.
● Any relational operators can be used with JOIN clauses to combine tuples of two tables.

Page 69 of 200
Unit 2. Advanced Python Programming
Chapter 4. File Handling in Python
4.1 Introduction to File: So far, we have written Python programmes that accept input, manipulate
it, and display the output. However, the output used is only available during programme execution,
and input must be made via the keyboard. This is due to the fact that variables in a programme have
a lifetime that lasts until the programme is executed. What if we wish to save the input data as well
as the created output indefinitely so that we can utilise them later? Organisations typically wish to
permanently retain information on employees, inventories, sales, and so on in order to prevent
recurrent processes of entering the same data. As a result, data is permanently preserved on
secondary storage devices for reusability. We save Python script-mode programmes with the a.py
extension. Each programme is saved as a file on the secondary device. Similarly, the data entered
and the output can be saved indefinitely in a file. So, what exactly is a file? A file is an identified area
on a secondary storage medium where data is permanently saved for subsequent access.
4.2 Types of file: Every file on a computer is stored as a collection of 0s and 1s, or in binary form.
As a result, each file is essentially just a series of bytes put one after the other. Data files are classified
into two types: text files and binary files. A text file is made up of human-readable characters that
can be opened with any text editor. Binary files, on the other hand, are made up of non-human-
readable characters and symbols that must be accessed using specialised programmes.
4.4.1 Text File: A text file can be understood as a sequence of letters consisting of alphabets,
numerals, and other special symbols. Files with extensions like .txt, .py, .csv, etc. are some examples
of text files. When we open a text file using a text editor (e.g., Notepad), we see numerous lines of
text. However, the file contents are not saved in this way internally. Rather, they are recorded as a
sequence of bytes consisting of 0s and 1s. In ASCII, UNICODE, or any other encoding method, the
value of each character in the text file is saved as bytes. So, while opening a text file, the text editor
translates each ASCII value and shows us the equivalent character that is readable by a human
being. For example, The ASCII value 65 (binary equivalent 1000001) will be displayed by a text editor
as the letter ‘A’ since the number 65 in the ASCII character set represents "A."
A text file's lines are separated by a special character known as the End of Line (EOL). In Python, for
example, the default EOL character is the newline (n). Other characters, however, can be used to
signal EOL. When a text editor or programmer interpreter encounters the ASCII equivalent of the
EOL character, the remaining file contents are shown, beginning with a new line. Whitespace is
typically used to separate values in a text file, but commas (,) and tabs (t) are also commonly used to
separate values in a text file.
4.4.2 Binary File: Binary files are also stored in bytes (0s and 1s); however, unlike text files, these
bytes do not represent ASCII character values. They instead reflect the real content, such as images,
audio, video, compressed copies of other files, executable files, and so forth. These files are not
readable by humans. Attempting to open a binary file using a text editor will result in some junk
values. To read or write the contents of a binary file, we require specialised software. A binary file is
a sequence of bytes that is stored on a computer. Even a single bit change in the file can corrupt it
and render it unreadable by the supporting application. Furthermore, because the stored information
is not human-readable, it is difficult to erase any errors that may exist in the binary file. Python
programmes can read and write both text and binary files.
4.3 File Handling: -In real-world applications, computer programmes deal with data from many
sources, such as databases, CSV files, HTML, XML, JSON, and so on. In general, we access files to
either write to them or read data from them. However, file operations include creating and opening
files, writing data to files, traversing files, reading data from files, and so on. The io module in Python
offers many functions for dealing with files.
4.3.1 Opening a file: In Python, we use the open () method to open a file. open () has the following
syntax: open file_object (access_mode, file_name)

Page 70 of 200
This function returns a file handle, which is saved in the variable file_object. We may use this variable
to read and write data to and from files by executing the functions specified in Python's IO module.
If the file does not already exist, the above command creates a new empty file with the name we
choose.
The file_object has properties that provide us with basic information about the file, such as:
● <file.closed> returns true if the file is closed and false otherwise.
● <file.mode> returns the access mode in which the file was opened.
● <file.name> returns the name of the file.
The file_name parameter should be the name of the file to be opened. If the file is not in the current
working directory, we must supply its full path as well as its name.
The access_mode parameter is an optional argument that represents the mode in which the
programme must access the file. It is also known as processing mode. In this context, mode refers to
the operation for which the file must be opened, such as reading, writing, +>both reading and writing,
or adding to the end of an existing file. Read mode is the default. We can also specify whether the file
will be handled in binary () or text () mode. Files are opened in text mode by default, which allows
strings to be read and written. Non-textual data files are opened in binary mode, which implies that
read and write operations are conducted in terms of bytes. Table 4.1 outlines the various file access
modes available that can be used in conjunction with the open() method. In the table, the file offset
position refers to the location of the file object when the file is opened in a specific mode.

Table 4.1 File Open Modes


Consider the following example: myObject=open (“myfile.txt”, “a+”)
In the given statement, the file myfile.txt is opened in append and read modes. The file object will be
at the end of the file. That implies we may put data at the end of the file, and at the same time, we
can also retrieve data from the file using the file object named myObject.
4.3.2 Closing a file Once we are done with the read and write operations on a file, it is a good practice
to close the file. Python provides a close() method to accomplish this. While closing a file, the system
frees the memory allocated to it. The syntax of close() is:
file_object.close()
Here, file_object is the object that was returned while opening the file.

Page 71 of 200
Python ensures that any unsaved or unwritten data is flushed off (written) to the file before closing
it. As a result, whenever our job is completed, we should always close the file. In addition, if the file
object is reassigned to another file, the prior file is closed automatically.
4.4 Reading from a Text File
We can write a programme that reads the contents of a file. Before we begin reading a file, we must
ensure that it is opened in "r", "r+", "w+", or "a+" mode. There are three methods for reading the
contents of a file.
4.4.1 The read() method
This technique is used to read a specific number of bytes of data from a file. The syntax of the read()
method is:
file_object.read(n)
Consider the following set of statements to understand the usage of the read() method:
>>>myobject=open("myfile.txt,"'r')
>>>myobject.read(10)
'Hello ever'
>>> myobject.close()
If no argument or a negative integer is supplied in read(), the full file content is read. For example,
>>> myobject=open("myfile.txt,"'r')
>>>print(myobject.read())
Hello everyone
Writing multiline strings
This is the third line.
>>> myobject.close()
4.5 Writing to a Text File
To write to a file, first open it in write or append mode. When we open an existing file in write mode,
the old data is wiped, and the file object is moved to the beginning of the file. In contrast, because
the file object is at the end of the file, new data will be added at the end of the old data in append
mode. After we've opened the file, we can use the following methods to write data to it:
● write(): used to write a single string
● writelines(): for writing a string sequence
4.5.1 The write() method
write() method takes a string as an argument and writes it to the text file. It returns the number of
characters being written on a single execution of the write() method. Also, we need to add a newline
character (\n) at the end of every sentence to mark the end of line.
Consider the following piece of code:
>>> myobject=open("myfile.txt",'w')
>>> myobject.write("Hey I have started
#using files in Python\n")
41
>>> myobject.close()
On execution, write() returns the number of characters written on to the file. Hence, 41, which is the
length of the string passed as an argument, is displayed.
Note: ‘\n’ is treated as a single character
If numeric data are to be written to a text file, the data needs to be converted into string before writing
to the file. For example

Page 72 of 200
>>>myobject=open("myfile.txt",'w')
>>> marks=58
#number 58 is converted to a string using
#str()
>>> myobject.write(str(marks))
2
>>>myobject.close()
The write() actually writes data onto a buffer. When the close() method is executed, the contents from
this buffer are moved to the file located on the permanent storage.
Program 2-1 Writing and reading to a text file
fobject=open("testfile.txt","w") # creating a data file
sentence=input("Enter the contents to be written in the file: ")
fobject.write(sentence) # Writing data to the file
fobject.close() # Closing a file
print("Now reading the contents of the file: ")
fobject=open("testfile.txt","r")
#looping over the file object to read the file
for str in fobject:
print(str)
fobject.close()
In Program 4.1, the file named testfile.txt is opened in write mode and the file handle named fobject
is returned. The string is accepted from the user and written in the file using write(). Then the file is
closed and again opened in read mode. Data is read from the file and displayed till the end of file is
reached.
Output of Program 2-1:
>>>
RESTART: Path_to_file\Program2-1.py
Enter the contents to be written in the file:
roll_numbers = [1, 2, 3, 4, 5, 6] Now reading the contents of the
file: roll_numbers = [1, 2, 3, 4, 5, 6]
>>>
4.6 Reading File
File object includes the following methods to read data from the file.
● read(chars): reads the specified number of characters starting from the current position.
● readline(): reads the characters starting from the current reading position up to a newline
character.
● readlines(): reads all lines until the end of file and returns a list object.
The following C:\myfile.txt file will be used in all the examples of reading and writing files.
This is the first line.
This is the second line.
This is the third line.

Page 73 of 200
The following example performs the read operation using the read(chars) method.
f = open('C:\myfile.txt') # opening a file
lines = f.read() # reading a file
print(lines) #'This is the first line. \nThis is the
second line.\nThis is the third line.'
f.close() # closing file object
Above, f = open('C:\myfile.txt') opens the myfile.txt in the default read mode from the current
directory and returns a file object. f.read() function reads all the content until EOF as a string. If you
specify the char size argument in the read(chars) method, then it will read that many chars only.
f.close() will flush and close the stream.
4.6.1 Reading a Line
The following example demonstrates reading a line from the file.
f = open('C:\myfile.txt') # opening a file
line1 = f.readline() # reading a line
print(line1) #'This is the first line. \n'

line2 = f.readline() # reading a line


print(line2) #'This is the second line.\n'

line3 = f.readline() # reading a line


print(line3) #'This is the third line.\n'

line4 = f.readline() # reading a line


print(line4) #'This is the fourth line. \n’
f.close() # closing file object
As you can see, we have to open the file in 'r' mode. The readline() method will return the first line,
and then will point to the second line in the file.
4.6.2 Reading all Lines
The following reads all lines using the readlines() function.
f = open('C:\myfile.txt') # opening a file
lines = f.readlines() # reading all lines
print(lines) #'This is the first line. \n This is the second
line.\n This is the third line.'
f.close() # closing file object
The file object has an inbuilt iterator. The following program reads the given file line by line until Stop
Iteration is raised, i.e., the EOF is reached.
4.7 Writing to a Binary File
The open() function opens a file in text format by default. To open a file in binary format, add 'b' to
the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while
the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-
readable. When opened using any text editor, the data is unrecognisable.

Page 74 of 200
The following code stores a list of numbers in a binary file. The list is first converted in a byte array
before writing. The built-in function byte array() returns a byte representation of the object.
f=open("binfile.bin","wb")
num= [5, 10, 15, 20, 25]
arr=bytearray(num)
f.write(arr)
f.close()
4.8 Access Mode of Files
The file access mode is a string that indicates how a file should be opened. When you use the open
function in Python to open a file, you can specify the mode in which the file should be opened. In
Python, there are numerous file access modes available:
● "r" (read mode): Open the file for reading. If the file does not exist, a FileNotFoundError is raised.
with open('file.txt', 'r') as f: # open the file in read-only mode.
content = f.read()
print(content)
● "w" (write mode): Open the file for writing. If the file exists, its contents are truncated. If the file
does not exist in the file path, a new file is created.
With open("file.txt", "w") as f: # Open the file in write mode
f.write("Hello, world!") # Write "Hello, world!" to the file
● "a" (append mode): Open the file for writing, but instead of truncating its contents, new data is
added to the end of the file. If the file does not exist in the file path, a new file is created.
with open('file.txt', 'a') as f: # Open the file in append mode
f.write('This is a new line.') # Append to already existing content
● "x" (exclusive creation mode): Open the file for writing, but only if the file does not already exist.
If the file exists, a File Exists Error is raised.
● "b" (binary mode): Open the file in binary mode. When used in combination with other modes,
the "b" mode changes the way data is read from or written to the file. For example, "rb" opens the
file in binary read mode, and "wb" opens the file in binary write mode.
with open("file.bin", "rb") as f: # Read binary data from a file
data = f.read()
print(data)
//Output:# b'\x01\x02\x03\x04'
● 't': Text mode: Used in combination with other modes to indicate that the file
should be opened in text mode (default).
● "+" (read and write mode): Open the file for reading and writing. The file's
pointer is positioned at the starting of the file.
● 'r+' Read and write mode: Opens the file for reading and writing and the pointer is placed at the
beginning of the file. If the file does not exist, an error is raised.
with open('file.txt', 'r+') as f: # open the file in read and write mode
content = f.read()
print(content)
f.seek(0) # Seek to the beginning of the file

Page 75 of 200
f.write('This is a new line.') # Write to the file
● 'a+' Read and append mode: Opens the file for reading and writing and the pointer is placed at
the end of the file. If the file does not exist in the file path it is created.
with open('file.txt', 'a+') as f: # Open the file in read and append mode
content = f.read()
print(content)
f.seek(0, 2) # Seek to the end of the file
f.write('This is a new line.') # Append to the file
4.9 Setting Offsets in a File
The functions we've learned so far are used to access data sequentially from a file. However, if we
want to retrieve data at random, Python provides the seek() and tell() functions.
4.9.1 The tell() method
This function returns a number indicating the file object's current location in the file. The supplied
position is the byte location from the start of the file to the current position of the file object. The
syntax for tell() is:
file_object.tell()
4.9.2 The seek() method
This method is used to position the file object at a particular position in a file. The syntax of seek()
is:
file_object.seek(offset [, reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved.
reference_point indicates the starting position of the file object. That is, with reference to which
position, the offset has to be counted. It can have any of the following values:
0 - beginning of the file
1 - current position of the file
2 - end of file
By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file.
For example, the statement fileObject.seek(5,0) will position the file object at 5th byte position from
the beginning of the file. The code in Program 2-2 below demonstrates the usage of seek() and tell().
Program 2-2 Application of seek() and tell()
Print ("Learning to move the file object")
fileobject=open("testfile.txt","r+")
str=fileobject.read()
print (str)
print ("Initially, the position of the file object is: ",fileobject. tell())
fileobject.seek(0)
print ("Now the file object is at the beginning of the file: ",fileobject.tell()) fileobject.seek(10)
print ("We are moving to 10th byte position from the beginning of file")
print ("The position of the file object is at", fileobject.tell())
str=fileobject.read()
print (str)

Page 76 of 200
Output of Program 2-2:
>>>
RESTART: Path_to_file\Program2-4.py
Learning to move the file object
roll_numbers = [1, 2, 3, 4, 5, 6]
Initially, the position of the file object is: 33
Now the file object is at the beginning of the file: 0
We are moving to 10th byte position from the beginning of file
The position of the file object is at 10 numbers = [1, 2, 3, 4, 5, 6]
>>>
4.10 Creating and Traversing a Text File
Let us now do some fundamental operations on a text file after we have learned various techniques
for opening and closing a file, reading and writing data in a text file, locating the position of the file
object, and moving the file object to a specified location. Assume we'll be working with practise.txt to
carry out these tasks.
4.10.1 Creating a file and writing data
We use the open() method to create a text file, passing in the filename and mode. If a file with the
same name already exists, the open() function will behave differently depending on the mode (write
or add). If it is in write mode (w), all existing file contents will be lost, and an empty file with the same
name will be created. If, on the other hand, the file is created in append mode (a), the new data will
be written after the existing data. If the file does not exist, a new empty file will be created in both
circumstances. In Programme 2-3, a file called practise.txt is opened in write mode (w), and three
sentences are saved in it. as shown in the output screen that follows it.
Program 2-3 To create a text file and write data in it
# program to create a text file and add data
fileobject=open("practice.txt","w+")
while True:
data= input("Enter data to save in the text file: ")
fileobject.write(data)
ans=input("Do you wish to enter more data?(y/n): ")
if ans=='n': break
fileobject.close()
Output of Program 2-3:
>>>
RESTART: Path_to_file\Program2-3.py
Enter data to save in the text file: I am interested to learn about Computer Science
Do you wish to enter more data?(y/n): y
Enter data to save in the text file: Python is easy to learn
Do you wish to enter more data?(y/n): n
>>>
4.10.2 Traversing a file and displaying data

Page 77 of 200
We will return to the previous example, where we created the file practise.txt, to read and show data
saved in a text file. The file will be opened in read mode, with reading beginning at the beginning.
Program 2-4 To display data from a text file
fileobject=open("practice.txt","r")
str = fileobject.readline()
while str:
print(str)
str=fileobject.readline()
fileobject.close()
Output of Program 2-4:
>>>
I am interested to learn about Computer SciencePython is easy to learn
4.11 The Pickle Module
We already know that Python treats everything as an object. As a result, all data types, including
lists, tuples, dictionaries, and so on, are considered as objects. During the execution of a programme,
we may need to save the current state of variables so that we can return them to their original value
later. Assume you're playing a video game and want to quit after a while. As a result, the programme
should be able to save the current state of the game as a Python object, containing the current level
or stage, your score, and so on. Similarly, you might want to save a Python dictionary as an object
to be able to recover it later. Python includes a Pickle module that allows you to preserve any object
structure as well as data. Pickle is a Python package for serialising and de-serialising any Python
object structure. Pickling is a method of preserving food by immersing it in a solution that extends
its shelf life. In other words, it is a means of storing food for later use.
The pickle module handles binary files. Data is not written but dumped here, and data is not read
but loaded. To load and dump data, the Pickle Module must be imported. To work with binary files
for pickling and unpickling, the pickle module provides two methods: dump() and load().
4.11.1 The dump() method
This method is used to convert (pickling) Python objects for writing data in a binary file. The file in
which data are to be dumped, needs to be opened in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)
where data_object is the object that has to be dumped to the file with the file handle named file_object.
For example, Program 2-6 writes the record of a student (roll_no, name, gender and marks) in the
binary file named mybinary.dat using the dump(). We need to close the file after pickling.
Program 2-5 Pickling data in Python
Import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()
4.11.2 The load() method
This method is used to load (unpickling) data from a binary file. The file to be loaded is opened in
binary read (rb) mode. Syntax of load() is as follows:
Store_object = load(file_object)

Page 78 of 200
Here, the pickled Python object is loaded from the file having a file handle named file_object and is
stored in a new file handle called store_object. The program 2-7 demonstrates how to read data from
the file mybinary.dat using the load().
Program 2-6 Unpickling data in Python
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb") objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Output of Program 2-6:
>>>
RESTART: Path_to_file\Program2-7.py
The data that were stored in file are: [1, 'Geetika', 'F', 26]
>>>
4.11.3 File handling using pickle module
In the same way that we read and write data in a text file, we will add and display data in a binary
file. Programme 2-8 accepts an employee record from the user and appends it to the binary file TV.
The records are then read from the binary file and presented on the screen using the same object.
The user is free to enter as many records as they want. Before beginning the reading process, the
programme also displays the size of binary files.
Program 2-7 To perform basic operations on a binary file using pickle module
# Program to write and read employee records in a binary file import pickle
print("WORKING WITH BINARY FILES")
bfile=open("empfile.dat","ab")
recno=1 print ("Enter Records of Employees")
print()
#taking data from user and dumping in the file as list object
while True:
print("RECORD No.", recno)
eno=int(input("\tEmployee number : "))
ename=input("\tEmployee Name : ")
ebasic=int(input("\tBasic Salary : "))
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
print("\tTOTAL SALARY : ", totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,bfile)
ans=input("Do you wish to enter more records (y/n)? ")
recno=recno+1
if ans.lower()=='n':
print("Record entry OVER ")

Page 79 of 200
print()
break
# retrieving the size of file
print("Size of binary file (in bytes):",bfile.tell())
bfile.close()
# Reading the employee records from the file using load() module
print("Now reading the employee records from the file")
print()
readrec=1
try:
with open("empfile.dat","rb") as bfile:
while True:
edata=pickle.load(bfile)
print("Record Number : ",readrec)
print(edata)
readrec=readrec+1
except EOFError: pass
bfile.close()
Output of Program 2-7:
>>>
RESTART: Path_to_file\Program2-8.py
WORKING WITH BINARY FILES
Enter Records of Employees
RECORD No. 1
Employee number : 11
Employee Name : D N Ravi
Basic Salary : 32600
Allowances : 4400
TOTAL SALARY : 37000
Do you wish to enter more records (y/n)? y
RECORD No. 2
Employee number : 12
Employee Name : Farida Ahmed
Basic Salary : 38250
Allowances : 5300
TOTAL SALARY : 43550
Do you wish to enter more records (y/n)? n
Record entry OVER
Size of binary file (in bytes): 216

Page 80 of 200
Now reading the employee records from the file
Record Number : 1
[11, 'D N Ravi', 32600, 4400, 37000]
Record Number : 2
[12, 'Farida Ahmed', 38250, 5300, 43550]
>>>
As each employee record is stored as a list in the file empfile.dat, hence while reading the file, a list
is displayed showing the record of each employee. Notice that in Program 2-8, we have also used try.
except block to handle the end-of-file exception.

Page 81 of 200
Chapter 5. Exception Handling in Python
5.1 Introduction to Exception Handling
Each and every exception has to be handled by the programmer to avoid the program from crashing
abruptly. This is done by writing additional code in a program to give proper messages or instructions
to the user on encountering an exception. This process is known as exception handling.
Here is simple syntax of try....except...else blocks :
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
5.1.1 Syntax Error: Syntax errors are detected when we have not followed the rules of the particular
programming language while writing a program. These errors are also known as parsing errors. On
encountering a syntax error, the interpreter does not execute the program unless we rectify the errors,
save and rerun the program. When a syntax error is encountered while working in shell mode, Python
displays the name of the error and a small description about the error as shown in figure 5.1

Figure 5.1: A syntax error displayed in Python shell mode


So, a syntax error is reported by the Python interpreter giving a brief explanation about the error and
a suggestion to rectify it.
Similarly, when a syntax error is encountered while running a program in script mode as shown in
Figure 5.1, a dialog box specifying the name of the error (Figure 5.1) and a small description about
the error is displayed as shown below.

Page 82 of 200
Fig 5.2: An error in the script
5.1.2: Exceptions: Even if a statement or expression is syntactically correct, there might arise an
error during its execution. For example, trying to open a file that does not exist, division by zero and so
on. Such types of errors might disrupt the normal execution of the program and are called exceptions.
An exception is a Python object that represents an error. When an error occurs during the execution of
a program, an exception is said to have been raised. Such an exception needs to be handled by the
programmer so that the program does not terminate abnormally. Therefore, while designing a program,
a programmer may anticipate such erroneous situations that may arise during its execution and can
address them by including appropriate code to handle that exception.
It is to be noted that Syntax Error shown in Figures is also an exception. But, all other exceptions
are generated when a program is syntactically correct.
5.1.3 Need of Exception Handling: Exception handling is being used not only in Python
programming but in most programming languages like C++, Java, Ruby, etc. It is a useful technique
that helps in capturing runtime errors and handling them so as to avoid the program getting crashed.
Following are some of the important points regarding exceptions and their handling:
• Python categorises exceptions into distinct types so that specific exception handlers (code to
handle that particular exception) can be created for each type.
• Exception handlers separate the main logic of the program from the error detection and
correction code. The segment of code where there is any possibility of error or exception, is placed
inside one block. The code to be executed in case the exception has occurred, is placed inside
another block. These statements for detection and reporting the exception do not affect the main
logic of the program.
• The compiler or interpreter keeps track of the exact position where the error has occurred.
• Exception handling can be done for both user-defined and built-in exceptions.
5.2 User Defined Exception: Python detects all the critical errors that occur during Compile-
time and Runtime. It stops the program's execution if the error occurs and raises an exception. Some
commonly raised Exceptions are ArithmeticError, AttributeError, ImportError, IOError,
FileNotFoundError, etc.
Sometimes we must enforce constraints on the values that specific program variables can take or
save the program from running into an undesired state. In such situations, Python allows
programmers to create User-defined Exceptions.

Page 83 of 200
To create a User-defined Exception, we need to create a class directly or indirectly derived from the
built-in Exception class.

Fig 5.3: User-defined Exception


In the example above, Just Exception is a user-defined Exception. This class implements the
Exception class. We have defined the __init__ function that takes the message of the String type as
a parameter and prints the message. This JustException class can implement everything like a
normal class in Python.
5.3 Raising or Throwing Exceptions: Raise allows you to throw an exception at any time. assert
enables you to verify if a certain condition is met and throw an exception if it isn't. In the try clause,
all statements are executed until an exception is encountered. except is used to catch and handle
the exception(s) that are encountered in the try clause.

Fig 5.4: Try Catch block exception


5.4 Handling Exceptions: Each and every exception has to be handled by the programmer to avoid
the program from crashing abruptly. This is done by writing additional code in a program to give
proper messages or instructions to the user on encountering an exception. This process is known as
exception handling.
5.5 Catching Exceptions: An exception is said to be caught when a code that is designed to handle
a particular exception is executed. Exceptions, if any, are caught in the try block and handled in the
except block. While writing or debugging a program, a user might doubt an exception to occur in a
particular part of the code. Such suspicious lines of codes are put inside a try block. Every try block
is followed by an except block. The appropriate code to handle each of the possible exceptions (in the
code inside the try block) are written inside the except clause.
While executing the program, if an exception is encountered, further execution of the code inside the
try block is stopped and the control is transferred to the except block. The syntax of try.. except
clause is as follows:
Try:
[ program statements where exceptions might occur]
except [exception-name]:
[ code for exception handling if the exception-name error is encountered]

Page 84 of 200
Program 1-2 Using try.except block
print ("Practicing for try block") try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print(quotient)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO.... not allowed") print(“OUTSIDE
try..except block”)
In Program 1-2, the ZeroDivisionError exception is handled. If the user enters any non-zero value as
denominator, the quotient will be displayed along with the message “Division performed
successfully”, as shown in Figure 1.10. The except clause will be skipped in this case. So, the next
statement after the try..except block is executed and the message “OUTSIDE try.. except block” is
displayed.
However, if the user enters the value of the denominator as zero (0), then the execution of the try
block will stop. The control will shift to the except block and the message “Denominator as Zero…. not
allowed” will be displayed, as shown in Figure 1.11. Thereafter, the statement following the try..except
block is executed and the message “OUTSIDE try..except block” is displayed in this case also.

Fig 5.5: Output with exception raised

Fig 5.6: Output without an error


Sometimes, a single piece of code might be suspected to have more than one type of error. For
handling such situations, we can have multiple except blocks for a single try block as shown in the
Program 1-5.

Page 85 of 200
Program1-3 Use of multiple except clauses
print ("Handling multiple exceptions") try:
numerator=50
denom=int(input("Enter the denominator: "))
print (numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
In the code, two types of exceptions (ZeroDivisionError and ValueError) are handled using two
exception blocks for a single try block. When an exception is raised, a search for the matching except
block is made till it is handled. If no match is found, then the program terminates.
However, if an exception is raised for which no handler is created by the programmer, then such an
exception can be handled by adding an except clause without specifying any exception. This except
clause should be added as the last clause of the try..except block. The Program 1-4 given below along
with the output given in Figure 1.11 explains this.
Program 1-4 Use of except without specifying an exception
print ("Handling exceptions without naming them") try: numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print ("Division performed successfully")
except ValueError:
print ("Only INTEGERS should be entered")
except:
print (" OOPS.....SOME EXCEPTION RAISED")
If the above code is executed, and the denominator entered is 0 (zero), the handler for
ZeroDivisionError exception will be searched. Since it is not present, the last except clause (without
any specified exception) will be executed, so the message “OOPS.....SOME EXCEPTION RAISED” will
be displayed.

Fig 5.7: Output of Program 1-4


5.6 Try –Except – Else Clause: We can put an optional else clause along with the try...except clause.
An exception block will be executed only if some exception is raised in the try block. But if there is

Page 86 of 200
no error then none of the except blocks will be executed. In this case, the statements inside the else
clause will be executed. Program 1-5 along with its output explains the use of else block with the
try... except block.
Program 1-5 Use of else clause
print ("Handling exception using try...except...else") try: numerator=50
denom=int(input("Enter the denominator: "))
quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
else:
print ("The result of division operation is ",quotient)
Output:

Fig 5.8: Output of Program 1-5.


5.7 Try–Finally Clause: The try statement in Python can also have an optional final clause. The
statements inside the finally block is always executed regardless of whether an exception has
occurred in the try block or not. It is a common practice to use the finally clause while working with
files to ensure that the file object is closed. If used, finally should always be placed at the end of the
try clause, after all except blocks and the else blocks.
Program 1-6 Use of finally clause
print ("Handling exception using try...except...else...finally") try: numerator=50
denom=int(input("Enter the denominator: ")) quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
else:
print ("The result of division operation is ", quotient) finally:

Page 87 of 200
print ("OVER AND OUT")
In the above program, the message “OVER AND OUT” will be displayed irrespective of whether an
exception is raised or not.
5.8 Recovering and continuing with finally: If an error has been detected in the try block and
the exception has been thrown, the appropriate except block will be executed to handle the error.
But if the exception is not handled by any of the except clauses, then it is re-raised after the execution
of the finally block. For example, Program 1.4 contains only the except block for ZeroDivisionError.
If any other type of error occurs for which there is no handler code (except clause) defined, then also
the final clause will be executed first. Consider the code given in Program 1-7 to understand these
concepts.
Program 1-7 Recovering through finally clause
print (" Practicing for try block")
try: numerator=50
denom=int(input("Enter the denominator")) quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
else:
print ("The result of division operation is ", quotient)
finally:
print ("OVER AND OUT")
While executing the above code, if we enter a non-numeric data as input, the finally block will be
executed. So, the message “OVER AND OUT” will be displayed. Thereafter the exception for which
the handler is not present will be re-raised. The output of Program 1-7 is shown in the below Figure.

Fig 5.9: Output of Program 1-7


After execution of the finally block, Python transfers the control to a previously entered try or to the
next higher-level default exception handler. In such a case, the statements following the finally block
are executed. That is, unlike except, execution of the finally clause does not terminate the exception.
Rather, the exception continues to be raised after execution of finally.
To summarise, we put a piece of code where there are possibilities of errors or exceptions to occur
inside a try block. Inside each except clause we define handler codes to handle the matching

Page 88 of 200
exception raised in the try block. The optional else clause contains codes to be executed if no
exception occurs. The optional finally block contains codes to be executed irrespective of whether an
exception occurs or not.
S. Name of the Explanation
No Builtin Exception
1. SyntaxError It is raised when there is an error in the syntax of the Python code.
4. ValueError It is raised when a built-in method or operation receives an argument
that has the right data type but mismatched or inappropriate values.
5. IOError It is raised when the file specified in a program statement cannot be
opened.
4 KeyboardInterrupt It is raised when the user accidentally hits the Delete or Esc key while
executing a program due to which the normal flow of the program is
interrupted.
5 ImportError It is raised when the requested module definition is not found.
6 EOFError It is raised when the end of file condition is reached without reading any
data by input ().
7 ZeroDivisionError It is raised when the denominator in a division operation is zero.
8 IndexError It is raised when the index or subscript in a sequence is out of range.
9 NameError It is raised when a local or global variable name is not defined.
10 IndentationError It is raised due to incorrect indentation in the program code.
11 TypeError It is raised when an operator is supplied with a value of incorrect data
type.
12 OverFlowError It is raised when the result of a calculation exceeds the maximum limit
for numeric data type.

Built-in Exception Classes - Commonly occurring exceptions are usually defined in the
compiler/interpreter. These are called built-in exceptions.
Python’s standard library is an extensive collection of built-in exceptions that deals with the
commonly occurring errors (exceptions) by providing the standardised solutions for such errors. On
the occurrence of any built-in exception, the appropriate exception handler code is executed which
displays the reason along with the raised exception name. The programmer then has to take
appropriate action to handle it. Some of the commonly occurring built-in exceptions that can be
raised in Python are explained in the following table.
Table 1.1 Built-in exceptions in Python
Figure shows the built-in exceptions viz, ZeroDivisionError, NameEError, and TypeError raised by
the Python interpreter in different situations

Page 89 of 200
Fig. 5.10: Example of built-in exceptions
A programmer can also create custom exceptions to suit one’s requirements. These are called user-
defined exceptions. We will learn how to handle exceptions in the next section.

Page 90 of 200
Chapter 6. Implement data structure
using python
6.1 Stack:
The stack is known as a Last in First Out (LIFO) list because the pieces added at the end are
eliminated first. In a stack, the topmost piece that was inserted last is processed first, while the first
element that was placed is handled last. As shown in the figure, this is a stack of books in which the
first book will be placed at the bottom, and the second book will be placed on top. Inserting the third
book will move it to the top of the list. Inserting an element into the stack is referred to as a "push,
while removing an element from the stack is referred to as a "pop." A pointer keeps track of the stack's
top element.

Fig 6.1: Stack of Books


6.2 Implementation using python
So far, we've learned that a stack is a linear and ordered collection of components. The data type list
is a straightforward way to implement a stack in Python. To insert or remove components, we can
set either side of the list as TOP. It should be noted that the stack is implemented using the list's
built-in functions append() and pop(). Because these built-in methods insert or delete members at
the rightmost end of the list, TOP does not need to be explicitly declared.
Let us design a programme that will:
• insert or delete elements (glasses);
• verify if the stack is empty (no glasses in the stack);
• find the number of elements (glasses) in the stack.
In the stack, read the value of the topmost element (the number on the topmost glass). To carry out
these operations, the programme must specify the following functions. Let's start with an empty stack
called GlassStack. We will accomplish this by associating the identifier named with an empty list.
glassStack: glassStack = list()
A function named Empty to check whether the stack glassStack is empty or not. Remember trying to
remove an element from an empty stack would result in ‘underflow’. This function returns True if the
stack is empty, else returns False.
def isEmpty(glassStack):
if len(glassStack)==0:
return True
else:
return False

Page 91 of 200
An opPush function that inserts (pushes) a new element into the stack. This function takes two
parameters: the name of the stack (glassStack) into which the element is to be added and the element
to be inserted. We know that an element is always inserted at the top of the stack. As a result, we'll
use the list's built-in method append() to add an element to the stack, which always adds at the end
of the list. Because Python has no restriction on the size of a list, the implemented stack will never
be full unless there is no more memory available. As a result, we will never encounter the 'overflow'
(no space for new elements) stack situation.
def opPush(glassStack,element):
glassStack.append(element)
A function named size to read the number of elements in the glassStack. We will use the len() function
of list in Python to find the number of elements in the glassStack.
def size(glassStack):
return len(glassStack)
A function named top to read the most recent element (TOP) in the glassStack.
def top(glassStack):
if isEmpty(glassStack):
print('Stack is empty')
return None
else:
x =len(glassStack)
element=glassStack[x-1]
return element
A function named opPop to delete the topmost element from the stack. It takes one parameter - the
name of the stack (glassStack) from which element is to be deleted and returns the value of the
deleted element. The function first checks whether the stack is empty or not. If it is not empty, it
removes the topmost element from it. We shall use the builtin method pop() of Python list that
removes the element from the end of the list
def opPop(glassStack):
if isEmpty(glassStack):
print('underflow')
return None
else:
return(glassStack.pop())
A function named display to show the contents of the stack.
def display(glassStack):
x=len(glassStack)
print("Current elements in the stack are: ")
for i in range(x-1,-1,-1):
print(glassStack[i])
Once we define the above functions we can use the following Python code to implement a stack of
glasses.
glassStack = list() # create empty stack

Page 92 of 200
#add elements to stack
element='glass1'
print("Pushing element ",element)
opPush(glassStack,element)
element='glass2'
print("Pushing element ",element)
opPush(glassStack,element)
#display number of elements in stack
print("Current number of elements in stack is",size(glassStack))
#delete an element from the stack
element=opPop(glassStack)
print("Popped element is",element)
#add new element to stack
element='glass3'
print("Pushing element ",element)
opPush(glassStack,element)
#display the last element added to the
#stack
print("top element is",top(glassStack))
#display all elements in the stack
display(glassStack)
#delete all elements from stack
while True:
item=opPop(glassStack)
if item == None:
print("Stack is empty now")
break
else:
print("Popped element is",item)
The output of the above program will be as follows:
Pushing element glass1
Pushing element glass2
Current number of elements in stack is 2
Popped element is glass2
Pushing element glass3
top element is glass3
Current elements in the stack are:
glass3
glass1

Page 93 of 200
Popped element is glass3
Popped element is glass1
Underflow
Stack is empty now
6.3 PUSH and POP Operations
PUSH adds a new element to the stack's top. It is an implantation procedure. We can add pieces to
a stack until it is completely filled. When no more items can be added to a stack, it is said to be full.
Attempting to add an element to a full stack results in an 'overflow' exception. The POP operation is
used to remove the stack's topmost element, that is, the element at the top of the stack. It's a removal
operation. We can delete elements from a stack until it is empty, which means it has no elements.
Attempting to delete an element from an empty stack raises the 'underflow' exception.
In LIFO order, elements are inserted and deleted from a stack. The same concept is used when adding
and removing glasses from a pile. Assume that each glass is numbered and build a stack of glasses.
Figure 6.2 depicts visual representations of PUSH and POP operations on a stack of glasses.

Fig: 6.2 PUSH and POP operations on the stack of glasses


# Stack implementation in python
# Creating a stack
def create_stack():
stack = []
return stack
# Creating an empty stack
def check_empty(stack):
return len(stack) == 0
# Adding items into the stack
def push(stack, item):
stack.append(item)
print("pushed item: " + item)
# Removing an element from the stack
def pop(stack):
if (check_empty(stack)):

Page 94 of 200
return "stack is empty"
return stack.pop()
stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
6.4 Implementation of queue using Python
There are many ways in which queues can be implemented in a computer program, one way is using
the list data type of Python. For creating a queue structure in the program, following functions need
to be defined:
Let’s create a queue named myQueue. We can create it by assigning an empty list.
myQueue = list()
A function (enqueue) to insert a new element at the end of the queue. The function has two
parameters - name of the queue and element which is to be inserted in the queue.
def enqueue(myQueue, element):
myQueue.append(element)
Note: append() function always adds an element at the end of the list, hence Rear of queue.
We don’t need to implement Is Full, as Python being a dynamic language, does not ask for the creation
of lists having fixed size. Hence, we will never encounter a situation when the queue is full.
A function (isEmpty) to check, if the queue has an element or not? This can be done by checking the
length of the queue. The function has a parameter -- name of the queue and returns True if the queue
is empty False otherwise.
def isEmpty(myQueue):
if len(myQueue)==0:
return True
else:
return False
A function (dequeue) to delete an element from the front of the queue. It has one parameter - name
of the queue and returns the deleted element. The function first checks if the queue is empty or not,
for successful deletion.
def dequeue(myQueue):
if not (isEmpty(myQueue)):
return myQueue.pop(0)
else :
print(“Queue is empty”)
Note: The pop() function with index[0] will delete the element from the beginning of the list, hence
Front of queue.

Page 95 of 200
A function (size) to get the number of elements in the queue. We can use the len() function of Python’s
list to find the number of elements in the queue. The function has one parameter - name of the queue
and returns the number of elements in the queue.
def size(myQueue):
return len(myQueue)
A function (peek) to simply read, but not to delete, the element at the front end of the queue. For this,
we can read the element at index[0] of the queue. The function has one parameter - name of the
queue and returns the value of element at Front if the queue is not empty, None otherwise.
def peek(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
return None
else:
return myQueue[0]
Let us consider the example of a queue that people form while waiting at a bank cash counter.
Usually, following are the events that occur in queue:
• Two friends come together and go to the cash counter, i.e. they form a queue - enqueue operation
is performed two times.
• As soon as the person at the front is serviced, he will be removed from the queue - thus dequeue
operation is performed. Cashier calls Next to serve the next person who is now at the front of the
queue.
• Cashier wants to know the length of the queue - size of the queue is checked.
• Meanwhile, a few more people walk in the bank, and three of them join the queue at the cash
counter, i.e. enqueue happens 3 times.
• Another person gets served and leaves the counter, i.e. dequeue is performed. Cashier calls Next
to serve another person.
• The Next three people get served one after another, i.e. dequeue is performed thrice.
• Cashier calls Next and realises that there are no more people to be served - underflow situation
happens
Now, let us write the code for the above scenario of the bank
Program 3-1 :
myQueue = list()
# each person to be assigned a code as P1, P2, P3,...
element = input("enter person’s code to enter in queue :”)
enqueue(myQueue,element)
element = input("enter person’s code for insertion in queue :")
enqueue(myQueue,element)
print("person removed from queue is:", dequeue(myQueue))
print(“Number of people in the queue is :”,size(myQueue))
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")

Page 96 of 200
enqueue(myQueue,element)
print("Now we are going to remove remaining people from the queue")
while not isEmpty(myQueue):
print("person removed from queue is ",
dequeue(myQueue))
Output of Program 3-1:
enter person’s code to enter in queue :P1
enter person’s code to enter in queue :P2
person removed from the queue is :p1
number of people in the queue is :1
enter person’s code to enter in queue :P3
enter person’s code to enter in queue :P4
enter person’s code to enter in queue :P5
Now we are going to remove remaining people from the queue
person removed from the queue is :p2
person removed from the queue is :p3
person removed from the queue is :p4
person removed from the queue is :p5
Queue is empty
6.5 Operations on Queue Using Python
1. ENQUEUE (Insert)
2. DEQUEUE (Delete)
1. ENQUEUE: It is used to insert a new element to the queue at the rear end. We can insert elements
in the queue till there is space in the queue for adding more elements. Inserting elements beyond
capacity of the queue will result in an exception - known as Overflow.
⮚ Let’s create a queue named myQueue. We can create it by assigning an empty list.
myQueue = list()
⮚ A function (enqueue) to insert a new element at the end of the queue. The function has two
parameters - name of the queue and element which is to be inserted in the queue.
def enqueue(myQueue, element):
myQueue.append(element)
Note: append() function always adds an element at the end of the list, hence Rear of queue.
2. DEQUEUE: It is used to remove one element at a time from the front of the queue. We can delete
elements from a queue until it is empty, trying to delete an element from an empty queue will result
in an exception - known as Underflow.
⮚ Let’s create a queue named myQueue. We can create it by assigning an empty list.
myQueue = list()
A function (dequeue) to delete an element from the front of the queue. It has one parameter - name
of the queue and returns the deleted element. The function first checks if the queue is empty or not,
for successful deletion.
def dequeue(myQueue):

Page 97 of 200
if not (isEmpty(myQueue)):
return myQueue.pop(0)
else :
print(“Queue is empty”)
Note: The pop() function with index[0] will delete the element from the beginning of the list, hence
Front of queue.
To perform enqueue and dequeue efficiently on a queue, following operations are also required:
• IS EMPTY : used to check whether the queue has any element or not, so as to avoid Underflow
exceptions while performing dequeue operation.
def isEmpty(myQueue):
if len(myQueue)==0:
return True
else:
return False
• PEEK : used to view elements at the front of the queue, without removing it from the queue.
def peek(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
else:
return myQueue[0]
• IS FULL : used to check whether any more elements can be added to the queue or not, to avoid
Overflow exceptions while performing enqueue operation.
Program3-2:
myQueue = list()
# each person to be assigned a code as P1, P2, P3,...
element = input("enter person’s code to enter in queue :”)
enqueue(myQueue,element)
element = input("enter person’s code for insertion in queue :")
enqueue(myQueue,element)
print("person removed from queue is:", dequeue(myQueue))
print(“Number of people in the queue is :”,size(myQueue))
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
print("Now we are going to remove remaining people from the queue")
while not isEmpty(myQueue):
print("person removed from queue is ",

Page 98 of 200
dequeue(myQueue))
Output 3-2
enter person’s code to enter in queue :P1
enter person’s code to enter in queue :P2
person removed from the queue is :p1
number of people in the queue is :1
enter person’s code to enter in queue :P3
enter person’s code to enter in queue :P4
enter person’s code to enter in queue :P5
Now we are going to remove remaining people from the queue
person removed from the queue is :p2
person removed from the queue is :p3
person removed from the queue is :p4
person removed from the queue is :p5
Queue is empty
6.5.1 Double ended Queue
A deque (pronounced "deck") is an arrangement in which element(s) can be added or removed from
any end, i.e., head/front or tail/rear. This data structure has no restrictions on the side from which
elements can be added or removed; hence, it can be used to build a stack or queue in a programme.
It is also known as a double-ended queue since it allows for insertion and deletion of operations from
either end.

Fig: 6.3 Basic deque structure displaying head and tail to implement stack or queue
6.6 Sequential / Linear Search
The most basic and simplest search method is linear search. It is a method of exhaustive searching
in which each member of a given list is compared to the object to be searched (commonly referred to
as the 'key'). As a result, each element in the list is compared to the key one by one. This method is
repeated until an element matching the key is located and the search is declared successful. If no
element matches the key after traversing the complete list, we declare the search to be unsuccessful,
indicating that the key is not present in the list. This item-by-item comparison is performed in the
order in which the elements appear in the list, commencing with the first element and progressing to
the last. As a result, it is also known as sequential search or serial search. This strategy is excellent
for collecting materials that are small and unsorted. Given a list n umL is I of n elements and key
value K, Algorithm uses a linear search algorithm to find the position of the key K in numL is I.
Algorithm fi. J: Linear Search
LinearSearch(numList, key, n)
Step 1: SET index = 0
Step 2: WHILE index < n, REPEAT

Page 99 of 200
Step 3: IF num1ist[index]= key THEN
PRINT “Element found at position”, index+ 1 STOP
ELSE
index = index+ 1
Step 4: PRINT “Search unsuccessful”
Example: Assume that the numList has seven elements [8, -4, 7, 17, 0, 2, 19]
so, n = 7. We need to search for the key, say 17 in numList. Table 6.1 shows the
elements in the given list along with their index values.
Table 6.1: Elements in numList alongwith their index value

The step-by-step process of linear search using Linear Search Algorithm is given in Table 6.2

Table 6.6. Linear search for key 17 in numList of table


Observe that after four comparisons, the algorithm found the key 17 and will display ‘Element found
at position 4’.
Let us now assume another arrangement of the elements in numList as [17, 8, -4, 7, 0, 2, 19] and
search for the key K=17 in numList.
Table 6.3 Elements in numList alongwith their index value

Table 6.4 Linear search for key 17 in numList given in Table 6.3

From Table 6.4 Therefore, it is obvious that the algorithm only needed to perform one comparison to
display 'Element found at location 1'. As a result, if the search key is the first item in the list, the
linear search algorithm will always have to conduct only one comparison. This is the very minimum

Page 100 of 200


amount of work that the linear search algorithm would have to perform. Assume that the elements
in numList are arranged as [8, -4, 7, 0, 2, 19, 17] and search for the key K =17 in numList. On a dry
run, we can see that the linear search method must compare each element in the list until the
message 'Element found at position 7' is displayed.As a result, if the key to be searched is the last
entry in the list, the linear search algorithm must perform n comparisons, where n is the number of
elements in the list. This is the greatest amount of work that the linear search algorithm would have
to perform.
Let us now assume another case, where the key being searched is not present in the list. For example,
we are searching for key = 10 in the num List.
In this scenario, the algorithm must also compare each element in the list until the message 'Element
is not found in the list' is displayed. If the key is not found in the list, the linear search algorithm
must perform n comparisons. This is another situation where the most work is done. Let us now
examine the linear search programme. It accepts a list of elements and the key to be searched as
input and returns either the location of the element in the list or an error message indicating that
the key is not present in the list.
Program3-3: Linear Search/Sequential Search
def linearSearch(list, key): #function to perform the search
for index in range(0,len(list)):
if list[index] == key: #key is present
return index+1 #position of key in list
return None #key is not in list
#end of function
list1 = [] #Create an empty list
maximum = int(input("How many elements in your list? "))
print("Enter each element and press enter: ")
for i in range(0,maximum):
n = int(input())
listl.append(n) #append elements to the list
print("The List contents are:", listl)
key = int(input("Enter the number to be searched:"))
position = linearSearch(list1, key)
if position is None:
print("Number",key,"is not present in the list")
else :
print("Number",key,"is present at position",position)
Output3-3:
How many elements are in your list? 4
Enter each element and press enter:
12
23
3
-45
The List contents are: [12, 23, 3, -45]

Page 101 of 200


Enter the number to be searched:23
Number 23 is present at position 2
6.7 Binary Lookup Consider the following scenario: We need to look up the definition of the word
"zoology" in an English dictionary. Where do we look in the dictionary?
1. In the first half of the game?
2. In the centre?
3. What happens in the second half?
Because the term begins with the letter 'Z,' it is best to look for it in the second half of the dictionary.
If we were looking for the definition of the word "biology", we would have looked at the first section of
the dictionary.
We were able to pick where to look in the dictionary because we were aware that all terms in an
English dictionary are listed alphabetically. We might avoid redundant comparisons by starting with
the first word in the dictionary and working our way to the end until we find the needed word.
However, if the words in the dictionary were not alphabetically organised, we would have to conduct
a linear search to determine the meaning of a word.
The binary search is a search strategy that uses the ordering of list members to quickly find a key.
The entries in the list for numeric values can be arranged in ascending or descending order of their
key values. Textual data can be organised alphabetically from a to z or from z to a.
The key to be searched is compared with the element in the middle of a sorted list in binary search.
This could lead to one of the following outcomes:
i) The element in the middle position corresponds to the key or
ii) The middle element is larger than the key; or
iii) The middle element is smaller than the key.
If the element in the middle place matches the key, the search is declared successful, and the
operation is completed.
If the middle element is greater than the key, it implies that the key must be in the first half of the
list. As a result, we can immediately disregard the second half of the list and repeat the search
procedure solely in the first half.
If the middle element is less than the key, then it follows that the key must be in the second half of
the list. As a result, we can immediately disregard the first half of the list and repeat the search
procedure solely in the second half. This splitting and list-size reduction proceeded until the key was
found or the remaining list contained only one item. If that item is not the key, the search fails since
the key is not in the list.
Thus, unlike a linear search of elements one by one, binary search can search more efficiently if the
list from which we wish to search is arranged in some order. In other words, the list must be sorted.
If the list to be searched has an even number of elements, the floor division (//) operator is used to
determine the mid value. If the list contains ten members, the middle position (mid) equals 10//2 =
5. As we know, the first element in the list has index value 0, hence the sixth member in the list is
regarded as the middle element. If necessary, the list is broken into two halves, with the first half
containing 5 elements and the second half containing 4 elements.
It's worth noting that the intermediate comparisons that don't locate the key still tell us about the
area of the list where the key might be found! They tell us whether the key is before or after the
current middle position in the list, which we use to restrict or narrow our search area. As a result,
each failed comparison reduces the number of elements left to be searched by half, hence the term
binary search. Let us now look at the binary search algorithm.
Given a list numList of n elements and key value K, Algorithm shows steps for finding position of the
key K in the numList using binary search algorithm.

Page 102 of 200


Algorithm Binary Search BinarySearch(numList, key)
Step 1: SET first = 0, last = n- 1
Step 2: Calculate mid = (first+las t) / /2
Step 3: WHILE first <= last REPEAT Step4
Step 4: IF numList[mid] = key
PRINT “Element found at position”,
" mid+ 1 STOP
ELSE
IF numList[mid] > key, THEN last
= mid-1
ELSE first = mid + 1
Step 5: PRINT “Search unsuccessful”
Example: Consider a sorted list comprising of 15 elements:
numList [2,3,5,7,10,11,12,17,19,23,29,31,37,41,43]
We need to search for the key, say 17 in numList. The first, middle and last element identified in
numList alongwith their index values are shown in Table
6.5 Table Elements in sorted numList alongwith their index value

6.6 Table Working of binary search using steps given in Algorithm

Note that the algorithm had to make only 1 iteration to display ‘Element found at position 8’. This is
because the key being searched is the middle element in the list. Thus, binary search requires only
1 iteration when the key to be searched is the middle value in the list. This is the minimum amount
of work binary search would have to do to confirm that a key is present in the list.
Now, let us search for key 2 in the list. numList = [2,3,5,7,10,11,12,17,19,23,29,3 1,37,41,43]
In the first iteration, we have the mid value as 17. As 2 is smaller than the mid value (17), we have
to search for the first half of the list in the second iteration. We now consider only 7 elements. As 2
is smaller than the new mid value (7), we have to search for the first half of the remaining list in the
third iteration. We now consider only 3 elements. Observe that the number of elements in the numList
is halved each time. It reduces from 15 elements in iteration 1 to 7 elements in iteration 2, and to 3
elements in iteration 3. In the 3rd iteration, the algorithm finds that key 2 is smaller than the new

Page 103 of 200


mid value (3), we have to search in the first half of the remaining list. The list now has only 1 element
in the fourth iteration and on comparison, it is found that the element is the same as key. Hence, the
search terminates successfully and returns the position of key. Steps followed for binary search are
given in Table 6.7.
Table 6.7. Searching key = 2 in the numList using binary search

As we can see, the binary search algorithm had to make 4 iterations to narrow down the list to a
single element and decide that the search key is the first element of the list. This is clearly the
maximum work required to find a key in the given list.
Program3-6. Binary Search
def binarySearch (list, key):
first = 0
last = len (list) - 1
while (first <= last):
mid (first + last) //2
if list [mid] == key:
return mid
elif key> list(mid]:
first mid + 1
elif key < list [mid]:
last = mid -1
return -1
listl=[] #Create an empty list
print ("Create a list by entering elements in ascending order")
print ("press enter after each element, press -999 to stop")
num = int(input ())
while num!=-999:
list1.append(num)
num = int (input())

Page 104 of 200


n = int(input ("Enter the key to be searched: "))
if (pos ! = -1):
print ( n, “ is found at position “,pos + 1)
else :
print (n, is not found in the List
Output of program 3-4
Create a list by entering element in ascending order press enter after each element, press -999 to
stop
1
3
4
5
-999
Enter the number to be searched: 4
4 is found at position 3
Second run of the program with different data:
Create a list by entering elements in ascending order
Press enter after each element, press -999 to stop
12
8
3
-999
Enter the number to be searched: 4
4 is not found in the list
6.8 Bubble Sort
The first sorting technique we'll look at is bubbles sort. It sorts a given list of components by
comparing neighbouring elements repeatedly and swapping them if they are unordered. Swapping
two elements means shifting their relative positions. Every iteration of each element of a list in an
algorithm is referred to as a pass. To sort a list of n entries, the bubble sort makes a total of n minus
1. The needed pairs of nearby list entries will be compared in each pass. To organise elements in
ascending order, after each pass, the largest element is detected and placed in the appropriate
location in the list. This is the largest element that has 'bubbled up'. As a result, the name Bubble
Sort" was coined. This sorted element is not examined in the subsequent passes, so the list of
elements is reduced in each run.
Figure 6.4 Demonstrates the working of the bubble sort method to arrange a list in ascending order.
Let us consider a list having 5 elements as numList = [5, 1, 4, 2, 8, ]. In the figure, elements being
compared are highlighted with blue colour and sorted elements are highlighted with White colour. If
the first element is bigger, it is swapped with the second. Else, no change is done. This continues till
the end of the list is reached. After the first pass, the largest element will reach the end of the list as
shown in Figure 6.4 with white colour

Page 105 of 200


Fig6.4: Bubble Sorting
Program
using System;
public class Bubble_Sort
{
public static void Main(string[] args)
{
int[] a = { 3, 0, 2, 5, -1, 4, 1 };
int t;
Console.WriteLine("Original array :");
foreach (int aa in a)
Console.Write(aa + " ");
for (int p = 0; p <= a.Length - 2; p++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("\n"+"Sorted array :");
foreach (int aa in a)
Console.Write(aa + " ");

Page 106 of 200


Console.Write("\n");
}
}
Output:
Original array :
3 0 2 5 -1 4 1
Sorted array ;
-1 0 1 2 3 4 5
6.9 Selection Sort
Another sorting technique is selection sorting. To sort an n-element list, the selection sort (n-1) passes
through the list. The list is separated into two lists: the left list, which contains the sorted
components, and the right list, which contains the unsorted elements. The left list is initially empty,
and the right list contains all of the entries.
To arrange elements in ascending order, the first pass traverses the entire unsorted list to identify
the smallest element. The smallest element is then exchanged with the unsorted list's leftmost
element. This element is the first in the sorted list, and it is ignored in subsequent passes. In the
second pass, the next smallest element is chosen from the remaining elements in the unsorted list
and swapped with the unsorted list's leftmost element. This element is positioned second in the sorted
list, and the unsorted list shrinks by one element for the third run.
This operation is repeated until the n-1 smallest elements are located and moved to their proper
locations. The nth element is the final one, and it is already present. The figure shows how the
selection sort method works to arrange a list in ascending order. The items being compared in this
figure are represented by arrows, and the smaller element in a comparison is highlighted in blue. The
items that have been sorted are highlighted.

Fig 6.5: Selection Sort

Page 107 of 200


Program
<?php
function selection_sort($data)
{
for($i=0; $i<count($data)-1; $i++) {
$min = $i;
for($j=$i+1; $j<count($data); $j++) {
if ($data[$j]<$data[$min]) {
$min = $j;
}
}
$data = swap_positions($data, $i, $min);
}
return $data;
}
function swap_positions($data1, $left, $right) {
$backup_old_data_right_value = $data1[$right];
$data1[$right] = $data1[$left];
$data1[$left] = $backup_old_data_right_value;
return $data1;
}
$my_array = array(3, 0, 2, 5, -1, 4, 1);
echo "Original Array :\n";
echo implode(', ',$my_array );
echo "\nSorted Array :\n";
echo implode(', ',selection_sort($my_array)). PHP_EOL;
?>
Output:
Original array :
3 0 2 5 -1 4 1
Sorted array ;
-1 0 1 2 3 4 5
6.10 Hashing
Hashing is a technique for determining the presence of a key in a list in a single step. The notion is
that if we already know the value at each index position in a list, we can check the presence or
absence of a key in that list with a single comparison. Hashing improves the efficiency of search
processes. The value at an index in the list is calculated using a formula known as the hash function.
As a result, a hash function iterates across a list, generating an index value for each entry. This will
create a new list known as the hash table. Each hash table index can only retain one item, and the
places are indexed by integer values beginning with 0. It should be noted that the hash table can be
larger than the list.

Page 108 of 200


A simple hash function that works with numeric values is known as the remainder method. It takes
an element from a list and divides it by the size of the hash table. The remainder generated is called
the hash value.
h(element) = element % size(hash table)
We can easily implement a hash table using Python's list. Let us consider an empty hash table having
10 positions as shown in Table
Table6.8: An Empty hash table with 10 positions

Let us consider a list of numbers (34, 16, 2, 93, 80, 77, 51). We can use the hash function remainder
method explained earlier to create a hash table as shown in table below
Table6.9: hash function element % 10 applied on the elements of list

After computing the hash values, each element is inserted at its designated position in the hash table
shown in Table below

Now, to search for a key, we can calculate its index using the hashing function and compare the
element at that index with the key to declare whether the element is present in the list or not. This
search operation involves just one comparison and hence the same amount of time is always required
to search a key irrespective of the size of the list.
Program: Use of hashing to find a key in the given list L
#Function to check if a key is present or not def hashFind(key,hashTable)
Def hashfind (key, hashtable):
If (hashtable [key % 10] == key): #key is present
return ( ( key % 10)+ 10) #return the position
else:
return None #key is not present
#end of function
#create hashtable with 10 empty positions
Hashtable = [None, None, None, None, None, None, None, None, None, None
]
Print (“We have created a hashtable of 10 positions :”)
Print (hashTable)

Page 109 of 200


L = [34, 16, 2, 93, 80, 77, 51 ]
Print (“The given list is”, L [ : : ] )
# Apply hash function
For i in range (0, Len(L) ) :
hashTable [L[ I ] % 10 ] = L [ i ]
print (“ The hash table contents are: “ )
for i in range (0, len(hashtable) ) :
print (“hashindex=”, I,” , value =”, hashTable [i] )
key = int (input (“Enter the number to be searched: “) )
position = hashFind (key, hashTable)
if position is None:
print (“Number”, key, “is not present in the hash table”)
else:
print (“Number”, key, “ present at “, position, “ position”)
Output
We have created a hashTable of 10 positions:
[None, None, None, None, None, None, None, None, None, None]
The given list is [34, 16, 2, 93, 80, 77, 51)
The hash table contents are:
hashindex= 0 , value = 80
hashindex= 1 , value = 50
hashindex= 2 , value = 2
hashindex= 3 , value = 93
hashindex= 4 , value = 34
hashindex= 5 , value = None
hashindex= 6 , value = 16
hashindex= 7 , value = 77
hashindex= 8 , value = None
hashindex= 9 , value = None
Enter the number to be searched: 16
Number 16 present at 7 position

Page 110 of 200


Unit 3. Software Engineering
Chapter 7. Software Engineering Concepts
INTRODUCTION
In this session, you will understand the concept of software engineering and how software products
are developed. The various software development models and the various concepts associated with
software engineering such as requirement specification, analysis, design, coding, testing and
maintenance are also discussed. You will also understand the importance of software engineering in
the software product development process.
7.1 SOFTWARE ENGINEERING
Before discussing software, engineering let’s find out what exactly software and engineering is. The
word software engineering is the creation of two words, software and engineering. The first word
software means it is a set of programs, the program containing instructions that provide particular
functionality. It also consists of data structure. These data structures help us in manipulating
information. The second word engineering means the set of steps followed to design and build
something that serves a particular purpose. Finally, software engineering is a systematic approach to
the development, operation and maintenance of desired software.
Program and software are related terms but have distinct differences:
Program: A program is a set of instructions written in a programming language that performs a specific
task. It is a sequence of code that can be executed by a computer to solve a particular problem.
Programs are often designed for a specific purpose and are not typically used by end-users directly.
Examples of programs include compilers, interpreters, and code libraries.
Software: Software is a broader term that refers to a collection of programs, data, and other supporting
elements that work together to perform a specific function or set of functions. Software can be thought
of as a complete package that provides a solution to a particular problem or set of problems. Software
includes programs, documentation, and any other components required to use or support the program.
Software can be categorised into different types, such as system software, application software, and
utility software.
Program Software
A set of instructions written in a A collection of programs, data, and other
programming language that performs supporting elements that work together to
a specific task. perform a specific function or set of functions.

Often designed for a specific Can be thought of as a complete package that


provides a solution to a particular problem or
purpose and are not typically used
set of problems.
by end-users directly.
Examples include compilers, Includes programs, documentation, and
interpreters, and code libraries.
any other components required to use or
support the program.
Typically created by developers to Can be categorised into different types,
solve a specific problem. such as system software, application software,
and utility software.
Can be executed by a computer Can be used by end-users to accomplish
to solve a particular problem. specific tasks or achieve specific goals.

Software Engineering: "Software engineering as an engineering branch associated with the


development of software products using well-defined scientific principles, methods and procedures.

Page 111 of 200


The outcome of software engineering is an efficient and reliable software product”. (When software is
made for a specific purpose. It is known as software as a product.)
7.1.1 Program vs. Software Product:
I. A program is a set of instructions that are given to a computer in order to achieve a specific task
whereas software is when a program is made available for commercial business and is properly
documented along with its licensing. Software = Program+documentation+licensing.
II. A program is one of the stages involved in the development of the software, whereas a software
development usually follows a life cycle, which involves the feasibility study of the project,
requirement gathering, and development of a prototype, system design, coding, and testing.
Software Engineering is mainly used for large projects based on software systems rather than single
programs or applications. The main goal of software engineering is to develop software applications for
improving the quality, budget and time efficiency. Software Engineering ensures that the software that
has to be built should be consistent, correct, also lies in budget, efficiency and fulfil requirements.
7.1.2 NEED OF SOFTWARE ENGINEERING
The need for software engineering arises due to the increasing importance of software in our daily lives.
With the advent of computers and the internet, software has become an essential component of many
products and services, from smartphones and web applications to complex systems used in industries
and organisations.
Software engineering provides a structured and systematic approach to the development of software
systems, which ensures that the software is reliable, efficient, and effective. Some of the key reasons
why software engineering is essential include:
(i) Meeting customer requirements: Software engineering ensures that software systems are
developed to meet the specific requirements of the end-users. This helps to ensure that the
software systems are useful, usable, and satisfy the needs of the customers.
(ii) Managing complexity: Software engineering provides methods and techniques to manage the
complexity of developing large-scale software systems. It allows developers to break down the
system into manageable components and focus on each component's development.
(iii) Improving quality: Software engineering provides a structured and systematic approach to
software development, including testing and quality assurance, which helps to ensure that the
software is reliable, efficient, and effective.
(iv) Reducing development costs: Software engineering helps to reduce the cost of software
development by providing a framework for project management, risk management, and change
management. This helps to ensure that the project stays within budget and timeline.
(v) Ensuring software security: Software engineering provides methods and techniques to ensure
the security of software systems. This is important as software systems can be vulnerable to
cyber-attacks, and their security can have serious consequences, from data breaches to financial
losses.
In conclusion, software engineering is essential to ensure the development of high-quality, reliable,
and efficient software systems that meet the needs of customers and businesses while staying within
budget and timeline. It provides a structured and systematic approach to software development, which
ensures that software systems are secure and meet the required quality standards.
7.1.3 CHARACTERISTICS OF GOOD SOFTWARE
Good software is a software that is well-designed, reliable, efficient, maintainable, and user-friendly.
Here are some characteristics that define a good software:
(i) Functionality: Good software should fulfil its intended purpose and meet the requirements of
its users. It should perform the functions it was designed to do efficiently and accurately.
(ii) Reliability: Good software should be reliable and predictable. It should perform consistently and
without errors, even under varying conditions.

Page 112 of 200


(iii) Usability: It should be easy to use and understand. It should have a user-friendly interface that
is intuitive and straightforward, making it easy for users to accomplish tasks quickly and
efficiently.
(iv) Efficiency: Good software should be efficient and fast, using minimal system resources while
performing its functions. It should run smoothly without causing slowdowns or crashes.
(v) Maintainability: It should be easy to maintain and update. It should have a modular design that
allows for changes to be made without affecting the entire system, and should have a clear and
concise code structure.
(vi) Security: Good software should be secure, protecting against unauthorised access and malicious
attacks. It should implement security measures such as encryption, authentication, and access
control.
(vii) Scalability: Good software should be able to handle increasing demands as the user base grows.
It should be designed to scale up or down depending on the workload.
In summary, good software is characterised by its functionality, reliability, usability, efficiency,
maintainability, security, and scalability. By adhering to these characteristics, software developers can
create high-quality software that meets the needs of users and businesses.
7.1.4 Importance of studying software engineering
There are several reasons why studying software engineering is important:
(i) In-demand career: The demand for software engineers is constantly growing due to the
increasing reliance on technology in all aspects of business and daily life. Studying software
engineering can lead to a highly lucrative career with excellent job opportunities and potential
for growth.
(ii) Continuous innovation: Technology is constantly evolving, and software engineering provides
the skills and knowledge necessary to stay up-to-date with the latest developments in the field.
Studying software engineering ensures that individuals are equipped to adapt to changing
technologies and create innovative solutions.
(iii) Problem-solving skills: Software engineering involves identifying problems and developing
solutions using a systematic and analytical approach. Studying software engineering provides
individuals with critical thinking and problem-solving skills that can be applied to a wide range
of fields and industries.
(iv) Collaboration and teamwork: Software engineering is a highly collaborative field that requires
individuals to work together to develop and implement solutions. Studying software engineering
can help individuals develop communication and teamwork skills that are essential in any career.
(v) Quality assurance: Software engineering involves rigorous testing and quality assurance
processes to ensure that the software is reliable and meets the needs of users. Studying software
engineering can provide individuals with the skills necessary to design and implement high-
quality software that meets user needs.
Studying software engineering provides individuals with in-demand skills, problem-solving abilities,
collaboration and teamwork skills, and a commitment to quality assurances, all of which are highly
valued in a wide range of industries and can lead to rewarding and fulfilling careers.
7.1.5 PURPOSE OF SOFTWARE ENGINEERING
I. Reduces complexity: Software engineering divides large problems into several small problems
and solves them one by one. All these problems are solved independently.
II. Minimising software cost: Software engineering programmers plan everything and reduce all
those things that are not required.
III. Time saving: If you are developing software according to software engineering approach then it
will reduce a lot of time.
IV. Handling big projects: To handle big projects without any problems a company has to go for a
software engineering approach.

Page 113 of 200


V. Reliable Software: To deliver software products in time and solve any bugs in the software is
the company's responsibility. if any bugs are in the software then the company is responsible
for solving all these bugs.
VI. Effectiveness: Effectiveness comes if anything has been made according to the standards.
VII. Productivity: Proper care of software productivity is taken because it has a testing system at
every level.

Handling Big
Project

Effectiveness Reliable

Purpose of
Software
Engineering
decrease
Productivity
time

Reduces Manage the


complexity cost

Fig. 7.1: Purpose of software engineering


7.2 SOFTWARE DEVELOPMENT LIFE CYCLE
SDLC stands for “Software Development Life Cycle” model given below is a pictorial and diagrammatic
representation of the software life cycle. It describes the sequence of phases or steps to develop any
software. It is a collection of processes which are followed to develop software. SDLC is a methodology
that defines some processes which are followed to develop high-quality software. It covers the detailed
plan for building, deploying and maintaining the software. The main aim of SDLC is to define all the
tasks required for developing and maintaining software. It is followed by a software project within a
software developing organisation. Generally, it can be defined as the framework for developing the
software. In simple words “entire life time of software from beginning to ending".

Requireme
nt Analysis

Feasibility
Maintenance
Study

SDLC
(Software
Development
Deployment Life Cycle) Design

Testing Coding

Fig.7.2: The phases of software development life cycle

Page 114 of 200


(i) Requirement Analysis: It is the first phase of SDLC in which all necessary information is collected
from the customer to develop the software as per their expectation. Some important questions like:
What is the need of software, who will be the end-user, what is the future scope of that software etc.
are discussed. The main aim of this phase is to collect the details of each requirement of the customer
so that the developers will clearly understand what they are developing and how to fulfil the customer's
requirements.
(ii) Feasibility Study: It is the second phase of SDLC in which an organisation discusses the costs
and benefits of the software. It is conducted in order to find answer to the following question:
● Do we require resources and technologies to build the project?
● Do we receive profit from the project?
It is an important phase because profit from the software plays an important role as if cost is very high
then company may face loss. After the feasibility study, the project may be accepted, with modifications
or rejected. It measures how beneficial the product is for the organisation.
There are five types of feasibility study: -
a) Technical feasibility
b) Economic feasibility
c) Legal feasibility
d) Operational feasibility
e) Scheduling feasibility
a) Technical feasibility: In technical feasibility, we check whether we have required technical
resources (like hardware and software) to develop the project. This feasibility study also analyses
technical skills and capabilities of the technical team, existing technology can be used or not,
maintenance and up-gradation is easy or not for chosen technology etc.
b) Economic feasibility: In economic feasibility study, cost and benefit of the project is analysed. In
this feasibility study, a detailed analysis is carried out to know what will be the cost of the project
including hardware and software resources required, design and development cost and so on. It is
also analysed whether the project will be beneficial for the organisation or not.
c) Legal feasibility: In legal feasibility study we investigate whether the project is legal or not and
whether it tells into the category of all sorts of legal authoritarian permits and requirements.
d) Operational feasibility: In operational feasibility study, we examine whether the project satisfies
the requirements identified in the requirement analysis phase or not.
e) Scheduling feasibility: In scheduling feasibility study, we estimate the time necessary to complete
the project. We also consider the organisation’s capabilities and determine whether that amount of
time is available or not.
(iii) Design: It is the third phase in which architects start working on logical designing of the software.
In this phase a SRS (System Requirement Specification) document is created which contains all logical
details like how the software will look like, which language will be used, database design, modular
design etc. This phase provides a prototype of the final product. Basically, all it includes is the design
of everything which has to be created.
(iv) Coding: When the designing of the software is completed, then, a group of developers start coding
of the design using a programming language. The interface of the software and all its internal working
according to design phase in implemented coding phase. A number of developers code the modules
and then all modules are arranged together to work efficiently. It is the longest phase of SDLC.
(v) Testing: Once the software development is completed, then it is sent to the testers. The testing
team starts testing the functionality of the entire system. In this phase, the software is checked for
bugs or errors. Whenever a bug is found, then the software is re-sent to the coders to fix it and then

Page 115 of 200


overall software is re-tested. This is done to verify that the entire application works according to the
customer requirement.
(vi) Deployment: After overall testing of the software and after checking that it is bug free, then the
software is launched and available for the users to use it. Even after deployment of the software, if any
bugs or errors are still found the software is re-evaluated by the maintain team and then it is re-
deployed with a new version.
(vii) Maintenance: The maintenance team looks over the software usage and user’s feedback.
Maintenance is necessary to eliminate errors in the system during its working life. The bug fixing,
upgradation and enhancement of the software is looked over by the maintenance.
7.3 Software Development Life Cycle Models
SDLC Models stands for software development life cycle models. It is also known as a process model.
The process followed in the development of the software depends upon the life cycle model chosen for
development. There are various SDLC models defined and designed which are followed during the
software development process. Every developed software is different and it requires a suitable SDLC
approach to be followed based on the internal and external factors. Life cycle model defines the major
phases during and after the software development process.
SDLC models can be categorised into the following types:
(i) Waterfall model
(ii) Iterative model
(iii) Prototype model
(iv) Spiral model
(v) Agile model
7.3.1 Waterfall Model
The waterfall model was the first SDLC Model to be introduced. It is based on the basic concept of
SDLC. In the waterfall approach, the whole process of software development is divided into separate
phases. Each phase must be completed before the next phase can begin and there is no overlapping
in the phases. The waterfall model illustrates the software development process in a linear sequential
flow. It means that every phase in the development process begins only if the previous phase is
completed. It is also known as a linear-sequential life cycle model. In this SDLC model, the outcome of
one phase acts as the input for the next sequentially. Using the waterfall model when it is appropriate
a) There are very clear and fixed requirements.
b) There are no ambiguous requirements.
c) Tools and technology used are consistent and are not changing.
d) Resources are well prepared and are available to be used.
e) The duration of a project is short.
f) There is minimum risk.

Page 116 of 200


Fig.7.3: The phases of waterfall model
Advantages of Waterfall Model
a) Simple and easy to understand and use.
b) Works well for smaller projects where requirements are well-understood and fixed.
c) Easy to manage due to the rigidity of the model.
d) Phases are processed and completed one at a time.
e) Clearly defined stages.
f) Easy to classify and prioritise tasks.
g) Process and results are well-documented.
Disadvantages of Waterfall Model
a) Not the best choice for complex and large projects.
b) Not suitable for the projects where requirements are at a moderate to high risk of changing.
c) Adjusting scope during the life cycle can end a project.
d) No working software is produced until late during the life cycle.
7.3.2 Iterative Model
In the Iterative model, the software development is initiated with small sets of requirements and then
the software is iteratively enhanced, until the complete system is implemented and ready to be
deployed. In this model, software development is not attempted to be started with a full specification
of requirements. Instead, development begins by specifying just some part of the software, which is
then reviewed to identify the software for bugs and evolution. Enhancing software is repeated and
reviewed by the developers and hence results in producing a new version of that software at the end
iteration of the model. This model focuses on an initial, simplified implementation, which then
progressively gains more complexity and broader features until the final system is completed. The basic
concept behind this method is to develop a software through repeated cycles (iteration) and in smaller
portions at a time (incremental). For rapid or fast development, the iterative model has become a
generally accepted SDLC model over the traditional waterfall model. Using the iterative model when it
is appropriate with the following terms/requirements/features:
a) Major requirements of the project are clearly defined.

Page 117 of 200


b) Some features or enhancements may be added with time.
c) The project is large.
d) Resources with needed skill sets are not available and are planned to be used on a contract basis.
e) High-risk features and goals which may change in the future.
f) A new technology is being used and is being learnt by the development team while working on the
project.

Fig.7.4: The phases of Iterative Model


Advantages of Iterative Model:
a) Bugs and errors in the software can be identified early.
b) Software is produced early which facilitates customer evaluation and feedback.
c) It is easily adaptable to the ever-changing needs of the client.
d) The end-user can give their feedback quickly.
e) Parallel development can be planned.
f) Risks are identified and resolved during iteration.
g) Less time is spent on documentation and more time is given to development.
Disadvantages of Iterative Model:
a) As the requirements change, many times it causes over budget and over time.
b) It is not suitable for smaller projects.
c) Project completion date is unpredictable as the software keeps on changing in iterations.
d) There can be a requirement for more resources.
e) More management attention is required
f) Design can be changed time by time because of imperfect requirements.
7.3.3 Prototype Model
Prototype model is a software development model in which a prototype (i.e., an early sample of the final
product) is built and evaluated. And then rework is done on it as per client requirements, until a final
acceptable product is achieved. A prototype is a working model of software with some limited
functionality. A prototype does not always hold the exact logic used in the actual software application.
It helps to get valuable feedback from the customer and helps software designers and developers to
understand what exactly is expected from the product under development. Using the prototype model
when it is appropriate as per the following points:
a) When requirements are not clear.
b) If the project is a Mid-Large project.
c) When a client can communicate without delays.

Page 118 of 200


Fig.7.5: The phases of Prototype Model
Advantages of Prototype Model:
a) Client feedback is received quickly which speeds-up the development process.
b) Developed prototypes can be used later for any similar projects.
c) Missing functionalities and errors can be detected early.
d) Software designers and developers understand what exactly is expected from the product.
Disadvantages of Prototype Model:
a) Prototyping may be a slower and time-taking process.
b) Risky for fresh developers.
c) Poor documentation due to changes in the requirements.
d) Regular meetings are vital to keep the project on time.
7.3.4 Spiral Model
Spiral model is a risk handling software development model. It is a combination of the iterative nature
of prototyping with the controlled and systematic aspects of the waterfall model. There are some phases
that are completed in one iteration (Spiral) and the output is a small prototype of the large software.
Then the same iteration is repeated until the entire software is built. The entire process of development
is divided into four different stages which keeps on repeating until the entire project is completed.
These stages are:
a) Determining objectives and alternate solutions
b) Identifying and resolving risks
c) Development of product
d) Review and plan for the next phase
The spiral model is also called “Meta-Model” as it includes all the other SDLC models. It is a risk-driven
model, i.e, the overall success of a project highly depends on the risk analysis phase. Using the spiral
model when it is appropriate as per the following points.
a) This model is best used for large projects which involve continuous enhancements.
b) It is used where the requirements are of developing a full-fledged, large, complex system with a lot
of features and facilities from scratch.
c) It may be used when experimenting on technology, trying out new skills, or where the user is not
able to offer requirements clearly.

Page 119 of 200


Fig.7.6: The phases of spiral Model
Advantages of Spiral Model
a) Bulky and complex systems can be made easily because of the risk management factor.
b) Changing requirements can be accommodated.
c) Users see the system early.
Disadvantages of spiral model
a) Management of this model is more complex.
b) Completion of the project may not be known early.
c) Not suitable for small or low risk projects and could be expensive for small projects.
d) The successful completion of the project is very much dependent on Risk analysis.
e) Time estimation is very difficult.
7.3.5 Agile Model
The term Agile means “the ability to respond to change” and here changes refer to changes in terms of
requirement, technology and people. Agile model is a combination of iterative and incremental process
models. It is basically a methodology that promotes continuous development and testing of the software
throughout the SDLC of the project. It focuses on process flexibility and customer satisfaction by fast
delivery of working software products. In this the product is broken into small builds provided in
iterations. Iteration typically lasts from about two to three weeks. Each iteration comprises cross
functional teams working simultaneously on various areas. Each build is incremental in terms of
features and the final build holds all the features required by the customer. A working product is
provided to the customer and important stakeholders at the end of each iteration. Using the agile
model when it is appropriate as per the following points:
a) When frequent changes are required.
b) When a highly qualified and experienced team is available.
c) When a customer is ready to have a meeting with a software team all the time.
d) When project size is small.

Page 120 of 200


Fig.7.7: The phases of Agile Model
Advantages of Agile Model:
a) It can be delivered fast.
b) It facilitates Face-to-Face communication with clients.
c) It is responsible for efficient design and fulfils the business requirement.
d) Changes in this model are acceptable any time.
e) The process of feedback is fast.
f) It Improves the quality of software development.
Disadvantages of Agile Model
a) It depends heavily on customer interaction, so if the customer is not clear, the team can be driven
in the wrong direction.
b) It needs less documentation.
c) It requires good collaborations between team members.
d) It requires a client-oriented team.
7.4 REQUIREMENTS ANALYSIS AND SPECIFICATION
Before we start to develop our software, it becomes quite essential for us to understand and document
the exact requirements of the customer. Experienced members of the development team carry out this
job. They are called system analysts.
The analyst starts requirements gathering and analysis activity by collecting all information from the
customer which could be used to develop the requirements of the system. He then analyses the
collected information to obtain a clear and thorough understanding of the product to be developed,
with a view to remove all ambiguities and inconsistencies from the initial customer perception of the
problem. The following basic questions pertaining to the project should be clearly understood by the
analyst in order to obtain a good grasp of the problem:
(a) What is the problem?
(b) Why is it important to solve the problem?

Page 121 of 200


(c) What are the possible solutions to the problem?
(d) What exactly is the data input to the system and what exactly is the data output by the system?
(e) What are the likely complexities that might arise while solving the problem?
(f) If there is external software or hardware with which the developed software has to interface, then
what exactly would the data interchange formats with the external system be?
After the analyst has understood the exact customer requirements, he proceeds to identify and resolve
the various requirements problems. The most important requirements problems that the analyst has
to identify and eliminate are the problems of anomalies, inconsistencies, and incompleteness. When
the analyst detects any inconsistencies, anomalies or incompleteness in the gathered requirements,
he resolves them by carrying out further discussions with the end users and the customers.
7.4.1 Properties of a good SRS document
The important properties of a good SRS document are the following:
(i) Concise: The SRS document should be concise and at the same time unambiguous, consistent,
and complete. Verbose and irrelevant descriptions reduce readability and also increase error
possibilities.
(ii) Structured: It should be well-structured. A well-structured document is easy to understand and
modify. In practice, the SRS document undergoes several revisions to cope up with the customer
requirements. Often, the customer requirements evolve over a period of time. Therefore, in order to
make the modifications to the SRS document easy, it is important to make the document well-
structured.
(iii) Black-box view: It should only specify what the system should do and refrain from stating how to
do these. This means that the SRS document should specify the external behaviour of the system
and not discuss the implementation issues. The SRS document should view the system to be
developed as black box, and should specify the externally visible behaviour of the system. For this
reason, the SRS document is also called the black-box specification of a system.
(iv) Conceptual integrity: It should show conceptual integrity so that the reader can easily understand
it.
(v) Response to undesired events: It should characterise acceptable responses to undesired events.
These are called system responses to exceptional conditions.
(vi) Verifiable: All requirements of the system as documented in the SRS document should be
verifiable. This means that it should be possible to determine whether or not requirements have
been met in an implementation.
Requirements engineering is the process of gathering, analysing, documenting, and managing the
requirements for a software system. It is a crucial step in software development because it ensures
that the software being developed meets the needs and expectations of stakeholders, including
customers, users, and developers. The importance of requirements engineering lies in its ability to
ensure that the software being developed satisfies the needs of all stakeholders.
Here are some specific reasons why requirements engineering is important:
I. Understand user needs: Requirements engineering helps to understand the needs and
expectations of the users, which is crucial to developing software that is user-friendly and meets
the customer/users' needs.
II. Define system scope: It helps to define the scope of the system, including what it will and will
not do. This helps to ensure that the development effort is focused on the most important features
and functions.
III. Identify potential problems: Requirements engineering helps to identify potential problems early
in the development process, before significant resources have been invested in the project. This
can help to reduce the risk of costly mistakes later on.
IV. Manage project scope: Requirements engineering helps to manage the scope of the project,
ensuring that the development effort remains within budget and on schedule.

Page 122 of 200


V. Facilitate communication: It helps to facilitate communication between stakeholders, including
customers, users, and developers. This can help to ensure that everyone is on the same page and
that there are no misunderstandings or miscommunications.
Requirements engineering is a critical step in software development that helps to ensure that the
software being developed meets the needs and expectations of all stakeholders. By understanding user
needs, defining system scope, identifying potential problems, managing project scope, and facilitating
communication, requirements engineering can help to ensure the success of software development
projects.
7.4.2 Problems without a SRS (Software Requirements Specifications) document
The important problems that an organisation would face if it does not develop a SRS document are as
follows:
(a) Without developing the SRS document, the system would not be implemented according to
customer needs.
(b) Software developers would not know whether what they are developing is exactly what is required
by the customer or not.
(c) Without an SRS document, it will be very much difficult for the maintenance engineers to
understand the functionality of the system.
(d) It will be very much difficult for user document writers to write the users’ manuals properly
without understanding the SRS document.
7.4.3 Problems with an unstructured specification:
(a) It would be very much difficult to understand that document.
(b) It would be very difficult to modify that document.
(c) Conceptual integrity in that document would not be shown.
(d) The SRS document might be unambiguous and inconsistent.
7.4.4 Parts of a SRS document:
(i) Functional and non-functional requirements
(ii) The software requirements document
(iii) Requirements specification
(iv) Requirements engineering processes
(v) Requirements elicitation and analysis
(vi) Requirements validation
(vii) Requirements management
(i) Functional and non-functional requirements: Functional requirements are specific actions that
a system must perform in order to meet the needs of its users. They describe the features and functions
of the system that enable it to perform its intended tasks. Examples of functional requirements might
include:
● The ability to log-in and out of the system.
● The ability to search for and retrieve information.
● The ability to process and store data.
● The ability to generate reports and other output.
● The ability to send and receive messages or notifications.
Non-functional requirements, on the other hand, describe how the system should perform its
functions, rather than what those functions are. They define the overall characteristics of the system,

Page 123 of 200


such as its performance, reliability, and security. Examples of non-functional requirements might
include:
● Response time: The system should respond to user inputs within a certain amount of time.
● Scalability: The system should be able to handle increasing amounts of data or users without
significant degradation in performance.
● Availability: The system should be available for use by users for a certain period of the time.
● Security: The system should be designed with appropriate security measures to protect against
unauthorised access or data breaches.
Both functional and non-functional requirements are important for defining the scope and
specifications of a system or project. It is important to clearly identify and document both types of
requirements to ensure that the system meets the needs of its users and stakeholders.
(ii) The software requirements document: A software requirements document (SRD) is a formal
document that outlines the requirements for a software project. It serves as a communication tool
between stakeholders, such as the development team, project managers, and clients, to ensure that
everyone is on the same page about what the software should do and how it should perform.
The SRD typically includes both functional and non-functional requirements, as well as any
constraints or assumptions that the project is working within. The document should be as detailed
and specific as possible to ensure that the development team has a clear understanding of what needs
to be built.
Some key components of an SRD (software requirements document) may include:
a) Introduction: This section should provide an overview of the project, including its goals, scope,
and stakeholders.
b) Functional requirements: This section should detail the specific features and functions that the
software must perform, including any user interfaces or data processing requirements.
c) Non-functional requirements: This section should detail the performance, reliability, and
security requirements of the software, including any regulatory or compliance standards that must
be met.
d) Assumptions and constraints: This section should detail any assumptions or constraints that
the project is working within, such as budget or time constraints.
e) Acceptance criteria: This section should detail the criteria that will be used to determine whether
the software has met the requirements outlined in the document.
f) Sign-off: This section should include a place for stakeholders to sign-off on the document,
indicating their agreement with the requirements.
(iii) Requirements specification: Requirements specification is the process of defining, documenting,
and validating the requirements for a software project. The goal of requirements specification is to
create a clear and complete set of requirements that accurately reflect the needs of the project's
stakeholders, including users, customers, and other interested parties.
The requirements specification process typically involves several steps, including:
a) Requirements gathering: This involves collecting information about the project's stakeholders,
their needs, and any constraints or limitations that may impact the project.
b) Requirements analysis: This is responsible for analysing the information gathered in the first
step to identify patterns and themes, as well as any conflicts or inconsistencies that may need to
be resolved.
c) Requirements documentation: This comprises documenting the requirements in a clear and
concise manner, using a standard format such as a software requirements document (SRD).

Page 124 of 200


d) Requirements validation: This includes reviewing and validating the requirements to ensure that
they accurately reflect the needs of the stakeholders and that they are achievable within the
constraints of the project.
The requirements specification process is iterative, meaning that it may need to be repeated several
times as new information becomes available or as the project's requirements change over time. The
process requires input from a wide range of stakeholders, including users, customers, project
managers, developers, and quality assurance professionals.
Therefore, effective requirements specification is essential to the success of a software project, as it
provides a clear and well-defined set of requirements that can guide the development process and
ensure that the final product meets the needs of its users and stakeholders.
(iv) Requirements engineering processes: Requirements engineering is the process of eliciting,
analysing, specifying, validating, and managing the requirements for a software project. Requirements
engineering processes typically include the following steps:
(i) Elicitation: This involves identifying stakeholders and gathering their needs and requirements.
Elicitation techniques may include interviews, surveys, focus groups, observation, and workshops.
(ii) Analysis: This involves analysing the gathered requirements to identify patterns, conflicts, and
inconsistencies. Analysis techniques may include modelling, prototyping, and scenario-based
analysis.
(iii) Specification: This includes defining the requirements in a clear, complete, and unambiguous
manner. Requirements may be documented in a variety of formats, such as natural language, use
cases, or formal models.
(iv) Validation: This is a collective effort for ensuring that the specified requirements are correct,
complete, and consistent with stakeholder needs. Validation techniques may include peer reviews,
testing, and simulation.
(v) Management: This involves tracking and controlling changes to the requirements throughout the
software development lifecycle. Requirements management may involve tools and techniques such
as version control, traceability matrices, and change management processes.
Effective requirements engineering processes are critical to the success of a software project, as they
help to ensure that the software meets the needs of its users and stakeholders. The processes require
collaboration among various stakeholders, including business analysts, developers, testers, and
project managers. Iterative and incremental development methodologies such as agile are often used
in conjunction with requirements engineering to facilitate continuous feedback and adaptation to
changing requirements throughout the development process.
(v) Requirements elicitation and analysis: Requirements elicitation is the process of identifying and
gathering information from stakeholders to determine their needs and requirements for a software
project. The purpose of requirements elicitation is to understand the problem domain, the
stakeholders, and their expectations of the software system. Requirements analysis is the process of
examining the gathered requirements to identify patterns, conflicts, and inconsistencies. The purpose
of requirements analysis is to ensure that the requirements are complete, consistent, and accurate.
The following are some techniques commonly used for requirements elicitation:
a) Interviews: One-on-one conversations with stakeholders to elicit information about their needs
and requirements.
b) Focus groups: A group of stakeholders come together to discuss their needs and requirements.
c) Surveys: A set of questions sent to stakeholders to gather information about their needs and
requirements.
d) Observations: Observing stakeholders in their work environment to understand how they interact
with the current system or processes.

Page 125 of 200


e) Workshops: Facilitated sessions to bring stakeholders together to collaborate and identify their
needs and requirements.
Once the requirements have been elicited, the following techniques may be used for requirements
analysis:
(i) Requirements modelling: Creating models that represent the requirements and how they relate
to one another.
(ii) Prototyping: Building a working model of the system to help stakeholders visualise the
requirements.
(iii) Use case analysis: Developing use-cases to describe how users interact with the system.
(iv) Requirements traceability: Tracking the requirements through the software development lifecycle
to ensure they are being met.
Requirements elicitation and analysis are critical processes in software development, as they help
ensure that the software system meets the needs and expectations of stakeholders. Effective elicitation
and analysis require collaboration among various stakeholders, including business analysts,
developers, testers, and project managers. The processes are iterative and require ongoing refinement
throughout the software development lifecycle.
(vi) Requirements validation: Requirements validation is the process of ensuring the requirements
for a software project are correct, complete, and consistent with the needs and expectations of the
stakeholders. The purpose of requirements validation is to identify and correct any errors, omissions,
or misunderstandings in the requirements before development begins.
The following are some techniques commonly used for requirements validation:
a) Reviews: Reviewing the requirements with stakeholders to identify errors, omissions, or
misunderstandings.
b) Walkthroughs: Walking through the requirements with stakeholders to ensure that they
understand them and to identify any issues.
c) Prototyping: Building a working model of the system to help stakeholders visualise the
requirements and ensure that they meet their needs.
d) Testing: Testing the requirements to ensure that they are complete, correct, and consistent with
the needs and expectations of the stakeholders.
e) Simulation: Simulating the system to ensure that it meets the needs and expectations of the
stakeholders.
Requirements validation is an important process in software development, as it helps to ensure that
the software system meets the needs and expectations of the stakeholders. Effective requirements
validation requires collaboration among various stakeholders, including business analysts, developers,
testers, and project managers. The process is iterative and requires ongoing refinement throughout
the software development lifecycle. By validating requirements, a software development team can
minimise the risk of project failure; ensure that stakeholders are satisfied with the final product, and
save time and resources that might otherwise be wasted on rework or redesign.
(vii) Requirements management: Requirements management is the process of identifying,
organising, documenting, and controlling changes to the requirements throughout the software
development lifecycle. The purpose of requirements management is to ensure that the requirements
remain accurate, complete, and consistent with the needs and expectations of the stakeholders.
The following are some of the activities involved in requirements management:
a) Requirements identification: Identifying the requirements for the software project and
documenting them in a requirements document.
b) Requirements organisation: Organising the requirements into a hierarchy or a matrix to show
their relationships and dependencies.

Page 126 of 200


c) Requirements tracking: Tracking the requirements through the software development lifecycle
to ensure that they are met.
d) Requirements change control: Controlling changes to the requirements to ensure that they
remain accurate, complete, and consistent with the needs and expectations of the stakeholders.
e) Requirements communication: Communicating the requirements to all stakeholders to ensure
that everyone is aware of the requirements and their status.
Effective requirements management is critical to the success of a software project, as it helps to ensure
that the software system meets the needs and expectations of the stakeholders. It also helps to manage
the risks associated with changing requirements, by providing a mechanism for tracking and
controlling changes throughout the software development lifecycle. Requirements management
requires collaboration among various stakeholders, including business analysts, developers, testers,
and project managers. The process is iterative and requires ongoing refinement throughout the
software development lifecycle. By managing requirements effectively, a software development team
can minimise the risk of project failure, ensure that stakeholders are satisfied with the final product,
and save time and resources that might otherwise be wasted on rework or redesign.
7.5 Case Study:
Sample of software requirement specification document for an Online Student Registration System:
Online student registration system SRS
(i) Introduction: The Online Student Registration System (OSRS) is a web-based application that
allows students to register for classes online. The system will allow students to view available courses,
register for courses, and pay for courses online. The system will also allow administrators to manage
course offerings, monitor student enrolment, and manage student accounts.
(ii) User Requirements:
User Classes and Characteristics
There are two types of users in the system:
● Students: These are the primary users of the system. They can view available courses, register for
courses, and pay for courses online.
● Administrators: These are the secondary users of the system. They can manage course offerings,
monitor student enrolment, and manage student accounts.
Operating Environment:
The system will be a web-based application that can be accessed through a standard web browser.
User Requirements
1. Students should be able to:
● View available courses
● Register for courses
● Pay for courses online
● View their course schedules
● Add/drop courses
● View their grades
● Update their personal information
2. Administrators should be able to:
● Add/modify/delete courses
● View student enrolment
● Manage student accounts

Page 127 of 200


● Generate reports on student enrolment and course offerings
● Update their personal information
(iii) Functional Requirements
a) Authentication and Authorisation
● The system should require users to login with a valid username and password.
● Students should only be able to access their own personal information and courses.
● Administrators should only be able to access information related to their administrative
responsibilities.
b) Course Management
● Administrators should be able to add, modify, and delete courses.
● Course information should include course name, course number, instructor, schedule,
prerequisites, and available seats.
c) Student Registration
● Students should be able to view available courses.
● Students should be able to register for courses if they meet the prerequisites and if seats are
available.
● Students should be able to add/drop courses.
● Students should be able to pay for courses online.
d) Student Information Management
● Administrators should be able to manage student accounts.
● Student information should include name, contact information, course history, and grades.
e) Reporting
● The system should generate reports on student enrolment and course offerings.
● Reports should be available to administrators.
(iv) Non-Functional Requirements
a) Performance
● The system should be able to handle a large number of concurrent users.
● Response times should be fast.
b) Security
● The system should have secure authentication mechanisms to prevent unauthorised access.
● Student information should be encrypted and stored securely.
c) Usability
● The system should have a user-friendly interface that is easy to navigate.
d) Reliability
● The system should be available 24/7.
● Regular maintenance should be performed to ensure optimal performance.
The Online Student Registration System will provide a convenient way for students to register for
courses and manage their academic records. It will also provide administrators with the tools they
need to manage course offerings and monitor student enrolment. The system will be user-friendly,
secure, and reliable.

Page 128 of 200


Fig. 7.8: Use case diagram of online student registration system
This diagram shows the different actors and use cases in the system. The Student actor can view
available courses, register for courses, pay for courses, view their course schedules, add/drop courses,
view their grades, and update their personal information. The Admin actor can add/modify/delete
courses, view student enrolment, manage student accounts, and generate reports on student
enrolment and course offerings. Both actors access the system through separate portals. The system
administrator manages the database that stores course and student information.
7.6 Management Information System (MIS): A Management Information System (MIS) is a computer-
based system that collects, processes, stores, and disseminates information to support managerial
decision-making within an organisation. It combines technology, people, and processes to provide
accurate and timely information to managers at different levels within the organisation.
Key components of a Management Information System include:
(i) Data collection: MIS collects data from various sources within the organisation, such as
transactional systems, databases, and external sources. Data can be collected through manual
data entry, automated processes, or integration with other systems.

Page 129 of 200


(ii) Data processing: Once data is collected, it undergoes processing to transform it into meaningful
information. Data processing involves activities such as data validation, data cleaning, aggregation,
calculations, and summarisation to generate relevant reports and metrics.
(iii) Database management: MIS relies on a Database Management System (DBMS) to organise and
store data efficiently. The DBMS manages data storage, retrieval, and security, ensuring that data
is accessible to authorised users when needed.
(iv) Information generation: MIS generates various types of information, including reports,
dashboards, and key performance indicators (KPIs). These provide insights into the organisation's
performance, operational trends, financial metrics, and other relevant data to support decision-
making.
(v) Decision support: MIS provides decision support tools and techniques to assist & managers in
making informed decisions. These tools include ad-hoc querying, data visualisation, scenario
analysis, and forecasting capabilities, allowing managers to explore data and evaluate different
options.
(vi) Communication and collaboration: MIS facilitate communication and collaboration by enabling
the sharing of information and reports among different departments and levels of management. It
supports the flow of information across the organisation, ensuring that relevant data reaches the
right stakeholders.
(vii) Security and privacy: MIS incorporate security measures to protect data integrity,
confidentiality, and availability. It includes user access controls, encryption, backups, and disaster
recovery plans to safeguard information assets from unauthorised access or loss.
7.6.1 Benefits of a Management Information System:
(i) Improved decision-Making: MIS provides managers with accurate and timely information,
enabling them to make informed decisions based on data-driven insights.
(ii) Enhanced efficiency: By automating data collection, processing, and reporting, MIS reduces
manual effort, improves data accuracy, and streamlines business processes.
(iii) Better resource allocation: MIS helps allocate resources effectively by providing visibility into
resource utilisation, identifying areas of inefficiency, and optimising resource allocation based on
organisational priorities.
(iv) Increased collaboration: MIS fosters collaboration by facilitating information sharing across
departments, promoting teamwork, and supporting coordinated efforts.
(v) Competitive advantage: An effective MIS enables organisations to gain a competitive edge by
leveraging data and information to identify market trends, customer preferences, and emerging
opportunities.
(vi) Strategic planning: MIS supports strategic planning by providing insights into the organisation's
performance, market dynamics, and industry trends. It aids in setting goals, monitoring progress,
and adapting strategies as needed.
(vii) Compliance and risk management: MIS helps organisations comply with regulations and
manage risks by providing visibility into operational activities, monitoring compliance metrics, and
identifying potential risks or issues.
A Management Information System plays a crucial role in supporting managerial decision-making,
improving operational efficiency, and driving organisational performance by harnessing the power of
data and information.

Page 130 of 200


Chapter 8. Software Testing
INTRODUCTION
In this session, you will understand the concept of software testing and different types of software
testing. Software testing is the process of evaluating a software item to detect differences between given
input and expected output. Also, to assess the features of a software item. Testing assesses the quality
of the product. Software testing is a process that should be done during the development process. In
other words, software testing is a verification and validation process.
8.1 SOFTWARE DEVELOPMENT LIFE CYCLE
8.1.1 SOFTWARE VERIFICATION
The process of determining whether the software meets the needs of users is known as validation. It
happens after the SDLC (Software Development life cycle). Validation determines whether or not the
software meets the requirements for which it was designed.
(i) Validation ensures that the product under development will meet the needs of the users.
(ii) Validation answers the question, "Are we creating a solution that attempts to meet all of the
software needs of the user?"
(iii) Validation focuses on the user's requirements.
8.1.2 Different types of Software Validation:

Medical Device Software Production Software

Quality Management Software for FDA-


Software Regulated Records

Fig. 8.1 Different types of software validation


(i) Medical Device Software: Medical device software includes both software that is used as a
component, part, or accessory of a medical device and software that is a medical device in and of
itself. Following are the examples of medical devices that contain software that requires validation.

Fig. 8.2 Medical device software

Page 131 of 200


(ii) Production Software: Software and systems used in the manufacture of FDA-regulated products
are referred to as production software. This group includes:
• Software that controls manufacturing equipment (i.e., PLCs, CNCs, inspection software etc).
• Software that manages the manufacturing process (i.e., factor automation, production monitoring,
bill of materials etc).
• Laboratory testing software.
• Labelling software.
• Management software for the production environment (i.e., building management systems).
• Manufacturing software that automates important computations.
These are not exhaustive examples; however, FDA Warning Letters for validation issues contain
citations from these categories.
(iii) Quality Management Software: Quality management software and systems are used in systems
to ensure the quality of products.
The FDA has issued warning letter citations for all of the following examples:
• Product returns or recalls management software
• Complaints software
• Change control software
• Preventative maintenance software
• Inventory control software (i.e., ERPs)
• Document Management software
• Deviation Tracking software
• CAPA (Corrective and Preventive Action) software
(iv) Software for FDA-regulated records: Software for FDA-regulated records is defined as the
software used to create, modify, maintain, archive, retrieve, or transmit FDA-required records.
According to the FDA, electronic records must be submitted.
FDA-regulated records software examples include:
• IRB records software
• Adverse Event reporting software
• Organ/Tissue Donor records
• Call center records software
• Validation records software
• Prescription order
• Fulfilment software
• Clinical Trial Records software
8.2 SOFTWARE VERIFICATION: Software verification is the process of reviewing any software to find
flaws. Verification is the process of ensuring that software meets its objectives without flaws. It is the
process of determining whether the product developed is correct or not. Document review can begin
as early as the software requirement and analysis phase, with the end product being the SRS
document. There are numerous methods for practising software verification, such as peer reviews,
walkthroughs, inspections, and so on, that can assist us in the prevention of potential flaws, which
could lead to software failure.
(i) Verification ensures the product being developed is according to design specifications.

Page 132 of 200


(ii) Verification answers the question- "Are we developing this product by firmly following all design
specifications?"
(iii) Verifications concentrate on the design and system specifications.
Methods of Verification:
(a) Peer Reviews: The peer-review method is the simplest and most informal way of reviewing
documents or programs/software during the verification process to identify flaws. In this method,
we distribute the document or software programmers to others and ask them to review, with the
expectation that they will provide feedback on the quality of product as well as identify flaws in
the program/document. SRS document verification, SDD (Solid State Drive) verification, and
programmer verification are all possible activities for this method. Using this method, the reviewers
can also prepare a brief report on their observations or findings.
Advantages:
● You can expect some good results without spending any significant resources.
● It is very efficient and significant.
Disadvantages:
● If the reviewer is inexperienced, this can lead to poor outcomes.
(b) Walk-through: When compared to peer-review, walk-throughs are a more formal and methodical
approach to verification. In a walkthrough, the software document's author displays the document
to a group of two to seven people. Nothing is demanded of the participants in terms of preparation.
The meeting's preparation is the responsibility of the presenter. All participants receive a copy of
the document(s). The author introduces the content during the walk-through meeting to
familiarise them with it, and all attendees are allowed to ask any questions they may have.
Advantages:
● It could assist us in identifying potential flaws.
● It can also be used to collaborate on documents.
Disadvantages:
● The author may unnecessarily stress some specific aspects of his or her passion while
concealing others.
(c) Inspections - Inspections are the most structured and formal way of verification and are generally
referred to as inspections. A group of three to six people is formed, with an unbiased moderator
in charge. Everyone in the group engages openly, actively, and according to the guidelines for
conducting such a review. Everyone may be allowed to share their points of view, potential flaws,
and critical regions. After the meeting, the moderator's essential suggestions are included in a
final report.
Advantages:
● It can be very effective for finding potential faults or problems in the documents like SRS, SDD,
etc.
● Critical inspections may also aid in the detection of flaws and the improvement of these
documents, thereby preventing the spread of a flaw across the software development life cycle.
Disadvantages:
● They take time and require discipline.
● It requires more cost and also needs skilled testers.
● In the field of software testing, the phrases, verification and validation are sometimes
misunderstood. You'll come across (or have come across) a variety of uses and meanings of
these terms, Lets understand the major difference between Verification and Validation.

Page 133 of 200


Table 8.1 Verification Vs Validation
Criteria Verification Validation
Definition The practice of reviewing work The processes of determining if
products (rather than the final
the software meets stated
result) from a development phase to
see if they match the phase's business requirements during or
requirements. at the end of the
development process.
Objective To guarantee that the product To guarantee that the product fits the
needs of the users and that the
is built by the specifications
specifications were correct, to begin
and requirements. with. To put it another way, to show
To put it another way, to that the product performs as expected
in its intended setting.
ensure that work products
meet the standards.
Question to Are we building the Are we building the right product?
be asked products, right?
Evaluation Plans, Requirement Specifications, The actual product/software.
Items
Design Specifications, Code, Test
Cases
Activities Reviews Walkthroughs, Testing of the software.
Inspections, etc.

It's completely feasible that a product will pass verification yet fail validation. This can occur when, for
example, a product is created according to specifications yet the specifications do not satisfy the user's
demands. It is important to:
• Trust but Verify
• Verify but also Validate
8.3 Test Cases
A test case is a document that contains a set of test data, preconditions, expected results, and post
conditions created for a specific test scenario in order to verify compliance with a specific requirement.
Following the application of a set of input values, the programmer has a final result and exits the
system at some point, also known as the execution post condition.

Page 134 of 200


Fig. 8.3 Test case parameters
8.3.1 Writing Good Test Cases:
(i) Write test cases in such a way that you only test one thing at a time, if at all possible. Test cases
should not be overlapping or complicated. Make your test cases as atomic as possible.
(ii) Make certain that both good and negative eventualities are covered.
(iii) Language:
• Write in simple and easy-to-understand language.
• Use active voice instead of passive voice: Do this, do that.
• Use exact and consistent names (of forms, fields, etc).
(iv) Characteristics of a good test case:
• Accurate: Exacts the purpose.
• Economical: No unnecessary steps or words.
• Traceable: Capable of being traced to requirements.
• Repeatable: Can be used to perform the test over and over.
• Reusable: Can be reused if necessary.
8.4 Testing can either be done manually or using an automated testing tool:
• Manual - This testing is performed without taking help of automated testing tools. The software
tester prepares test cases for different sections and levels of the code, executes the tests and reports

Page 135 of 200


the result to the manager. Manual testing is time and resource consuming. The tester needs to
confirm whether or not the right test cases are used. Major portion of testing involves manual
testing.
• Automated testing - This testing is a testing procedure done with aid of automated testing tools.
The limitations with manual testing can be overcome using automated test tools.
8.8.1 Tests can be conducted based on two approaches –
1. Functionality testing 2. Implementation testing

Fig 8.4 Testing Approaches


When functionality is being tested without taking the actual implementation into consideration it is
known as black-box testing. The other side is known as white-box testing where not only functionality
is tested but the way it is implemented is also analysed.
1) Black Box Testing: Black-Box Testing, also known as Behavioural Testing, is a software testing
method in which the internal structure/ design/ implementation of the item being tested is not known
to the tester. These tests can be functional or non-functional, though usually functional.

INPUT Black Box Testing OUTPUT

Fig 8.5 Black-box testing


The above Black-box can be any software system you want to test. For example: an operating system
like Windows, a website like Google, a database like Oracle or even your own custom application.
Under Black box testing, you can test these applications by just focusing on the inputs and outputs
without knowing their internal code implementation.
(i) This method attempts to find errors in the following categories:
• Incorrect or missing functions
• Interface errors
• Errors in data structures or external database access
• Behaviour or performance errors
• Initialisation and termination errors
(ii) There are many types of Black box testing but following are the prominent ones:
• Functional testing - This black-box testing type is related to functional requirements of a system;
it is done by software testers.
• Non-functional testing - This type of black-box testing is not related to testing of a specific
functionality, but non-functional requirements such as performance, scalability, usability etc.
• Regression testing - Regression testing is done after code fixes, upgrades or any other system
maintenance to check the new code has not affected the existing code.

Page 136 of 200


Black box testing has its own life cycle called Software Test Life Cycle (STLC) and it is relative to every
stage of Software Development Life Cycle. Some famous Black Box testing techniques are Boundary
value analysis, state transition testing and equivalence partitioning.
2) White Box Testing: It is also known as Clear box Testing, Open box Testing, Glass box Testing,
Transparent box Testing, Code-Based Testing or Structural Testing. It is a software testing method in
which the internal structure/ design/ implementation of the item being tested is known to the tester.
The tester chooses inputs to exercise paths through the code and determines the appropriate outputs.
Programming know-how and the implementation knowledge is essential. White box testing is testing
beyond the user interface and into the nitty-gritty of a system.

Fig 8.6 White-box testing


This method is named so because the software program, in the eyes of the tester, is like a white/
transparent box; inside which one clearly sees.
White-box testing, on its own, cannot identify problems caused by mismatches between the actual
requirements or specification and the code as implemented but it can help identify some types of design
weaknesses in the code. Examples include control flow problems (e.g., closed or infinite loops or
unreachable code), and data flow problems (e.g., trying to use a variable which has no defined value).
Static code analysis (by a tool) may also find these sorts of problems, but doesn't help the
tester/developer understand the code to the same degree that personally designing white-box test
cases does.
3) Grey-box Testing: Grey-box testing is a software testing method which is a combination of Black
box testing method and White box testing method. In Black box testing, the internal structure of the
item being tested is unknown to the tester and in White box testing the internal structure is known.
In Grey box testing, the internal structure is partially known. This involves having access to internal
data structures and algorithms for purposes of designing the test cases, but testing at the user, or
black-box level.
Grey box testing is named so because the software program, in the eyes of the tester, is like a grey/
semi-transparent box; inside which one can partially see.

Black White Grey


Box Box Box
Testing Testing Testing
Fig 8.7 Grey-box testing
Grey box testing gives the ability to test both sides of an application, presentation layer as well as the
code part. It is primarily useful in integration testing and penetration testing. Grey-box testing is a

Page 137 of 200


perfect fit for web-based applications. Grey-box testing is also the best approach for functional or
domain testing.
Techniques used for Grey box testing are-
● Matrix testing: This testing technique involves defining all the variables that exist in their
programs.
● Regression testing: To check whether the change in the previous version has regressed other
aspects of the program in the new version. It will be done by testing strategies like retest all, retest
risky use cases, retest within the firewall etc.
● Orthogonal array testing or (OAT): It provides maximum code coverage with minimum test cases.
● Pattern testing: This testing is performed on the historical data of the previous system defects.
Unlike black-box testing, grey box testing digs within the code and determines why the failure
happened.
Usually, Grey-box methodology uses automated software testing tools to conduct the testing. Stubs
and module drivers are created to relieve testers from manually generating the code.
8.8.2 Difference between Black box, Grey box and White box testing
S.No. Black Box Testing Grey Box Testing White Box Testing
1 The internal workings Somewhat knowledge Testing has full
of an application are of the internal knowledge of the
not required to be working is known internal working
known of the application
2 Also known as closed Another term for grey Also known as clear
box testing, data box testing is box testing,
driven testing translucent testing as structural testing or
and functional testing the tester has code
limited knowledge of based testing
the insides of
the application
3 Performed by end Performed by end users Normally done
users and also by and also by testers by testers
testers and developers and developers and developers
4 Testing is based Testing is done on the Internal workings
on external expectations basis of high level are fully known
- Internal behaviour of database diagrams and and the tester
the application data flow diagrams can design test
is unknown data accordingly
5 This is the least Partly time consuming The most exhaustive
time-consuming and exhaustive method and time consuming
and exhaustive method type of testing
of testing
6 Not suited to Not suited to Suited for
algorithm testing algorithm testing algorithm testing
7 This can only be done Data domains Data domains
by trial and error and internal and internal
method boundaries can be boundaries can be
tested, if known better tested
8.8.4 There are many other types of testing like

Page 138 of 200


Business Case Release Testing

Acceptance Testing
Requirements

System Testing
System

Integration
System
Testing
Design
Unit
Unit design
Testing
Development
Fig 8.8 Types of testing
(i) Acceptance testing: Acceptance testing is often done by the customer to ensure that the delivered
product meets the requirements and works as the customer expected. It falls under the category of
black-box testing
(ii) Regression testing: Regression testing is the testing after modification of a system, component, or
a group of related units to ensure that the modification is working correctly and is not damaging or
imposing other modules to produce unexpected results. It falls under the category of black-box testing.
(iii) Beta testing: Beta testing is the testing which is done by end users, a team outside development,
or publicly releasing full pre-version of the product which is known as beta version. The aim of beta
testing is to cover unexpected errors. It falls under the category of black-box testing.
(iv) Unit testing: Unit testing is the testing of an individual unit or group of related units. It falls under
the category of white-box testing. It is often done by the programmer to test that the unit he/she has
implemented is producing expected output against given input. Statements, functions, methods,
interfaces i.e. units of the code are individually tested for proper execution. It can be automated or can
be done manually. Usually small data is used for unit testing.
(v) Integration testing: Integration testing is testing in which a group of components are combined to
produce output. Also, the interaction between software and hardware is tested in integration testing if
software and hardware components have any relation. It may fall under both white-box testing and
black-box testing. Different approaches used in integration testing are: top down and bottom up
integration testing, sandwich testing (combination of both).
(vi) Stress testing: Stress testing is the testing to evaluate how a system behaves under unfavourable
conditions. Testing is conducted beyond the limits of the specifications. It falls under the category of
black-box testing.
(vii) Performance testing: Performance testing is the testing to assess the speed and effectiveness of
the system and to make sure it is generating results within a specified time as in performance
requirements. It falls under the category of black-box testing.
(viii) Functional testing: Functional testing is the testing to ensure that the specified functionality
required in the system requirements works. It falls under the category of black-box testing.
(ix) System testing: System testing is the testing to ensure that by putting the software in different
environments (e.g., Operating Systems) it still works. System testing is done with full system
implementation and environment. It falls under the category of black-box testing. It is performed after

Page 139 of 200


integration testing. Various approaches used are: load testing, smoke-testing, security testing,
migration testing etc.
(x) Usability testing: Usability testing is performed from the perspective of the client, to evaluate if
the GUI is user-friendly? How easily can the client learn? After learning how to use it, how proficiently
can the client perform? How pleasing is it to use its design? This falls under the category of black box
testing.
8.5 Testing Documentation
Testing documents are prepared at different stages-
Before Testing
Testing starts with test case generation. The following documents are needed for reference:

SRS Document Test Strategy


Test Policy document Traceability Matrix
document

This is a document
This mentions relating to the
This describes how detail aspects of requirement gathering
Functional test team,
far testing should phase in the SDLC. This
Requirements responsibility
take place before matrix is updated as
releasing the matrix and new needs are
product. rights/responsibili received. These
ty of test matrices assist testers
manager and test in determining the
engineer. source of the demand.
They can be followed
in both directions.

Fig 8.9 Documents needed before Testing


While being tested
The following documents may be required while testing is started and is being done

Test Case
Test description Test Case report Test logs
document

This document
contains list of
tests required to This document is a
be conducted. It detailed This document This document
includes Unit test description of all contains test case contains test logs
plan, Integration test cases and report as a result for every test case
test plan, System procedures to of the test. report
test plan and execute them.
Acceptance test
plan.

Fig 8.10 Documents required while testing

Page 140 of 200


Chapter 9. Software project using python
INTRODUCTION
Various administrative processes in educational institutions can be streamlined and automated with
the help of the School Management System (SMS), an integrated software solution. The goal of this
project report is to give a general overview of a school management system that consists of the following
four crucial modules: student registration, library management, fee deposit, and marksheet
generation.
9.1 SCHOOL MANAGEMENT SYSTEM AND LIBRARY MANAGEMENT SYSTEM
Let’s understand each module in the following section: -
(i) Student registration:
The student registration module makes it easier to manage student data effectively. It has elements
like student enrolment, admission information, private data, and contact information. Administrators
can create distinctive student IDs, maintain correct records, and preserve pertinent papers with the
help of this module. It guarantees data security and accuracy while also making the registration
process simpler.
(ii) Library management:
Operations involving libraries are automated via the Library Management module. It enables librarians
to CatLog books, keep inventory current, monitor book borrowing and return, and produce reports.
Students can also use it to see their borrowing histories, make reservations, and conduct book
searches. The module encourages efficient resource utilisation, boosts accessibility, and streamlines
library operations.
(iii) Fees deposit:
The management of fee-related transactions is made easier by the Fees Deposit module. Admin can
use it to create receipts, view payment histories, and make online payments. Administrators have the
ability to create reports for accounting use and monitor the status of payments. Transparency is
ensured, manual work is minimised, and financial management is improved and all this becomes
possible due to modules.
(iv) Marksheet generation:
The Marksheet generation module automates the process of generating student mark sheets and
progress reports. It allows teachers to input exam scores, calculate grades, and generate
comprehensive mark sheets. This module also enables parents and students to access and download
mark sheets online. It simplifies result processing, ensures accuracy, and enhances communication
between stakeholders.
9.1.1 Purpose/Objective
The purpose of creating a school management system project with a DBMS is to streamline
administrative processes, centralise data, and enhance communication within educational
institutions. The DBMS enables efficient management of student information, attendance records,
academic performance, and administrative details. It automates tasks, reduces errors, and provides
secure data storage. It facilitates data analysis and reporting, aiding decision-making and evaluating
educational programs. The system promotes collaboration among teachers, parents, and students
through online portals and messaging systems. Overall, it aims to improve efficiency, data integrity,
and communication within schools, leading to enhanced operational effectiveness and a better
educational experience.
9.1.2 Scope of school management system
The scope of a school management system project includes various functional areas within an
educational institution. It encompasses student enrolment and admission processes, attendance
tracking, grading and academic performance management, fee management, and generating reports.

Page 141 of 200


The system should facilitate communication between teachers, parents, and students through
messaging systems and online portals. It may also incorporate features for managing school resources
such as libraries and laboratories. Additionally, the project can encompass administrative functions
like staff management, payroll processing, and inventory management etc. The system should be
scalable and adaptable to accommodate the specific requirements of different types and sizes of
schools. Ultimately, the scope of the project is to provide a comprehensive solution for effectively
managing and streamlining the operations of a school.
9.1.3 Definitions
School Management System (SMS) - A school management system streamlines various
administrative tasks within a school through software. It automates student enrolment, course
scheduling, fee management, library management, attendance tracking, and more. It is the goal of a
school management system to simplify and improve these processes, saving time and reducing errors.
User Interface (UI) - The user interface is the point at which human users interact with a computer,
website or application. It is the goal of effective UI to make the user's experience easy and intuitive,
requiring minimum effort on the user's part to achieve maximum results.
UI is created in layers of interaction that appeal to the human senses (sight, touch, auditory and more).
They include both input devices like a keyboard, mouse, trackpad, microphone, touch screen,
fingerprint scanner, e-pen and camera, and output devices like monitors, speakers and printers.
Devices that interact with multiple senses are called "multimedia user interfaces." For example,
everyday UI uses a combination of tactile input (keyboard and mouse) and a visual and auditory output
(monitor and speakers).
Database Management System (DBMS)- The database stores and retrieves, inserts and deletes
interrelated data efficiently. Data can also be organised in the form of tables, schemas, views, and
reports using this technology.
For example: The college Database organises the data about the admin, staff, students and faculty
etc.
9.1.4 Process Model
The waterfall model is a sequential software development process that consists of distinct phases, each
building upon the previous one. Here's how the waterfall model can be applied to a school management
system project:
(i) Feasibility study: In this phase, the school management project evaluates the project's technical,
economical, and operational viability. It assesses if the proposed solution is technically feasible and
can be implemented effectively. It examines the financial aspects, including the cost of development,
maintenance, and potential returns on investment. Additionally, it considers the operational feasibility
by analysing the project's alignment with the school's goals, organisational capabilities, and potential
impact on stakeholders. The feasibility study helps in determining if the project is worth pursuing and
provides valuable insights for decision-making.
(ii) Requirements gathering: In this phase, the project team interacts with stakeholders to
understand their needs and requirements for the school management system. This involves identifying
key functionalities, data requirements, and system constraints.
(iii) System design: Based on the gathered requirements, the system's architecture, database
structure, and user interface design are created. The design phase involves creating detailed
specifications for each component of the system.
(iv) Implementation: This phase involves coding the system according to the design specifications.
The development team writes code for different modules, such as student enrolment, attendance
tracking, library transactions and fee management.
(v) Testing: The system is thoroughly tested to ensure it meets the specified requirements. Test cases
are designed and executed to identify and resolve any defects or issues.

Page 142 of 200


(vi) Deployment: The fully tested system is deployed on the school's infrastructure, including servers,
databases, and networks. Data migration and system integration activities take place during this
phase.
(vii) Maintenance: After deployment, ongoing maintenance and support activities are conducted to
address any issues, implement enhancements, and ensure the system's smooth operation.
The waterfall model provides a systematic approach to develop the school management system,
ensuring that each phase is completed before moving to the next. It emphasises a structured and
disciplined approach to project execution, enabling better planning and control throughout the
development lifecycle.

Fig 9.1 Process Model


9.2 SOFTWARE REQUIREMENT SPECIFICATIONS (SRS)
Introduction
The School Management System aims to streamline administrative processes and enhance efficiency
in student registration, fee management, library management, and marks entry. This SRS document
outlines the specific requirements for each module.
9.2.1 Product prospective
The existing school management system lacks efficiency and faces numerous challenges. This
necessitates the development of a new and improved system to address these issues effectively. The
current system struggles with manual and time-consuming processes, leading to errors, delays, and a
lack of transparency. Administrative tasks, such as student enrolment, attendance tracking, and fee
management, require substantial manual effort, resulting in inefficiencies and increased workload for
staff members. Additionally, communication between stakeholders, including teachers, parents, and
students, is often fragmented and ineffective.
The school management system aims to streamline administrative processes, automate routine tasks,
and enhance communication and collaboration. By implementing an integrated digital platform,
student enrolment can be simplified, and attendance records can be accurately maintained. Fee
management can be automated, enabling easy tracking of payments and generating financial reports.
Furthermore, the system can facilitate effective communication through centralised channels, such as
notifications, emails, and online portals, fostering a stronger partnership between teachers, parents,
and students.

Page 143 of 200


9.2.2 PRODUCT FUNCTIONS
(i) Module1: login
Login Module outlines the requirements for user authentication and login functionality. It specifies the
need for a secure login mechanism to ensure only authorised users can access the system. The SRS
defines the user roles and permissions, such as administrators, teachers, and staff, and details their
respective access levels. It also covers password management, including password encryption and reset
procedures, to enhance system security and user data protection.
(ii) Module2: Student registration:
The system should allow administrators to register new students by collecting their personal details
such as name, address, contact information, and date of birth. Each registered student should be
assigned a unique student ID for identification purposes. The system should validate the input data
to ensure accuracy and prevent duplication. It should generate an acknowledgment or confirmation
upon successful registration.
(iii) Module3: Fee deposit:
The system should facilitate fee deposit for students, capturing details such as fee amount, due date,
and payment mode (cash, check, online). It should generate fee receipts for each payment made,
containing transaction details and student information. The system should send notifications or
reminders to students and parents about upcoming fee dues and overdue payments. It should
calculate and track any applicable late fees or fines for delayed payments.
(iv) Module4: Library management:
The system should maintain a catalogue of books available in the library, including information like
title, author, publication, and availability status. Students should be able to request books through
the system, and librarians should be able to issue and return books. The system should track the due
dates for book returns and generate notifications for overdue books. It should calculate and manage
any fines or penalties for late book returns.
(v) Module 5: Marks entry:
The system should allow teachers to enter and update students' exam marks for different subjects. It
should calculate overall grades or grade point averages based on the entered marks and predefined
grading criteria. Teachers should be able to view and analyse students' academic performance through
the system. The system should provide reports or summaries of marks and grades for individual
students or classes.
(vi) Non-functional Requirements:
• The system should be user-friendly, intuitive, and provide an engaging user-interface.
• It should be accessible from various devices such as computers, tablets, and smartphones.
• The system should ensure data security, protecting student and financial information from
unauthorised access.
• It should have a reliable backup and recovery mechanism to safeguard against data loss.
• The system should be scalable to accommodate a growing number of students and data.
This SRS document provides an overview of the functional and non-functional requirements for the
School Management System with four modules. It serves as a foundation for the development team to
design and implement the system, ensuring it meets the specific needs of the educational institution.
9.2.3 General Constraints
The database of the software is not accessible to every user of the software. Making changes in the
database requires permissions that have been given to certain specific individuals.
9.2.4 Assumptions and Dependencies
The ARS is assumed to be compatible with the computer systems on which it has been loaded for
customer use.

Page 144 of 200


9.3 HARDWARE AND SOFTWARE REQUIREMENTS
● Hardware Specification
Processor: Intel Pentium4 and above
Processor speed: 1GHz to 2GHz and above
RAM: 512MB to 1GB and above
Hard disk: 4GB to 30GB and above
Keyboard: 104 keys
● Software Specification.
Language: Python
Database: SQL, sqlite3
Operating System: Windows 7 / Windows 10/ Windows 11
9.4 DATA FLOW DIAGRAM
9.4.1 LEVEL- 0 DFD

Student School Student


Profile Marksheet
Management
Entry
System

Library
Generate Fee Management
Report System

Page 145 of 200


9.4.2 Level -1 DFD

LOGIN

MENU

Student Profile Student


Entry Marksheet

Generate Fee Library


REPORT Management
SYSTEM

Data Base

Page 146 of 200


9.5 Entity Relationship Diagram

Mother
dob Name Addres
s Student Name
Mobile
numbe
r
Name Student Fee Report
Information
Branch
Email
Father ID Receipt
Name No.
Gende
r Date
Admission
No.

Book Book Mobile


title No. ID No.
Post
Student
Code Marksheet

Author
Code
Address Enter Roll Number
No.
Library Mangement

Book
Borrowed
Surname

Find Create new Marks


Entry

Date Days in
Due Loan
Student Marksheet
First
Email
Name Gender id
Member
type No.
School Name
Reference
Name
No.

Mother Name DOB

Father
Name

Page 147 of 200


9.6 PROJECT MANAGEMENT
9.6.1 Functional Point Estimation (FPE): Functional point estimation is a technique used to
estimate the size and effort required for software development based on functional requirements.
Here's an example of how you can estimate the functional points for a School Management System
in a tabular form:
Function Type Description Complexity Weight
External Inputs (EI) User inputs that affect the internal
processing
Student registration Registering a new student Low 3
Student attendance Recording student attendance Low 3
Exam results Entering and managing exam results Medium 4
Teacher feedback Capturing feedback from teachers Low 3
External Outputs User-generated outputs that leave the
(EO) system
Student reports Generating student reports Medium 4
Timetable generation Creating and managing class timetables Medium 4

Teacher schedules Generating schedules for teachers Medium 4


Internal Logical Files Internal files maintained by the system
(ILF)
Student database Managing student records High 7
Teacher database Maintaining teacher information Medium 5
Class database Storing class details Medium 5
External Interface Files used by the system but maintained
Files (EIF) externally
Exam results file Importing and exporting exam results Low 3
Student attendance Importing student attendance data Low 3
file
Total Weighted FPs 50
To calculate the total weighted functional points, you multiply the Complexity and Weight for each
function type and sum them up. In this example, the total Weighted FPs for the School Management
System is 50.
Please note that this is a simplified example, and in a real-world scenario, you would need to consider
a more comprehensive list of functional requirements and assign complexity and weight based on the
specific needs of the School Management System.
9.6.2 Risk Table
Risk ID Risk Description Probability Impact Severity Mitigation Strategy
R1 Inadequate user Medium High High Conduct thorough requirement
requirements analysis, involve stakeholders,
gathering and perform frequent reviews.
R2 Technology Low High Medium Research and select compatible
compatibility technologies,
issues conduct compatibility tests, and
have backup plans.

Page 148 of 200


R3 Insufficient Low Medium Medium Ensure a well-defined project
development plan, allocate appropriate
resources resources, and consider
outsourcing if necessary.
R4 Scope creep and High High High Implement a robust change
changing management process,
requirements document changes,
and seek approval before
implementation.
R5 Security Medium High High Implement strong security
vulnerabilities measures, encrypt sensitive
and data data, conduct regular security
breaches audits, and follow best
practices.
R6 Lack of user Medium Medium Medium Provide comprehensive user
training and training, conduct user
adoption acceptance testing, and offer
ongoing support.
R7 Integration Low Medium Low Perform thorough integration
challenges with testing, ensure compatibility,
existing systems and have fall back plans.
R8 Inadequate Medium High High Implement a structured testing
testing and process, conduct thorough QA
quality assurance checks, and use automated
testing tools.
R9 Dependency on Low Medium Low Maintain open communication,
external service establish Service Level
providers Agreements (SLAs), and have
contingency plans.
R10 Project delays and High High High Regularly monitor project
resource progress, identify bottlenecks,
constraints and take appropriate
measures to mitigate delays.
9.7 DATA DESIGN
Student_Information
Name str
Father’s Name str
Mother’s Name str
Gender str
E-mail str
Mobile no INTEGER
Address str
DOB INTEGER
Library Management
First Name str

Page 149 of 200


Surname str
Address str
Post str
Mobile No. INTEGER
Book ID INTEGER
Book Name str
Author CodeNo. INTEGER
Student Marksheet
Name str
Father Name str
Mother Name str
Gender str
E-mail str
Mobile No. INTEGER
Gender str
DOB INTEGER
School Name str
Fee Report
Receipt str
Admission No. str
Date INTEGER
Branch str
Student Name str
9.8 Test Cases
a) Test case for login module.
Verify that a registered user can log in successfully with valid credentials.
Verify that an error message is displayed if invalid credentials are entered.
Verify that different user roles have the appropriate access privileges.
b) Test case for student registration:
Verify that a new student can be registered successfully with valid information.
Verify that an error is displayed if required fields are left blank.
Verify that duplicate registration with the same student ID is not allowed.
c) Test case for fee deposit:
Verify that a fee payment can be successfully recorded for a student.
Verify that the correct fee amount is calculated based on the student's grade level.
Verify that an error is displayed if an invalid payment amount is entered.
d) Test case for library management:
Verify that a book can be added to the library catalogue with all the required details.
Verify that a book can be borrowed by a student, and the availability status is updated.

Page 150 of 200


Verify that an overdue book generates a notification for the student and librarian.
e) Test case for marksheet generation:
Verify that marks can be entered for different subjects and exams for a student.
Verify that the overall grade or grade point average is calculated correctly based on the entered
marks.
Verify that the generated marksheet accurately reflects the student's academic performance.
These are just a few examples of test cases for a school management system project. The actual test
cases will depend on the specific functionality and requirements of the system. It is important to
cover both positive and negative scenarios to ensure proper system behaviour.
9.10 School Management:
Program 1: Write a program to display Login Menu in Python.
The program is coded in Python IDLE and its output is shown below.
Programming step 1 Login Menu

Page 151 of 200


Page 152 of 200
Program 2: Write a program to display Main Menu in Python.
The program is coded in Python IDLE and its output is shown below.
Programming step 2 Main Menu

Program 3: Write a program to display student information in Python.

Page 153 of 200


The program is coded in Python IDLE and its output is shown below.
Programming step 3 Student Information

Page 154 of 200


Page 155 of 200
Page 156 of 200
After filling one entry in Student Information.

Program 4: Write a program to display Fee Generation Report in Python.


The program is coded in Python IDLE and its output is shown below.
Programming step 4 Fee Generation Report

Page 157 of 200


Page 158 of 200
Page 159 of 200
Page 160 of 200
Page 161 of 200
After filling one entry in the Fee Report.

Program 5: Write a program to display Library management systems in Python.


The program is coded in Python IDLE and its output is shown below.
Programming step 5 Library management system

Page 162 of 200


Page 163 of 200
Page 164 of 200
Page 165 of 200
Page 166 of 200
Page 167 of 200
Page 168 of 200
After filling one entry in the Library Management System.

Program 6: Write a program to display Search Page in Python.


The program is coded in Python IDLE and its output is shown below.

Page 169 of 200


Programming step 6 Search Page

Search Page.

Page 170 of 200


Program 7: Write a program to display Create new Marks Entry in Python.
The program is coded in Python IDLE and its output is shown below:
Programming step 7 Create new Marks Entry

Page 171 of 200


Page 172 of 200
Page 173 of 200
Page 174 of 200
Page 175 of 200
Page 176 of 200
Page 177 of 200
After filling one entry in new Marks Entry.

Page 178 of 200


Chapter - 10 Contact Management System
INTRODUCTION
The current contact management process in our organisation is manual and inefficient, leading to
difficulties in maintaining and accessing contact information. The lack of a centralised and organised
system results in time-consuming searches for contact details, outdated information, and missed
opportunities for effective communication. There is a need for a Contact Management System that
streamlines the process of storing, managing, and retrieving contact information.
10.1 CONTACT MANAGEMENT SYSTEM
The objective of the Contact Management System (CMS) project is to develop a user-friendly and
efficient software solution that allows users to store, update, and search for contact details in a
centralised database. The system should enable easy categorisation and grouping of contacts, provide
quick search capabilities, and allow for the addition of additional information such as email addresses,
phone numbers, and notes. It should also support features like contact import/export, data backup,
and security measures to safeguard sensitive information.
By implementing a contact management system, we aim to enhance productivity, improve
communication, and ensure up-to-date and accurate contact information, ultimately leading to more
effective and efficient business operations.
10.1.1 PURPOSE/OBJECTIVE
The purpose of developing a contact management system project is to provide a comprehensive and
user-friendly solution for efficiently managing and organising contact information. The system aims to
address the limitations of manual contact management processes and offer numerous benefits to
individuals or organisations:
(i) Centralised contact storage: The system allows users to store all contact information in a
centralised database, eliminating the need for scattered records or multiple platforms.
(ii) Easy accessibility: Users can quickly access contact details anytime and anywhere, facilitating
efficient communication and saving valuable time.
(iii) Efficient contact organisation: The system enables categorization and grouping of contacts
based on various parameters such as name, company, or relationship, enabling efficient contact
organisation and retrieval.
(iv) Streamlined communication: With instant access to up-to-date contact information, users can
communicate more effectively with clients, colleagues, or partners, fostering stronger professional
relationships.
(v) Reminder and scheduling: The system can include features like reminders and scheduling,
ensuring timely follow-ups, meetings, or important events related to contacts.
(vi) Integration and collaboration: Integration with other tools and platforms (such as email clients
or Customer relationship management (CRM) can enhance collaboration and enable seamless data
sharing.
(vii) Data Security: Implementing security measures and user access controls ensures the
confidentiality and privacy of sensitive contact information.
By developing a contact management system, an organisation can enhance productivity, streamline
communication, improve organisation, and optimise the overall contact management process.
10.1.2 SCOPE
The scope of the contact management system includes the development of a software solution that
allows for centralised storage, efficient organisation, and easy retrieval of contact information. It
encompasses features such as contact categorisation, search functionality, data import/export,
reminder and scheduling capabilities, and data security measures.
10.1.3 PROCESS MODEL

Page 179 of 200


The Contact Management System (CMS) can follow the agile software development process model,
which emphasises iterative development and flexibility. Following is an overview of the process model
for the CMS project:
(i) Requirements gathering: Engage with stakeholders to gather and document functional and non-
functional requirements for the CMS. It defines the scope, goals, and user expectations.
(ii) System design: It is responsible for designing the overall architecture and user interface of the
CMS. Determine the database structure, user workflows, and integration requirements.
(iii) Iterative development: It adopts an iterative approach to development. It divides the project into
smaller sprints or iterations, each delivering a functional increment of the CMS. Continuously
gather feedback from stakeholders and adjust the development plan accordingly.
(iv) Coding and testing: It develops the CMS functionalities using an appropriate programming
language (e.g.,Python) and framework. It is also responsible for conducting unit testing to ensure
the reliability and quality of the code.
(v) Integration and testing: It integrate different components of the CMS, including the database,
user interface, and external systems if required. It also conducts integration testing to verify the
proper functioning of the system as a whole.
(vi) User acceptance testing: It involves end-users to perform acceptance testing and gather feedback
on the usability and effectiveness of the CMS. It incorporates their suggestions and refine the
system accordingly.
(vii) Deployment and Maintenance: It deploys the CMS to the production environment and
ensures its stability and performance. Provide ongoing maintenance and support, addressing any
issues that arise and implementing future enhancements as required.
Throughout the process, it is important to maintain clear communication with stakeholders, conduct
regular meetings, and document all decisions and changes. This iterative and collaborative approach
allows for flexibility, adaptability, and continuous improvement of the Contact Management System.
10.2 SRS - SOFTWARE REQUIREMENT SPECIFICATIONS
10.2.1 Functional Requirements:
I. User registration and authentication: The system shall allow users to register and create an
account. The system shall provide authentication mechanisms to ensure secure access to the
CMS.
II. Contact management: The system shall allow users to add, edit, and delete contact information.
Users shall be able to categorise contacts based on name, company, or relationship. The system
shall support additional fields for contact details, such as email address, phone number, and
notes. Users shall be able to search and retrieve contacts using various parameters.
III. Contact import and export: The system shall allow users to import contacts from external
sources, such as CSV files. Users shall be able to export contacts to different formats, such as
CSV or vCard.
IV. Reminder and Scheduling: The system shall provide reminder and scheduling functionality for
important events or follow-ups related to contacts. Users shall receive notifications or alerts for
scheduled events.
V. User collaboration: The system shall allow users to share contacts or contact lists with other
authorised users. Users shall be able to collaborate and update contact information collectively.
Non-Functional Requirements:
(i) Performance: The CMS shall handle a large number of contacts efficiently and provide quick
response times. The system shall support concurrent user access without significant performance
degradation.

Page 180 of 200


(ii) Security: The CMS shall ensure the confidentiality and integrity of contact information. User
authentication and access control mechanisms shall be implemented to prevent unauthorised
access.
(iii) Usability: The CMS shall have a user-friendly interface with intuitive navigation and clear
instructions. The system shall provide error handling and validation mechanisms to guide users
in data entry.
(iv) Constraints: The CMS shall be developed using Python programming language. The system shall
utilise a MySQL database for storing contact information. The project shall adhere to a predefined
budget and timeline.
(v) Assumptions and Dependencies:
The CMS assumes a stable internet connection for online access. The project is dependent on the
availability and compatibility of third-party libraries or APIs for certain functionalities.
This software requirements specification provides an overview of the functional and non-functional
requirements for the contact management system project. It serves as a foundation for the development
and implementation of the CMS, ensuring that it meets the needs and expectations of the users.
10.2.2 Product functions
The Contact Management System (CMS) is a software application designed to streamline the
management and organisation of contact information. It provides users with a centralised platform to
efficiently add, search, and delete contacts. The CMS aims to simplify contact management processes,
enhance productivity, and improve communication. The product functions under CMS are as follows:
(i) Add contact module:
The Add Contact module allows users to easily enter and store contact details. Users can input
information such as name, company, email address, phone number, and additional notes. The module
provides a user-friendly interface with input validation to ensure accurate and complete data entry.
Upon adding a contact, the system validates and saves the information into the centralised database,
ensuring that it is readily available for future use.
(ii) Search and delete module:
The search and delete module enable users to find specific contacts based on various search criteria.
Users can search for contacts by name, company, or any other relevant parameters. The module
retrieves matching contacts from the database and displays them for easy access. Users can then
select and delete contacts that are no longer needed or relevant, ensuring a clean and up-to-date
contact list.
(iii) Contact update module:
The contact update module is a crucial component of the Contact Management System project. It
empowers users to modify and update contact details seamlessly. With this module, users can easily
edit information such as names, phone numbers, email addresses, and additional notes. The module
ensures data accuracy and enables users to keep their contact database up-to-date. It offers a user-
friendly interface with intuitive editing features, validation checks, and error handling mechanisms.
The contact update module plays a vital role in maintaining accurate and relevant contact information,
contributing to effective communication and efficient contact management within the system.
With these two key modules, the CMS provides a robust contact management solution. Users can
quickly add new contacts, ensuring that all relevant information is stored securely in one place. The
search functionality allows for easy retrieval of contacts, saving time and effort in locating specific
individuals or organisations. Additionally, the delete module ensures that the contact list remains
organised and clutter-free by allowing users to remove unnecessary or outdated contacts. Overall, the
CMS enhances efficiency, improves organisation, and streamlines contact management processes for
individuals and organisations alike.
User Characteristics:

Page 181 of 200


(i) Individual users: Individuals who want to manage and organise their personal contacts effectively.
(ii) Business professionals: Professionals who need to maintain a centralised and up-to-date contact
database for effective communication and networking.
(iii) Organisations: Businesses and institutions that require a contact management system to store
and manage contact information of clients, employees, and stakeholders.
10.2.3 General constraints
General constraints for contact management system (CMS) in software requirements specification
(SRS):
I. Time constraint: The CMS project must be completed within the specified timeline.
II. Budget constraint: The project development and implementation should adhere to the allocated
budget.
III. Technology constraint: The CMS must be developed using the Python programming language and
utilise a MySQL database for contact storage.
IV. Assumptions and dependencies for contact management system: The CMS is assumed to be
compatible with the Computer Systems on which it has been loaded for personal or organisational
use.
10.3 DATA FLOW DIAGRAM
10.3.1 DFD 0 Level

Contact
Adding New
Management Delete Contact
Contacts
System

Updating
Contacts

10.3.2 DFD 1 Level Contact Management


System

Adding New Updating Contacts Delete Contact


Contacts

Data
Base

Page 182 of 200


10.4 Project management
10.4.1 Functional point estimation
Function Type Complexity Complexity Complexity
Weight (Low) Weight (Average) Weight (High)
External Inputs (EI) 3 4 6
External Outputs (EO) 4 5 7
External Inquiries (EQ) 3 4 6
Internal Logical Files (ILF) 7 10 15
External Interface Files (EIF) 5 7 10

Based on the complexity weightings, one can assign a value to each function point based on the
complexity of the specific functionality in the Contact Management System project. The total functional
points can then be calculated by multiplying the assigned values with the complexity weights and
summing them up for each function type.
For example, if you determine that adding a contact has average complexity, the functional points for
External Inputs (EI) would be 4. Similarly, if displaying contact details is of low complexity, the
functional points for External Outputs (EO) would be 4. After assigning values to each function point,
you can calculate the total functional points by summing up the function points for each function type.
10.4.2 Risk Table
Risk ID Risk Description Probability Impact Severity Mitigation Strategy
R1 Lack of User High High High Conduct thorough user training
Adoption and provide ongoing support.
Ensure the system is intuitive and
user-friendly.
R2 Data Loss or Medium High High Implement robust data backup and
Security Breach security measures, such as
encryption and access controls.
Regularly perform security audits.
R3 Integration Medium Medium Medium Conduct thorough compatibility
Challenges with testing and ensure proper
APIs documentation and support for API
integration. Have fullback options
in case of integration issues.
R4 Inadequate Medium Medium Medium Optimise code and database
System queries for efficient performance.
Performance Perform load testing to identify and
address performance bottlenecks.
R5 Requirement High Medium High Maintain effective communication
Changes or Scope with stakeholders, document
Creep requirements clearly, and manage
scope changes through change
control processes.
R6 Inadequate Medium Medium Medium Implement a comprehensive testing
Testing and plan, including unit testing,
Quality Assurance integration testing, and user
acceptance testing. Follow best
practices for code review and
quality assurance.

Page 183 of 200


10.5 Data Design
Add New
FIRST NAME str
LAST NAME str
GENDER str
AGE int
ADDRESS str
CONTACT str
Update Data
FIRST NAME str
LAST NAME str
GENDER str
AGE str
ADDRESS str
CONTACT str
Mem_id int
ER Model
Delete

First Name
Contact

Add New Member id


Last Name

Addres
s
Gender
Age

First Name
Contact

Updating Contacts

Last Name

Address

Gender
Age

Page 184 of 200


HARDWARE AND SOFTWARE REQUIREMENTS
➢ Hardware Specification
• Processor: Intel Pentium iv and above
• Processor Speed: 1GHz to 2GHz and above
• RAM: 512MB to 1GB and above
• Hard Disk: 4GB to 30GB and above
• Keyboard: 104 keys
➢ Software Specification.
• Language: Python
• Database: SQL, sqlite3
• Operating System: Windows 7 / Windows 10/ Windows 11
10.6 Testing
a) Test case for adding a contact:
Verify that a new contact can be added with all the required fields (name, phone number, email,
etc.).
Verify that an error is displayed if any of the required fields are missing.
Verify that duplicate contacts with the same phone number or email are not allowed.
b) Test case for searching a contact:
Verify that a contact can be searched by name, phone number, or email.
Verify that the search returns the correct contact(s) matching the given criteria.
Verify that an appropriate message is displayed if no matching contacts are found.
c) Test case for editing a contact:
Verify that the details of an existing contact can be successfully updated.
Verify that the changes are reflected correctly in the contact list.
Verify that an error is displayed if trying to update a non-existent contact.
d) Test case for deleting a contact:
Verify that a contact can be deleted from the contact list.
Verify that the contact is removed and no longer appears in the list.
Verify that an error is displayed if trying to delete a non-existent contact.
e) Test case for listing all contacts:
Verify that all the contacts in the contact list are displayed correctly.
Verify that the contacts are sorted alphabetically by name.
Verify that an appropriate message is displayed if the contact list is empty.
The actual test cases will depend on the specific functionality and requirements of the system. It is
important to cover both positive and negative scenarios to ensure proper system behaviour.

Page 185 of 200


Program 1: A program to display Contact Management System in python.
The program is coded in Python IDLE and its output is shown below.

Programming step 1 Create Contact Management System

Page 186 of 200


Page 187 of 200
Page 188 of 200
Add New Contact 1

After Add New Contact following will appear:

Delete any record

Page 189 of 200


Updating Contacts

Page 190 of 200


Unit 4. Emerging Trends and Social Impact
Chapter 11. Emerging Trends and New Technology
Introduction
Emerging Trends in Technology have been around for a long time. Every day, new technologies and
projects emerge. We must keep an eye on upcoming trends in order to grasp existing technology and
have a better understanding of the advancements around us. Almost every day, new technologies are
introduced. Some of these fails and fade away over time. Some of these new technologies thrive and
persist over time, earning user attention. Emerging trends are cutting-edge technologies that gain
popularity and establish a new trend among consumers. This chapter will let us understand some
developing trends that will have a significant impact in the future on the digital economy and social
interaction in digital societies.
11.1 Artificial Intelligence (AI)
Have you ever wondered how maps on your smartphone can direct you to the quickest path to your
destination by assessing real-time data such as traffic congestion? Have you ever uploaded a photo
to a social networking site and had your friends in the snap detected and tagged automatically? These
are some instances of artificial intelligence applications. AI powers intelligent digital personal
assistants such as Siri, Google Now, Cortana, and Alexa.
Artificial intelligence attempts to imitate human intellect in machines, causing them to behave
intelligently. An intelligent machine is designed to mimic human cognitive functions such as learning,
decision-making, and problem solving. Machines are trained to construct a knowledge base and make
judgements based on it in order to complete jobs with minimal human intervention. To make new
decisions, AI systems can learn from previous experiences or outcomes.
11.2 Machine Learning
Machine learning is an artificial intelligence subsystem in which computers can learn from data using
statistical techniques without being explicitly trained by humans. It consists of algorithms that use
data to learn and forecast on their own. These algorithms, known as models, are first taught and
tested using training and testing data. Following multiple trainings, once these models are capable
of producing findings with an acceptable level of accuracy, they are used to forecast new and
unknown data.
11.3 Internet of things (IOT)
The internet of things (IoT) is a set of technologies that uses sensors and actuators to inform us about
the status of everyday items such as vehicles, tools and even living beings. It allows us to interact
with them, enabling connectivity with platforms in the cloud that receive and process information for
posterior analysis. This analysed data is then used to make decisions.

Fig. 4.1 Internet of things (IOT)


11.3.1 There are three types of IoT applications
Billions of devices are connected to the internet, collecting and sharing information with one another.
They range from smart home setups like cooking appliances and smoke detectors to military-grade
surveillance equipment. The list below outlines a few of the most common types of IoT applications.

Page 191 of 200


1. Consumer IoT
2. Industrial Internet of Things (IIoT)
3. Commercial IoT
1. Consumer IoT: - Consumer IoT refers to personal and wearable devices that connect to the
internet. These devices are often referred to as smart devices.
2. Industrial Internet of Things (IIoT): - The industrial Internet of Things is the system of
interconnected devices in the industrial sector. Manufacturing machinery and devices used for
energy management are a part of the industrial Internet of Things.
3. Commercial IoT: - Commercial IoT refers to the tools and systems used outside of the home. For
example, businesses and health care organizations leverage commercial IoT for auditable data
trails and consumer management.
11.3.2 Internet of Things platform
An IoT platform manages device connectivity. It can be a software suite or a cloud service. The
purpose of an IoT platform is to manage and monitor hardware, software, processing abilities, and
application layers.
• Sensor technologies: - IoT sensors, sometimes called smart sensors, convert real-world variables
into data that devices can interpret and share. Many different types of sensors exist. For example,
temperature sensors detect heat and convert temperature changes into data. Motion sensors
detect movement by monitoring ultrasonic waves and triggering a desired action when those waves
are interrupted.
• Unique identifiers: - The core concept of the IoT is communication among devices and users.
Unique identifiers (UIDs) establish the context of a device within the larger network to enable this
communication. Identifiers are patterns, like numeric or alphanumeric strings. One example of a
UID that you might be familiar with is an internet protocol (IP) address. They can identify a single
device (instance identifier) or the class to which that device belongs (type identifier).
• Internet connectivity: - Sensors can connect to cloud platforms and other devices through a
host of network protocols for the internet. This enables communication between devices.
• Artificial intelligence (AI) and machine learning: - Natural language processing (NLP) in IoT
devices makes it easier for users to input information and interact with devices. One common
example of an IoT device that utilizes NLP technology is the Amazon Alexa. Machine learning also
enhances the analytical capabilities of IoT devices.
• Edge computing: - Edge computing is a computing framework. It aims to conserve resources and
speed up response time by moving computational resources like data storage closer to the data
source. The IoT accomplishes this by utilizing edge devices like IoT gateways.
11.3.3 Internet of Things examples
• Smart home devices: -Smart devices are interactive electronics that use wireless connections to
understand user instructions. To an extent, smart home devices like thermostats and home
security systems can work autonomously to assist with daily tasks. For example, you may program
your smart thermostat to adjust automatically to a cooler setting before you arrive home from
work. Or, you may receive a security camera notification to inform you that someone is at the door
when you are not home.
• Wearable technologies: - One of the most common Internet of Things examples is smartwatches.
Wearable IoT technology like Fitbits and Apple Watches connect to other devices (like your
smartphone) to share data. They typically also connect to the internet to track GPS locations.
• Personal medical devices: - Personal medical devices like pacemakers are also IoT devices.
Remote medical devices can help monitor and share a patient's vital signs or detect early signs of
health issues for fast intervention.
• Autonomous vehicles: - Self-driving cars and other connected vehicles rely on the internet to
share real-time information. Sensors throughout the vehicle help map its surroundings, transmit
camera footage, and respond to traffic signals.

Page 192 of 200


11.4 Web of Things (WoT)
The Internet of Things enables us to interact with many gadgets via the internet via smartphones or
computers, resulting in the creation of a personal network. However, in order to interface with 'n'
different devices, we must install 'n' distinct apps. Wouldn't it be easier to have a single interface to
link all of the devices? The web is already being used to communicate with one another. So, will it be
able to use the web in such a way that all things may communicate with each other as efficiently as
possible by integrating them? The Web of Things (WoT) uses online services to connect things in the
physical world in addition to human identities on the web. It will open the road for development.
Sensors
The display also shifts from vertical to horizontal depending on how we hold our phone. This is made
possible by two sensors: the accelerometer and the gyroscope (gyro). The accelerometer sensor in
mobile phones senses the phone's orientation. The gyroscope sensors detect the rotation or twisting
of your hand and supplement the information provided by the accelerometer. Sensors are widely used
in real-world applications to monitor and observe elements. The evolution of smart electronic sensors
is making a significant contribution to the evolution of the IoT. It will result in the development of
new sensor-based, intelligent systems. A smart sensor is a gadget that receives input from the
environment.
Smart Cities
Cities that are smart with rapid urbanisation are known as ‘Smart cities. The burden on our cities
grows by the day, and there are challenges in managing resources such as land, water, waste, air
pollution, health and sanitation, traffic congestion, public safety, and security, as well as overall city
infrastructures such as roads, rail, bridges, electricity, subways, disaster management, sports
facilities, and so on. Many city planners throughout the world are looking for wiser solutions to
address these difficulties and make cities more sustainable and liveable.

Fig 4.2 Smart Cities


11.5 Blockchain
Traditionally, we do digital transactions by saving data in a centralised database, and the
transactions are updated in the database one by one. That is how ticket-booking websites and banks
work. However, because all data is housed in a centralised location, there is a risk of it being hacked
or lost. Blockchain technology is based on the idea of a decentralised and shared database, with each
computer having a copy of the database. A block is a secure chunk of data or a valid transaction.
Each block has certain data called its header that is visible to every other node, but only the owner
has access to the block's private data.

Page 193 of 200


Fig 4.3 Blockchain
Blockchain can be defined as a system that enables a collection of connected computers to keep a
single, updated, and secure ledger. Each computer or node in the blockchain receives a complete
copy of the database. It keeps an 'append only' open ledger that is only updated after all nodes in the
network authenticate the transaction. The safety and security of transactions are assured since all
network members keep a copy of the blockchain, making it impossible for a single network member
to make changes or alter data.
11.6 Social engineering
● All approaches intended to convince a target to reveal specific information or perform a specific
activity for illegitimate reasons are referred to as social engineering. Though such deception has
always occurred, it has evolved tremendously as a result of ICT technology. Social engineering
techniques in IT can be viewed from two perspectives in this new context:
● Either by using psychological manipulation to gain additional access to an IT system where the
scammer's actual objective resides, such as impersonating an important client via a phone call
to lure the target into browsing a malicious website to infect the target's workstation; or by using
IT technologies as support to psychological manipulation techniques to achieve an objective
outside the IT realm, such as obtaining banking credentials via a phishing attack to then steal.
The growing usage of IT technology has naturally increased the use of such approaches, as well
as their combination, to the point where most cyberattacks now contain some sort of social
engineering.
11.7 Data Analytics
Data analytics is the process of examining data sets to derive conclusions about the information
contained within them using specialised systems and software. Data analytics tools and
methodologies are becoming increasingly prevalent. They are used in commercial industries to help
businesses make better business decisions. It can be useful for researchers in the realm of science
and technology to validate or reject scientific models, theories, and hypotheses. Pandas is a Python
computer language package that may be used as a tool to make data analysis much easier.

Page 194 of 200


Chapter 12. Social impact on technology
12.1 Impact on Health
We are spending more time in front of screens, whether mobile, laptop, desktop, television, game
console, music, or sound device, as digital technologies have permeated several areas. However,
conversing in an incorrect posture can be harmful to our health, both physically and mentally.
Furthermore, excessive Internet use can be addictive and have a harmful impact on our physical and
psychological well-being.
However, these health risks can be addressed to some extent by paying attention to how we position
such gadgets and our posture. Ergonomics is a branch of science concerned with creating or
arranging workplaces, including furniture, equipment, and systems, so that the user is safe and
comfortable. Ergonomics assists us in lowering the burden on our bodies, such as weariness and
injuries caused by continuous use. When we stare at a screen for long periods of time to watch, type,
chat, or play games, our eyes are constantly exposed to the glare from the screen. Looking at small
handheld devices aggravates the situation. Eye strain is a common complaint among digital gadget
users.

Fig. 12.1 Impact on Health


Maintaining the viewing distance, angle, and position ergonomically can be beneficial. The posture
that should be maintained to reduce weariness induced by continuous usage of a computer system
and other digital devices However, to avoid dry, watery, or itchy eyes, it is best to focus on distant
objects and take breaks for outside activities. Bad posture, backaches, and neck and shoulder
difficulties can be avoided by structuring the office according to ergonomic guidelines.
Overuse of keyboards (physical or touchscreen-based virtual) that are not ergonomically aligned can
result in a painful condition of the wrists and fingers, necessitating medical attention in the long
term. Other negative consequences of excessive use of digital gadgets include stress, physical
weariness, and weight gain.
12.2 Cyber Crime
Cybercrime refers to criminal acts or offences committed in a digital context. In such crimes, the
computer is either the target or is used as a tool to perpetrate the crime. Cybercrime is committed
against an individual, a group, an organisation, or even a country with the goal of causing physical
harm, financial loss, or mental harassment, either directly or indirectly. A cybercriminal targets a
computer or a network in order to gain access to other computers and disable or damage data or
services. A cybercriminal may also use viruses and other malware to obtain private and confidential
data for blackmail and extortion.
A computer virus is a piece of malicious code that can replicate itself and cause harm to computers
by deleting data or damaging the system. Malware, on the other hand, is software that is intentionally
designed to obtain unauthorised access to computer systems. Criminal activity is becoming
increasingly sophisticated, with frequent allegations of hacking, ransomware assaults, denial-of-
service attacks, phishing, email fraud, banking fraud, and identity theft.
(i) Hacking

Page 195 of 200


Unauthorised access to a computer, computer network, or other digital system is referred to as
hacking. Hackers typically have technical knowledge of both hardware and software. They search for
defects in order to exploit them and gain access to the system. When done with good intention,
hacking is referred to as ethical hacking. White-hat hackers are those who hack ethically. They are
experts in uncovering any vulnerability or gap during software testing. As a result, they contribute
to software security. An ethical hacker may abuse a website to identify security flaws or
vulnerabilities. He then informs the website's owner of his findings. As a result, ethical hacking
actually prepares the owner for any cyberattack. A non-ethical hacker is someone who attempts to
obtain unauthorised access to computers or networks in order to steal sensitive data with the purpose
of causing system damage or failure. They are referred to as black hat hackers or crackers. Their
primary focus is on security flaws and data theft. They employ their talent for illicit or evil reasons.
Such hackers attempt to breach system security in order to steal identities, get monetary gain, bring
a competitor or rival site down, disclose important information, and so on.
(ii) Phishing and Fraud E-mails
Phishing attacks are the practice of sending fraudulent communications that appear to come from a
reputable source. It is usually done through email. The goal is to steal sensitive data like credit card
and login information, or to install malware on the victim’s machine. Phishing is a common type of
cyber-attack that everyone should learn about in order to protect themselves.
Phishing starts with a fraudulent email or other communication that is designed to lure a victim. The
message is made to look as though it comes from a trusted sender. If it fools the victim, he or she is
coaxed into providing confidential information, often on a scam website. Sometimes malware is also
downloaded onto the target’s computer.
(iii) Ransomware
Another type of cybercrime is one in which the attacker gains access to the computer and prevents
the user from accessing it, typically by encrypting the data. The attacker blackmails the victim into
paying a ransom to gain access to the data or threatens to disclose personal and sensitive information
or images if the ransom is not paid. Ransomware can be downloaded when users browse fraudulent
or untrustworthy websites or download software from questionable repositories.
Some ransomware is distributed as email attachments in spam messages. It can also enter our
system if we click on a malicious advertisement while surfing the web. Cybercrime combat and
prevent the challenges of cybercrime can be addressed by being vigilant and seeking legal counsel.
The following precautions can be taken to lessen the risk of cybercrime:
• Back-up vital data on a regular basis;
• Use antivirus software and keep it up to date; and
• Avoid installing unlicensed software. Download software only from well-known and secure (HTTPS)
websites.
• Always keep the system software, which includes the Internet browser and other application
software, up to date.
• Never visit or download anything from untrusted websites.
• Use a strong password for web login and update it on a regular basis. Use a different password for
each website. Use different combinations of alphanumeric characters, including special
characters, in your password. Avoid using familiar words or names.
12.3 PRIVACY
It all depends on who you ask. In broad terms, privacy is the right to be alone or the freedom from
disturbance or intrusion. The right to regulate how your personal information is gathered and used
is referred to as information privacy. When you ask most people what they think of privacy these
days, you're likely to hear about big data breaches, wearable technology, social networking, and
targeted advertising gaffes—not to mention the Snowden revelations. Furthermore, different cultures
have drastically divergent perspectives on what a person's private rights are and how they should be

Page 196 of 200


managed. Data privacy is concerned with the usage and regulation of personal data, such as
establishing procedures to ensure that consumers' personal information is acquired, shared, and
utilised appropriately. It is impossible to halt the advancement of new technologies. However, its use
may be restricted. Privacy as a basic right is governed by special rules that place innovative firms
that engage with personal data in a framework designed to preserve data subjects' rights while not
jeopardising the concept of innovation. The impact of technology on privacy is supplied by a mix of
diverse inventions that are applied differently in different circumstances around the world, forming
privacy legislation in a way that meets the new problems of innovation. Because of technological
advancements, many new concepts and roles have been incorporated into privacy legislation, which
is constantly updated in tandem with technology.
12.4 Intellectual Property Right (IPR)
We say that someone owns property when they own a house or a motorcycle. Similarly, if someone
comes up with a new idea, that person's original idea is their intellectual property. Inventions, literary
and artistic expressions, designs and symbols, names, and logos are all examples of intellectual
property. Such notions are owned by the originator or the holder of the intellectual property. This
allows the inventor or copyright holder to gain recognition or a financial advantage from the use of
their creation or invention. Copyright, patents, trademarks, and other forms of intellectual property
are legally protected.
(i) Copyright
Copyright gives authors legal rights to their original works, such as writing, photography, audio
recordings, video, sculptures, architectural works, computer software, and other creative works such
as literary and aesthetic works. Creators and authors are automatically granted copyright. Copyright
law grants the copyright holder a set of legal rights that they alone can exercise. The rights include
the right to duplicate (reproduce) a work, the right to develop derivative works based on it, the right
to distribute public copies of the work, and the right to publicly display or perform the work. It forbids
others from stealing, using, or selling the work. For example, Rudyard Kipling owns the rights to his
novel 'The Jungle Book,' which relates the story of Mowgli, the jungle kid. If someone used parts of
the novel without permission, it would constitute an infringement of the author's copyright. To use
the copyrighted material of others, a licence must be obtained from them.
(ii) Patent
Inventions are typically granted patents. In contrast to copyright, the inventor must apply (file) for
patenting the innovation. When a patent is issued, the owner obtains the exclusive right to prohibit
anyone from using, selling, or distributing the protected innovation. The patentee has complete
discretion over whether or not the innovation can be used by others. As a result, it encourages
inventors to share their scientific or technological breakthroughs with others. A patent protects an
invention for 20 years before it can be used freely. Recognition and/or financial gain develop the ideal
environment and motivate more creativity and innovation.
(iii) Trademark
A trademark is any visual symbol, term, name, design, slogan, label, or other feature that identifies
one brand or commercial entity from another. For example, no company other than Nike may sell
shoes or clothing under the Nike brand. It also prevents others from using a mark that is confusingly
similar, such as words or phrases. For example, confusing labels such as "Nikke" are not permitted.
However, applying for the Nike trademark for unrelated goods such as notebooks may be possible.
12.5 IT act of India
The Information Technology Act of India (IT Act) many examples of cybercrime, fraud, cyberattacks,
and cyberbullying have been documented as the Internet has grown in popularity. The types of
fraudulent actions and crimes are always evolving. To combat such threats, many governments have
enacted legislation to secure sensitive personal data and defend Internet users' rights. The
Information Technology Act, 2000 (commonly known as the IT Act) of the Government of India, as
revised in 2008, gives instructions to users on the processing, storage, and transmission of sensitive

Page 197 of 200


information. Many Indian states have cyber cells in police stations where any cybercrime can be
reported.
By recognising electronic records and digital signatures, the act creates a legal framework for
electronic government. The statute defines cybercrime and the punishments for it. The Cyber
Appellate Tribunal was established to settle disputes stemming from cybercrime, such as tampering
with computer source documents, hacking the computer system, using another person's password,
releasing sensitive personal data of others without their authorisation, and so on. The act is required
so that consumers can conduct credit card transactions over the Internet without fear of fraud. Not
only individuals but also government departments are empowered by the act to accept the filing,
creation, and preservation of official documents in digital formats.
12.6 Digital Carbon footprints
Digital Carbon footprints Have you ever looked up information on the internet? Have you ever
purchased an internet ticket, answered an email from a friend, or checked the score of a game online?
When we use smartphones, tablets, laptops, and so on to access the Internet, we leave a trail of data
representing our online habits, which is known as our digital footprint. Our digital footprint can be
generated and exploited with or without our awareness. It comprises websites we visit, emails we
send, and any information we submit online, among other things, as well as the computer's IP
address, location, and other device-specific facts. Such information might be used for targeted
advertising, but it could also be misused or exploited.
As a result, it is important to be mindful of the data trail we may be leaving behind. This awareness
should make us more cautious about what we write, upload, download, or browse on the internet.
There are two types of digital footprints that we leave. Active digital footprints, which comprise
information that we knowingly publish online. This includes emails we send, responses or postings
we make on various websites or mobile apps, and so on. Passive digital footprints are the unintended
digital data trails we leave online. This includes information created when we visit a website, using a
mobile app, exploring the Internet, and so on.

Fig. 12.2 Digital Carbon footprints


Everyone who is connected to the Internet may have a digital footprint. With more usage, the trail
grows. Reviewing the browser settings, we can find out how it stores our browsing history, cookies,
passwords, auto fills, and many other types of data. Besides the browser, most of our digital footprints
are stored on servers where the programmes are housed. We may not have access to remove or
destroy the data, nor do we have any influence on how that data will be utilised.
As a result, once a data trail is created, even if we subsequently attempt to delete data about our
online actions, the digital footprints remain. There is no certainty that digital traces will be completely
removed from the Internet. As a result, we must exercise greater caution when using the internet! All
of our online activities leave a data trail on both the Internet and the computing equipment we use.
This can be used to track the user's location, device, and other usage information.

Page 198 of 200


Page 199 of 200
Page 200 of 200

You might also like