About Pearson
Pearson is the world’s learning company, with presence across 70 countries
worldwide. Our unique insights and world-class expertise comes from a
long history of working closely with renowned teachers, authors and
thought leaders, as a result of which, we have emerged as the preferred
choice for millions of teachers and learners across the world.
We believe learning opens up opportunities, creates fulfilling careers and
hence better lives. We hence collaborate with the best of minds to deliver
you class-leading products, spread across the Higher Education and K12
spectrum.
Superior learning experience and improved outcomes are at the heart of
everything we do. This product is the result of one such effort.
Your feedback plays a critical role in the evolution of our products and you
can contact us – [email protected]. We look forward to it.
OceanofPDF.com
PYTHON PROGRAMMING
S. Sridhar
Department of Information Science and Technology
CEG Campus
Anna University, Chennai
J. Indumathi
Department of Information Science and Technology
CEG Campus
Anna University, Chennai
V. M. Hariharan
Vels Infoway
Chennai
OceanofPDF.com
This book is dedicated to my Grandmother and Father-in-Law,
Late Tmt. A. Ambujam Ekambaram and Late Shri T.C.Nagarajan
for their eternal inspiration for writing this book.
S. Sridhar
This book is dedicated to my Mother, Father, Husband, and Sister,
Tmt. A. Indrani, Shri V. Jayaraman, Shri P.Jeyakumar, and
Dr. J. Gitanjali.
I fervently hope that this book will repay the perpetual debt I owe.
J. Indumathi
This book is dedicated to my Father and my Mother
Shri S. Velmurugan and Tmt. P. Nagalakshmi, who taught
me about life.
V. M. Hariharan
OceanofPDF.com
CONTENTS
Preface
About the Authors
1. Introduction to Programming
1.1 Basics of Programming Language
1.2 Programming Versus Software
1.3 Python Programming Language
1.4 Compiler Versus Interpreter
1.5 Integrated Development Environment (IDE)
1.5.1 Python in interactive mode (windows OS)
1.5.2 Python in script mode
1.5.3 Command Line Interpreter
1.6 Various Resources for Python Programming
2. Problem Solving and Algorithms
2.1 Problem-solving and Computational Skills
2.2 Computational Thinking
2.3 Software Development Cycle
2.4 Algorithm Sesign Strategies
2.5 Algorithm Design
2.6 Flow Charts
2.7 Example Algorithms
2.7.1 Algorithm for Subtraction of Two Numbers
2.7.2 Algorithm for Finding the Area of a Circle
2.7.3 Algorithm for Finding the Largest Among Three Numbers
2.7.4 Factorial of a Number
2.7.5 Euclid Algorithm
2.7.6 Linear Search
2.7.7 Insertion Sort
2.8 Analysis of Algorithms
2.9 Efficiency of the Algorithms
3. Introduction to Python
3.1 What is a Program?
3.2 Elements of Python Language
3.3 Python Block Structure
3.3.1 Importance of Indentation
3.3.2 Comments in Python
3.4 Variables and Assignment Statement
3.5 Python Objects
3.6 Data Types in Python
3.6.1 Basic Data Types in Python
3.6.2 Collection Data Types
3.7 Operations
3.7.1 Arithmetic Operators and Expressions
3.7.2 Compound Assignment Operators
3.7.3 Boolean Operations
3.7.4 Relational Operators and Boolean Expressions
3.7.5 Bitwise Operators
3.7.6 Identity and Membership Operators
3.8 Type Conversions
3.9 Simple input/output Statements
3.9.1 Reading Numbers from the Keyboard
3.9.2 print() Statement
3.10 Formatting Using Print Statement
3.10.1 Built-in Methods
3.10.2 String Interpolation (old Style Formatting)
3.10.3 String Formatting
3.10.4 Formatted String Literals (F-string or f-string)
3.11 Built-in Functions
4. Decision Structures
4.1 Need for Decision Structures
4.2 Forming Conditions
4.3 if-statement
4.4 The if-else Statement
4.4.1 Inline-if-Statement
4.5 if-elif Statement
4.6 Nested-If Conditions
5. Looping Statements
5.1 Introduction to Looping
5.2 Python Built-in Functions for Looping
5.3 Loop Statements
5.3.1 while Loop
5.3.2 for Loop
5.3.3 Nested Loops
5.4 Jump Statements
5.4.1 break Statement
5.4.2 continue Statement
5.4.3 pass Statement
6. Introduction to Functions
6.1 Introduction to Functions
6.2 Types of Functions
6.3 Anatomy of a User-defined Function
6.4 Types of Functions Based on Parameters
6.4.1 Non-Parameterised Function
6.4.2 Parameterised Functions
6.5 Void Functions and Fruitful (Value Returning) Functions
6.5.1 Void Function
6.5.2 Fruitful Functions
6.6 Types of Function Arguments
6.6.1 Functions with No Arguments
6.6.2 Functions with Positional Arguments
6.6.3 Keyword Arguments
6.6.4 Default Arguments
6.6.5 Variable Length Arguments
6.6.6 Variable Keyword-Based Arguments
6.6.7 Python Anonymous Functions
6.7 Passing of Objects Reference
6.8 Namespaces, Global Variables, and Scope
7. Lists
7.1 Introduction to Iterable Object
7.2 List
7.2.1 Creating a List
7.2.2 Indexing in a List
7.2.3 List Slicing
7.3 Operations in the List
7.4 Built-in Functions and Methods in List
7.5 Nested List
7.6 Mutability of List
7.7 Looping with List
7.8 List as a Stack
7.9 List as Queue
8. Tuples
8.1 Introduction to Tuples
8.2 Creating a Tuple
8.3 Indexing and Slicing in a Tuple
8.4 Operations in Tuples
8.5 Immutability in Tuples
8.6 Built-in Function and Tuples
8.7 Built-in Methods in Tuples
8.8 Nested Tuples
8.9 Selection
8.10 Looping with Tuples
9. Sets and Dictionary
9.1 Introduction to Sets
9.2 Creating a Set
9.3 Basic Operation in Sets
9.4 Mathematical Operations of Sets
9.5 Looping Operations in Sets
9.6 Frozen Sets
9.7 Introduction to Dictionary
9.8 Creating Dictionaries
9.9 Basic Dictionary Operations
9.10 Operations on Python Dictionary
9.11 Nested Dictionary
9.12 Looping Over a Dictionary
10. Strings
10.1 Introduction to Strings
10.2 Creating a String
10.3 Operations in Strings
10.4 Built-in Methods for Strings
10.5 Built-in Functions for Strings
10.6 Looping with Strings
10.7 Regular Expressions
11. Advanced Functions
11.1 Introduction to Functional Programming
11.2 Functions as Objects
11.3 Lambda Functions
11.4 List Comprehensions
11.5 List Comprehension with Sequence Processing
11.5.1 Map Function
11.5.2 Filter Function
11.5.3 reduce Function
11.6 Iterators
11.7 Generators
11.7.1 Generator Expressions
11.7.2 Itertools, Special Iterators, and Generators
11.8 Recursion
11.9 Closed Functions and Function Annotators or Decorators
12. Object-Oriented Programming (OOP)
12.1 Introduction to Object Oriented Programming (OOP)
12.2 Classes and Objects
12.3 Class Attributes
12.4 Methods in Class
12.4.1 Private Methods
12.4.2 Constructor
12.4.3 Destructor
12.5 Built-ins
12.5.1 Built-in Methods in Class
12.5.2 Built-in Attributes in Objects
13. Inheritance
13.1 Introduction
13.2 Single Inheritance
13.3 Multiple Inheritance
13.4 Multi-Level Inheritance
13.5 Hybrid Inheritance
14. Advanced Class
14.1 Overloading
14.1.1 Method Overloading
14.1.2 Operator Overloading
14.2 Method Overriding
14.3 class Method and static Method
14.4 Abstract Base Class (ABC)
14.5 Meta Class
15. Modules in Python
15.1 Introduction to Modules and Packages in Python
15.2 Creation of a Module
15.3 Importing Modules
15.4 Uses of Standard Modules in Python
15.4.1 Math and Sympy Packages
15.4.2 os Module
15.4.3 sys Module
15.4.4 py_compile
15.5 Packages
16. Errors and Exceptions
16.1 Introduction to Errors and Exceptions
16.2 Handling Exceptions
16.3 Multiple Exceptions
16.4 Raising Exceptions
16.5 Exception Chaining
16.6 Built-In Exceptions
16.7 User-Defined Exceptions
16.8 Clean-Up Actions
17. Debugging and Testing
17.1 Introduction to Python Debugger
17.1.1 Python Debugger (pdb)
17.1.2 pdb through Primitive GUI Mode in IDLE
17.2 Testing in Python
17.2.1 Doctest
17.2.2 Unittest
17.2.3 Pytest
17.3 Introduction to Profiling
17.3.1 Python timeit()
17.3.2 cProfile
18. File Handling
18.1 Introduction to Files
18.2 Types of Files
18.3 Input and Output Operations
18.4 File Handling Operations in Text Files
18.5 File Handling Operations in Binary Files
18.5.1 Pickle
18.6 File Handling Operations in Excel Files
18.7 Python Context Manager
19. Introduction to Graphical User Interface
19.1 Introduction
19.2 Tkinter Module
19.2.1 Display Text with Label Widgets
19.2.2 Button Widgets
19.2.3 Frame Widget
19.2.4 Input Text Box
19.2.5 Grid
20. Introduction to Data Analytics with Python
20.1 Introduction to Data Analysis
20.2 Dataset and Data Analysis
20.3 Arrays in Python
20.3.1 Importing and Creating an Array
20.3.2 Array Operations
20.4 Introduction to NumPy
20.4.1 Array Creation in NumPy
20.4.2 NumPy Properties
20.4.3 Indexing and Slicing Operations
20.4.4 Arithmetic Operations on NumPy
20.4.5 Data Analysis Using NumPy
20.5 Pandas
20.5.1 Series in Pandas
20.5.2 CRUD Operations with Pandas
20.5.3 Series Indexing
20.5.4 Series Statistical Methods
20.5.5 Data Frames with Pandas
20.5.6 Indexing in Data Frames
20.5.7 Data Manipulation in Data Frames
20.5.8 Data Analysis using Pandas
20.6 Data Visualization Using Pandas and Matplotlib
20.7 Scikit-Learn and Data Analysis
Bibliography
Index
ONLINE CHAPTERS
(Refer to inside front cover for details to access online content)
21. Database and Parallel Programming
22. Networks and Security
Appendix 1: Python Installation
Appendix 2: Anaconda Installation
Appendix 3: Installation of Python Packages and Command Line
Interpreter (CLI)
Appendix 4: Turtle
Appendix 5: Optimization
Case Studies
Case Study 1: ZigZag
Case Study 2: Sieve of Eratosthenes
Case Study 3: Magic 8 Ball with a List
Case Study 4: Adding Bullets to List
Case Study 5: Pig Latin
Case Study 6: MultiClipboard Automatic Messages
Case Study 7: Phone Number and Email Address Extractor
Case Study 8: ATM
Case Study 9: Compressing files using Zip Files
Case Study 10: Renaming Files
Case Study 11: Download images from the Website
Case Study 12: Fetching Current Weather Data
Case Study 13: USD to INR using GUI
Case Study 14: Finding the number of ways of arranging the letters of the
given word
OceanofPDF.com
PREFACE
In the 20th Century, one of the most game-changing technological
advancements was the invention of personal computers. Everyone uses
computers for a variety of activities like sending and receiving emails,
games, online chats, and accessing government records online. Basic
knowledge is required to operate computers even for those who are not
exposed to the usage of computers. The development of such proficiency is
necessary for everyone in the present society.
Computer Science and Information Technology students are expected to go
further to develop software applications by satisfying the increasing
demands of society, schools, colleges, and universities. As a direct
consequence, the programming profession is in great demand. It is
predicted that the students will need to have an in-depth understanding of
the fundamentals of programming to have a successful professional career
in IT industry. Python is an open-source programming language that
emerges as a popular choice among students for its ease of learning and
simplicity. The knowledge of Python along with problem-solving skills are
rated as the greatest requirements of any modern IT Industry.
About the Book
Python Programming is designed to be a textbook for undergraduate and
postgraduate students to provide programming knowledge and enhance
problem-solving skills. The book begins with the basics of Python
programming followed by the basics of algorithms and further focuses on
topics like introduction to programming language, looping, functions, and
data structures like lists, tuples, sets, dictionary, and strings. It covers
functional programming concepts such as higher-order functions. It also
covers Object Oriented Programming concepts such as inheritance and
polymorphism. The book also deals with modules and packages, errors and
exceptions, Python testing, profiling, debugging, and file handling. The
book then focuses on GUI programming. An introduction to data analytics,
databases, network programming, and security is provided in this book.
The book is also helpful for diploma courses, school curricula, competitive
examinations like GATE, coding interviews, and coding competitions. This
book can also be used for imparting training and certificate courses to
professionals. This book is also ideal for self-study who wishes to make a
career in the IT field.
Whether a beginner or an experienced coder, this book will help readers
navigate through Python and enable them to maximize its power, versatility,
and general-purpose nature as a programming language.
Organisation of the Book
This book, is divided into twenty two chapters. A brief description of all the
chapters is mentioned below:
Chapter 1 titled “Introduction to Programming” introduces the basic
concepts of programming. The reader is taken on a journey through the
fundamentals of Python as well as the terminologies that are associated with
Python.
Chapter 2 titled “Problem Solving and Algorithms” introduces the basics of
computational skills that are necessary for solving problems. This chapter
places a strong emphasis on the importance of algorithms. This chapter
deals with algorithm writing, flowcharts, algorithm analysis, and asymptotic
notations.
Chapter 3 titled “Introduction to Python,” sheds light on the basics of
programming in Python, data types, and how to manipulate them with
different operators.
Chapter 4 titled “Decision Structures” provides insights into how to take
decisions. if statements, if-else statements, if-elif statements, and nested-if
statements are dealt in this chapter.
Chapter 5 titled “Looping Statements” deals with elaboration on how the
computer can assist repetition using programming constructs like while, for,
and nested loops. In this chapter, statements like break, continue, and pass
statements are discussed.
Chapter 6 is on functions. The chapter illustrates how a function should be
constructed.
Chapter 7 illustrates the list data structure. The chapter focuses on list
creation, list processing, list functions, and methods.
Chapter 8 illustrates the tuple data structure. This chapter focuses on tuple
creation, tuple processing, tuple function, and methods.
Chapter 9 titled, “Sets and Dictionary” provides the reader with an
introduction to the unordered collection of items as well as their respective
implementations. This chapter introduces sets, set operations, and set
methods. This chapter also focuses on dictionary operations.
Chapter 10 is about strings. This chapter focuses on the storage and
manipulation of strings. The chapter also focuses on string functions and
methods.
Chapter 11 titled, “Advanced functions”, introduces the functional
programming paradigm. It takes the basics of chapter 6 and illustrates the
advanced function concepts like higher-order functions like map, filter, and
reduce. This chapter also introduces recursion and function decorators.
Chapter 12 titled, “Object Oriented Programming”, explains the
fundamentals of object-oriented programming (OOP), including class,
objects, attributes, and methods.
Chapter 13 titled, “Inheritance” focuses on the intricacies of working with
inheritance in Python. This chapter deals with the fundamentals of
inheritance, different forms of inheritance, and the significance of UML in
object-oriented programming.
Chapter 14 titled, “Advanced class” introduces concepts like overloading,
overriding, static methods, and class methods, as well as abstract base class
(ABC) and metaclass.
Chapter 15 titled, “Modules in python” focus on modules and packages.
The construction of large Python programs using modules and packages is
explained in this chapter. Also, this chapter focuses on built-in packages
like sys, os, sympy and py_compile.
Chapter 16 titled, “Errors and Exceptions” explains errors and error
handling using try-except, try-except-else statements. Multiple exceptions
and user-defined exceptions are discussed in this chapter.
Chapter 17 titled “Debugging and Testing” focuses on Python tools like Pdb
which is helpful in debugging programs. The chapter also deals with Python
testing tools like docTest, Unittest, and Pytest. The chapter ends with
profiler tools like cProfile.
Chapter 18 is about File handling. This chapter provides a general overview
of files and file handling. This chapter also assists in recognizing the
differences between the processing of text, Excel, and binary files using
Pickle.
Chapter 19 titled “Introduction to GUI”, covers not only the graphic user
interface (GUI), but also the process of designing a GUI in Python, in
addition to a wide range of graphical user interface components known as
widgets.
Chapter 20 titled, “Introduction to data analytics with python”, introduces
data analytics. This chapter provides an overview of the fundamentals of
data science. Python tools like NumPy, Pandas, Matplotlib, and Scikit-
Learn are explained in this chapter.
Chapter 21 titled, “Database and Parallel programming”, focuses on the
concepts behind the database. CRUD operations of SQLite are discussed in
this chapter. The chapter provides a working knowledge of asynchronous
programming, threads, their interactions, and multiprocessing functionality.
Chapter 22 titled, “Networks and Security” introduces networks and
security. It also provides an overview of networking and socket, the process
of web scraping, and finally, the cryptographic algorithms are presented.
Acknowledgment
A book of this nature could not have been possible without the help of
friends, colleagues, and well-wishers. We thank all the students who have
motivated us to write a book on Python Programming. We express our
gratitude to all our colleagues at the Department of Information Science and
Technology, Anna University, National Institute of Technology,
Thiruchirapalli, Vellore Institute of Technology, Vellore, SRM University,
MGR University for motivating us and providing constructive suggestions
for the betterment of this book. S. Sridhar acknowledges the support
provided by N. Vasanthy, S. Shobika, and S. Shreevarshika for providing
constant support and encouragement. J. Indumathi acknowledges and
accolades A. Indrani, V. Jayaraman, P. Jeyakumar, and J. Gitanjali for being
her pillar of strength. V. M. Hariharan acknowledges the support provided
by S. Velmurugan and P. Nagalakshmi for providing constant support and
encouragement. The authors also acknowledge the support provided by
Neha Goomer, Purushothaman and reviewers of the book. The authors
express their gratitude to Pearson Education for its continuous
encouragement and support.
S. Sridhar
J. Indumathi
V. M. Hariharan
Pearson would like to credit the reviewers (mentioned below) for their
timely and valuable feedback that helped in enhancing the content:
Achintya Singhal
Associate Professor
Department of Computer Science
Institute of Science
Banaras Hindu University Barkachha
Mirzapur, Uttar Pradesh
Geetanjali V Kale
Professor and Head
Department of Computer Engineering
SCTR’s Pune Institute of Computer Technology
Pune, Maharashtra
Mousoomi Bora
Assistant Professor
Department of Computer Science and Engineering
The Assam Kaziranga University
Jorhat, Assam
R. Devi Priya
Professor
Department of Computer Science and Engineering
KPR Institute of Engineering and Technology
Coimbatore, Tamil Nadu
K. P. Supreethi
Professor
Department of Computer Science and Engineering
JNTUH University College of Engineering
Hyderabad, Telangana
S. Shankar
Professor and Head
Department of Computer Science and Engineering
Hindusthan College of Engineering & Technology
Coimbatore, Tamil Nadu
OceanofPDF.com
ABOUT THE AUTHORS
S. Sridhar is presently Professor and Head of the Information Science and
Technology College of Engineering, Guindy Campus, Anna University,
Chennai. He possesses a doctorate in Medical Image processing from Anna
University.
He has around 30 years of academic experience in active teaching and
research. He has served Anna University, the National Institute of
Technology (NIT), Tiruchirappalli, and for around two years at SRM
University.
He has many international and national papers to his credit. He has
conducted seminars and workshops on Image Processing and Matlab. He
served as a resource person for many Faculty Development Programs.
He has delivered video lectures on algorithms in EDUSAT television
Programmes of Anna University and E-Pathasala. He has also worked on
student projects in the Council of Scientific and Industrial Research (CSIR),
Chennai, Indian Space Research Organization (ISRO), Bengaluru.
His expertise includes Machine Learning, Programming, Digital Image
Processing, Algorithms, Deep Learning, and Big Data.
J. Indumathi, a polymath, has taught and researched for over 33 years. At
present, she is serving as a Professor in the Department of Information
Science and Technology at Anna University, Guindy campus, Chennai.
ISMS (Information Security Management System) Lead Auditor, Project
Management Professional (PMP), Privacy Lead Assessor, Information
Privacy Manager, Information Privacy Professional/Europe, Network
Defense Architect (CNDA), and Test Manager, are a few of her value-added
credentials.
Apart from routine academic work, she has conducted many continuing
technical edification programmes. She has given technical invited talks,
plenary talks, keynotes, and chaired conference sessions worldwide; besides
the talks on television/ radio like EduSat Anna University, Doordarshan,
and All India Radio. She has undertaken and accomplished efficaciously,
several project consultancy services and her pooled rich experience has set
up an imperturbable empire; providing Consultancy services to various
categories of organizations such as Government, Industry, Academic, Public
Welfare, Police, and Judiciary. Owing to her noteworthy, illustrious,
Consultancy works, she was honored with an Active Consultant Award, in
addition to other 78 awards.
V. M. Hariharan is presently CEO of Vels Infoway, an MSME-based IT
company. He has worked across 34 international IT projects. He also has 4
years of teaching experience as part of TechZ Academy. He is presently
working on a skill development project which provides an e-platform for
students and academicians to learn professional skills. His expertise
includes topics like Data Analytics, Machine Learning, Deep Learning, and
DevOps.
OceanofPDF.com
“The beginning of wisdom is the definition of terms.”
Socrates
CHAPTER 1
Introduction to Programming
This chapter introduces the concept of solving problems using computers.
The basics of Python and associated jargons are introduced to the readers.
The chapter explains how interpreters and compilers facilitate collaboration
between a Computer’s hardware and software in the system’s architecture.
Learning Objectives
After completing this chapter, the reader will be familiar with the following
concepts.
Become familiar with the terminologies used in programming languages
and coding.
Acquire a fundamental understanding of programming and software.
Know how interpreters and compilers facilitate collaboration between a
computer’s hardware and software in the system’s architecture.
Overview of the Python Integrated and Learning Environment.
Ability to use Python as a scripting language.
Provide awareness about Python programming resources.
Overview of Python documentation and Support.
1.1 Basics of Programming Language
Generally, programmers are excellent problem solvers. The problem-
solving skill involves identifying issues, formulating problems, devising
solutions, and communicating them. The steps involved in solving a
problem are as follows: gaining insight into the current problem;
conducting in-depth analysis; designing and developing a workable
solution; drafting and testing the necessary code; and finally, releasing the
solved answer to the world.
A computer can do incredible things only if given the right direction. They
must be given commands on how to perform a task, evaluate a condition to
choose a path, handle data from a device or network, and react to
unexpected events. Consequently, the definition of a computer program is a
comprehensive, step-by-step set of instructions that tells a computer exactly
what it should be doing.
Coding is a subset of computer programming. Coding enables Computers to
understand the human levels of linguistic comprehension. It is the medium
by which humans exchange detailed information with machines. To put it in
simple words, it refers to the language used by humans to convey their
ideas and intentions to computers as instructions to carry out a specific task.
These instructions (or commands) guide the computer on how to define and
complete a task (on what to do) in a way (how to do it) that it can
understand.
Coding Mechanism-Coding guarantees that a computer’s software and
hardware can connect by compiling the code into assembly language. High-
level and assembly-level languages are then converted into binary coded
signals (1’s and 0’s) to allow computational hardware and software to
communicate.
Learning to code is crucial because it enhances problem-solving and logic
abilities, leverages technology to power business operations, and facilitates
fine-tuning. Learning to code leads to better job prospects. Programmers
make apps, websites, and other digital products that change how people live
worldwide.
1.2 Programming Versus Software
“Programming” involves giving the computer the instructions and data it
needs on time. Programming is what makes it possible for computers to
carry out specific tasks. Programming languages like Python, C++, and
Java help innovations in technology.
Programming Mechanism-The process of programming is intricate.
Programming is done in phases, such as composing problem statements,
drawing flowcharts, designing coding algorithms, writing a computer
program, analyzing and evaluating software, technical writing, keeping
software up to date, and so on.
Programming helps to analyze real-life situations, make decisions, finish
tasks, repeat selected tasks, and halt or stop them on demand. Practical
problem-solving abilities are required for handling situations as they
emerge. For instance, a person wants to eat bread toast. To solve the
problem with a program, the individual must collect all the necessary
inputs, including the essential resources, like a plate, an instant toaster, a
teaspoon, toppings, and add-ons. These gathered inputs will be subjected to
conditions to produce the expected and the desired output (by setting the
knob at a higher or lower temperature). The constraints are necessary for
the input-to-output transformation to take place. A deduction is reached
once all steps in the procedure have been carried out in order. Once the
output is generated, it can be examined for accuracy. Conversely, this is all
needed to identify and implement a solution. The program’s benefits
include dependability, low cost, ease of maintenance, and high return on
investment.
The process of creating software is called programming. Computer
software is a set of program instructions. Computer software includes data
and documentation to be executed by the computer. Professionals with
specialised training and experience are the ones who typically create
software. A software’s essential characteristics are security, safety,
dependability, correctness, etc. Writing a code is a shorter time commitment
than developing software.
Program vs. Script
A program is executed. A program is a bunch of instructions directed by a
computer to perform a specific task.
The script is interpreted. A script is a code snippet or a small program. A
code written in a scripting language is called “script.” It is a type of
programming language, and the code is used to control another software
application.
To solve potential problems, Computer Scientists develop elaborate,
granular plans, called algorithm. An algorithm is a set of instructions
explaining how a particular computation should be performed in detail. A
programming language is a language for writing down the instructions
that a computer will follow. A programming language has a structure of
syntax (precise form) and semantics (actual meaning). There are two types
of languages, namely low-level languages and high-level languages.
Computer hardwares can only run programs written in low-level
languages/machine languages/assembly languages. Since the high-level
language has to be processed before it can run, they take extra processing
time, which is a disadvantage of high-level languages.
Fig. 1.1: Pyramid Showing the Level of the Translation of Languages
A translator (a unique computer program) translates the written programs
into a form (machine language/binary) that the computer can understand. In
Fig.1.1, the pyramid shows the different levels of language translation. A
computer program is a series of instructions written in a high-level
language (which looks much like a human language) to solve a problem.
While machine language uses only binary and hexadecimal digits, assembly
languages are written with human readability in mind. The computer
hardware directly executes the machine language instructions.
1.3 Python Programming Language
It is a high-level, general-purpose programming language for solving
problems on modern computer systems. Python programs can be run on any
operating system. The Python language and many supporting tools are
available as open source. The centre of the Python universe is at
www.Python.org, where one can download Python, its documentation, and
related materials. Python is an excellent, robust, and versatile Object-
Oriented Programming Language that has been created to facilitate the
creation of programs in various fields.
Facts
Dutch computer scientist and mathematician Guido Van Rossum is credited
with gifting the world with Python. It was a project he was starting to play
with over Christmas 1989 at the National Research Institute for
Mathematics and Computer Science. Inspired by “Monty Python’s Flying
Circus,” he named it Python. After first being released to the public around
1991, the language quickly rose to prominence and is now widely regarded
as one of the most advanced programming languages. Python comes from
ABC, Modula-3, C, C++, Algol-68, SmallTalk, Unix shell, and other
scripting languages. ABC, the predecessor of Python, handled exceptions
and interacted with the Amoeba Operating System. The programming
languages ABC language and Modula-3 have influenced Python. Python
source code is available under the GNU General Public License despite
being copyrighted (GPL). The Institute’s core development team maintains
Python.
Characteristics of Python Programming Language – Python supports
multiple programming patterns, including object-oriented programming
(classes and multiple inheritances), fundamental data types (floating point,
complex, and unlimited-length long integers), raising and catching
exceptions for easier error handling, self-referential objects, and
metaprogramming.
1. Python is an open-source, high-level, interpreted, general-purpose,
dynamic programming language with an emphasis on the readability of
source code. Using indentation makes it easy and excellent for beginners
as it enforces a certain amount of readability. The language includes
resources for contemporary applications, including media computing and
networks.
2. Python is relatively easy to pick up and use. Python language is simple,
powerful, and perfect for quick iterations and ad hoc coding without
compromising on maintainability. Python’s clean and concise syntax
makes writing a program code a pleasure. Both statements and
expressions use algebraic notation and look like pseudo-code algorithms.
Fewer hours are needed to master the syntax of the programming
language. Python has the benefits of being short and flexible. The design
enables one to start writing valuable programs quickly.
3. Extensive libraries are available for Python. Python is backed by a large
community of developers and has access to numerous resources. Pre-
installed libraries that are both comprehensive and easy to use make it
possible to perform a wide range of everyday tasks. The Python Package
Index is a free resource that lists all available Python libraries, both those
that come with the language and those that can be downloaded
separately (PyPI).
4. Python’s flexibility as a programming language means it can also be
used as an add-on to make highly customizable programs. It is widely
used in science, has well-designed libraries for complex scientific
computing, and makes manipulating strings of characters easy. It can
also be easily integrated with existing tools (for example, strings of DNA
bases and protein amino acid residues, which biologists are particularly
fond of).
5. Cross-platform language- Python is a platform-independent
programming language(portable), meaning it can be used on various
computers. This means that Python’s applicability is universal. Python’s
interpreter can easily incorporate C and C++ functions and data types (or
other languages callable from C). Python’s installation path and different
configuration settings can be altered to make it run on any system. The
platform’s focus on portability automatically handles the nuances of
various operating systems and platform-specific interfaces, saving
developers time.
6. Software quality. Has readability, coherence, and quality, as its intrinsic
multi-paradigm nature (either uses it as a scripting language or object-
oriented, imperative, and functional programming styles). Python
contains advanced programming features such as generators and list
comprehensions. It is versatile.
7. Python data types are strongly and dynamically typed. There is no need
to use data types to declare variables because Python is dynamically
typed, so the user can write a=10 to express an integer value in a
variable. Mixing incompatible types (e.g., attempting to add a string and
a number) causes an exception to be raised, so errors are identified in a
briefer time.
Facts on Programming
The term “programming paradigm” refers to a specific way of
approaching the solution of a problem in any given programming
language. The term “paradigm” refers to a pattern or model, whereas
“paradigm in programming” refers to a model that solves a problem.
The paradigm is the guiding principle that all computer science
programming languages should adhere to when attempting to solve a
problem. There are three essential programming paradigms in this
computer era: Object Oriented Programming (OOP), Functional
Programming, and Procedural Programming. In specific applications,
other application-specific paradigms are preferred.
Object Oriented Programming (OOP) is based on the objects which
contain the data as variables and perform activities using the associated
methods. Functional Programming involves creating programs by
constructing and applying functions in which functions are the trees of
expressions that map the values to other values. Procedural
Programming involves programs that consist of data and procedures that
operate on the data. Critical differences between OOP, Functional
Programming, and Procedural Programming are listed in Table 1.1.
Table 1.1: Object Oriented Programming Vs. Procedural Programming
Vs. Functional Programming
Python is more of an Object-Oriented language-. Python’s
interpretive nature, dynamic typing, and pragmatic approach to Object-
Oriented programming make it a universal language, well-suited for
scripting and rapid application development across various domains and
platforms.
8. Python is embedded easily into any application to provide a
programmable interface. With Python, it’s simple to add a scripting
language to any program. Python programmers frequently use scripting
to automate daily tasks, reporting, server management, security, social
media management, business growth and development, financial trading,
automating software, and many other intelligent solutions.
9. Developer productivity. Because of the smaller size of these programs, it
runs faster. As a result, the market response is faster, with less code to
write, less code to read, and less code to maintain, debug, and refactor.
Python runs deprived of the need for extensive and time-consuming
compilation and linkage steps.
10. Python’s rapid edit-test-debug cycle and lack of a compilation step make
development and debugging much more efficient. Eventually, it’s
attractive enough to do whatever is essential. Some instances where this
would be beneficial are data analysis, website creation, website scraping,
graphical user interface development, system administration, game
development, etc.,
Limitations of Python
Speed is an issue. Since Python is an interpreted language, it is several
times slower than compiled languages.
Design restrictions. The dynamically typed language comes with errors
only at runtime; Python’s global interpreter lock and whitespace are a
few design issues.
What is Scripting?
Scripting is a complex and vital programming discipline, but its lexicon is
alienating. Scripting is used to do repeated activities on computers.
Scripting is writing code that performs a real-world action without
compiling. Scripting may be done on many systems via the command line
and various computer languages. Scripting may be done in a wide range of
contexts across a wide range of settings, including servers, operating
systems, applications, games, websites, and software, as well as other
languages and the command line.
Implementations Using Python
Python is utilized for database access, Internet scripting, distributed
programming, and extension-language works.
Python is used as Graphical User Interfaces (GUI), shell tools -system
admin tools, and command line programs.
Python has language-based modules -instead of special-purpose parsers.
Python is helpful for rapid prototyping and development. Python is used
in system programming, web programming, gaming and robotics, rapid
prototyping, system integration, data science, database applications,
games, and more.
Python is also used in Web Application development, building desktop
GUIs, Email Parsing, Network programs, Desktop applications, Internet
Protocol, System administration, and Video Games Development.
Many companies use Python. Naming a few companies that are using
Python today are: Google, One Tube, Dropbox, Yahoo, Industrial Light
& Magic, Walt Disney Feature Animation, Pixar, NASA, NSA, Red Hat,
Nokia, IBM, Netflix, Yelp, Intel, Cisco, HP, Qualcomm, and JPMorgan
Chase, etc.,
Interesting Facts
Python is the language of the Python Interpreter and those who can
converse with it.
An individual who can speak Python is known as a Pythonista.
Nearly all known Pythonistas use software initially developed by Guido
van Rossum.
1.4 Compiler Versus Interpreter
A computer program is a series of instructions written in a high-level
language (which looks much like a human language) to solve a problem.
Fig. 1.2 depicts the process involved in creating a computer program.
Fig. 1.2: The Process of Creating a Computer Program
A Compiler (Fig 1.3) meticulously scans and converts the source code
before launching the program. In this perspective, the high-level program is
called the source code, and the translated program is called the object code
or the executable. Compiling a program allows users to run it repeatedly
without retranslating it each time. A hardware executor can then run the
compiled object code.
Fig. 1.3: Structure of a Compiler
A program is written in an editor to instruct a computer how to perform a
task. Python interpreter (Refer Fig. 1.4) reads the expression or statement
(source code) and confirms that it is well formed. In this step, the
interpreter checks the syntax and semantics of the statement. If any error is
encountered, it stops translation with an error message (Refer Fig. 1.5).
Fig. 1.4: High-Level Block diagram of an Interpreter
If there is no error in the code, the interpreter translates it to an equivalent
form in a low-level language called byte code, which gets executed on the
python virtual machine, and the output is displayed on the computer. (see
Fig 1.5). The comparisons between a compile and Interpreter is given in
Table 1.2.
Fig. 1.5: Python Interpreter
Table 1.2: Comparison of Compiler and Interpreter
1.5 Integrated Development Environment (IDE)
IDEs (Integrated Development Environments) offer niceties like an
integrated compiler, tags auto-completion, and facilitating testing and
debugging as the application mature to speed up and simplify programming.
The IDE has a deeper comprehension of the code than a text editor. Python
IDLE stands out among the many IDEs available because of its ease of use,
making it an excellent choice for those just starting in the programming
world.
A software IDLE lets one converse with the computer outside the main
program. IDLE stands for Integrated Development and Learning
Environment (IDLE), and is the Python equivalent of IDE. It is an
interpreter, that translates the code into a language the computer can
understand. Python’s default implementation, IDLE (“Integrated
Development and Learning Environment”), includes the complete set of
IDE packages and fundamentals. IDLE is commonly used because it is
bundled with Python and is simple.
Upon installation, Python IDLE can either serve as a shell (interactive
interpreter) or an editor. Though Python uses two processes, it is usually
considered an interpreted language because of how programmers interact.
Python’s paths, directories, file-handling commands, and how to download
and install Python can be found in Appendix 1.
Python interpreters read and execute Python programming statements. The
Python interpreter can run in any of the two modes: interactive mode and
script mode.
1.5.1 Python in interactive mode (windows OS)
The computer awaits a keypress in interactive mode before executing the
user’s command. To run Python in interactive mode (windows OS).-Start
Python in Windows, click on
Shell or Interactive shell or Interactive Interpreter- The Python IDLE
instantly launches a Python shell, saving time. It is a Read-Eval-Print Loop
(REPL). It reads every statement, evaluates, and displays a Python
statement. Code snippets can be run in Python’s Interpreter shell.
Fig. 1.6 shows the working of the Interactive mode. As stated earlier, the
Python Interpreter, a binary collection of instructions in memory, performs
a transformation. The Python Virtual Machine (PVM) and a compiler are
involved in the process. The compiler first converts platform-specific
source code into byte code for the PVM to understand the sample program.
The processor reads the byte code and runs it on the hardware, but PVM
does it. This is how the Python interpreter works.
Python’s Interactive mode (also called Interpreter mode) allows users to run
commands without first writing a python file (without.py). This is used for
testing swiftly even minor designs and executing small and modest codes.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.6: Python Interpreter: Interactive mode
Python Shell is an interactive interpreter. The Python shell allows the
programmer to execute a single Python command where the result of the
command is displayed immediately. The program checks the code and
provides feedback whenever the user presses the enter button. The first
command prompt looks like this (>>>), and when it’s seen, it prompts the
user to type something while waiting for the next entry on the line. The
secondary prompt “…” indicates waiting for additional user input.
The following code snippets show how to use the Python interpreter in the
command line (interactive mode).
Example 1: Illustration of calculation operations in Python command
prompts
Using the Interpreter mode, even the most detailed programs (addition,
subtraction, multiplication, and division) can be written directly and run
immediately. Within this setting, the command prompt >>> can be used to
type in programs’ instructions.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.7: Interactive mode screenshot demonstrating the calculation
operations
Fig 1.7 shows the illustration of calculation operations. (i) Two plus five is
typed adjacent to the command prompt to get the summed-up value of 7 on
the subsequent line. (ii) Two multiplied by five is typed adjacent to the
command prompt to get the total value of 10 on the subsequent line. (iii)
Two divided by five is typed adjacent to the command prompt to get the
total value of 0.4 on the subsequent line.
Example 2: Illustration of the “print()” function at the Python
command prompt
Type the above welcome note (“Welcome to the world of Python”) and
press the enter key. Upon hitting the return key, the screen shown in Fig. 1.8
will pop up.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.8: Python Interpreter- command prompts in Interactive mode
screenshot
The greetings message enclosed within the double quotes (“Welcome to the
world of Python”) is displayed on the screen without quotes.
Example 3: Illustration of the “print()” function at the Python
command prompt
The output screen is shown below in figure 1.9:
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.9: Python Interpreter–command prompts in Interactive mode
screenshot
The greetings message enclosed within the double quotes (“This exercise is
to show the working of a command prompt”) is displayed on the screen
without quotes.
Example 4: Illustration of help() in Python command prompt
Fig. 1.10 displays the output screen generated by the help() command.
The output screen is as shown below:
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.10: Python Interpreter: Python Interactive mode screenshot for
help()
Editor windows can be used simultaneously. Linux and Windows each have
their main menus. Each menu specifies the window type it applies to. Editor
output windows are used by commands like Edit > Find in Files. They share
the top menu but have different default titles and context menus. On
macOS, there’s one app menu. It automatically adjusts to the active
window. Some items in the IDLE menu have been moved to comply with
Apple’s standards.
1.5.2 Python in script mode
A Python script or program is read line by line and executed when run in
script mode. In script mode, code can be written in a standalone file (ending
in.py) and run independently. These lines of code are easily modifiable and
reusable. The ability to reuse scripts without having to retype them makes
modular programming a better fit. Modularization allows for easier
maintenance and updates of an extensive program. Its purpose is to
facilitate rapid error detection and correction. Script Mode is best for
writing and saving programs for later use. Script mode is activated when
using the Edit window.
Instructions on how to proceed while in “Script” mode- To create a new
file, select it from the File menu or press Ctrl + N (see Fig. 1.11).
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.11: Python shell -File Drop Down Menu
A brand-new editing window will pop up. Then, a script text editor with a
single blank line appears. The Python editor allows us to write and save
Python Scripts and execute them using a terminal (in Linux) or command
prompt (in windows).
Example 5: Illustration of running of a Python Script
In the text editor window, type the following:
Now, the typed Python editor is shown below (Fig.1.12):
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.12: Screenshot of the Python editor
Inputting Python Script into a Save File. The file must be saved after
entering the code. The file is named sample.py and is saved (Fig. 1.13).
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.13: Screenshot of saving the file sample.py
To create a save box similar to the one shown, go to
Since Python automatically appends.py to saved files, it’s essential to
specify a different destination folder and name the Python code files
without the extension (if need be).
Running the program -Use an interpreter or press the F5 button (Fig.
1.14). For a precise result, the saved code can be run and displayed. Click
the toolbar’s “run” icon to get started.
Launch the command, or hit the F5 key, to initiate execution. This will
cause the results to be displayed in the console. In this case, print(“Dear
Learner, Welcome to the world of Python”) will return “Dear Learner,
Welcome to the world of Python” as output, without quotes. If a piece of
code fails to run because of an error, checking it once to find and fix it will
ensure that it runs without further problems.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.14: Screenshot showing the run module or F5 button
The following screenshot (Refer Fig. 1.15) was obtained by executing the
Python program (sample.py) in the IDLE shell environment.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 1.15: Screenshot showing the output display of the
program(sample.py)
When the Python program (sample.py) is executed in Windows PowerShell,
the screen capture shown below in Fig. 1.16 is produced.
Credit: Screenshot of Microsoft Windows PowerShell, © Microsoft 2022
Fig. 1.16: Screenshot showing the output of the program in the
Windows PowerShell
IDLE also functions as a debugger; the button is at the top of the Shell
window (see Fig. 1.16). The debugger program aids in detecting bugs.
IDLE changes the colours of the entered text, and each colour has a specific
coding. To list some of IDLE’s most common syntax colours and their
meanings.
Common Python syntax colours: Colour coding in IDLE aids in
understanding the meaning so that one can easily visualize the mistakes
made.
Keywords in orange colour
Strings in green colour
Comments in red colour
Definitions in blue colour
Misc. Words in black colour
For example, the absence of the green colour indicates that the quotation
marks were forgotten when entering the string. The colours can also be
customized according to the user’s personal preferences.
1.5.3 Command Line Interpreter
As a generic term, a command line interface can refer to any command line
interpreter application (Refer Fig.1.17). It is also referred to as a Command
Line Interpreter (CLI), Console User Interface (CUI), command processor,
shell, command line shell, and command interpreter. A command line
interpreter, also known as a console or shell, takes user input (typically in
the form of a single command) and applies it to the system. In the absence
of errors, it executes the command and returns the expected data; otherwise,
an error message is displayed.
Credit: Screenshot of Microsoft Windows PowerShell, © Microsoft 2022
Fig. 1.17: Python command prompt
CLI is just a command handler. Compared to software that relies on the
mouse to interact with buttons and menus, command line interpreters take
text input from the keyboard and change it into commands the operating
system can comprehend and execute. When the Python program
(sample.py) is implemented in the command prompt, the output screen
displays the contents in the print function.
1.6 Various Resources for Python Programming
Python developers can select from several helpful and informative guides.
Only a few are listed below.
Python code editors are softwares that allow users to write and change code
using the Python programming language. Though one has the option of
other editors, better choose a specific or simple text editor.
Sublime Text 3: the popular sublime text editor supports even Python. An
extensive online community backs it up and is lightning-fast and adaptable.
Some basic Python functionality is pre-configured upon installation. Many
add-ons are available, such as debugging, auto-completion, and code
linting. Flask and Django are also accessible for scientific development. By
simply changing Sublime Text, one can transform it into a fully functional
Python working environment. Sublime Text’s is a open free trial version.
Occasionally, a pop-up would state, “one needs a license to continue using”
but Sublime Text 3 is open to everyone, from novices to experts.
Notepad++: Using its syntax highlighting features, the free and open-
source text editor Notepad++ can identify the syntax for various
programming languages. These languages include Python as well as many
more in between. Programmers prefer using Notepad++ because of its many
useful features, including the highlighting of syntax errors. It might make
the page or window simpler to read and comprehend if combined and
compressed the code pieces. When creating Python code, the indentation
hints provided by Notepad++ come in helpful since the Python
programming language employs different degrees of indentation rather than
braces to indicate functional code areas.
Atom: Both newcomers and seasoned players will be benefitted; in other
words, Atom is completely free. It has a lot in common with Sublime Text.
The Atom platform is extremely configurable. One can make changes to the
program to meet specific requirement. Python debugging is available in
Atom, together with autocomplete-python and linter-flake8.
Visual Studio Code: this is for professionals and free of cost. Visual Studio
Code, Microsoft’s free and open-source Python development environment,
may be used to program in Python (VS Code). VS Code’s extensions allow
one to customise one r Python programming environment. Just a few of its
capabilities include code completion and linting for possible errors,
debugging, and unit testing. VS Code, despite its tiny size, has great
potential. As a result, the Python community is embracing it.
Vim: Vim is now at a level of 8. It is for Professionals and there is no cost.
Vim is a text editor pre-installed on UNIX and Mac OS X platforms. Some
people enjoys Vim’s keyboard shortcuts, and some despise them.
Programmers acquainted with Vim may find it a helpful addition to their
toolbox for Python development. To get the most out of Vim with Python,
one has to familiarise with the editor’s features. One may add plugins for
syntax highlighting, debugging tools, and refactoring to turn Vim into a
full-fledged Python working environment.
Python IDLE Editor
Spyder: It is possible to utilise Spyder as a beginner and an expert. There is
no charge to use this service. Spyder is a popular open-source IDE in the
scientific community. The easiest method to get Spyder up and running is to
use the Anaconda distribution. Anaconda is a widespread distribution for
data science and machine learning, in case one did not know. Anaconda has
many helpful tools, including NumPy, Pandas, scikit-learn, matplotlib, and
more. Spyder has an iPython shell, autocompletion, and debugging.
Thonny: Thonny is an IDE for Python 3 that is pre-installed. Once Python
is installed, one may begin writing code in it. People who are new to the
game will like Thonny. For the sake of simplicity, the user interface has
been kept uncomplicated. When one has some expertise with Python coding
but does not necessarily need a full-featured IDE, Thonny is an excellent
choice. In addition to syntax error highlighting and as a debugger, this tool
also provides code completion and step-by-step expression evaluations. The
view should be the first stop for newcomers, followed by variables. In the
variables view, one can see the list of variables in the application and their
values.
PyCharm: It is a programming language and the environment called
PyCharm. An excellent IDE for programmers, PyCharm is available for free
download. JetBrains is the developer of one of the most popular software
development tools. PyCharm comes in two types: the community edition of
the software is free and open source, and it is a good fit for Python and
scientific study. The Professional edition, which may be purchased, includes
web development assistance. In addition to code completion and code
inspections, PyCharm also provides error detection and remediation. In
other words, everything on this list is included. PyCharm’s resource use is
one of the most often voiced grievances. If computer has a low amount of
RAM, it may experience a delay (typically less than 4 GB).
Getting Help in Python
One can always get help in Python as the interpreter. For example, if one
wants to know how an object works, all one has to do is call for
help(function)!
The most up-to-date and current source code, binaries, documentation,
news, etc. are available on the official website of Python.
Python Official Website: https://www.Python.org/
To download the latest version of Python, one can visit
https://www.python.org/downloads/.
Python’s documentation, tutorials, and guides can be browsed or
downloaded from the following site.
Python Documentation Website: https://www.Python.org/doc/
Summary
This chapter introduces computational problem solving, the verbiage of
Computers, fundamental computer concepts, Programming language,
Coding, etc.
1. Problem-Solving is “the ability to formulate problems, think creatively
about solutions, and express a solution clearly and accurately.”
2. The stages of Problem Solving are - Understanding the problem,
analyzing the situation, developing the solution, writing the code, and
deploying.
3. A computer program is a detailed, step-by-step set of instructions
directing a computer exactly what to do.
4. Coding is used to communicate the steps to do something to the
computer, using the language it comprehends.
5. Computer scientists list steps to solve problems in real-time. The
sequence of steps that describe each of the computational processes is
called an algorithm.
6. A programming language is similar to a code for writing down the
instructions a computer will follow.
7. There are two types of languages, namely low-level languages and
high-level languages. Computers can only run programs written in low-
level languages/machine languages/assembly languages.
8. Software (programs) rules the hardware (the physical machine). The
process of creating software is called programming.
9. Computer software is a set of program instructions. The program is
executed. A program consists of instructions a computer can follow to
carry out a specific task.
10. Script is a small snippet of code. Scripting is interpreted. A code written
in a scripting language is called “script.” It is a type of programming
language, and the code is used to control another software application.
11. A translator (a particular computer program) translates the written
programs into a form (machine language/binary) that can be
understood by the computer.
12. Program Creation - A computer program is a series of instructions
written in a high-level language (which looks much like a human
language) to solve a problem.
13. Implementation & execution of the machine language instructions -
The computer directly executes the machine language instructions.
14. Interpreters and Compilers are programs that process high-level
languages into low-level languages.
15. A Compiler reads the program and translates it thoroughly before it
starts running. In this perspective, the high-level program is called the
source code, and the translated program is called the object code or the
executable.
16. Script Mode is best for writing and saving programs for later use. Work
in the edit window is running in Script mode.
17. The interactive mode is for testing and trying small ideas quickly. Once
in the Shell window, one runs in an interactive mode used for testing and
swiftly trying minor designs.
18. A command line interface can refer to any command line interpreter
application. It is also referred to as a Command Line Interpreter(CLI),
Console User Interface (CUI) command processor, shell, command line
shell, and command interpreter.
19. Python code editors are windows that allow users to write and change
code using the Python programming language.
Glossary
Algorithm: Algorithms are step-by-step instructions for solving a
problem.
Byte Code: A low-level version of Python code is called bytecode.
Python’s bytecode is not binary and cannot be executed directly on the
target machine.
Command Line interpreter (CLI): CLI is a text-based user interface
for executing programs, managing files, and connecting to the computer
(UI). Console and character user-interface interfaces are similar.
Compilers: A compiler converts high-level code to assembly, object,
and machine code. (1s and 0s) Before running, the code is changed.
Computer Code: Instructions or rules in a programming language are
known as computer code.
Computer Program: A set of instructions written in a programming
language and executed or interpreted by a computer.
Cross-platform Language: Cross-platform languages are used to create
software compatible with multiple systems and for cross-platform digital
activities.
High-level languages: High-level languages are user-friendly. High-
level languages are easy to grasp and learn for any human being.
Integrated Development Environments (IDEs): An IDE is a collection
of tools for creating and testing computer programs.
Interactive mode: The interactive model of a command line shell
executes previously entered commands in active memory while
providing immediate feedback for each utterance.
Interpreted Language: An interpreted language does not require the
program to be translated into machine language before being executed
by an implementation.
Interpreters: An interpreter is used to translate each line of code while
the program runs.
Low-level languages: Low-level languages are unusable by humans
because they rely on machines. Therefore, they’re hard to understand
and learn by a human being.
Object-Oriented language: Object-oriented language (OOL) is a high-
level programming language used by programmers to construct software.
Problem Solving: Problem-solving involves determining the cause of a
problem, brainstorming solutions, and implementing them.
Programming: Programming is the process of putting together a set of
instructions to teach a computer how to carry out a particular activity.
Python Package Index (PyPI): Python programmers use PyPI to store
and share code. Programmers use this method to share and sell software.
PyPI simplifies Python packaging.
Python virtual machine (PVM): PyVm is a free programming
environment for the Python programming language. Bytes are translated
into machine instructions that the computer can execute and display.
Conceptual Questions
1. List the steps involved in solving a problem.
2. Define a computer program.
3. What is coding? Explain the coding mechanism.
4. Outline the notion of “Programming” and explain the Programming
Mechanism
5. Compare and contrast programming and scripting.
6. List out the design paradigms in a programming language.
7. With the pyramid illustration, show and explain the various levels of the
translation of languages.
8. List the characteristics of the Python Programming Language.
9. What is scripting?
10. List the shortcomings of using Python.
11. What are compilers, and how do you explain them? Give an example.
12. What is an interpreter? Provide an example.
13. What features of an interpreter does a compiler also have? What makes
them unique from one another?
14. What are the critical differences between compiled code and interpreted
code?
15. What tool does a computer programmer use to create Python source
code?
16. What are the prerequisites for putting a Python program into action?
17. Give some examples of how Python is used daily.
18. Enumerate the benefits of working with the Python programming
language.
19. Describe what the command line interpreter is.
20. Write notes concerning the Integrated Development and Learning
Environment (IDLE).
21. List the features of Python IDLE.
22. How to select a code editor for Python that can meet the requirements?
23. If someone needs assistance with Python, where can they go, and how
can they get it?
24. Write the standard Python syntax colours for keywords, strings,
comments, and definitions.
25. Why use the command line when you can use intuitive software and a
Graphical User Interface (GUI)?
26. How can the CMD (Command Prompt) compile and run a Python
program?
27. What is Python virtual machine (PVM)?
Try out the Questions
1. Develop your capacity to tackle complex problems by applying your
knowledge to actual situations. Use programming language features to
foster creative problem-solving skills and encourage logical reasoning.
2. Pick the right programming paradigm to apply to some real-world issues.
3. Why is the Future of Python Language Bright?
[https://www.probytes.net/blog/python-future/]
4. Why Python uses 0-based indexing? [http://python-
history.blogspot.com/]
5. The Only Way To Learn Programming [https://renanmf.com/the-only-
way-to-learn-programming/]
Programming Exercises
1. Investigate how Google employs Python.
Hints:
1. Use for maintenance and delivery.
2. To rewrite Google’s Perl and Bash scripts as it is easy to deploy and
maintain. Google’s first web-crawling spider was rewritten in Python.
3. How. Python is one of Google’s four production-ready server-side
languages, along with C++, Java, and Go.
2. Explore Spotify Python usage and why they chose to use it.
Hints:
1. For data analysis
2. For Back-end inter-service communication -. ZeroMQ, an open-
source networking library and framework, is written in Python
3. For fast development.
4. Spotify’s infrastructure implemented with Gevent -fast event loop and
high-level synchronous API have been used to implement Spotify’s
recent infrastructure changes.
3. Find out how Uber is benefiting from Python.
Hints:
Python powers many user services with estimations and business logic.
Uber’s core platform runs on Python and Node.js, Java, and Go. Python
is used for arrival times, route planning, region research, and supply-
demand balance requires higher-order thinking.
4. Learn how Python is used on the Amazon platform.
Hints:
1. Amazon’s analysis and recommendation system use AI and ML to
find what customers want.
2. Python’s scalability and compatibility with Hadoop made it ideal for
Amazon’s data processing needs.
3. Amazon uses Python for a wide variety of purposes, such as Machine
Learning and automating AWS resources, and one way it does this is
with Jupyter notebooks.
5. Look into how the Dropbox desktop client uses the Python
programming language for cloud-based storage.
OceanofPDF.com
Computers are good at following instructions, but not at reading your mind.
Donald Knuth
CHAPTER 2
Problem-Solving and Algorithms
This chapter is about problem-solving. One of the most important keys to
mastering programming is to learn the art of problem-solving. This chapter
focuses on the essential process of problem-solving and algorithm design.
After completing this chapter, the reader will know the basics of problem-
solving.
Learning Objectives
To know the basics of problem-solving
To understand the Python development cycle
The basics of computational thinking
Learning to write algorithms
Learn to draw flowcharts
Know the basics of algorithm analysis
Know about asymptotic analysis and its notations
2.1 Problem-solving and Computational Skills
One can use Python language to solve problems. What sort of problems it
can solve? Well, the problems that computers can solve are called
algorithmic problems. An algorithmic problem is an instance of an abstract
problem whose solutions can be computed using computers. For example,
sorting is an algorithmic problem, as sorting can be applied to numbers or
strings. Hence, sorting a set of numbers is a problem instance. So, there is a
difference between a problem and a problem instance. A well-defined
problem is a problem that is specified by the set of input and output
specifications that satisfy the problem statement. An ill-defined problem is a
problem where there are no clear specifications.
The problem-solving process begins with the formulation of a problem
statement. It includes all stages from problem definition to the
implementation of the algorithm in the form of a programming code.
Computer problem-solving is not an easy task. It is a highly demanding task
that requires many important skills like problem understanding, planning,
design, implementation, logical reasoning, and creativity. It is a creative
process that is both an art and a science.
Problem-solving is an art because the creativity and experience of the
problem solver play an important role in solving problems. It is also
considered a science because there are some standard patterns or ways of
problem-solving. One methodology of finding a solution can be imitated for
solving other problems. Extraction of such patterns helps to formalise
problem-solving methodologies. For example, searching for a book and a
cell phone have some common patterns even though some differences exist
like a call to the missing cell phone cannot be done for a book. But there is
no guarantee that the given pattern can solve any given problem, but
studying patterns provide a scientific basis for solving a given problem.
George Polya was a Mathematician who wrote a book titled “How to solve
it” in 1945. In that book, he proposed many techniques for problem-solving
that served as benchmarks for many decades. He proposed a four-step
approach to problem-solving as shown in Fig 2.1.
Fig. 2.1: Stages of Problem Solving
The stages are: 1. problem understanding; 2. devising a plan; 3. execution
of a plan; and 4. looking back and reviewing. Formulation of a problem
statement, requires a thorough understanding of the problem. One of the
main aspects of problem-solving is problem understanding. In the problem
definition phase, one must consider what must be done instead of how to do
it. Does it involve asking the right questions, such as what is asked? What is
the relevant information given? What is the unknown? What is the
information that is irrelevant to the given problem?
In stage one, the focus is to derive a problem statement that must convey a
set of precisely defined tasks. Often it makes sense to start with a specific
example of a general problem that one wishes to solve. Usually, it provides
insight into the problem.
A problem may be well-defined or ill-defined. This is the domain of a study
called computability theory. Is the problem solvable? In a computable
theory, a well-defined problem normally is solvable as it has clear
specifications. Similarly, ill-defined problems are those problems that lack
clear specifications.
Intractable problems are problems that the computer cannot solve. If one
asks the opinion of literature on a computer, the computer may be unable to
solve the problem as writing programs for these problems are impossible.
There is another category of hard problems. For example, a travelling
salesperson’s problem is difficult or ‘hard’. The travelling salesperson
problem is where a salesperson starts in a city, visits all other cities only
once, and returns to the town where he began. This problem can be solved
for a few towns, say 10. However, when the problem is called to 1000
towns, the problem’s complexity increases and the problem requires huge
resources such as memory and CPU time. However these resources are
limited, and problems are unsolvable because of the requirement of huge
resources. Such problems are called ‘hard’ problems. Therefore, a problem
solver skill is supposed to know the nature of problems as he/she may not
like to end up with a ‘hard’ problem that do not have solutions.
The second stage involves designing a plan. The plan consists of finding the
mathematical formula or a model or finding heuristics. Heuristics guess an
answer and helps us in formulating the solutions. Therefore, planning is a
search for a specific strategy. Plan also involves looking for similar
problems and patterns; answer guessing; making a diagram, table, chart, or
model; and working backwards from the solution to the problem statement.
Comparing the problem with known problems helps find the similarities of
the problem. It is useful to take multiple perspectives on a given problem.
The third stage is executing the plan and recording the steps of step 2. This
can be done manually using paper and pencil that can be later converted to
flowcharts, algorithms, or programs.
The last stage is the review and looking back. This stage is crucial and
involves reviewing the previous activities and learning a lesson from them.
Also, it is better to find out how the solution has arrived and provided ideas
about problem-solving skills. This step is also useful for checking the
answer and finding it reliable.
2.2 Computational Thinking
Problem-solving has led to a new area of study called computational
thinking. Computational thinking provides the foundation of how a program
can be solved. Jeanette M. Wing coined the word called ‘computational
thinking’. It is a skill that helps to identify, present, and solve problems by
looking for patterns, patterns among problems, and the reusability of
patterns. Computational skill is considered the basic skill that is necessary
to solve problems. It looks for shared patterns among commonly available
problems.
Computational thinking is needed because of the following two reasons:
1. It helps to clear and refine the thought process required to solve the
problem. This organisation and refining of thought processes is the
algorithm part of problem-solving.
2. Computational thinking helps to communicate the problem-solving
process to another person in the form of designs, diagrams, and program
codes.
The elements of computational skill are listed below:
1. Decomposition
2. Pattern matching
3. Pattern generalisation and abstraction, and
4. Algorithms design
1. Decomposition
This is a skill that decomposes a problem into small problems when the
problem is big. It is a skill to break a larger problem into smaller, more
manageable ones. This forms the basis for the top-down design discussed in
section 2.3 of this chapter.
2. Pattern matching
Pattern matching (or pattern recognition) is the skill of finding similarities
or patterns among items to extract extra information necessary to solve a
problem. Recognition of patterns is required to solve problems. One can
compare the recognition of patterns with the human ability to distinguish
one object from another using their visual features. Humans all recognise
objects that surround us by their features. A chair is different from a table,
as its features are different. This is a sort of visual pattern recognition. This
skill of finding a pattern among data is a unique skill called pattern
matching.
In programming, pattern recognition is the process of finding similarities or
patterns among numbers.
E.g., Consider the Fibonacci sequence,
1, 1, 2, 3, 5, 8, 13,…..
Is there any pattern in the Fibonacci sequence? It can be observed that every
number is a sum of the previous two numbers. It is not sufficient to
understand the pattern; it is necessary to find the formula that expresses the
pattern as F(i) = F(i-1) + F(i-2).
3. Pattern generalisation and abstraction
Pattern generalisation is to ensure that the solution is applicable for all
instances of the given problem and abstraction focuses only on things that
are required for the solution of the problem and on irrelevant discord things.
Abstraction is distinguishing between what is important and what is not
important. It is a skill of collecting important information necessary to solve
a problem. Abstraction is a skill that is useful for modelling. For example, if
one wants to find the shortest path among four cities, it is better to model
that as a graph where each node is a city and edges as roads that link the
towns. Each edge carries a weight, say km or mile. Then the problem of
finding a route can be reduced to the shortest path algorithm.
4. Algorithms design
The outcome of computational skill is algorithm design. The outcomes
provided by the problem-solving process have two components.
Algorithms
Data structure
What is an algorithm? An algorithm is a well-defined step-by-step
procedure for solving a problem that takes a valid input to give correct
outputs. The goal of problem-solving is the formulation of efficient
algorithms and appropriate data structures.
Historical Note
An algorithm is a word that is derived from a Persian mathematician, Abu
Ja’fer Mohammed Ibn Al Khowarrizmi. He lived in the town of
Khowarizm, Uzbekistan. When his books were translated into Latin, his
name was quoted as Algorismus. The term ‘Algorismus’ later become an
algorithm.
Some of the essential characteristics of algorithms are:
1. An algorithm can have zero or more inputs.
2. Algorithms should have at least one output.
3. Algorithms have a finite number of instructions.
4. All steps in algorithms are definite and should be executed in a specific
order.
5. The algorithms should be correct and effective.
6. The algorithms should be generic and independent of language and
programming environment.
The other outcome of problem-solving is the data structure. The data
structure is concerned with how data and their relationships are stored.
Primarily, the data structure captures the organisation of data. The role of
data structures and algorithms is complementary. A data organisation
without a procedure is useless; similarly, a procedure with no data
organisation is futile. Therefore, the popular saying is
Thus, problem-solving requires the skills to identify an algorithm with a
suitable data structure and an efficient algorithm.
Notes
Two aspects of algorithms are important to us. One is correctness and
another is efficiency. Correctness is the first and foremost criterion as an
algorithm that does not find a correct answer is useless irrespective of how
efficient that algorithm can be! Algorithm efficiency is important as
efficiency is the measure of how much it uses computer resources such as
time and space effectively. Efficient algorithms should run fast and use less
memory.
2.3 Software Development Cycle
In a small programming environment, the complexity of the programming
is relatively less. For example, a student is expected to solve a problem that
involves no great project management techniques like costing or manpower.
But in the larger context, software requires project management principles.
The differences are listed below in Table 2.1.
Table 2.1: Difference Between Algorithm Development and Software
Development
Large-scale software development in large business organisations requires
many sophisticated steps. This is called the software development cycle that
involves many stages that are listed below:
1. Understanding the problem
The first stage is problem understanding. The focus here is to figure out the
problem exactly, as discussed in section 2.1, to create a problem statement.
The primary question in this stage is “What and not how”. Computational
skills help to understand the given problem.
2. Problem analysis and specifications
The second stage is to know what the program is supposed to do! The focus
of this stage is to focus on what the program is supposed to do and not on
how it will do it! The main focus is the details of input and output and how
they are related.
3. Algorithm design
The third stage is program design. Here, the focus is to develop algorithms
that can meet the user specifications of the given problem. Here, the
question of how the specifications will be met is discussed as algorithm
design in section 2.3 of this chapter. One of the major problems in design is
that often the specifications of software change. So, refactoring needs to be
done to prevent potential design decay.
4. Implementation
In this stage, the program design is converted to a program using a
computer language, such as Python. The goal of the coding process is to
convert a design document to code. Coding is an important activity and
coding affects the testing and maintenance of code. So the goal of coding is
to minimise the test and maintenance effort. One of the simplest coding
processes is incremental coding where a small portion of the functionality is
implemented as code. If the code passes through all tests, the developer
adds further functionality to the code. This form of the development process
where code is developed incrementally along with testing is called the
incremental coding process.
5. Debugging and testing
In debugging, the errors are spotted and removed. In software terms, these
can be defined as follows:
1. Error: Error may refer to problems in code, design, or requirements. It is
defined as the difference between expected and actual or observed value.
2. Fault: identifying the conditions that cause the system to fail is called a
fault.
3. Failure: failure is a condition where the system is not working according
to the specification.
To uncover the errors, test cases are required. A test case helps to uncover
errors. Say, trying to find the square root of a negative number results in an
error. So giving a negative number can be considered a test case for the
square root program.
The errors may be related to the syntax of the programming language called
syntax errors or logical errors called semantic errors. The error is called a
bug. Removing the bug is called debugging.
Once the coding is done, it should be verified before the code is used by
others. This can be a simple code reading to detect errors and defects. This
is also done by
1. Code inspections: after the code is successfully compiled, code
inspections can be done where the code is distributed to a group of
inspection professionals. All the quality issues and defects are identified
and rectified.
2. Static analysis: this is a stage where only program text is taken as input,
and potential errors are analysed. Dynamic analysis and model checking
can also be done.
Unit testing is one such popular methodology where the programs are
executed with test cases except that the focus is on smaller programs or
modules. The smallest unit can be a function, a collection of functions, or a
class. This involves the selection of test cases and checking the behaviour
of the program. If the behaviour is not satisfactory, the process of removing
the bugs is done, which is called debugging.
Debugging
Debugging is a process of removing bugs. The aim of debugging is to
uncover errors in the implementation of programs. Pdb is a popular Python
utility that can be used for debugging. These details are covered in Chapter
17 of this textbook.
Testing
A testing process is one where legal input is given, and the program is
checked for its correctness. The focus of the testing process is to detect
errors. It checks the programs with test cases to find out whether the
program works as per user requirements.
Test-driven Development is one such testing development process that was
initially proposed as part of extreme programming. Here, first, the test
scripts are written. Then only the code is written. Then the whole process is
done incrementally where test cases are developed for specifications and
code is written to pass the test. Therefore, the whole process is user driven.
The testing process is a complicated process involving many stages:
1. Unit tests: here, different modules are tested against specifications to test
the logic of the program.
2. Integration test: modules are combined to form subsystems and
integration tests to check whether the integration is done properly.
3. System test: the entire developed system is checked.
4. Acceptance test: In acceptance test, developed system is deployed and
checked with the customers whether the program is working
satisfactorily.
Python provides many utilities like Pytest and Doctest to implement the
testing. These details are provided in Chapter 17 of this textbook.
6. Program maintenance
The last stage of software development is called maintenance. The program
is used and continuously modified as user specifications keep on changing.
This process is called refactoring. This continuous evolution of software
over the period is called maintenance.
2.4 Algorithm Design Strategies
Algorithm design is the process of writing unambiguous step-by-step
procedures for solving problems. A design strategy is an important necessity
for algorithm writing. Consider, for example, searching for a name in a
telephone directory. Certainly, one would not search for names one by one.
Such a strategy is called brute-force strategy. The brute-force strategy will
not work for name searches in the telephone directory. Instead, one can take
advantage of the fact that the names are indexed in the telephone directory.
So, one can randomly pick a page, compare the target name with the name
at the top of the directory, and decide to go forward or backwards. This
process can be repeated till the target name is reached. Hence, design
strategies can lead to effective design and implementation.
Some of the popular design strategies are listed below:
1. Stepwise Refinement
Stepwise refinement is a fundamental algorithm design. It is to evolve a
set of control structures that can be directly implemented in any
programming language. Stepwise refinement starts with a general outline
of the problem. This problem is divided into a set of subproblems or
subtasks. The subproblems are further subdivided. This decomposition
process continues until a level is reached where a subproblem or subtask
can be directly solved. This kind of algorithm construction is called step-
wise refinement or top-down design. One example of top-down design
is the ‘Divide and conquer’ paradigm. Some of the advantages and
disadvantages of this design approach are listed below in Table 2.2.
Table 2.2: Advantages and Disadvantages of Top-Down Design
2. Bottom-up Approach
Another way of algorithmic construction is called bottom-up design.
This is also known as program synthesis or compositional approach.
The problem is divided into many subproblems or subtasks, as in the
top-down approach. The main difference between the bottom-up and the
top-down approaches is that the subproblems are solved first in a
bottom-up approach. Then, the subproblems’ results can be used to solve
much bigger problems that require this result. Thus, the direction is
bottom to up, and this process continues until the final solution is
achieved. Some of the advantages and disadvantages of this design
approach are listed below in Table 2.3.
Table 2.3: Advantages and Disadvantages of Bottom-up Design
3. Problem Reduction
Recursion is a design paradigm that solves the problem using the
principle of problem reduction. It splits the problem into a set of
subproblems. Every subproblem is a copy of the original problem with
less problem input size. If every subproblem is written as a function,
then recursion attempts to solve the problem by calling itself. Then, the
problem’s solution is computed using the solutions of the subproblems.
Functional languages do provide recursion as a means for performing the
repetition of tasks. Recursion is also supported by many procedural
programming languages. Recursion is a way of defining an object using
the idea of self-reference. The concept of recursion can be extended to
functions and procedures.
A recursive function is a function that calls itself conditionally. An
algorithm is called a recursive algorithm if it uses a recursive function. A
recursive definition has two important components: the base case, which
refers to initial values and the recursive case, where often a rule gives a
solution to a problem in terms of its subproblems.
For example, the factorial of a number N, a positive number, is given as
N * (N-1)!
The base case is 0! =1, that is, the termination part, and N * (N-1)! is the
recursive part.
2.5 Algorithm Design
Design strategy gives some design templates that must be communicated to
the programmer. Communication can be in the form of natural language,
programming language, or pseudocode. The only reasonable choice for
writing algorithms is to use pseudocode as a natural language, such as
English, can have many ambiguous words. Programming languages as
algorithm specifications may pose problems for those who do not know the
programming language and the novice. Pseudocode is a generic way of
describing or writing algorithms formally independent of any programming
language or environment. Thus, the pseudocode approach combines the
advantages of both natural language and the power of mathematics. The
advantages and disadvantages of the mode of communication in an
algorithm design are given in Table 2.4.
Table 2.4: Mode of Communication in Algorithm Design
Another concept that is helpful in algorithm design is structured
programming. Structured programming is a programming paradigm that
forces a designer to follow a rigorous discipline that facilitates a better
algorithm design. The basic principle of structured programming is to
implement programming solutions to any given problem using only three
structures or control structures, i.e., sequence, selection, and repetition.
Algorithms are built using three basic building blocks: sequence, selection,
and repetition.
Three building blocks are elaborated below:
1. Sequence
A sequence is a chain of steps that are executed one by one. The order is
maintained, and it involves no decisions or repetition. The sequence is a
control structure that allows the set of statements to be executed
sequentially, as shown below:
2. Selection
Selection is a control structure that allows selecting a task among two or
more alternative tasks based on the given Boolean condition. For example,
if the statement shown here evaluates Boolean condition C, and if it is true,
select task A for execution; otherwise, perform task B.
3. Repetition (or Iteration)
Computers are known for their effectiveness in solving repetitive tasks.
Many programming languages provide a statement that allows repeating
certain task or tasks multiple times, called iteration. Iteration is a process of
repeating task or tasks towards accomplishing a goal. Iteration constructs
are programming constructs that will perform the repetition of tasks based
on certain conditions. Informally, we say often in the English language,
“Write this sentence 100 times”. This is an example of the repetition of a
task.
Repetition can be implemented using a for or while statement in a
programming language. for loop is an entry-controlled loop; that is,
conditions in the control statements are tested first. If the result of the test
conditions is true, then the body of the loop is executed. If the condition
turns out to be false, the lines of code in the body of the loop will not be
executed. Whereas the do-while loop is a classic example of exit-controlled
where conditions are checked at the end of the loop structure.
2.6 Flow Charts
Some would prefer to use flowcharts for representing algorithms
graphically. Thus, a flowchart is a pictorial form representing the algorithm.
The graphical layout of an algorithm is useful for better understanding as it
helps show the algorithm’s logic. The flowchart illustrates the control flow
from one task to another task.
The start is the symbol used to indicate the start of the flowchart. It is a
flattened ellipse symbol. The symbol start can have no input, but it can have
only one exit line. On the other hand, the exit symbol has no exit flowlines.
The symbol for start and stop symbols is given in Fig. 2.2.
Fig. 2.2: Start/Stop Symbol
Flowlines are important indicators that indicate the flow of the control. The
flowlines are given in Fig. 2.3.
Fig. 2.3: Flowlines
Flowlines show the directions of both data and control flow. The purpose of
the flowlines is to connect the blocks and show the directions. The symbol
for the process is given in Fig. 2.4.
Fig. 2.4: Process Symbol
The process symbol has one entrance and one exit. The process can be
calculations or file operations.
The parallelogram shown in Fig. 2.5 indicates the input and output symbols.
It has one input and one output. This symbol is used to input data from the
keyboard and sends the output to the display.
Fig. 2.5: Input/Output Symbol
The symbol diamond shown below in Fig. 2.6 indicates the decision
symbol. It has one input and two output symbols. The diamond symbol has
a condition; if the condition is true, then one exit will be used, and if the
condition is false, the other exit is used.
Fig. 2.6: Decision Symbol
The symbol shown in Fig. 2.7 is used for denoting procedures/modules. It
has one entrance and one exit.
Fig. 2.7: Procedure Symbol
Finally, the flowchart may be a longer one. It can extend over an entire
single page or across pages. The connectors shown in Fig. 2.8 are used to
connect the flowcharts.
Fig. 2.8: Connectors
Connectors have labels either as numbers of labels. The connectors connect
parts that should share the same labels.
2.7 Example Algorithms
Some examples of algorithms are given in this section to illustrate the
process of algorithm writing.
2.7.1 Algorithm for Subtraction of Two Numbers
How to perform subtraction for two numbers? The algorithm is shown in
Table 2.5. It can be observed that step 1 initiates the start of the algorithm.
In step 2, one gets two numbers from the user and stores them in variables
(here x, y). In step 3, the subtraction process is carried out and the result is
stored in the variable z, and step 4 prints the result generated by step 3
(which is stored in variable z).
Table 2.5: Algorithm for the Subtraction of Two Numbers
Algorithms and pseudocode are essentially the same, except that
pseudocode is more mathematical and suitable for better communication.
The flowchart is given in Fig. 2.9.
Fig. 2.9: Flow Chart for the Subtraction of Two Numbers
The flowcharts for the subsequent algorithms are left out, and readers can
refer to the above flowchart for further examples.
2.7.2 Algorithm for Finding the Area of a Circle
How to find the area of a circle? The algorithm is given below in Table 2.6.
It can be observed that step 1 initiates the start of the algorithm. In step 2,
the algorithm reads the value of the radius as input from the user and stores
it in the variable ‘r’. In step 3, the algorithm affects the calculation of the
area of the circle using the formula, area = 3.14*r*r. Store the result in the
variable area in step 4, and print the result generated by step 3 (stored in the
variable area).
Table 2.6: Algorithm for Finding the Area of a Circle
The flowchart for finding the area of a circle is shown in Fig. 2.10.
Fig. 2.10: Flowchart for Finding the Area of a Circle
2.7.3 Algorithm for Finding the Largest Among Three Numbers
How to find the largest of the three numbers? The algorithm is given below
in Table 2.7. It can be observed that step 1 initiates the start of the
algorithm. In step 2, the algorithm reads the values of a, b, and c. In-step
comparisons are made, and if ‘a’ is greater than b or c, then appropriate
variables are printed. In step 3, if the value of ‘b’ is greater than c, then b is
printed as the largest variable. Otherwise, c is the largest variable.
Table 2.7: Algorithm for Finding the Largest Number
The flow for finding the largest number is shown in Fig. 2.11.
Fig. 2.11: Flowchart for Finding the Largest Number
2.7.4 Factorial of a Number
Non-recursive (or Iterative) algorithms do not use the concept of recursion.
The problem of finding the factorial of a number, say n! where n>=0 can be
stated formally as
n! =n× (n-1) ×(n-2)×…×0!
Where 0! =1. The formula for finding n! is n! = n x (n -1)! with an initial
condition that 0! =1.
The non-recursive strategy can be observed as follows,
prod = 0! = 1
prod = prod x 1 = 1 * 1 = 1!
prod = prod x 2 = 1! * 2 = 2!
prod = prod x 3 = 2! * 3= 3!
The above steps can be generalised as
prod = prod * i where i range from 1 to n.
The algorithm can be written as a pseudocode, as shown in Table 2.8.
Table 2.8: Algorithm for Finding the Largest Number
The flowchart for finding the factorial of a number is given in Fig. 2.12.
Fig. 2.12: Flowchart for Finding the Largest Number
A recursive version of the factorial problem is also feasible. One can
recollect the recursive version of the factorial function is
The recursive version is given below.
It can be observed that the recursive algorithms are very lucid and simple.
2.7.5 Euclid Algorithm
Let us discuss some of the most common algorithms encountered in
computer science. Euclid is widely considered the father of Geometry. In
his book “Elements”, he proposed an algorithm for computing the greatest
common divisor (GCD). GCD is finding the greatest divisor given two
positive numbers.
The algorithmic steps of the GCD calculation are given informally in Table
2.9.
Table 2.9: Algorithm for Finding GCD
The flowchart for finding the GCD of two numbers is shown in Fig. 2.13.
Fig. 2.13: Flowchart for Finding GCD of Two Numbers
2.7.6 Linear Search
Linear search is also known as a sequential search. Formally stated, given
an array of N elements and a target key, find the presence or absence of it.
The array A shown in Fig. 2.14.
Fig. 2.14: Initial Array
Let us assume that the target key is 25. In a sequential search, the
comparison starts at the index, and element 25 is compared with 10. The
search moves on to the next element if there is no match. Element 25 is
compared with 20. The search carries on to the next part if there is no
match. Element 25 matches with target element 25. Therefore, the search
process reports success and prints the index element.
Table 2.10: Algorithm for Linear Search
The pseudo-code for the above procedure is given in Table 2.10. The
flowchart for the linear search algorithm is shown in Fig. 2.15.
Fig. 2.15: Flow Chart for Linear Search Algorithm
2.7.7 Insertion Sort
Insertion sort is an important sorting algorithm. Given an array of unsorted
elements, Insertion sort uses a decrease-and-conquer strategy to give a
sorted order. In the decrease and conquer strategy, problem P of size n is
reduced to problem P of size ‘n-1’. The subproblem is solved using an
iterative algorithm.
The individual element is inserted into a solution of the subproblem to give
a final solution. For example, consider the following array shown in Fig.
2.16. The initial position is called sentinel and assumes the value of the
minimum value –∞.
Fig. 2.16: Initial Array
The first element is considered a sorted item as it is greater than –∞. This is
as shown in Fig. 2.17.
Fig. 2.17: First Phase
In pass 2, the unsorted element 15 is inserted before or after element 10 so
that it is ordered. This results in the following scenario in Fig. 2.18.
Fig. 2.18: Second Phase
In pass 3, the unsorted element 22 is inserted before or after element 15, so
the list is in order. This results in the following scenario in Fig. 2.19.
Fig. 2.19: Third Phase
In pass 4, the last element, 20, is inserted into its appropriate place for the
sorted list to be obtained as in Fig. 2.20.
Fig. 2.20: Final Sorted List
The pseudocode is given in Table 2.11.
Table 2.11: Algorithm for Insertion Sort
The flowchart for the insertion sort algorithm is shown in Fig. 2.21.
Fig. 2.21: Flowchart for Insertion Sort
2.8 Analysis of Algorithms
Algorithmic Complexity theory is a branch of algorithm study that
analyses algorithms. Algorithm analysis tries to measure the computer
resources such as time and space. These are respectively called time and
space complexity.
What is the use of complexity analysis? Complexity analysis is useful for
comparing algorithms. For example, problem A may have multiple
solutions and hence multiple algorithms. Therefore, it is necessary to find
efficient algorithms. Comparisons are done by comparing the time and
space complexity. So, algorithmic complexity theory is necessary for
designing efficient algorithms.
Notes
One encounters program implementation in two types of time in algorithm
analysis: run time and compile time. Only run time matters for analysis as
a program may be compiled and recompiled many times and therefore is
dependent on a machine. So, compile run is ignored. Run time is a function
of input data n and is often expressed as a function f(n).
Algorithm Analysis
There are two types of analysis:
1. Mathematical analysis of algorithms
2. Empirical program analysis
Mathematical analysis is done before the code is written. Here, the
algorithm is analysed based on the run time. The empirical analysis is
often called profiling. Here, a code is developed and made to run on
many data sets called benchmark data sets. The statistics of time and
usage of functional calls and variables are made out and profiling is
done. In this textbook, profiling is discussed in detail in Chapter 17.
Now, let us discuss mathematical analysis in a detailed manner, as it is
necessary to have a strong foundation in looking out for efficient
algorithms.
Mathematical Framework for Algorithm Analysis
The framework for complexity analysis is given below:
1. Measure the input size, i.e., the input data length. This is because the
complexity of an algorithm is based on the notion of input size. The
input size is the length of the input of an algorithm. The input size is
defined formally as the binary bits of the input. Why should input
size serve as a basis for complexity? For example, sorting a list of 10
numbers can be easy! But the same sorting method involving a million
numbers can be difficult. This shows that there is a direct relationship
between input size and complexity.
2. Measure the running time using either a step count or a count of basic
operations. A basic operation is an operation whose execution time is
bounded by the constant. The value of the constant depends on the
programming environment or language. This constant is ignored in the
subsequent asymptotic analysis.
Elementary, or basic operations, are primitive and often implemented
directly with less effort. Some of the basic operations are listed below.
Assignment operations (Assignment operator)
Comparison operations (like comparisons)
Arithmetic operations (Like multiplications and divisions)
Logical operations (like and or not)
Non-elementary are not atomic and may involve many elementary
operations. Counting the elementary operations provides a clue of algorithm
complexity.
The counting techniques for control structures are given in Table 2.12.
Table 2.12: Procedure for Computing Costs
In sequential structure, the whole algorithm segment requires s1+s2
operations. This is called the addition principle. In decision structure, the
time complexity of this program segment is given as T(n) = Maximum {P,
Q}, and in an iterative design, the time complexity of the algorithm for
loops is given as s1. Thus, a statement s1 that requires m operations is
executed ‘n’ times in a loop, and then the program segment requires m X n
operations. This is called the multiplication principle.
Example 1: Illustration of Complexity Analysis of an algorithm
Apply complexity analysis for the following algorithm segment.
Solution:
The cost of computing for adding an algorithm is given in Table 2.13.
Table 2.13: Computing Cost for Adding Algorithm
So, the total run time cost (As the sum of
all constants yields another constant). Therefore, f(n) = c. This is a constant
time algorithm.
Example 2: Illustration Complexity Analysis of Selection Sort
Apply complexity analysis for the following program segment
Solution:
The cost of computing for selection sort is given in Table 2.14.
Table 2.14: Computing cost for selection sort
So, the total run time cost is
Having obtained f(n), which represents the running time, one has to find the
best, worst, and average case time complexity. Why? It is naïve to think that
the algorithm works uniformly for all inputs. There are circumstances
where the algorithm’s performance is also dependent on the input data
distribution. The running time varies as per the input. Hence, there are three
types of scenarios.
1. Worst-case complexity is the maximum time the algorithm takes for all
inputs of size n.
2. Best-case complexity is the minimum time the algorithm takes for all
inputs of size n.
3. Average-case complexity is the average time an algorithm takes for
random inputs.
Asymptotic Notations
The next step is to express the best-, average-, and worst-case complexity as
notation. It is better to say . Here, O (Pronounced as
Big-Oh) is an asymptotic notation. Asymptotic notations give the closest
approximation of run time. Let us try to formally define these notations
Can we call ? Fine, let f(n) and g(n) be two functions that map
a set of natural numbers to positive real numbers, then O(g(n) is the set of
all functions such that, for all values of constant c > 0 and for all n ≥ n0. If
this condition holds good, then function f(n) is said to be in O(g(n).
Big-oh notation can also be defined in terms of limits. If the limit
holds good, then this implies t (n) = O(g(n)). This is called the Big Oh ratio
theorem.
Example 3: Illustration of Big-Oh Notation
Let f(n) = 10n2. Prove that this is of the order O (n2).
Solution:
One way of proving this is
Therefore, c =10. This condition holds good for all n >= 1. This implies that
O (n2) is correct.
The Big-oh notation indicates that the worst-case complexity defines input
for which the algorithm takes maximum time and causes the computer to
run slower. Even worst-case complexity of an algorithm gives us an upper
bound on the algorithm and is crucial as it measures the maximum
computational effort required to solve the given problem.
The best-case complexity means that the algorithm must spend a minimum
effort. This is given by Big-Omega notation. The formal definition is as
follows:
Let f(n) and g(n) be the positive functions, then (g) is the set of all functions
such that for all values of constant c > 0 and for all n ≥ n0. Thus, the
function f(n) is said to be in (g(n)).
Example 4: Illustration of Big-Omega Notation
Let t(n) = 8n+4. Prove that this is of the order Ω (n).
Solution:
One way of proving this is
Therefore, f(n) = Ω (n)
Average-case complexity indicates the analysis if the input is random and
provides a prediction about the algorithm’s running time for random input.
It is given using q Notation. This notation illustrates both upper and lower
bounds. The definition of average-case complexity is given below:
The formal definition is as follows. Let f(n) and g(n) be two functions that
map a set of natural numbers to positive real numbers, then if f(n) = O(g(n),
as well as f(n) = Ω(n), then the function f(n) is said to be in θ(n).
2.9 Efficiency of the Algorithms
The final step is to identify the rate of growth. This step determines the
algorithm’s performance when the input size is scaled higher. Scaling
means changing the input size to a larger value and carrying out the
measurement of the behaviour of the algorithms. The algorithm behaviour
can be determined only after observing the algorithm’s performance when
the input data are scaled high. The analysis is useful to classify the
problems into sets of problems that share common characteristics; they are
polynomial algorithms and exponential algorithms. The algorithms whose
input, if scaled, do not increase time are called linear algorithms. The linear
algorithms are listed below in Table 2.15.
Table 2.15: Complexity of algorithms
The other type of algorithm is called exponential time. These algorithms are
whose operations are bounded by constants raised to the power of a
constant. For example, O(n!) is an exponential algorithm. The traveling
salesperson problem is an example of exponential algorithm. Exponential
algorithms are difficult to solve by computers as many problems are known
to be intractable (Unsolvable problems).
Summary
1. Problem-solving includes all stages, from problem definition to code
implementation.
2. Algorithm is a complete sequence of actions to accomplish a task.
3. The error is called a bug. Debugging is the process of removing a bug.
4. The continuous evolution of software over the period is called
maintenance.
5. Computational thinking is a skill that helps identify, present, and solve
problems. The elements of computational skills are decomposition,
pattern matching, abstraction, and algorithms.
6. Decomposition is a skill that decomposes a problem into manageable
subproblems. Pattern matching is the skill of finding similarities among
items to extract information that is necessary to solve a problem.
Abstraction is a process of distinguishing between important and non-
important information.
7. Stepwise refinement is a strategy of evolving a set of control structures
that can be implemented in Python language. This strategy is also known
as top-down design.
8. In bottom-up design, the small problems are solved first. Then, these
results are used to solve bigger problems.
9. A recursive function is a function that calls itself. A recursive algorithm
uses recursive functions.
10. Structured programming enforces control structures that are sequence,
selection, and repetition.
11. Communicating algorithm design using pseudocode is called algorithm
specification.
12. Pseudocode is a generic way of writing algorithms formally independent
of any programming language or environment.
13. Flowchart is a pictorial way of representing an algorithm.
14. Algorithm complexity theory is a branch of algorithm study that analyse
time and space.
15. Input size is the length of the input of the algorithm.
16. Worst-case complexity defines an input for which an algorithm takes
maximum time. It can be expressed as Big-oh notation. Best-case
complexity defines an input for which an algorithm takes a minimum. It
can be described as a Big-omega notation. Average-case complexity
represents a random input for which an algorithm time is on average. It
can be expressed as Big-theta notation.
17. An efficient algorithm takes lesser time and space.
18. A polynomial time algorithm gets solved in a reasonable amount of time.
19. Intractable problems are difficult problems to get solved by computers.
Glossary
Algorithm - An algorithm is a complete sequence of actions that
accomplish a task.
Debugging - The process of removal of errors is called debugging. An
error is called a bug.
Algorithmic problem - This is an instance of an abstract problem whose
solutions can be computed using a
Problem-solving process - A process that begins with the formulation of
a problem statement to the final program code.
Intractable problems - Problems that cannot be solved by computers.
Hard problems - Problems that are difficult to solve for want of
unlimited computing resources.
Maintenance - The continuous evolution of software is called
maintenance.
Computational thinking - A skill that identifies, poses, and solves
problems.
Program decomposition - A skill that decomposes a larger problem into
smaller and more manageable ones.
Pattern matching - Finding similarities among items to extract the
necessary information to solve a problem.
Abstraction - A skill of removing unnecessary information.
Stepwise refinement - A strategy of evolving control structures that can
be implemented in a programming language.
Bottom-up approach - A strategy of solving smaller problems and then
using its solutions to solve bigger problems.
Recursion - A concept of function that calls itself.
Recursive algorithm - A algorithm that uses recursive functions is called
a recursive algorithm.
Sequence - A control structure that allows a set of statements to be
executed sequentially.
Selection - A control structure that allows selecting a task among two or
more alternate tasks based on a given condition.
Repetition - A structure that repeats a task multiple times based on a
condition.
Pseudocode - A generic way of describing or writing algorithms formally
independent of any programming language or environment.
Flowchart - A graphical representation of an algorithm.
Run time - The execution time of an algorithm.
Input size - The length of the input of an algorithm.
Worst-case analysis or upper bound - The maximum time required to
execute a given input.
Best-case or lower bound - The input that causes the algorithm to take
minimum time to solve a problem.
Average case - The algorithm’s average time for random inputs.
Asymptotic analysis - A sort of algorithm analysis that does
approximation and expresses it compactly.
Numerical Problems
1. Perform analysis on bubble sort.
2. Perform analysis on the summation of an array.
3. Perform analysis of finding the maximum of an array.
4. Let t(n) = 7n3 + 6n2 + 5, prove that this is O(n3 ).
5. Let t(n) = 7n2 + 6n, what is Big-Omega notation?
6. Perform asymptotic analysis of linear search.
Programming Exercise
1. Write an algorithm for swapping two variables without an intermediate
variable.
2. Write an algorithm for finding a number prime or not.
3. Read a set of employees and their year of birth. Write an algorithm to
print the employees who were born in odd years.
4. Write an algorithm to check the number Armstrong number or not.
5. Write an algorithm to solve the towers of Hanoi problem.
6. Write a recursive algorithm to find the sum of an array.
7. Write an algorithm for solving a binary search problem.
8. Write an algorithm to solve a cashier problem. A cashier problem returns
the minimum number of coins for a given request.
9. Write an algorithm for finding a perfect number.
10. Solve the 3n + 1 problem to generate a sequence of numbers.
OceanofPDF.com
Python is an experiment in how much freedom programmers need. Too
much freedom and nobody can read another’s code; too little and
expressiveness is endangered.
Guido van Rossum
CHAPTER 3
Introduction to Python
This chapter aims to provide insight into how to write simple Python
programs. This chapter focuses on giving the necessary fundamentals of
Python to write Python programs.
Learning Objectives
At the end of this chapter, the reader will be able to
Write simple python programs
Understand the need for comments in Python
Reading data using the input() function
Print data using the print() function
Know the data types and type conversions
Understand the operators and their types
Know about the expressions
Know about built-in functions
3.1 What is a Program?
Every Python program is a set of statements that are executed one by one. A
python program is called an script. A script is vaguely a short program that
can be executed in Python interactive script. A statement is a line of code or
command or instruction for computers to perform. A statement may
initialize a number, perform some computation, do some control flow, and
do some targeted assigned tasks. A function, on the other hand, is a
collection of statements that perform an intended action and returns a value.
There are two types of statements – Simple and compound. A simple
statement generally carries out a single simple task. Examples of simple
statements are expressions and assignment statements. A compound
statement is a group statement that spans multiple lines and a compound
statement consists of one or more statements that control the execution of
other statements of that program directly or indirectly. For example, if,
while, and functions are examples of the compound statement.
For example, let us try to swap two numbers, say x = 10 and y = 20. One
way to write this python is by using an interactive mode as follows:
Example 1: Illustration of swapping of variables in Interactive mode
It can be observed, interactive mode facilitates entering a script line by line.
Notes about IDLE
IDLE has a Python shell that interacts with the user. >>> is called prompt
where one types the python script line by line. Executing Python in IDLE is
often referred as Read, Evaluate, Print and Loop. This is often called REPL.
If the script is not correct, the Python interpreter shows an error. Error is a
mistake present in the program. A Syntax error is especially committing
mistakes in writing statements where the program is not allowed to run. The
following script shows an error where there is a mismatch in double quote
in a print statement.
Another error is called run-time error, where the error occurs at the runtime.
The following script illustrates a run time error where a printing of a
variable is attempted while the variable is not defined.
Python shows a set of statements called traceback as soon as it stops
executing the script. It gives a valuable message indicating that the variable
is printed before it is declared or assigned. This is called traceback.
The semicolon is the standard symbol used at the end of the statement in
most programming languages like C. In Python, a semicolon is not
necessary. Even if used, the Python interpreter ignores the semicolon at the
end of the statement. In addition, it is strongly discouraged by most of the
style guides available for Python. However, Python requires a semicolon as
a statement separator when multiple statements are given in a single line, as
shown below:
Example 2: Illustration of multiple statements in a single line
A typical length of a Python statement is 79 characters.
If the statement spans multiple lines, the line should end with a
backslash(\). For example, the statement, print(“How are you?”) can be
written in multiple lines as below:
Example 3: Illustration of a single line statement in multiple lines
Throughout this text, Python version 3.11 (or in general version 3.x) is
used. Python 3.x is different from Python 2.x in many ways. Python 3.x is a
new and updated language. The difference between Python 3.x and Python
2.x is given in this Table 3.1.
Table 3.1: Differences Between Python 2.x and Python 3.x
In this book, only Python 3.x is used. The version used exactly in this
book is 3.11.
3.2 Elements of Python Language
The set of valid characters that a programming language can recognize is
called the character set of the language. Two types of character sets are
1. Source characters: The following is the list of source characters
1. Alphabets uppercase (A – Z) and lowercase(a–z), underscore(_)
2. special characters blank – ( +,-,*,/,^,~,%,!,&,|,(),{},[] ?’, ;, \, blank(”
“).
The token is the smallest lexical unit in a program. The types of tokens
are listed below:
1. Identifiers
2. Keywords
3. Literals
4. Punctuations.
Tokens are separated by a space, tab, or carriage return, which are
collectively known as whitespaces. Some of the tokens are listed below:
Identifiers
Identifiers are the names that represent programmable entity in Python.
Consider a typical Python statement, i = 0. Here, the value 0 is assigned to
the variable i. A variable is a named memory location where values are
stored. In Python, all variables, objects, and functions are given a name.
These names are called identifiers. Certain rules govern how a name can be
given. The guidelines are given below:
1. Every identifier must start with a letter or underscore (“_”)
2. The letter or underscore can be followed by any sequence of letters,
digits, or underscores.
3. Identifier cannot have a space
4. Identifiers are case sensitive. So the names celsius, Celsius, and
CELSIUS are different
The programmers can choose any names, but it is desirable to have self-
explanatory meaningful names.
Keywords
Some of the words or names are called keywords. What is the reason for it?
The reason is that Python uses these names for specific reasons. Some of
the selected keywords are listed below in Table 3.2.
Table 3.2: Keywords in Python
Similarly, Python has many inbuilt functions like input(). So, Python does
not allow a program to use these function names liberally. Therefore, it is
better to avoid any names that can disrupt the normal execution of the
programs
Keywords
The command import keyword followed by keyword.kwlist shows the
list of keywords.
Literals
Constants or constant values are the other names for literals. Literal is one
way of specifying data value. These will be the values that never modify
while the program is executing. Literals are constants, such as 18, - 7.8 or
“ABCD Corp” or ‘Tamil nadu’. Literals in Python are numbers, text, or
other data types that depict values stored in variables. Thus, a constant can
be a number, text, or other fixed data, unlike variables whose values change
during the action.
Python manipulates constants by assigning them to variables, and later it
manipulates the variables.
Some of the literals are listed below:
String literals: A set of characters are called strings. Strings are the
basis of the manipulation of text and constant strings are very useful.
Strings are composed using single, double and triple quotes. Spaces,
special characters, keywords placed inside the quotes are treated as
literals.
Example 4: Illustration of String Literals
Numerical literals: Python supports three types of numerical literals:
integer, float, and complex numbers. Integers are whole numbers without
any decimal points like 25, 36, 1487. Floating-point numbers are
fractional numbers with decimal points, such as 12.3 and 3.14. These are
complex numbers that have real and imaginary parts. Some of examples
of complex numbers are 4+32j, and 7+2j. Usually i is used to represent
imaginary part of the complex number. In python, however j is used:
Example 5: Example of handling constants as a variable
Boolean literals: Boolean literals are False and True. True and False are
Python literals in Python which corresponds to binary 1 and 0
respectively
Named Constants
In programming, it is not desirable to use magical numbers like 9 or 13.715
as understanding/maintenance becomes difficult. Therefore, it is better to
declare these as named constants so that maintenance is easy. It makes the
code more readable and if there is a change in named constants(in the case
of multiple instances), it can be changed in one place. That is the major
advantage of named constants.
Punctuators or Delimiters
These symbols can serve as a separator of values or used to enclose a set of
values. Some of the delimiters are shown in Table 3.3.
Table 3.3: Punctuators
3.3 Python Block Structure
This section reviews python block structure and the importance of
indentation for writing python programs.
3.3.1 Importance of Indentation
Indentation refers to the whitespaces (Tabs and Spaces) in a python
program. Python program is a set of statements called a block. In other
words, a block is a group of statements. Languages like C use curly braces
to structure a block as shown in Example 7.
Example 7: Illustration of Indentation in C language
In Python, there are no curly braces. Instead, python uses indentation to
specify a block As said earlier, indentation refers to whitespaces at the
beginning of the python line The equivalent of python would be like, see
Example 8,
Example 8: Illustration of Indentation in Python
It can be seen that Python makes programs readable using the indentation
rule. Therefore, indentation is not a programming style but rather a
programming requirement. In Python, all statements with the same distance
from the left margin are considered the same block. For example, the
following python program as shown in Fig. 3.1 shows three blocks.
Fig. 3.1: Python Block Structure
The amount of whitespace used is up to the user. To ensure uniformity and
readability four spaces or a tab is often used. Space bar of the keyboard
should be used and mixing Tab and space bar is not desirable as it may
cause syntax error. It should be observed that the whitespaces within the
python statements are ignored.
Another example of indentation of a python program is given below. Just
ignore what is written inside and instead just observe the indentation of the
program.
Example 9: Illustration of blocks in Python
Indentation need
Python style guides govern indentations. PEP8 is the most widely used
Python style guide. It is found at http://www.python.org/dev/peps/pep-
0008/. It is better to review this guide before the Python code is written.
One of the common errors in Python is the indentation error. If the program
is not typed according to the indentation, one gets errors. The following
screenshot shows that the indentation is not correct.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 3.2: Indentation Error
3.3.2 Comments in Python
A critical aspect of programming languages is comments. Comments are
helpful to explain code and context. Readability is one of the most desirable
features of a code. Comments improve readability by explaining the logic
of a code. Python provides a single option to give comments using the
pound sign (#). This pound sign is also known as the Octothorpe or hash
symbol. One can place the symbol anywhere in the code. Comments are
meant for users and Python interpreter ignores the comments.
Placing # at the beginning of a line makes the entire line a comment. These
kinds of comments are called block comments.
Example 10: Block comments illustration as a group of single-line
comments
Another kind of comment is called inline comments, where this # symbol
can be put anywhere in the python code. This pound symbol ensures that
everything that follows becomes a comment.
Example 11: Illustration of inline comments
Another way to emulate multiline comments is to use multiline strings. The
multiline is done by using a triple quotation mark. The quotation mark can
be either a single quote or a double quote.
Example 12: Illustration of multline quotes
Python interpreters ignore any strings that were not assigned to any
variables. Therefore, this is an indirect way of creating a multiline
comment. This kind of triple quote is like Docstring. Docstring is used to
comment on a function and uses triple quotes. Docstring is dealt with in
Chapter 6 of the following book.
Comments
Comments are required for self-documentation.
Comments should express “Intent” and should not explain the intrinsic or
commentary of a programming language. A python statement, I = I +1,
requires no comment. Instead, if the comments are given for intent, such as
programming logic, it will be very useful. A summary comment, such as the
date on which the code is created, the author, and the purpose, is very useful
for documentation purposes.
3.4 Variables and Assignment Statement
One can visualize a variable as a container. As the name indicates, its value
can change during the program execution. In Python, everything is an
object. Therefore, a variable also is an object. Thus, the rules for forming a
variable are the same as the identifiers. The rules are stated below:
1. A variable can be of any length,
2. Variable names are case sensitive as abc, ABC, and abC are all different,
3. It should start with a character and follow by a character, digit, or
underscore symbol (_)
4. Spaces, dashes, punctuation marks, and quotations are not allowed
5. Keywords are not be allowed.
The following Table 3.4 summarizes the valid and invalid variable names.
Table 3.4: Validity of Variables
More notes about variables
There are some naming conventions. There is a need for naming
conventions as they are needed for consistency.
1. Camel case variables that do not end with capital letters (e.g., varA), the
capital letter only at the start of the second word, no dot, underscore,
numbers, dashes, or any special characters.
2. Snake case is where variables are all lowercase but determined by an
underscore. Use underscore between the words instead of space. The
example is
Interest_per_year
When all uppercase letters are used, it is called a screaming snake case.
Python prefers snake cases.
Pascal’s case is a compound word that uses capital words to define words
in the compound words. The first letter is an upper case-capital letter to
delineate as words.
Special identifiers:
The variables that start with a double underscore and end with a double
underscore are called dunder variables or magic variables that are reserved
by python designers.
Assignment Statement
One of the most critical statement in Python is the assignment statement.
The syntax of the introductory assignment statement is given as follows:
An expression can be a literal, a calculation, a functional call, or a
combination of all. For example, the following statement assigns a value of
3 to the identifier x.
>>> x = 3
Let us analyse the statement x = 3 further. Here, x is called L-value or
lvalue. Lvalues are the objects to which a value or expression is assigned.
Lvalue can appear on the LHS or in RHS also as in statement x = x + 5.
However, the value five cannot appear on the LHS as 5 is Rvalue or rvalue.
Rvalues are literals or expressions. The assignment statement assigns RHS
to LHS and expressions.
An alternative form of an assignment statement is to assign /compute
several values simultaneously. The syntax of simultaneous assignments is
given as follows:
A simultaneous (or multiple) assignments is a powerful statement. It allows
the initialization of more variables as shown below and swapping is done
using the simultaneous assignment.
Example 13: Illustration of multiple assignments
The state diagram of the statement x = 3 is given as shown in Fig. 3.3.
Fig. 3.3: State Diagram for the Statement x = 3
It can be seen that the variable is now pointing to a value of 3. When a
variable is first used, the name is created using the assignment operator =.
Suppose, if 5 is added to the value of x as x = x + 5, the resulting state
diagram would be as shown in Fig. 3.4.
Fig. 3.4: State Diagram for the Reassigned Statement x = 8
Dynamic Type Checking
Type checking is also known as typing. Most of the programming uses
static typing. Here variables are checked in compilation. In static typing, a
variable needs to be declared before it is used. Example is C Language.
Python is a dynamically typed language. It means that Python does not
require one to declare variables. So, x can be modified to point to a string
object also as shown below:
The above statement results in the state diagram as shown below in Fig. 3.5.
Fig. 3.5: Illustration of Dynamic Typing
This concept is called dynamic Type Checking and is a significant
advantage. However, while it offers many advantages, one needs to be
cautious as it may involve incompatible operations. For example, x = x / 2
may result in an error as x is a string now and operation-like division that
involves a string is not correct, hence, one needs to be cautious.
Namespaces
Python interpreter uses a unique structure called namespace using which it
maintains a list of names and their associated values. Each name is
associated with a value. In other words, the name is the alias for the value.
Python updates the values and names during its course of action. For
example, it updates the list when a new variable is created. Similarly, it is
deleted in the namespace when the variable is deleted.
3.5 Python Objects
Let us return to the concept of objects. In Python, all are objects, variables,
functions, or even values. An object is linked with three concepts—Identity,
zero or more names, and a set of attributes.
Let us explore these concepts in detail now:
1. Identity of the object
Identity refers to the memory address. The memory address does not
change once it is created and is permanent. When an object is created, it
receives a unique identification number. For example, when an object is
assigned to a variable, some memory address is allocated to the object.
The python function to get the memory address is called the id()
function. This is a built-in function. For example, for the Python
statement i=i+7, the id(i) gives the identity of the variable i.
Example 14: id() function
It can be observed that the increment results in creating a new value of
20 and another unique address. Namespaces track all.
Notes about IDLE
One related concept is called the mutability of variables. Python supports
two types of objects.
1. Immutable–A variable does not allow values to be changed after they
are created or assigned. These types of objects are called immutable
objects.
Other immutable objects are data types like int, float, bool, strings,
and tuples.
2. Mutable objects allow changes after they are created and assigned.
Examples of mutable objects are lists, dictionaries, and sets.
2. Names
A name is binding to a value. Values are manipulated using a variable
name. The name is getting bound to the value using the assignment
operator as i = 5. Here i is the variable, 5 is the value, and = is the
assignment operator. As discussed earlier, in the statement x = 3, x is the
name of the value 3. Names are handy for programmers as it is a way to
manipulate values. A value can have multiple names also.
For example, consider the following code segment
The equivalent state-space diagram is given below as in Fig. 3.6.
Fig. 3.6: Illustration of multiple names for a Value
It can be observed that now x and y point to the same value. This is
because Python does not make a copy. All they do is label and relabel the
current value. Thus, a value can have one or more names as well.
3. Attributes
An attribute is the set of properties of an object. It is the data type and
operator. This is the topic of discussion in the next section.
3.6 Data Types in Python
A data type indicates what values a variable can hold, and an operator
indicates the kind of operations performed on the values. Attributes are
information about objects. One crucial attribute is called its data type and is
the subject of the next section. The data type is a kind of value that is
assigned to variables. For example, x = 3 makes x an integer variable as 3 is
a whole number and an integer (as it is not a fraction number). Python
supports many types of data. They can be classified as basic data types and
collections. Let us discuss them now:
3.6.1 Basic Data Types in Python
Python supports numbers, strings, and floating numbers. The classification
of data types is shown in Fig 3.7.
Fig. 3.7: Classification of Data Types
One can check the data type using the command type (). It is often useful to
check the data type of a value or variable. This is done as follows:
Example 15: Illustration of type
This command is handy when the exact type of data is unknown. Now let us
explore the data types:
1. Integers
Int or integers are primary data types. Integers are whole numbers. It can
be a positive or negative number without a decimal point. The types of
integers that Python can recognize are
1. Decimal integers: These are digits whose base is 10. Examples of
decimal numbers are 45, –96, 128, and 786.
2. Octal Integers: These are digits whose base is 8. These numbers are
indicated by 0O or 0o (zero followed by small/big 0)
Examples of octal numbers are 0o28 and 0O77.
3. Hexadecimal numbers: Hexadecimal numbers are numbers whose
base is 16. These numbers use the prefix 0X or 0x (zero followed by
small/big x)
Integers
Integer numbers can be of any length limited only by the physical
memory and by the python language. Python can support long numbers.
The word googol is used to represent a is 1 followed by hundred zeroes.
One can play with googol in Python. Ten-power googol is called
googolplex. Python can perfectly handle googolplex. While other
languages like C focus on double and float, Python primarily focuses on
int. Python 2.x long are replaced by int and are limited only the by
physical machine.
Real numbers are stored with double precision depending on the
hardware. It determines precision. Scientific notation is used for very
large and very small numbers
2. Float number
Fractional numbers have a decimal point are called float numbers. Float
numbers can be positive or negative numbers. Examples of floating
numbers are
Float numbers also can be of unlimited range depending on the machine.
3. Complex numbers
Complex numbers are extensions of floating-point numbers with real and
imaginary parts. Complex numbers are used heavily in the scientific
domain. The real and imaginary parts of the complex number can be
retrieved as shown below:
Example 16: Complex Numbers
2. Boolean types
A Boolean value has a data type called bool. A boolean type refers to True
and False. A Boolean object can have only two values – True and False. It
can be represented by integers as 0 and 1 as well. Therefore, one can create
a Boolean variable that can hold Boolean values.
Example 17: Illustration of Boolean Variables
The python statement creates a variable called time_up and sets its value to
True. True and False are built-in Python values. There is a Python function
called bool that can convert values to Boolean also. The zero or empty
string value is False, and the rest of the values are True.
Example 18: Illustration of Bool
A Boolean expression returns a value that is true or false as one knows that
an expression involves operators and operands.
5. None Type
None is a special data type that indicates that there is no value. In all other
programming languages, it is called Null. One can check its type as
The default return value of a function is None.
3.6.2 Collection Data Types
Containers are a way to group objects of a similar kind. This is also known
as collections. The objects may be ordered or sometimes unordered. Two
main kinds of collection types are sequence and another type called
mapping.
A sequence is an ordered collection of values. Three sequence types are
string, list, and tuples.
1. A string is a sequence of characters
2. A list is a sequence of values
3. A tuple is a sequence of values like lists but a tuple is immutable
All elements in a sequence are stored as the contiguous series of memory
locations that are indexed from 0 onwards. The last element in the index is
n-1. It can be numbered backward as -1 to -n also. For example, a string can
be of any length, and physical memory can limit that. Strings are stored like
this in Table 3.5.
Table 3.5: String Representation
Common Operations on the sequence are
1. Membership operations checks whether an element is present or not
2. Operations like concatenation, where two sequences can be combined as
seq 1+ seq 2
3. slicing operations like [start index: end index]
4. Built-in operations like shown in Table 3.6
Table 3.6: Validity of Variables
1. Strings
The string is an example of a sequence collection data type. It is a collection
of characters delimited by single, double, and triple quotes. It can include
letters, numbers, punctuations, and many special/unprintable characters.
Python allows us to specify strings using
1. Single quotes
2. Double quotes
3. Triple quotes.
Some examples of strings are “lion” and ‘14.0’. It is mostly preferred to use
single quotes to reduce typing effort, and triple quotes are used for
comments. Strings are immutable as one can change individual elements
and the size of the list. Either can access the characters by the positive index
or negative index.
Strings support many operations. Some of the elementary operations using
arithmetic operators are shown below in 3.7.
Table 3.7: String Operation
2. Lists:
A list is a set of items or elements. All these items are separated by commas
and enclosed within square brackets. In all aspects, lists are similar to arrays
but can have non-homogenous content.
To create a list, one can just enclose the sequence of objects in square
brackets.
For example, a list can be a collection of integers, strings, and floating
values.
Example 19: Illustration of List operations
Here, lst1 is a list that consists of elements within “Hello”, ‘world’, 13,3.14.
A list is a sequence type as the lists are indexed, and their contents are
accessed. For example, lst1[2] fetches 3.14. The index of the first element is
0. The index of the last element is n-1.
Example 20: Illustration of Slicing in Lists
Lists are mutable as the items can be changed. Some of the list operations
are adding an element, combing lists, insertion, deletion of items, and
deletion of lists. Essential list operations are discussed in Chapter 7 of this
textbook.
3. Tuples:
A tuple is a collection of items or elements. A tuple is also a sort of list but
the main difference is that tuples are immutable compared to mutable lists.
In other words, one cannot change the elements or size in a tuple. The tuple
items are enclosed in round brackets and separated by a comma. The empty
tuple is (). The tuple with a single element is called a singleton. There is no
restriction on the tuple content and it can be another tuple also.
To generate tuples, just enclose the sequence of objects in () and tuple
support indexing/slicing, and one cannot change the elements. Some of the
examples are,
Tuple supports operations like indexing, slicing, combining tuples, deletion
of tuples, insertion, and deletion of elements in the tuple. Further details of
tuple operations are discussed in Chapter 8 of this textbook.
4. Bytes and Byte arrays
Bytes is an immutable object that stores a long sequence of values from 0 to
255. These are used to print ASCII and Unicode strings. One can use this as
an index to access the bytes. This data type is useful for encoding ASCII,
Unicode 0 UTF-8, UTF – 16, images such as JPEG, PNG, and audio files
like MP3. The following segment is an example of how bytes can be
defined.
Example 21: Illustration of byte
# Store in a byte as indicated by the prefix b
The byte array is like a byte type useful for encoding images, Unicode, and
ASCII strings. These are similar to byes, except that byte arrays are
immutable.
5. Dictionary
A dictionary is an unordered collection of items in the form of keys and
values A dictionary is called as a mapping type. A map type consists of a set
of element pairs. The first element of the pair is called a key, and the second
element is called a value. To find the corresponding equivalent values, one
can search for a key or value.
A dictionary is created by a sequence of key-value pairs with curly brackets.
It uses curly braces {} and a colon to separate key-value pairs The key
should be an immutable object. Dictionary facilitates the addressing and
changing of items using assignments. Some examples of dictionaries are
given below:
Example 22: Illustration of a Dictionary
where ‘abc’, and ‘def’ are keys and ‘123’ and ‘456’ are associated
passwords. The colon (:) is used to separate the key and value. {} are used
to enclose the whole dictionary structure.
Some of the important characteristics of the dictionary are
1. The dictionary keys should be unique.
2. No two elements can have the same value.
3. Keys also should be immutable, and
4. Values do not have such restrictions.
The dictionary operations are the creation of a dictionary, adding elements
to the dictionary, removing the elements of the dictionary, and deletion of a
dictionary.
6. Sets/ Frozen Sets
A set is an unordered collection of zero or more elements with no
duplicates. As in mathematics, a set is an unordered collection of items with
no duplicate items that use curly braces for sets- {}. The set is mutable and
unique. The following is a set of operations is as shown in Table 3.8.
Table 3.8: Examples of set operations
An example of the set is given below.
Example 23: Illustration of Sets in Python
It can be observed that the duplicate is removed. In addition, there are two
types of sets—mutable sets where one can add or remove elements, and
frozen sets where after its creation process, the items cannot be changed.
Some of the set operations are the creation of a set, adding an element into
the set, removing an element into the set, the union of two sets, the
intersection of sets, and set difference.
Frozenset is similar to sets except that frozensets are immutable.
3.7 Operations
One of the other attributes of the objects is operations. Operations are
performed on operands. For example, in the expression ‘a+b’, ‘a’ and ‘b’
are called operands, and ‘+’ is the operator. Operations depend on the data
type. So, one needs to know the kind of operations that can be applied to the
data.
Operations
An operator applies an operation over operands to give a result as output.
The operator works over operands. For example, in expression ‘x * y’,
where, ‘x’ and ‘y’ are operands and ‘*’ is an operator. There are two types
of operators. They are
1. Unary operator
2. Binary operator
3. Ternary operator that works with three operands
A unary operator takes only one operand as input. For example, ~a is the
complementary operator that takes only one operand a. Binary operators
take two operands as input and return the result. For example, addition is a
binary operator, a + b, as it takes two operands and returns the result. A
ternary operator takes three arguments.
One cannot use an operator with incompatible data types and the python
interpreter gives an error. The following screenshot illustrates this.
The classification of operators is shown in Fig. 3.8.
Fig. 3.8: Classification of Operators
3.7.1 Arithmetic Operators and Expressions
Like any programming language, Python supports arithmetic operations.
Arithmetic operations that can be applied to integers and floating-point
numbers. Arithmetic operators are listed below in Table 3.9.
Table 3.9: Arithmetic Operators
+,-,*,/ are frequently used arithmetic operations. Some of them are shown
below:
Example 24: Illustration of Arithmetic Operators
// is an operator called an integer division operator. It returns the integer
quotient of an integer division. . This operator can be used for floating
numbers. The modulo operator % is used to return the remainder of integer
division like 13/3 = 1 and the operator, % operator can be used for floating
numbers also like 17.0%4=1. The exponential operator (**) is used for
exponentiation as 2 ** 4 is 16. Arithmetic operations on integers and
floating-point numbers involve operands and operators. The types of
arithmetic expressions are
1. Constant expressions
For example, 7+3
2. Integer and floating-point expressions involving variables
For example, a*b/2
Operator Precedence and Associativity for arithmetic Expressions
Operator precedence and associativity are two major concepts in the
evaluation of expressions.
Operator precedence:
If there is one expression with more than one operator with different
precedence, then the concept of operator precedence is coming into the
picture. Operator precedence is the way operations are carried out.
Operator associativity:
If an expression contains two or more operations having the same
precedence, then the concept of associativity is coming into the picture.
There are two types of associativity: left-to-right associativity and right-to-
left. All expressions are evaluated from left – to – right except
exponentiation, which is evaluated from right – to – left.
The operator precedence within the operators is given in the following
Table 3.10.
Table 3.10: Operator Precedence Table
This is summarized by the rule PEMDAS—Parenthesis, Exponentiation,
Multiplication, Division, Addition, and Subtraction.
Problem 1: Evaluate the following arithmetic expression 20 + (3+12) +2
** 4 using the operator precedence rules.
Solution
20 + (3+12) +2 ** 4 as brackets have higher precedence, it is evaluated
as follows:
20 + 15 + 2 ** 4
then exponentiation is having more precedence. So next exponentiation is
evaluated as follows:
20 + 15 + 16
hence, the final value is 51.
Problem 2: What is the output and data type of the result?
Here f and d are floats. Therefore, the result would be a float of 7.0. It is
added to 3, that is int. The result would be float 10. This is divided by 4,
which is int. The result would be a float of 2.5.
Associativity
If an expression has operators of the same precedence, then the arithmetic
expression mostly works from left to right.
Problem 3: Evaluate 20 * 3 * 2
All operators is having the same operator precedence. So, this evaluates to
(20 * 3) * 2 = 120.
3.7.2 Compound Assignment Operators
The compound assignment operators are the second category of operators.
For example, x = x+y can be written as x+=y. The compound assignment
operators are given below in Table 3.11.
Table 3.11: Compound Arithmetic Operators
3.7.3 Boolean Operations
There are three types of Boolean operators that Python supports. They are
listed in the following Table 3.12. These operators help to combine Boolean
expressions into complex expressions.
Table 3.12: Boolean operators
The truth tables of these logical operations are given below in Table 3.13(a-
c).
Table 3.13: Boolean Operators
Example 25: Illustration of Boolean operations
It can be seen that when the value ‘0’ is used with true, Python returns the
value as true as 0 is made false. Bool is also used to indicate empty strings.
The operator precedence among logical operators is given below in Table
3.14.
Table 3.14: Operator precedence among logical functions
3.7.4 Relational Operators and Boolean Expressions
Logical Expressions involve relational operators and logical operators. Any
expression that contains a comparison operator returns a Boolean value. For
example, x>=9 can have only two possible values, true and false. Some of
the comparison operators are listed in the following Table 3.15.
Table 3.15: Comparison (or Relational) operators
Example 26: Illustration of Relational Operators
As discussed earlier, a condition for a decision can be created using a
relational operator as shown below:
Example 27: Illustration of Conditions
Depending on the value of the hour, the value of the meeting would be set
as True or False.
All relative operators are having the same precedence as relational
operators are not associative.
The operator precedence among logical operators is given below in Table
3.16.
Table 3.16: Operator precedence among relational operators
3.7.5 Bitwise Operators
These operators deal with binary bits of numbers. It operates bit by bit. The
different types of bitwise operators are given below in Table 3.17.
Table 3.17: Bitwise Operators
The equivalent of digit 6 is 0110, and the equivalent of digit 7 is 0111.
Entering integers to their equivalent binary digits is possible using a built-in
function called bin(). The usage is shown below:
Example 28: Illustration of Bitwise operators
It can be observed that the prefix ob indicates that what is followed is a
binary number. Bitwise AND operator takes digits and converts them into a
binary equivalent. Then, if both arguments are one, the result is 1. Else, it is
zero.
Problem 4: Find the bitwise and of 10 and 9.
Solution
Bitwise OR operator takes digits and converts them into a binary
equivalent. Then, if either of the arguments is 1, the result is 1. Otherwise, it
is zero.
Problem 5: Find the bitwise AND of 10 and 9.
Solution
Bitwise Complement operator(~)
The complement of 1 is 0, and the complement of 0 is 1.
Example 29: Bitwise Complement operator
Bitwise Left shift operator(≪)
This operator shifts the bits to the left. For example, the command
Shifts the binary representation of 15 right by two bits. The empty cells are
filled with zeros.
Bitwise Right shift operator(≪)
This operator shifts the bits to the Right, and the empty cells are filled with
signed bits.
Shifts the binary representation of 15 left by two bits. The empty cells are
filled with zeros.
The operator precedence for bitwise operations is shown in Table 3.18.
Table 3.18: Operator Precedence Table
3.7.6 Identity and Membership Operators
Complex data types do support Identity operators and Membership
operations. There are two identity operators. They are listed in the
following Table 3.19.
Table 3.19: Operator precedence among identity operators
Identity operators compare two memory locations of objects. The operator
“is” produces True if two operands t1 and t2 are referring to the same
object.
Example 30: Identity Operator Illustration
The operator “is not” produces True if two objects are not referring to the
same object.
Example 31: Identity Operator Illustration
Membership operators test whether the given object is in the collection or
not. The operator precedence among membership operators is given in
Table 3.20.
Table 3.20: Operator precedence among Membership operators
Example 32: Sets Illustration
It can be observed that the operator “in” produces True if the object is
present in the collection. For example, red is present. The operator ‘not in’
produces True if the object is not present in the given collection.
Example 33: Membership Operation
The operator precedence of the identity and membership operators are as
given below in Table 3.21.
Table 3.21: Operator Precedence Table
Operator precedence of all operators
The operator can be left-associative or right-associative. Left-associative
means that the operations are carried out from left to Right. Similarly, right-
associative means that the operations are carried out from the Right to the
left.
Operator precedence charts are given as follows in Table 3.22.
Table 3.22: Operator Precedence
Operator precedence charts are given as follows in Table 3.23.
Table 3.23: Operator Precedence
3.8 Type Conversions
There are certain limitations to the data types. For example, one cannot do
'Hello"+4, because one is a string type and another is an integer. For
example, input() returns a string, so one needs to convert that to an integer
to perform operators. This concept is called type conversion.
There are two types of data conversion. They are
1. Implicit conversion (or Type Coercion)
2. Explicit conversion (or Type Casting)
Implicit conversion
Sometimes, Python implicitly converts a data type during compilation or
runtime. This is called type coercion. For example,
Python automatically converts an integer to a floating point to preserve the
fractional part.
Explicit conversion
The explicit conversion of a value from one data type into another data type
is called typecasting. There are many built-in functions available for type
conversion. Some of the built-in functions are listed below for integers in
Table 3.24. Factory functions create variables directly. Some of the
examples of factory functions are int(), str(), list(), tuple(), and dict().
Table 3.24: Functions for conversion to Int type
The conversion of integers and strings to float is by using the function
float()in Table 3.25.
Table 3.25: Functions for conversion to floating type
The type conversions for integers and floats to strings are given below in
Table 3.26.
Table 3.26: Functions for conversion to string type
Type conversions are available for collections also. Some of the crucial
functions are given below in Table 3.27.
Table 3.27: Functions for conversion to containers type
Notes about Type casting
Complex types cannot be converted to int or float.
Any object can be converted to strings provided the string representation
gives a meaningful code.
Integer numbers can be of any length limited only by the physical memory
and by the python language.
3.9 Simple input/output Statements
So far, all the values are assigned to variables using the assignment
operator. However, a program needs to communicate with the external
world through I/O devices such as a keyboard and console. The program
should be able to interact with the user through the keyboard and should
inform the results of the user through the console. Input function input() and
output function print() are used by python as I/O statements. Let us discuss
them now.
3.9.1 Reading Numbers from the Keyboard
One can read the input from the console using the input() function.
The syntax of the statement is given as
One can also accept a string using the input() function. The syntax is given
as
The following code illustrates this usage:
Example 34: Illustration of Input statement
The code prints the user-entered string.
Let us try the following code:
Example 35: Illustration of Type Error
The above code results in an error. Why?
The reason is that Python reads numbers also as a string. Input() produces
an only string. Therefore, even if the user input is an integer or float, it read-
only as a string. The only way to avoid it is by converting a string to an
integer. This is done as follows:
Example 36: Illustration of input statement
In this case, one needs to know what the user will type, whether it is an
integer or float. Also, there is a problem with truncating a float number. One
way to avoid that is to use the eval function. The Eval function takes a
string and treats it as a python expression.
The type before the input() function can be replaced by the eval function as
shown below:
Example 37: Illustration of eval
In this case, irrespective of what user types, int, float, string, or complex,
Python accepts a number and determines the type of value entered by the
user.
3.9.2 print() Statement
The contents can be displayed on the screen using a print statement. A
python print statement converts all python objects into a string and writes it
on in general i/o stream. The format of the print statement is as follows:
The argument can be integer, float, string, or any other data type. Some
examples of the print statements are given below:
Example 38: Illustration of print
It can be observed that double quotes or single quotes do for printing more
than one argument also. All that is to be done is to separate the arguments
by a comma. Similarly, the numbers can be printed directly as a print(100)
and variables can be printed directly as a print(x).
Notes about strings
If a particular character needs to be printed like This is Joyce’s car, then the
print statement should use an escape sequence (\’) or double quotes as
shown below.
Print statements can be used for printing multiple arguments also. For
example, the following code segment illustrates how an integer and a string
can be combined.
It can be seen that a space is added between these variables by default.
Also, an empty print() statement create new line \n character.
Extensions of Print Statement
There are two important flags - sep, and end. This is given in the following
Table. 3.28.
Table 3.28: Extensions of print Statement
The Python print() function has an attribute separator (Sep). The default is
space. In other words, when multiple arguments are printed, the default is
space. One can change this default with this flag – separator. For example,
consider the following segment to illustrate the use of the flag ‘sep’
Example 39: Illustration of sep
It can be observed that the separator flag ‘sep’ can be used as shown below
to print strings as per requirements.
“end” is another useful flag. By default, print statements use a new line for
printing before the arguments. This behavior can be changed using the flag
called “end”. The following python program illustrates this.
Example 40: Illustration of end (Listing1.py)
The above script is stored as listing1.py and executed as follows:
It can be observed how the end flag can be used to create a comma between
operands a and b.
Flush is another useful flag. A buffer stores temporarily all data in a buffer
till \n arrives at the end of that statement. If it is not so, it waits. One can
directly print the message without having to wait
3.10 Formatting Using Print Statement
Many times a simple printing of values is not sufficient. Many projects may
require fancy printing as per the requirements. It is called formatting. All
variables and constants need to be converted to string type for printing
purposes. The following methods are the ways to do that.
The following section reviews some of the string formattings that is
available for us.
3.10.1 Built-in Methods
These are sequences that start with a slash character. These characters are
interpreted at the execution time and have implementation-defined values.
Some of the unprintable characters can be printed using escape sequences.
Some of the character sequences are given in the following Table 3.29.
Table 3.29: Escape characters with descriptions
For example, \r is a carriage return, print a line over and over.
3.10.2 String Interpolation (old Style Formatting)
String interpolation is a programming language C-type old-style formatting.
String interpolation is a way of combining a string with constants or
variables at specific locations to create a user-specified output. This
replacement part is called a placeholder. Some of the standard placeholders
are given below in Table 3.30.
Table 3.30: Useful String Format symbol with conversion
% is an operator that replaces the placeholders. Python supports %
character to format the strings by replacing the placeholders with constants
and variables. The syntax of the string interpolation is given as follows:
Values are the arguments supplied to the format string so that the output is
as per the user’s specifications. Sometimes, it is necessary to format a long
number. For example, x = 10.3333333 can be formatted according to the
user requirement, say 10.33. It can be observed that two digits after the
decimal point is called precision.
For example, The above formats the floating point by printing two digits
after the decimal point as the string-specified point. In print, it can be
shown as
Example 41: Illustration of String Interpolation
The following python program shows how an integer and string can be
combined in the string interpolation method.
Example 42: String Interpolation
3.10.3 String Formatting
String formatting is the second way of formatting; here, there will be a
format string and arguments. The format of this string formatting is given as
The placeholders are in the form of {}. This curly {} braces are called
named replacements or placeholders. The format string format insert the
values into the string placeholder. Now, the print() function does not format
the strings simply but instead searches for the placeholders, and if it is not
available, it prints as it is without any formatting.
This formatting helps create emails, form letters, or web pages. This kind of
formatting is compelling and flexible way of formatting.
The following is an example of string formatting.
Example 43: Formating Illustration
Here, there is no placeholder, and hence, the string is printed as it is.
Python allows the replacement fields and placeholders to put variables and
concatenate strings for printing. The following example illustrates how it is
done.
Example 44: Old Style Formatting (Listing2.py)
The following python program illustrates how built-in formats can be used
for printing.
The above script is stored as Listing2.py and executed as follows:
Example 45: String Formatting (Listing3.py)
Instead of sending variables, one can pass index or positional order. In other
words, the arguments for the format can be identified by a position. The
arguments are passed as tuples and indexing starts from 0. The placements
are implicitly numbered from 0. The arguments would be substituted by
removing the placeholder. For example, consider the following code
segment.
Example 46: Illustration of String Formatting
In this case, ‘Name’ replaces the argument {0} and the word ‘is’ replaces
the argument {1} wherever that numbered placeholders are present. The
arguments can be out of order. For example, if the arguments are swapped
as shown above.
Or, the arguments can be presented multiple times as shown below:
Example 47: Illustration of String Formatting
The format can provide field width and precision. It can be specified as :n,
where ‘n’ is the number of characters in the field. If the data is having less
than the character width, the extra spaces are added to the right.
3.10.4 Formatted String Literals (F-string or f-string)
F-strings are also known as literal string interpolation. F-strings are string
literals that start with f or F at the beginning of the string. {} are
placeholders that hold the expressions that are replaced by the values.
The best way to format the string is with an f-string. Python f-string is the
most recent Python syntax for formatting strings, since Python 3.6. Python
f-strings make string formatting in Python faster, more readable, more
concise, and less error-prone. It’s also quicker than the other. One of the
primary benefits is that it simplifies string interpolation. It also insists on a
simple way to stuff a Python expression into a string literal for formatting.
The following python program illustrates the usage of f-strings.
Example 48: Illustration of F-String
Some examples are shown in Table 3.31.
Table 3.31: Some examples of F-string or f-string
3.11 Built-in Functions
One need not have to write code for everything. For example, if one needs
to find the square root of a number, the square root function need not have
to be developed from scratch. Instead, it should be available as a library
function to be called whenever it is necessary. This approach improves the
modularity of the program.
Python supports many predefined modules. These modules are called
standard library modules. A module is a python program (with an extension
of .py) that consists of variables, class definitions, statements, and
functions. Each of these modules is called a standard library module. In
addition, each of the modules has many predefined functions called library
functions.
A function is a group of statements for doing a task. The function can be
called from a program, so problem handling becomes more accessible and
reduces the code line. Also, functions facilitate easy options.
A function can be built-in function, module, or user-defined. A module is a
collection of related functions. Similarly, a user can develop a function from
scratch according to his need. Built-in functions are predefined functions
that are already available in Python. Already, Many such functions as
input(), eval(), print(), int(), float() are already discussed in this chapter. For
example, the math module has collections of functions related to math
computations. To access these functions, one must import the modules into
the python environment. Then the functions that are available in the
modules are accessed by membership functions. one such example is given
below:
Example 49: Illustration of math function
One can import math modules along with their functions also. In that case,
the specifications of modules can be removed as shown below:
Example 50: Math Function
It can be observed that math.sqrt is avoided. This is because the math
module has many predefined functions shown in Table 3.32.
Table 3.32: Basic Built-in Functions
In this way, Python extends its functionalities of Python effectively.
Complex expressions can be computed using standard modules and
functions. For example, the expression, , can be computed by
Python as
Example 51: Illustration of Python Expression
Thus, Python can be used as a powerful calculator for computing
expressions.
Summary
1. Every python program is a set of statements. A statement is a logical unit
of instructions to a python interpreter.
2. The statement need not have to be followed by a semicolon.
3. Python has a python character set and escapes sequences.
4. A token is the smallest lexical unit in a program. Some of the tokens are
identifiers, keywords, literals, and punctuations.
5. A name given to variables, models, and functions is called an identifier.
In addition, some of the words that Python uses is called reserved word.
6. Literals are constants. The different types of literals are string literal,
numeric, complex, and Boolean literals.
7. Comments are necessary, and there are two types of comments: block
and inline comments.
8. A variable is an identifier. A value can be assigned a name using an
assignment statement.
9. An input statement is used to get data from the keyboard. Similarly, the
contents can be displayed using a print statement on the screen.
10. Print statements can control the output using the flags, separator – sep,
and terminator – End.
11. A data type is a kind of value that is assigned to the variables. The basic
data types are int, float, complex, and strings. The collections are lists,
tuples, sets, and dictionaries.
12. Integers are whole numbers without fractions. Boolean types are True
and False. Floating numbers are numbers with a fractional point.
Complex numbers have real and imaginary parts. Strings are a group of
characters.
13. Containers contain other objects. The primary containers are lists and
tuples. The other containers are dictionaries and sets.
14. Lists are similar to arrays but can contain non-homogenous contents.
15. The difference between a tuple and a list is that the tuples are immutable
while the lists are mutable.
16. Dictionaries are robust data collections that can store key-value pairs. A
set is an unordered collection of elements.
17. Type conversions are methods to convert one data type to another data
type.
18. Formatting a text is possible using a programming language like the %
operator and the new type F-string.
19. Operators act on numbers, strings, or any data types. Some of the
operators are arithmetic, compound assignment, relational, logical, and
bitwise operators.
20. Arithmetic operators apply addition, subtraction, multiplication, division,
exponentiation, integer division, and remainder. Compound assignment
operators are shorthand operators that combine operation and assignment
symbols.
21. Relational operators perform comparisons and return a Boolean value.
Logical operations are not, and, or. Finally, bitwise operators deal with
binary bits of operands and operate bit-by-bit.
22. The bitwise operators are the bitwise complement operator, bitwise left
shift operator, and bitwise right shift operator.
23. The other types of operators are identity operators and membership
operators.
24. Expressions are a combination of symbols that may be operand and legal
operations.
25. Examples of expressions are constant, floating point, relational, logical,
bitwise, and assignment expressions.
26. Operator precedence is significant and dictates how an expression should
be evaluated. Associativity comes into action when operators all have the
same operator precedence. Associativity is left to right and right to left.
27. Python comes with predefined functions called built-in functions.
28. Python interactive environment helps to operate it as a calculator.
Glossary
Program – A set of statements to perform a task.
Statement – A logical unit of instruction.
Source character – A set of characters derived from alphabets
(uppercase, lowercase) and special characters.
Escape sequence – Escape sequences start with a backslash character.
Identifier – An identifier refers to a variable, function, or module name.
Keywords – Some words are reserved for a python interpreter called
keywords.
Literals – A literal is a constant or value assigned to a variable.
String – A set of characters are called strings.
Numeric literals – A number that can be an integer, floating, or complex
number.
Integer – A whole number without a decimal point.
Floating number – A number that has a decimal point.
Complex number – A number that has a real and an imaginary part.
Variable – A variable is an identifier that can hold a value.
Data type – A data type is a kind of value that is assigned to a variable.
List – A list is similar to an array that can contain non-homogenous
elements.
Tuple – A tuple is a list that contains immutable elements.
Type conversion – The explicit conversion of a number from one type to
another type is called type conversion or coercion.
Formatting – A way to convert the printed content according to the user’s
requirement.
Operators – A operator performs an action using an operand.
Expression – An expression is a combination of symbols that may be
operand and legal expressions.
Operator precedence – A rule that dictates how operations should be
carried out.
Built-in functions – A function that comes as a default function with the
Python interpreter is called a Built-in function.
Conceptual Questions
1. List out the rules for naming an identifier
2. Is there any difference between an identifier and a variable?
3. What is a literal?
4. What is the difference between block and inline comments?
5. What is the significance of a unique literal “None”?
6. List out the types of operators.
7. What is punctuation?
8. Distinguish between lvalue and rvalue?
9. What is meant by dynamic typing?
10. What is a namespace?
11. Give the complete syntax for the print statement.
12. What is the role of sep and end in the print statement?
13. Is there any difference between a statement and an expression?
14. How can Unicode be used as part of a Python string?
15. Explain the concept of block/code block in Python.
16. What are type checking and type coercion in Python?
17. Distinguish between mutable and immutable strings.
18. What are collections? List out the types that are in collections.
19. What is a chained comparison operator?
20. What is operator precedence? List out the rules for operator precedence
for arithmetic expressions.
21. List out some essential math functions of the math module.
22. What is a bug? List out the errors in the Python script.
Try out Questions
1. Specify which of the following identifiers are valid or invalid – Justify
1. serial_no
2. 123_no
3. no_Hours
4. no hours
5. true
6. while
2. identify the python statement for the following sentences.
1. Assign 15 to days
2. Assign “Stella” to name.
3. Write the output of the following segments
1. i = 0
i= i+7
print(i)
2. j = k+7
print(j,k)
4. Find out which of the following assignment statements are right or
wrong – Justify
1. i=j=0
2. 10 = k
3. m+=1
4. z = 068
5. How would python evaluate the following expression
1. 10+40*3
2. (20+30)+40*3
3. 15.0/3+7
6. Predict the output of the code
1. x = 40
y = x+7
print(x,y)
2. print(x=y=7)
3. a = 7
b = int(a/2)
print(a,b)
7. Find out the type of the following
1. type(0.7)
2. type(0.3)
3. type(8/2)
4. print(“Hello”,"name")
5. a = "abc"
b = "def"
print(a+b)
8. What is the output of the following code?
1. L1 = [10,20,30]
L1[1] = 70
print(L1)
2. nset = {10,20,30,30,20}
print(nset)
9. Find the value of the expression.
1. 7+4+3
2. 4/2 +3
3. 4 * * 3
4. 4 * * 0.5
5. 7.2 % 4
6. 7/3
7. 6.5//3
10. Find the output of the following relational expressions.
1. 7<3
2. (7<3) and (3<2)
3. L1 = [1,2,3]
L2 = [4,5,6]
L1 is L2
4. Given i=4, j=7, k=7
1. i<k
2. i==k
3. j!=k
4. j<=i
11. Find the order of evaluation
1. 5*7 <= 4 ** 16
2. a =7;b=7
a==b
3. a=7;b=7
a=b
12. Find out the result of the following segments
1. not 7
2. not -3
3. not(7==3)
4. 13&2
5. 12 | 4
6. 13^3
7. ~7
13. Find the order in which expressions would be evaluated
1. 7 ** 8 + 2
2. a = 10
b = 20
c = 30
print(a<b<=c)
14. Perform the type casting
1. int(3.4)
2. float(2)
3. complex(3)
4. str(3)
5. bool(1)
15. Write the Python expression for the following mathematical expression
1.
2.
3. x + tan x/cos x
4. |x2 – 1|
16. Find the errors (if any) in the following segments
1. name = "Ram"
name[2] = "b"
print(name)
2. i = input(‘Get name’)
j = i+10
print(j)
Programming Exercise
1. Write a Python program to get the values for a, b, c and evaluate the
following expression
2. Find out the temperature for 12 months of the year and find out the
year’s average temperature.
3. Take a three-digit number and find the reverse of the number.
4. Take the customer’s first name and last name and print them as last name
first followed by the first name.
5. Get the principal amount, interest, and year and find the simple interest
and compound interest.
OceanofPDF.com
“Talk is cheap. Show me the code”
Linus Torvalds
CHAPTER 4
Decision Structures
This chapter aims to provide insight into how to use decision statements
that allow the execution of a selected set of instructions to accomplish the
stated goal.
Learning Objectives
At the end of this chapter, the reader will be able to
To understand the need for decision structures
To formulate the conditions
To use the decision structures using the if-statement
To know the if-else statement and nesting of conditions
To use if-elif statements
4.1 Need for Decision Structures
An algorithm is a complete sequence of actions to solve a given problem if
followed exactly. Algorithms are built using three basic building blocks:
sequence, selection, and repetition.
Sequence: A control flow where the statements are executed in a
progressive order (usually from first to last), without decisions or
repetition.
Selection/Decision or Branching—A decision is made like condition
checking to decide execution of one or more statements. It is useful to
choose one action from several alternative actions.
Iteration/Repetition or Loop—A Loop is one or more instructions the
computer repeatedly performs. The execution of the loop structure is
repeated many times if the conditional is true in the main program till a
condition is satisfied.
This chapter deals with decision or selection structures. What is a decision?
In the real world, we make many decisions. For example, if it is raining, we
take an umbrella with us. Otherwise, we keep it in the home. This is a
decision where the condition is raining, and taking or leaving the umbrella
is a consequence. This can be stated as follows:
It can be noted that taking the umbrella and leaving it at home is not
possible as a single event. These kinds of statements are called control
statements or, precisely, selection control statements.
4.2 Forming Conditions
Let us discuss more on the formation of decisions. In Python, the conditions
are test expressions. The outcome of the test expression maybe two or more
outcomes. The decision to be taken depends upon the value of variables or
test expressions. These test expressions can be a Boolean value (True or
False), expressions involving relational operators, or expressions that the
logical operators combine.
There are three types of statements if, if-else, and if-elif.
if-statement is a one-way decision statement. It checks for a condition or
test expression to be True or False, and executes a set of statements or
instructions based on the outcome of condition or test expression. A set of
statements can be called a block/ code block or a suite.
if-else is a two-way decision where if the test expression is true, one set of
statement/statements is executed; otherwise, another set of
statement/statements are executed.
Finally, if-elif involves multi-way decisions where multiple conditions are
checked and depending on the values, different statements or suite is
executed. Multiple conditions are also known as a ladder.
Let us discuss the formation of conditions first.
1. Conditions using a Boolean variable
One type of special variable in Python is a Boolean variable. As discussed
in Chapter 3, the Boolean type variable has only two possible values: True
and False. A Boolean expression returns a value—True and False, One can
create a Boolean variable that can hold Boolean values as follows:
Example 1: Illustration of a Boolean Variable
In the above code, the python statement creates a variable called leapYear
and sets its value to true. True and False are built-in python values. There is
a python function called bool that can convert values to Boolean also. The
zero or empty string value is False, and the rest of the values are True. Any
value greater than 0 is taken as True by Python. This is illustrated below in
the interactive shell.
Example 2: Illustration of bool function in Interactive shell
2. Decisions using relational operators
Another way of forming a condition is by using relational operators. Some
of the relational or comparison operators are <, <=, >, >=, ==, and !=. These
operators are discussed in Chapter 3. Any expression that contains a
comparison operator returns a Boolean value. For example, x>=9 can have
only two possible values: True or False.
Example 3: Illustration of Relational Operators
Depending on the value of the hour, the value of the meeting is set to True
or False.
3. Chained conditions using Logical Operations
There are three types of Boolean operators that Python supports. These
operators are not, and, or. These operators are discussed in Chapter 3.
Using logical operators, one can combine operations as follows:
Example 4: Illustration of Chained Conditions
Here, the value 0 is False and 1 is True.
4.3 if-statement
A simple form of an if-statement is shown in Figure 4.1. if-statement is one
of the simplest forms of the decision control statement.
It is a selection control statement that tests a test expression or a condition
and executes a statement or many statements (Suite) based on the condition.
The syntax of the one-way decision statement can be given as pseudocode
as
Here, set of statements are called code block or suite. Here, Indentation is
crucial. Preferably, four spaces or a Tab can be used to indicate the body of
if-statement. Otherwise, improper indentation may lead to error.
It can be observed that the ‘if-statement’ construct is as follows:
1. It is a keyword ‘if’ that marks the start of the construction.
2. A condition or test expression that returns True or False.
3. The condition is followed by a colon :
4. There will be one or more statements. Multiple statements are called a
code block or suite.
If the condition is True, then the statement or code block (or suite)
corresponding to it is executed. The flowchart is given in Fig. 4.1.
Fig. 4.1: Simple form of if statement
Notes
In other programming languages, the statements usually start with begin
and end or brackets { and } would be used. In Python, this is done by
indentation. At least four spaces should be given to mark the presence of a
statement or suite of if—statements. If indentation is not proper, Python
Interpreter would report an error.
Example 5: Illustration of if-statement in Interactive mode
Let us assume that a school conducts an entrance examination for 500
marks. This year the cutoff marks for admission is 400. If a student scores
more than the cutoff marks, he would be admitted. The task is to print
admission is granted if this condition is satisfied.
The pseudocode for this program would be like this
The code would be as follows in interactive mode.
if the condition fails and does not print anything.
Example 6: Illustration of if statement (Listing1.py)
Let us assume that a bookstore is offering a discount of 15% for the staff
members. The python script should get the book price, check whether a
user, staff member or not, as y/n, and if it is a staff member bookstore
should provide a discount of 15%. How to write a python script?
The pseudocode would be written as follows:
The python script would be as follows:
The screenshot for one of the test case is shown below:
4.4 The if-else Statement
In the above topic, one has seen the working of the if-statement. Therefore,
we know that the block is executed only if the test expression or condition
is True. If the if-statement condition failed, we would not have any output.
For example 4.5, if the student scores less than the cutoff marks, one would
like to print that the admission is denied. This is done by a two-way
decision statement known as if-else.
In this statement, If the condition is not satisfied, an alternative statement or
suite specified by else is executed. The syntax of if-else is as follows:
The equivalent flow chart of the if-else statement is given in Fig. 4.2.
Fig. 4.2: if-else Statement
Example 7: Illustration of if-else statement (Listing2.py)
Consider example 5. Admission is granted if the entrance examination mark
is greater than the cutoff mark. What if the entrance examination mark is
lesser than the cutoff mark? In that case, a message should be printed as
admission is not granted. For these two-way decisions, the if-else statement
is useful. The pseudocode for this is given as follows:
The corresponding python script is shown below:
Example 8: Illustration of if-else statement (Listing3.py)
Let us modify the previous Example 6 of book discounts. Let us assume
now that the bookstore is offering a discount of 15% for the staff members
and a flat 10% for others. How should the program be written now? The
python script, as usual, should get the book price, whether a user is staff
member or not, as y/n, and user it is a staff member, the bookstore should
provide a discount of 15%; otherwise, it is 10%.
One can use an if-else statement. The pseudocode can be modified now as
follows:
The python script would be as follows:
Let us consider one more example.
Example 9: Another Example of if-else statement (Listing4.py)
Here, one tries to find the largest of two numbers x and y such that x is
lesser than y. If x is already lesser than y, then there is no need for swap.
Otherwise, x and y should be swapped. The pseudocode is given below:
One can see the sequence of steps. Step 1 initiates the start of the program.
In Step 2, the program gets two numbers as input and stores them in the
variables x and y. In Step 3, the algorithm performs the comparison of two
numbers into the variables x and y. If x is less than y, there is no need for a
swap. Else performs the swap. Finally, Step 7 terminates the program with a
stop.
This can be implemented in Python as follows:
Let us discuss one more problem called a perfect square.
Example 10: Checking whether a number as perfect square or not!
(Listing5.py)
A perfect square is a square of another integer with the same number. For
example. 81 is a perfect square as 81 = 9×9. Nevertheless, 42 is not a
perfect square, as its square root value is 6.48.
The algorithm for finding the perfect square can be written as follows:
The python program would be as follows:
4.4.1 Inline-if-Statement
An inline-if statement is a convenient form of representing a if-else
statement. This is also known as the ternary operator. This statement allows
one to execute conditional if statements in a single line. The main
advantage is that it allows the statement to be compact and allows the
statement to take less space. The general inline statement is given as
follows:
If the condition is True, then expression1 is executed, otherwise,
expression2 is executed. This ternary operator can be nested also.
Here, the condition1 is evaluated. If it returns True, then expression1 is
executed. If condition1 is False, then condition2 is evaluated. If condition2
is True, then expression2 is carried out, Otherwise, if condition False,
expression3 is returned.
Let us consider the following example. Example 4.7 can be rewritten using
the inline-if statement as follows:
Example 11: Illustration of inline-if statement (Listing6.py)
The above code checks the scored mark with the cutoff mark, and then the
flag is set to true; otherwise, it is set to false.
4.5 if-elif Statement
These are used when more than one comparison needs to be made. It
primarily checks the condition. The statements are executed if the condition
is true; otherwise, the control is transferred to another block. This statement
is also known as the nested-if statement.
Suppose a condition is evaluated first. If the value is true, statement block 1
is executed; otherwise, the following conditions in elif are evaluated, and
the respective statement blocks are executed. If none of the conditions is
satisfied, the other block is executed. This chain of conditions is also known
as a ladder.
The following example illustrates this.
Example 12: Illustration of if-elif statements (Listing7.py)
A university department awards grades based on the marks, as shown in the
following Table 4.1.
Table 4.1: Grade Table
The python program for the grade allocation can be done by the python
program as shown below:
Program 1: Finding the Body Mass Index of a person
It is about a measure called Body Mass Index (BMI). It is a measure
derived from the height and weight of the person. It is the amount of body
fat measured with height and weight. It applies to all genders. Lambert–
Adolphe–Jacques Quetelet designed this measure. If the height is
measured in kilograms and height is measured in metres, then the BMI can
be computed using the formula
BMI is a vital measure useful for a person’s normalcy with height and
weight. The BMI table is given below in Table 4.2.
Table 4.2: BMI Table
The python program should start with obtaining the height and weight of
the person and should obtain the BMI of the person. Then if-elif statement
can be used to implement the conditions as shown in above Table 4.2.
4.6 Nested-If Conditions
Instead of using multiple if statements, it is possible to place one if-
statement inside another if – statement. This is known as nested-if
statements.
Example 13: Finding of maximum among three numbers(Listing9.py)
Finding a maximum of three numbers
Let us consider one more example. Let us try to implement the following
algorithm for finding the largest of the three numbers.
The python script is self-explanatory and is given below:
Summary
1. Decision statements use conditions to execute a different sequence of
statements to reach the goal.
2. Selection is a program construct that allows a program to select an action
among diverse actions.
3. A decision is a process to determine actions based on conditions.
4. Conditions can be based on a Boolean variable, relational operators, and
logical operators.
5. A relative operator of an expression returns True or False.
6. Chained conditions can be formed using logical operators.
7. If-statement executes a block of instructions if the condition is True. This
is implemented using the if-statement.
8. Indentation is crucial. Lack of indentation may result in an error.
9. if-else statement executes a block of instructions. If the condition is true
else, it executes another set of instructions.
10. An inline-if-statement is a convenient form of representing the if-else
statement.
11. Nesting implies placing of if-statement inside another if-statement.
12. If-elif is a statement that facilitates checking multiple conditions to
decide the course of action among multiple alternatives.
Glossary
Algorithm—A set of instructions to accomplish a goal.
Selection—A program construct that allows a program to select from
diverse actions.
Decision—A process that resolves a question to yield two or more
outcomes.
Boolean variable—A variable that has only two states—True or False.
Logical Operator—An operator that is helpful to combine decisions like
and, or, Not.
Relational operator—An operator that helps to compare operands to
yield outcomes like true or false.
Inline-if—A convenient form of representation of if-else statement. This
is also known as the ternary operator.
Nesting—The idea of putting one condition inside another condition.
Exception—A condition that happens when an error occurs.
Questions
1. What is meant by Boolean operators?
2. Does the condition in a control statement always evaluate to true?
3. What is the difference between if and if-elif?
4. Illustrate the problems associated with the nested-if statement.
5. What is a ladder? How does the if-elif statement implement it?
6. List some logical operations and relational operations.
7. Write the expression for the following conditions.
1. If your age is less than 20 as well as greater than 50, renew your
driving licence
2. If the temperature in your room is lesser than your body temperature,
then switch on your heater.
Programming Questions
1. Write logical expressions for the following:
1. Either A is greater than B or A is less than C
2. The favourite city is either Mumbai or Chennai
3. Either the place is Bengaluru or Chennai and not Hyderabad
4. Mark is either greater than 60 but not less than 90
2. Predict the output of the code
1.
2.
3.
4.
5.
If the user enters marks 10, 40, and 10, 5 in successive runs, what
would be the output?
3. Write a python program to find the roots of a quadratic equation.
4. Write a program to find whether the given number is odd or even and
positive or negative.
5. Write a program to calculate bonuses for employees based on grade and
basic pay. (Given name, grade, basic pay, dearness allowance, HRA,
CCA, conveyance, etc.)
6. Write a program to display the day’s name, given the date of a specific
month.
OceanofPDF.com
“Programming is a skill best acquired by practice and example rather than
from books.”
Alan Turing
CHAPTER 5
Looping Statements
Iteration and repetition structures, which are frequently used in sequence,
selection, and repetition statements to assist the computer with selection and
repetition, make use of programming constructs like while, for, and nested
loops. This chapter deals with looping statements.
Learning Objectives
After completing this chapter, the reader will be able to :
Understand the process of stepwise refinement using repetition or
iteration structures.
Appreciate the importance of using control structures effectively, in
producing understandable, debuggable, maintainable programs, and
more likely to work correctly on the first try.
Understand the built-in functions for looping.
Learn to effectively use while loop, for loop, nested loops, and loop
control statements.
Use and implement the while and for repetition structures to execute
statements in a program repeatedly.
Learn to use break statement, continue statement, and pass statement
effectively.
5.1 Introduction to Looping
Based on demand, a set of instructions are repeated in the context of
programming. This repetitive execution of a set of instructions or groups of
statements is termed looping, iteration, or looping the loop. The execution
of the code is repeated until a specific condition is met. After meeting the
pre-set requirement (s), the implementation moves on to the subsequent
statement. In simple words, a loop is nothing but a block of code that is
recurring for a stated number of times until some condition is encountered.
An analogy of a loop structure is a giant wheel seen in an amusement park.
The giant wheel is an amusement ride with a rotating vertical wheel with
multiple pods attached to the rim. The code(like humans), placed inside the
the loops(like pods of the giant wheel), are carried out repeatedly, and is
called the loop's body.
For example, consider a code written to convert the temperature from
degrees Celsius to Fahrenheit. Assume the code has to translate five
different values provided by the user. The user will have to run the program
individually five times. This drudgery can be overcome by using loops. The
user can run the program only once and repeatedly enter five values to get
the output in one go.
Purpose of loops
Loops are used to implement instructions (which were traditionally written
manually) until the desired correct outcome emerges. Using the loop serves
purposes like minimization of coding using shortcuts, enhancing the
legibility of code, and lessening the complexity. Loops also aid in shifting
the conservative flow of a program and implementing a particular group of
instructions with developer-specified conditions to yield optimum results. A
cluster of specific requirements decides the course of action of a loop. This
cluster of instructions inclines to reiterate itself till the termination
condition. Next, the program re-reads the previously formulated appropriate
action. Consequently, the loops offer the prospect of carrying viable
analogous instructions together, paving the way for code reuse.
In a nutshell, the benefits of using loops in Python are as follows:
1. Loops aid in escalating the code reusability.
2. Loops simplify complex problems into simple ones, which are very easy
to handle.
3. Loops aid in easily crisscrossing the elements placed in arrays and linked
lists.
4. Loops aid the developers to remove the drudgery of physical writing,
and implement the upfront and small instructions repetitively.
Classification of flow controls
Iteration is based on a counter (or counter loop) or a condition (or condition
loop). As shown in Fig. 5.1, based on the number of iterations, loops are
divided into finite loops (limited) and infinite loops (unlimited). The finite
loops are further typified as definite and indefinite loops based on the
clarity in defining the number of iterations.
Fig. 5.1: Python Programming – Classification of Flow Control
The constructs in an indefinite loop are further subdivided into pre-test
loops or pre-checking loops or entry-controlled loops and post-checking
loops, or exit-controlled loops. In an entry-controlled loop, the condition is
tested before the commencement of the loop. In an exit-controlled loop, the
condition is tested after the commencement of the loop. Python
Programming uses six primary loops construct or flow controls: if, for,
while, break, continue, and pass. The while and for are either entry-
controlled loop or pre-test loops.
5.2 Python Built-in Functions for Looping
A function is a building block or an element in a programming language. It
is a collection of statements that can be reused more than once in a
program. Functions enhance the comprehensibility and quality of the
program, as well as lower the cost of the development and maintenance of
the software.
The necessity for the inbuilt looping functions
Python has four functions, namely, User-Defined Functions (UDF),
anonymous or Lambda functions, recursive functions, and Built-in
functions. Among them, Built-in functions are created or defined in the
programming framework and are always available. Python 3.X has 68 built-
in functions, which will be dealt in detail in later chapters. A simple,
frequently used Built-in function in looping is a range () function. – The
range() function is repeated over a sequence of numbers. It can even create
an iterator of arithmetic progressions. The syntax of the range is as follows:
The parameters of the range function are start, stop, and step.
start is an integer and is the beginning of the sequence of integers that
must be returned.
stop is an integer that is fixed beforehand and indicates the last number
in the sequence of integers that must be returned, and it does not include
the second number.
step is an integer value that limits the increment amongst each integer in
the sequence.
The range() function is typically put to two main uses:
1. Iterating through a for-code loop's body a given number of times.
2. Iterating integers faster than with lists or tuples.
Range()
The range class is immutable. Differentiation from the prior iteration is
indicated by increasing numbers.
Any and all inputs must be integers only.
All parameters can have positive or negative values.
Since range() and Python use zero-based indexes, list indexes start at
zero rather than one.
The range() function in a for loop may seem like a list of incrementing or
decrementing values. In addition, the for loop can make use of the range
object that is returned by the range() method. The range() function is not
itself an iterable type, but it can be indexed and sliced just like any other
iterable. Because slicing a range only produces a new range and not the
original list, it cannot be used to obtain the desired results. Significant
memory can be saved, which is especially helpful in large programs, by not
storing the entire list of numbers if only a subset is used. By default, list()
returns the underlying list, but this is overridden by the range() parameter.
Example 1: Program for simple Range functions
The Python script to showcase the simple range function would be as
follows:
5.3 Loop Statements
The statements are executed for every item of a given series in the loop
statements. To start with, an item deemed to be the first item of the iterable
is taken and implemented, and then it moves the pointer on to the next item
until it grabs the last item of the series.
To understand the General Structure of the while and for Loop, an analogy
of a toy train that takes in children as passengers is depicted in Fig. 5.2.a
and 5.2.b. The passengers are taken several times(based on desire), around
the Zoological Park to visit the Fauna there. In one round trip, they watch
the Elephants (Proboscidea) bathing, feeding, and playing as the battery car
passes the first station. Next, they see the Dolphins(Cetacea) swimming
near the second station. The third station is near the cages with feathered
and winged birds (Avians). Lions, Tigers, Leopards, (Carnivora), and others
are seen as the train passes through the fourth station.
Fig. 5.2:(a) Exemplification of for loops using a Toy Train going around
a Zoo.
Fig. 5.2:(b) Exemplification of while loops using a Toy Train going
around a Zoo.
Pinioning the real-life example, the reader can now understand that every
loop has an initial value, Counter or increment variable, the loop's body, and
End Value. while, do, and for are the three-loop statements. Python doesn't
have the do-while loop.
5.3.1 while Loop
Python while loop is a condition-controlled loop that supports repeated
execution of a statement or block of statements controlled by a conditional
expression. The while construct will keep performing the task specified in
the block as long as a particular condition is true. The while loop is picked
up to perform a task indeterminately till a specific condition is satisfied. In
the while loop of the toy train example (as shown in Fig. 5.2) at the end of
the pre-set three laps, the passengers alight from the battery car.
Syntax of while Statement: The following is a syntactical representation of
the while statement:
Implementing the while loop: After discussing a few example programs
that illustrate the while loop's logic and concepts, the reader should feel
comfortable implementing the loop on their own.
Example 2: Illustration to print integers less than six using the while
loop.
The Python script below uses a while loop to print integers less than six.
The while loop condition is to check if the variable "exercise" is less than or
equal to six (exercise <=6). The ensuing block of code is executed
repeatedly until the condition is true.
The variable exercise is initialized with value 6. Then, a while loop is given
with a condition that exercise <= 6. Since, the while loop is entry-check
loop (the body of the loop is executed only when the condition is True), the
condition is checked before the execution. On the first occurrence, the
condition is satisfied since the value of exercise is 1 and the statement
“exercise = 1” is printed and then the variable in incremented to 2 using the
statement exercise + = 1. After first occurrence, the condition is checked
and the loop body is executed. This looping occurs till the condition fails, in
this case, the loop terminates when the value stored in the variable exercise
6 becomes 7.
Example 3: Illustration for "Verbal Announcements in a University
Exam Room" that uses a while loop. (Listing1.py).
In the Python script, a while loop is used for an announcement in the
University Examination Hall. The while statement and an evaluating
expression (while minutes left is greater than zero) initiate the loop. The
loop body is executed if the condition returns a true value. It immediately
begins carrying out lines 3 and 4. After the loop's body has been executed,
the control passes back to the while statement to continue testing the
condition until it evaluates to true. This will keep happening until the test
condition fails. Then, the line 5 is printed once the controller exits the loop.
#The screenshot below shows the result of saving the above code as
Listing1.py and running it:
while loop with else: When a while statement's condition evaluates to false,
any else information (optional) that follows it can be used to define how the
processing of the code should proceed. To put it in simple words, it takes
control of the program automatically, and the code in the else block is then
executed. The else block is a block of one or more statements to be
completed. It must be indented more spaces.
Note
To understand the while loop with else, imagine "ABC" going to a
restaurant for an evening Makhana (a seed rich in antioxidants and
micronutrients) snack and tea. Assume "ABC" ordered one small Makhana
portion. 'ABC' will order a second Makhana portion if she's hungry after
finishing the first. Makhana is ordered until the stomach is full. 'ABC' won't
eat after hunger is satisfied. If the restaurant doesn't have Makhana, the
while condition becomes false. The code executes an else body if the
condition is false. Then "ABC" drinks tea. Therefore, write the additional
body with two statements: order_ tea and drink_ tea.
This scenario can be depicted as a flow chart for a While Loop with an Else
part. Based on the truth of the availability condition, the while true block
gets executed [namely, order_ makhana(), eat_ makhana()]. If the
availability condition turns out to be false, the else block gets executed
[namely, order_tea(), drink_tea()].
Fig. 5.3: A Flow Chart Depicting the Process of a While Else Loop.
Syntax of while loop with else clause: The following is a syntactical
representation of the while loop with the else statement.
Fig. 5.4: Syntax of while-else Loop in Python
The detailed flow of execution for a while else loop (Figs 5.4 and 5.5) is
as follows:
1. while statement (loop) is activated with while keyword.
2. Assess the test condition to see if it yields True or False.
3. If the condition is true, execute the code's body and then go back to
initial step.
4. If the condition is false, skip the body containing the code following the
while statement.
5. If false, execute the following statements in the rest of the program (like
the else block).
6. Loops until it reaches the end of the body; returns to the while statement
(containing the condition), re-examines the test condition, and updates it.
"Update" meaning is if it is true, repeat the execution of the body of the
loop until the process is over and until the condition fails or it becomes
false. If the condition is false, it goes to the else block and executes the
statements.
Fig. 5.5: Depiction of the workflow of a simple while–else loop in
Python.
Implementing the while else loop: The implementation is outlined via
simple examples.
Example 4: Illustration to output all integers under six using the while–
else loop
5.3.2 for Loop
For is a simple, single-line statement that is easy to use. It ensures the
sequential execution of all the stipulated conditions. It is used to execute a
block of code repeatedly at predetermined times. It is used in combo with
iterable objects or sequence types like list, tuple, set, range, etc.
In the example of the toy train, the for-loop condition is checked to see if it
is true or false. After reviewing the pre-set condition, the second round is
completed. Similarly, the third round is also done. After the third round's
completion, the battery car's trip stops. At the end of the three laps (final
value), passengers get down the battery car.
Syntax of for statement: The following is a syntactical representation of the
for loop.
A look at the fundamental structure of the syntax shows that
Implementing the for Loops: Generally, in all the for-loop programs, the
simplified main process flow is as shown in Fig. 5.6.
Fig. 5.6: Exemplification of the Process Flow of a Simple For Loop in
Python
For keyword in a loop determines the initial point in an iteration and
executes a set of statements in the next block when the iterable object
condition is true. Every time the for loop is executed, it grasps an element
in a sequence and runs the code assigned to it. When it has completed
picking all the elements and reaches the final one, it instantly leaves the
loop. If not, it keeps implementing the statements present under the loop
body. In Python, the for statement is controlled by a conditional expression,
which decides the fate of the rate of repeated execution of a statement or
block of statements. Python for loop is a count-controlled loop. The for
statement iterates over a range of values, a numeric range, or elements of a
data structure like a string, list, or tuple. The iteration expression controls
the number of times the code block or the body of the loop the statement is
to be repeated. The for loop is ideal for definite loops, where the number of
iterations is already known.
Example 5: Illustration of a simple for loop, iterating over a range.
[range (begin, end)] (Listing2.py).
When used in conjunction with a for loop, the range() function proves
particularly useful. Using the required syntax, the range() function returns
the appropriate numerical values. The python script would be as follows:
#The screenshot below shows the result of saving the above code as
Listing2.py and running it:
Work behind the scenes: This program first initializes variables 'n=1894
and sum=0'. Next, the For loop begins with the "counter" (optional
declared variable) taking one element from the iterable object (range(1 to
n+1)) in succession. It will execute the statements [sum = sum + counter]
in the body of the loop(block). The statement in the loops body prints the
computed sum[Sum of 1 until 1894: 1794565].
for-else Loop: The else keyword is useful in for loops in Python. The body
of the loop is followed by an else block (Fig. 5.7). In case the iteration fails,
the else block's statements will be run. It is only after the else block has
been executed.
Fig. 5.7: Depiction of the Work Flow of a Simple For else Loop in
Python
Syntax of for loop with else:: The following is a syntactical representation
of the for loop with else statement.
Fig. 5.8: Syntax of for else Loop in Python.
A look at the fundamental structure of the syntax (Fig. 5.8) shows that
The flow of an implementation of a for-loop (Fig. 5.7) is as follows:
1. The loop is activated with the keyword. After testing the membership
expression, it demands the next element or item from the iterable (a
collection of elements or items).
2. If the iterable is blank, exit the for-loop without running its body.
3. If the iterable did produce an element, allocate that element to <var>.
1. If <var> was not previously delineated, it becomes delineated).
4. Implement the bounded body of code equal to the number of items or
elements in the collection.
5. Return to the first line.
Implementing the for-else loop: Having understood the concepts and the
logic behind the for-else loop, the implementation is explained with simple
examples.
Example 6: Illustration to print the iteration of a loop over a range
function [ from one to seven ] using for-else loop(Listing3.py).
In this Python script, the Built-in range function is deployed to generate an
iterable sequence of numbers. It returns a range object, an iterable object,
that can be looped through. As already stated, look at the two enclosed
numbers in the range function. It will yield a sequence of numbers that start
from the first number "1" and proceeds up to the second number "7", and it
does not include the second number, "7". By default, the step count is taken
as 1.
#The screenshot below shows the result of saving the above code as
Listing3.py and running it:
5.3.3 Nested Loops
In Python, when a loop lies within or inside the body of another loop, it is
called a nested loop. The loop lying within another loop is called the inner
loop, and the loop outer to the inner one is called the outer loop.
Syntax of Nested Loops: Both inner and outer loops can be while loops or
for loops, while over for, or vice versa. Upon encountering the outer loop,
the program executes its first iteration, which triggers the inner, nested loop,
runs to completion. The program returns to the top of the outer loop,
completing the second iteration and again starting the nested loop.
Furthermore, the nested loop runs to completion, and the program returns to
the top of the outer loop until the sequence is complete or a break or other
statement disrupts the process. These are constructed like this:
The need for nested loops in a program: In circumstances, say, for
example where there are an excessive number of loop iterations, the nested
loop technique is employed. It is helpful in reducing the amount of memory
that is being used.
Implementing The Nested Loops: The depth of the nested loop hinges on
the intricacy of a problem.
Example 7: Illustration to print the iteration of a nested for loop
(Listing4.py).
The Python script would be as follows:
#The screenshot below shows the result of saving the above code as
Listing4.py and running it:
Example 8: Illustration that prints nested loops, including the while
and for loops. (Listing5.py).
The Python script would be as follows:
For loop begins with "x" as an optional declared variable and executes the
outer loop statements by taking one element from the iterable object
(range(5)). Declare and set the variable "y" to four in the outer for loop. The
loop starts with a while loop and a test condition. Evaluate the test
condition (y>=x:) to determine True or False. The loop body is executed if
true. The loop is iterated until the test condition is satisfied. If the condition
fails, the loop is terminated. The special contents of the print line are
printed next: print("@Illustration of break#$"). Set the initial value of the
"y" variable to "1". It loops endlessly when the print statement is
encountered. A break statement is necessary to immediately exit the infinite
loop, which is the next topic.
The output result of the above code is as follows:
5.4 Jump Statements
There may be times during the execution of the code when a pause or
termination of the code is necessary (also known as an abnormal loop
termination) due to the occurrence of a recursion statement. Generally, to
stop or pause the looping statements, control statements are used. Code
performance deviates from its usual pattern due to these iteration loop
control statements (directed by the code). To instantaneously exit the body
of a loop or recheck the condition(s) from the inside of the loop, these
control statements are used.
Python provides loop control statements to modify the behaviour of
recursion statements. Developers have more leeway in crafting the loops'
control logic with their help. break, pass and continue are all valid control
statements in Python.
5.4.1 break Statement
The break statement offered by Python is a unique purpose statement. An
immediate termination of the loop occurs when the break statement is
executed, but any lines of code that are reachable after the loop's body are
skipped over.
Examples: Taking a break in real life
Airplane running on the airstrip identifies that one of the plane's tires has
exploded. Therefore, it instantly aborts take-off, alighting the crew
members and passengers and shuttling them back to the terminal by bus.
Type B circuit breakers in homes trip when the current flowing through
it hits about three and five times the endorsed maximum or 'rated load'.
In a similar vein, the break statement assists in breaking away from the
current task at hand. It is functional inside the body of a for or while loop.
When the program reaches a break statement, control is transferred out of
the while or for loops, even if the loop condition is estimated to be True.
The break will only affect the innermost nested loop if there are multiple
levels of nesting. Restating in simple terms, a break statement in a loop
exits the loop immediately and continues the code from the break statement.
The necessity of a break statement: The break statement is used in a
sequential search. Consider an instance, wherein the program uses a for
loop to find an item in a list. When the hunted element is reached, it exits
the loop deprived of passing through the residual elements in a sequential
search. At this juncture, the break statement is used as a deliberate stoppage
to prohibit the for loop from running further. It actually eradicates and
directs the flow of control in the code. If there is no break statement, the
code implementation continues to the next case.
Syntax of break statement: The following is a syntactical representation
of the break statement:
To terminate a loop, use the keyword "break". The loop starts with the
optional declared variable, which takes one element from the iterable object
at a time to execute the statements in the next block until it encounters a
break.
The flow of iterations through a for loop that contains a break statement is
as follows (also it is shown in Fig. 5.9):
1. Loop begins
2. The for loop starts with the optional declared variable, which takes one
element from the iterable object at a time to execute the statements in the
next block.
3. Execute the first statement.
4. Execute the second statement.
5. Bumps into the break statement.
6. Jumps out of the loop.
7. Terminates the code execution.
Fig. 5.9: Exemplification of the Process Flow of Break Statement in
Python.
The working of a break Statement in a loop: The break statement can
also be visualized with a flowchart as shown in Fig. 5.10. The break
statement allows the early termination of a loop. Python's break statement
interrupts the current iteration of while or for loops and skips to the next
condition.
Fig. 5.10: A Flow Chart Depicting the Process of Using Break in Python
Loops.
The entry into the loop causes the statements to be executed like initialize,
declaration, etc. Next, it reaches the conditions in for (for var in iterable:)
and while (while condition:). The condition is verified to find out if it is true
or false.
If true, it goes to the break statement. Upon execution of the break
statement, the remaining block of statements gets executed and returns to
the conditional line.
If it is false, it exits the loop. The break statement is nested in the while
statement or for statement that it will end. This means that one or more
statements will be completed, not all of them.
To better demonstrate the value of the break statement, some examples of
programs that use it are provided below.
Implementing the break Statement: The break statement is executed in
the for and while loops, as shown below in Fig. 5.11.
Fig. 5.11: Python Loops Flow Control with the Break.
break statement in a while true loop: To repeatedly run some code until a
Boolean condition is false, the while loop is used. While True intentionally
creates an infinite loop. Forcibly exiting a while true loop is possible with
the break statement.
Example 9: Illustration for the use of a break statement inside a While
loop. (Listing6.py).
Without the continue statement, the loop will only print the even numbers
between 1 to 12. The if statement restricts printing to non-odd digits, and it
will break out of the loop when x is greater than or equal to 12.
#The output of the above python script is shown below:
Real-world situations may call for code to be able to ignore a stop condition
and continue running. In such cases, the control must be returned to the
beginning of the loop and the next iteration instead of continuing to execute
the statements of the current iteration.
5.4.2 continue Statement
Real-life Examples for continue
A martial arts student named "ABC" is asked to take a level 1 test to
demonstrate mastery. ABC cannot advance to the next higher level until
he/she passes level 1. 'ABC' continues doing it till he/she is eligible for
the next level. Here, the test is similar to continuing it passes.
Changing a diabetic's diet until sugar levels stabilise.
Air-conditioning a room to the desired temperature.
Photosynthesis (a chemical reaction) turns carbon dioxide and water into
food (glucose) and oxygen until it is self-sufficient.
The physical limit of how much food can be eaten at once depends on
hunger (continue). Consumption continues until the stomach feels.
Likewise, the continue statement function is also helpful for continuing the
present work till the condition is satisfied.
The necessity of a continue statement: When a continue statement is
bumped into an inner loop, the control navigates to the start of the loop and
the start of the next iteration; instead of implementing the statements of the
contemporary iterations. The continue statement helps neglect a specific
condition and continue with the execution.
Syntax of continue: The syntax for the continue statement is given below:
Flow Chart of continue Statement
Fig. 5.12: A Flow Chart Depicting the Process of Using continue in
Python Loops.
The flowchart is shown in Fig. 5.12. aids in the understanding of this
continue. Stepping into the realm of practical application, a handful of
programs are provided alongside their respective solutions.
Implementing the continue Statement: The Continue statement is
implemented in the for and while loops, as shown below (Fig. 5.13):
Fig. 5.13: Python Loops Flow Control With the Continue.
Example 10: Illustration showing the difference between a while loop
with continue statement. (Listing7.py).
The Python script would be as follows:
With the inclusion of a continue statement, odd numbers between 1 to 12
will be printed. If x% 2 == 0 then the continue statement will bypass the
remainder of the current iteration of the loop. The if statement restricts
printing to non-even digits, and it will break out of the loop when x is
greater than or equal to 12.
#The output of the above python script is shown below:
5.4.3 pass Statement
Pass is a null statement in Python. The pass is used either inside the body of
a function or class body. The Interpreter does not ignore the statement but
passes it without throwing an error.
Need for a pass Statement: Pass statement is used to write and implement
classes, empty loops, empty control statements, and functions in the future.
A pass statement is helpful in some cases where the parent class will have a
function that is not useful to that class. But it might be a valuable function
to its child class so it can evade any fault in the base class. Usually, the pass
statement thus comprehends itself as a placeholder in the compound
statement. The placeholder will either be blank, or nothing will be written
there. It is also used in places that are supposed to include a function with
no execution and to declare the function in a code that will be used in the
future. The code can be written only with the slow unfurling of many more
details to come in the future. Yet the body of the function, cannot be left
empty as it will raise an error. The error is raised and hurled either as
syntactically or systematically incorrect. A Python pass statement is used as
more details are needed to write the function's body.
In real time, it can be used to do complex multiplication or complex
mathematical operations. Say for instance, when there is a combination of
Mathematical operations like multiplication followed by division and
several other operations. Based on the required operations at any point in
time, one can select or/and skip any one of the particular functions with the
aid of a pass statement.
Example 11: Illustration showing the usage of the pass keyword
(Listing8.py).
The pass keyword can be used as shown in the following cases:
The above code is stored as Listing8.py and explained as follows.
#The output of the above python script is shown below:
The pass statement is typically used when there is no requirement for a
code, but a statement is by itself or is still vital to write the code
syntactically precisely for running. Null, or no operation (NOP) is called
so, as nothing happens when the pass is executed. Hence, the passkey is
also referred to as a No-operation statement.
Fig. 5.14: A Flow Chart Depicting the Process of Using Pass in Python
Loops
The pass statement evades the compilation errors by overlooking a block
and moving frontward. Throughout the implementation, the interpreter
overlooks and stays deprived of any error once it comes across the pass
statement. Exception catching, if, and elif chains also use the pass
statement. As a result, it is advantageous in applications that overlook a
block and progress forwards, deprived of any execution of operations.
The flowchart shown in Fig. 5.14 aids in understanding this keyword. When
the specified condition in a loop (while or for) is true, followed by a pass
statement, then the value or repetition of the loop is done and proceeds onto
the subsequent line of code. It thus transfers the control to the start of the
loop.
Example of a pass from real-life: consider a few scenarios in real
life to understand the use of a pass.
Smartphones provide removable microSD cards, which are external,
expandable memory. Some smartphone brands offer this provision to
their customers. This is provided so that, based on the demand in the
future, the customers can enlarge their smartphone's storage capacity.
Consider a three-storeyed flat constructed within a low budget (along
with slow funds flow from the flat buyers) and short period. The flat
promoter prioritizes completing the flat construction with basic
amenities and handing over the key to the ready-to-occupy flat to the
owners on time. The flat promoter offers a space provided by the flat
owners so that the lift installation can be done in the next phase with the
release of funds.
Proactive building of bunkers to offer a safe escape from enemies in
anticipation of artificial disasters like war.
For an upcoming feature film that will be unveiled later in the theatres at
the end of 10 months, the commercial advertisement, like a trailer or a
preview, is released, even if the shootouts are still ongoing.
Consider a simple example using if, where the pass statement is used. When
the values of a and b are known to possess the above values the if holds
good. (i.e., as the summed value is equal to the value 989).
Unfortunately, the values of a and b will be unknown in real-life situations.
It will result in errors. An empty body is preferable in this case. Indenting
the code around the colon is necessary. Executing the empty body causes
indentation errors. Thus, an error prevention mechanism is needed. Use a
pass statement in the fourth line to solve this problem. The pass statement
usually serves as a placeholder in the compound statement. The placeholder
will be blank or unwritten.
Example 12: Illustration of a for-else loop with pass. (Listing9.py).
The python script is as follows:
The inactive pass statement checks for even numbers in the if statement.
Processing ends if the result is even. "X is odd" appears if the value is odd.
After processing all numbers in the range, the else block will display "All
numbers have been processed."
#The output of the above python script is shown below:
The pass statement can be used temporarily (or Provisional) or eternally (or
Permanent).
Provisional uses of pass Statement: In this type, the pass statement is
helpful to develop codes faster in the earlier stages. There may arise
occasions in code development wherein no one will be aware of what
function is required in the future. At times, parts of the initial code might
be needed to be filled in the future. For example-a function needs to
perform a task by calling another function. Maybe the developer is
unaware of the hotkey function; to be called will work. In such
situations, the pass statement comes as support. Further, it helps to
understand the code, structure, and complexities.
Permanent uses of pass Statement: It is also suitable for Exception
Catching cases wherein the try … except block is used for handling
errors. The pass statement is used to avoid the incidence of an error.
Differences between the pass and continue
The loop controls used in Python are break, pass and continue statements.
The following table depicts the differences between the pass keyword and
the continue keyword (Table 5.3).
Table 5.3: Differences Between the pass and continue
Summary
1. A loop is a block of code that is recurring for a stated number of times or
up until some condition is encountered.
2. Loops aid in shifting the conservative flow of a program and
implementing a particular group of instructions with developer-specified
conditions to yield optimum results.
3. Using the loops has advantages like minimization of coding using
shortcuts, enhancing code legibility, minimizing complexity, and
simplifying complex problems.
4. Loops aid in easily crisscrossing the elements placed in arrays and linked
lists.
5. Loops aid the developers, remove the drudgery of physical writing, and
repetitively implement the upfront and even the little instructions.
6. While Loop can perform a task indeterminately till a specific condition
is satisfied.
7. The while construct will keep performing a block if a particular
condition is true.
8. The While Loop is chosen to perform a task indeterminately till a
specific condition is satisfied.
9. for loop ensures the execution of a block of code repeatedly at
predetermined times in a sequence.
10. For loop is used in combination with alterable objects or sequence types
like list, tuple, set, range, etc.
11. For loop allows the user to reuse the code, convert complex problems
into simple ones, and pass through the elements in data structures like an
array or linked lists.
12. The break statement interrupts the flow of the current loop, escapes
from the current loop, and transfers the program's control to the block of
code ensuing the current loop (if there are any).
13. The continue statement forces the reiteration of the loop from the start
of the code. Further, the indented lines of code ensuing (if there are any)
the continue statement in the loop will not be executed.
14. The continue statement helps write and implement classes, empty loops,
empty control statements, and functions in the future.
15. The pass statement does not do anything or ignore a code execution,
throw an error on execution, and comprehend itself as a placeholder.
16. The pass statement is helpful for Exception catching, if-elif chains, to
write and implement classes, empty loops, empty control statements, and
functions in the future.
Glossary
Iteration- sort of a repetitive execution of specific statements or group
of statements or a set of instructions or group of statements is termed as
looping or iteration or looping the loop
Loops- a loop is nothing but a block of code that is recurring for a stated
number of times or up until some condition is encountered
definite loop- A definite loop or counted loops has a predetermined
number of iterations and after the completion of execution it terminates.
finite loops- The terms "definite loop" and "finite loop" are often used
synonymously. It has a predetermined number of iterations and after the
completion of execution it terminates.
infinite loops- The quantity of iterations is unlimited in infinite loops.
Collection-controlled loop -This particular construct allows looping
through the elements of a "collection,” which can be an array, list, or
another ordered sequence.
Count-controlled loops- A construction for repeating a loop several
times. The Count-controlled loop in Python is the For Loop.
Condition-controlled loop- A loop will be repeated until a given
condition changes. While loops possess this behaviour.
entry-controlled Loop- the condition is tested before the
commencement of the loop.
exit-controlled- Loop-the condition is tested after the commencement of
the loop. The while and for are or entry-controlled Loop or pre-test
loops.
while loop- A loop that can perform a task indeterminately till a specific
condition is satisfied.
for loop- For loop ensures the execution of a block of code repeatedly at
predetermined times in a sequence.
Jump or control statements- They are loop control statements, which
alter the execution of code from its normal routine sequence (directed by
the code).
Break- The break statement interrupts the flow of the current loop,
escapes from the current loop, and transfers the program’s control to the
block of code ensuing the current loop (if there are any).
Continue- The continue statement forces the reiteration of the loop from
the start of the code. Further, the indented lines of code ensuing (if there
are any) the continue statement in the loop will not be executed.
Pass- The pass statement does not do anything or ignore a code
execution, and does not throw an error on execution, comprehends itself
as a placeholder.
Conceptual Questions
1. What is meant by looping or iteration?
2. What are the benefits of using iterations or loops in Python?
3. Explain the notion of a counter and a conditional loop.
4. What are finite and infinite loops? Explain the differences between finite
and infinite loops.
5. What are definite and indefinite loops?
6. Distinguish between entry-controlled and exit-controlled loops.
7. How do loops work, and what are entry-controlled and exit-controlled
loops?
8. What are built-in functions and built-in looping functions? Why do we
need built-in looping functions?
9. What are range functions? Why do we need the range functions?
10. Give the syntax for the range function. Name the parameters inside the
range function.
11. Outline the logic of a while loop with a flow chart.
12. Give the syntax for (a) while loop,(b) for loop,(c) jump statements,
13. The "Python While Loop" is a condition-based iteration structure.
Explain.
14. Explain the flow of execution for a while else loop.
15. What form will the while loop and the other component take?
16. Compare the for loop and while loop.
17. What is the logic behind a for-else loop, and how does it work?
18. Write the syntax for a nested while and for loops.
19. Why do we need nested loops in a program?
20. With the help of a flow chart, explain the working of a Break statement.
21. What are control statements?
22. What's a Break statement's purpose?
23. Show the functionality of a continue statement with a flowchart.
24. Give an overview of how a pass statement operates using a flowchart.
25. Is a pass statement really necessary?
26. Bring out the differences between the Pass statement and the Continue
statement.
Try-Out Questions
Write the output of the following code.
1. cricket_stadium_tickets = 0
while cricket_stadium_tickets <9:
print("Within Range")
cricket_stadium_tickets = cricket_stadium_tickets + 1
else:
print("Out of Range")
2. chickens_hatched=0
while chickens_hatched< 6:
chickens_hatched+=1
if chickens_hatched== 9:
continue
print(chickens_hatched )
3. avocado=6
papya=9
while papya <15:
avocado=papya-1
papya=2*papya-avocado
print(papya,avocado)
4. for x in range(6,65,4):
print(x)
5. for x in range(3,10,2):
print(x*"Python")
6. while True:
imei_no=input("Type in your IMEI:")
if len(imei_no)==8:
break
print("Your ID is"+imei_no)
7. i=0
while i<=8:
i+=1
if i == 5:
continue
print(i)
8. for i in [16,27,1281,494,74,75]:
if(i>=128):
pass
print("This is pass block",i)
print(i)
9. dogs = ["labrador"]
for i in dogs:
if i == "labrador":
dogs += ["pug"]
if i == "pug":
dogs += ["spitz"]
print(dogs)
10. dogs = ["labrador"]
for i in dogs[:]:
if i == "labrador":
dogs += ["pug"]
if i == "pug":
dogs += ["spitz"]
print(dogs)
11. fibonacci = [0,1,1,2,3,5,8,13,21,26,83,90,101,108,120]
for i in range(len(fibonacci)):
print(i,fibonacci[i])
print()
12. numbers = [10, 40, 120, 230]
for i in numbers:
if i > 100:
break
print('current number', i)
13. accounts=['Assets',' Equity','Income','Expenses']
for account in accounts:
if account.startswith('I'):
print('Account name starts with I: '+account)
break
print('Account name does not start with I: '+account)
14. for letter in 'Never Give Up':
# break the loop as soon it sees 'U' or 'G'
if letter == 'U' or letter == 'G':
break
print('Current Letter :', letter)
15. Common_Name=["Saffron", "Turmeric","Cumin","Nutmeg",
"Coriander","Cardamom"]
Botanical_Name=[["Crocus Sativus"], ["Curcuma Longa"], ["Cuminum
Cyminum"], ["Myristica Fragrans"], ["Coriandrum Sativum"], ["Elettaria
Cardamomum"]]
m = len(Common_Name)
n = len(Botanical_Name)
for i in range(m):
n = len(Botanical_Name[i])
print(Common_Name[i])
for j in range(n):
print(Botanical_Name[i][j])
16. breakfast=["Appam", "Idiappam","Idly","Dosai"]
for fooditems in breakfast:
if fooditems=="Idiappam":
print("Idiappam is healthy, as it has carbohydrates and fats!")
continue
print("You will be glad to consume,nutritious,delicious food like
"+fooditems)
else:
print("I want to see how the else block is working!")
print("Demonstrates that this part of the block will not be seen on the
screen")
17. healthy_fruits=[ "Amla", "Banana","Fig", "Guava",
"Jackfruit","Lemon", "Mango", "Orange","Papaya","Rambutan","Star
Apple", "Watermelon"]
for item in healthy_fruits:
if item=='Is it ripe?':
print('yes it is ripe')
else:
pass
print('You can pick the fruit of your choice')
18. Rewrite the following code after removing all syntax error(s). Also, list
the output.
zoo_visitors =1008;
count = 0
i=1
while i <= zoo_visitors
count = count + i
i = i+1
Print("Total number of visitors to the zoo are ", count)
19. Rewrite the following code after removing all syntax error(s). Also, list
the output.
adjective=["green","small","yummy"]
fruits=["guava","mango","orange"]
for x in adj:
for y in fruits:
print(x,y)
20. Remove any indentation mistakes from the following code and rewrite it.
Also, provide a list of results.
for juice in ["Avacado","Celery", "Cucumber","Lemon","Mango"]:
print(juice)
if juice[0]=="G":
break
print("Best Juice combo!")
21. Use a for loop with a continue statement to iterate over each of the
inputted numbers. When a nine-valued element is encountered, the
sequence advances to the next elements. Find and write the output of the
following Python code:
x=[3,5,9,21,108,517,191,403,215,78,29,9]
for y in x:
if y == 9:
continue
print(y)
22. while True:
answer = input("")
if answer == '81':
print("The Greatness of a King!")
break
print("Courage, liberality, wisdom, energy, vigilance, knowledge,
bravery, unfailing virtue are the qualities of a leader")
23. for n in range(1278,5489,100):
pass
print(n)
24. a=108
while a<=254:
b=111
while b<=225:
print(b)
b+=100
a+=100
print("end of the inner loop")
print("end of the outer loop")
25. passengers=150
while passengers<155:
print("Ticket available")
passengers+=1
print("House full")
Programming Exercises
1. Write a program to use a for loop and a continue statement to go through
all of the entered numbers one by one. The subsequent elements in the
sequence are activated when a particular element in the sequence has a
value of 16.
Given x=[23,43,56,78,16,9,7,12,14,15]
2. Write a Python program that handles the following situation with while
and if-else statements.
"Gourmande patrons a restaurant on a regular basis. The Gourmande can
place an order from the restaurant's menu. If the order is identical to the
"Signature dish", the server notes it and takes it down; otherwise, they
may recommend the "Signature dish" instead of the favourite dish."
3. Make use of for-loop statements in the code to deal with the following
scenario.
"Books can be checked out as many times as needed, but the library only
allows 10 visitors per day. Find out how many books were asked for in
total."
4. Make use of a for-else loop and break statements in the code to deal with
the following scenario.
Due to pandemic regulations, an online store can only ship to a
maximum of 6 USERS and up to 50 accessories at a time. Users 1 and 2
each place an order of INR 78 and INR 54. As stock runs out, deny the
third request.
5. Exhibit how Pass can be used as a placeholder for future code in a
1. for loop in range(3,7).
2. for loop in range(12).
6. Write a for loop for this scenario, "In this game, you have FIVE chances
to win. The player will get what he wants if his request is granted. If the
program continues, he can play again."
7. Create a program to check whether movie tickets are available or not.
8. Iterate through the given string printing each letter using the for loop.
9. Iterating through a fibonacci_series= (1,1,2,3,5,8,13,21,34,55,89) by
using the for loop as the primary control structure
10. Write a program to use Python for loops for the fibonacci_series=
(1,1,2,3,5,8,13,21,34,55,89)
11. Write a program using the for loop that outputs each word in the list.
rice= ["Arborio", "Wehani", "Basmati" ]
Also, print each word's characters instead.
str="Hakuna Matata"
12. Create a program utilizing a for loop, an if statement, and break in
Python to post a list of rice and verify their presence or absence.
rice= ["Arborio", "Wehani", "Basmati" ]
13. Convert the following “while loop” into a “for loop”:
x=804
while(x<=1008):
print(x*100)
x+=15
14. Create some code to generate the following pattern using two “for
loops”.
15. Create some code to generate the following pattern using two "for
loops".
16. Write a program using a while loop to calculate the mean(= total/25) of a
set of numbers using a while loop.
Given while loop condition = count<6
17. Create a Python code using for loop that outputs all the multiples of
5 between 108 and 508.
18. Develop a Python code using for loop, which, in response to an integer
input(x) from the user, returns all the integers in line. Given
range(18,25).
19. Create a program using for loop to determine the highest common factor
or greatest common divisor of a pair of numbers.
20. Write a Python code using for loop to show all the odd numbers lying
between any two input numbers (1,9).
21. Create a Python script using for loop that will flip any user-entered word.
22. Write a Python program using a while loop that multiplies two integer
numbers (1,9) using repeated addition (without using the '*' operator).
23. Write a Python program to demonstrate the use of a pass in a simple for-
if loop.
Given range (2786,2799). Print when multiples of 5 are found.
24. Create some code that uses an iterative for-else loop to cycle through the
fruits strings. Given list of
fruits=['Banana', 'Fig', 'Guava', 'Jackfruit', 'Lemon', 'Mango', 'Orange',
'Papaya', 'Rambutan', 'Star Apple', and 'Watermelon'].
25. Create a code that uses an iterative for-else loop with a pass to cycle
through the fruit strings. Given sequence strings are
Juice=["Amla Juice", "Orange Juice", "Pomegranate Juice", and
"Pineapple Juice"].
HealthiestJuice is the optional declared variable to be used in for loop
26. Create a code that uses an iterative for-else loop with continue to cycle
through the fruit strings. Given sequence strings are
Juice=["Pomegranate Juice", "Orange Juice", "Pineapple Juice", and
"Grapes Juice"].
HealthiestJuice as the optional declared variable to be used in for loop
27. Write a Python program to print the quadruple of positive numbers.
28. Write a Python program using break to print the triple of positive
numbers.
29. Construct a Python program that returns a 2D array with the specified
number of rows and columns, x and y respectively.
30. Create a script in Python that determines if the given number is a
palindrome.
Given numbers:
1. 7890987
2. 324576
Hint: Words, phrases, numbers, and strings that read the same both ways
are palindromes.
OceanofPDF.com
Computers are good at following instructions, but not at reading your mind.
Donald Knuth
CHAPTER 6
Introduction to Functions
One of the most important components of programming is functions. A
function is a group of statements that performs a given specific task.
Learning Objectives
By the end of the chapter, the reader would be able to:
Know the basics of functions.
Know the components of a function.
Distinguish the difference between user-defined functions and built-in
functions.
Understand the difference between void and fruitful functions.
Understand the arguments and parameters.
Identify the different types of arguments.
Identify the difference between function calls.
Understand namespace and scope rules.
6.1 Introduction to Functions
A function is a group of statements that aims to perform a specific task. For
example, in mathematics, a mathematical function f(x)=x2 takes the value
of x and returns x2 as output. Diagrammatically, it can be shown in Fig. 6.1.
Fig. 6.1: A Sample Mathematical Function
Mathematical function can be implemented straightaway as a function in
Python. For that, it is necessary to group statements to perform the square
function. A function is also known as a procedure, routine, or subprogram.
Formally stated, a function is a set of statements that can be called when
needed. When one calls a function, one need not have to worry about how
the function is implemented. This principle is called abstraction, as one can
ignore the inner-level details of the function and instead worry only about
the higher-level details. This is called the principle of abstraction.
The advantage of a function is that, instead of writing a long program, one
can divide that program into a set of functions. As a result, one can get the
following advantages:
1. The main principle in programming is avoiding duplicate code. So, a
function can be created whenever there is a repeating code. Whenever
needed, a function can be called. Therefore, functions are convenient for
removing duplicate code.
2. It is difficult to maintain a long program. So, it is better to divide the
long program into manageable functions. This facilitates better reading.
3. A function can have many local variables. So, one can change local
variables, if necessary without affecting the other functions.
Functions are closely related to program design. One can recollect the
stepwise refinement paradigm introduced in Chapter 2. In stepwise
refinement, a problem is divided into a set of problems, which are then
divided further into subproblems. This decomposition is done until the
problem is divided into tasks that can be converted into code, typically a
function. For example, a daily wage calculation problem can be divided in
terms of functions as shown in Fig. 6.2.
Fig. 6.2: Stepwise Refinement of Daily Wage Problem
Three functions or modules can be created to handle inputs, compute daily
wages, and finally print all the wage details. The main function can be
created to handle all these functional calls. Thus, the a lengthy program is
split into a program of manageable functions, as shown in Fig. 6.3. It makes
sense to write a function that can accomplish such a task.
Fig. 6.3: Illustration for Use of Functions
This approach has many advantages. Some of the advantages are listed in
the following Table 6.1.
Table 6.1: Advantages of the Function-Oriented Approach
6.2 Types of Functions
There are many types of functions. One way to classify the functions is
shown in Fig. 6.4.
Fig. 6.4: Types of Function Classification
Already, the built-in functions are discussed in Chapter 3 of this textbook.
What is a built-in function? The user can use these functions without the
need to write the code from scratch. These Python-provided functions are
called built-in functions.
Python provides certain functions, such as int(), float(), print(), and input()
to facilitate rapid program development, as a user cannot develop
everything from scratch. Python provides many such built-in functions.
Chapter 3 of this textbook provides some introduction to built-in functions.
The following script illustrates some of these built-in functions.
Another related concept is a module. A module is a higher-level concept of
a collection of many functions. Simply stated, a module is a Python
program that consists of many variables, objects, and functions that can be
imported into any other Python program, modules, and packages exist.
For example, a math module is a collection of many math functions. To use
a math module, one has to import it. The usage is illustrated in the
following script.
Also, one can write functions based on the needs of the program. Python
cannot provide all functions. The users can add their own functions, called
user-defined functions, based on their requirements and specifications.
These functions are called user-defined functions, and it should be noted
that functions are meant to do a specific task.
The rest of the chapter deals with user-defined functions.
6.3 Anatomy of a User-defined Function
The syntax of the user-defined function is given below.
The components of a function are given below.
1. Function definition
2. Function call
Function Definition
A function definition specifies a function using two components – function
header and function body. This is shown in Fig. 6.5a-b.
Fig. 6.5a: A Function Template
Fig. 6.5b: A Sample Python Function (Listing1.py)
The function header is the first line of the function. It consists of a keyword
def (short form of define), followed by the function name, and an optional
list of parameters inside the parentheses. The function header is also known
as the function signature. It must be noted that the parameters are optional,
as there are functions without any parameters. In the sample function (Fig.
6.5b), the header is def mult(x,y). Here, mult is the name of the function, x
and y are called arguments, and the function body has comments called
docstring and one or more Python statements.
Naming conventions of a function name
Legal python identifiers
Upper or lower case letters
Underscore (except the first letter)
No keywords
If standard keywords are used, the user function would be overwritten on
the standard functions and the user may no longer have access to it. One
can use a del command in the prompt to get back to the old state.
Dunder or magic variables (Those which start with a double underscore
and end with a double underscore would not be used as they are reserved
by the python developers for future use.
Function Body
The function body consists of statements that are indented under the
function name. It includes a docstring and a group of statements called a
block, or suite. The end of the block is the first line that is not indented.
Function Call
A function is activated only when it is called upon. The function should be
called or invoked. Otherwise, functions are never executed. A function call
is done by writing a function name and a list of parameters (if desired). A
function (Refer to Fig. 6.6) may be invoked by a function call that is
mult(6,7).
When a function is called, a set of values are passed. One or more values
are passed. These are called arguments. Inside the function, these arguments
are assigned to variables called parameters.
Fig. 6.6: A Sample Function Call
The order of execution is called the flow of execution. It refers to the way
statements are executed or run. The function definition does not change the
flow of execution. When a function call is made, the order of execution is
altered. It creates a stack frame where all Python objects are recorded. The
flow of execution jumps to the function and all statements of the function
are executed. After that, the flow of execution returns back to the point of
function call.
Let us go one step further and assume a function called mult(x,y) exists as
shown in Fig. 6.7. Then, the function can be called by the main program by
passing values 10 and 20 to the function mult (which also exists in the same
Python program).
An argument is any data passed into a function when the call is made (in
this case 8 and 9). A function that passes parameters to the function is called
the caller program and the other program is called the callee (Refer to Fig.
6.7). The function computes the result and send it back to the caller
program or print the result in the function itself.
Fig. 6.7: Arguments and Parameters
Arguments Vs Parameters
The data from the function call to the functions are called arguments. An
argument can be called an actual argument or an actual parameter also. An
argument is a value that is passed from the function call to the function and
serves as input. For example, if a call is made as mult(8,9), then information
8 and 9 are called arguments.
The arguments can be:
Literals
Variables
Expressions
One can combine variables and expressions and can pass through the
function.
Functional composition means the ability to combine small building blocks,
such as variables, expressions, and statements.
A parameter is a placeholder for actual values that are provided by the caller
program.
Parameters are called formal parameters or formal arguments. By itself, a
parameter that does not have any value. In the mult function of Fig. 6.7, the
variables x and y that are used to receive the values of a function call are
called the parameters. In this case, x and y are called parameters.
Docstring is optional function documentation. The following note gives a
complete explanation of it.
Docstring
One can use a docstring to describe the purpose of the function. Docstring
is the optional documentation that can be added at the start of the function.
Docstring comments are useful to describe the function. Docstrings are
triple quotes (three double quote comments) and extend over multiple ones.
The example is given below:
It can be observed that as part of the docstring, any comments can be given.
What is the advantage? The command help() can be used to get a function’s
documentation.
It is beneficial to describe the function so that when help() is used, these
descriptions will be displayed providing help to the programmer. Python
Interpreter during the function creation, by default, creates two variables
called Dunder or magic variables such as,
The variable, __name__ stores the name of the function.
The variable __doc__ stores all the docstrings. The above document can be
printed using this variable.
6.4 Types of Functions Based on Parameters
Functions can be classified based on parameters as non-parametrised and
parameterised functions, as shown in Fig. 6.8.
Fig. 6.8: Types of Functions
6.4.1 Non-Parameterised Function
A function need not send any information to the called function. In other
words, it is possible to write a Python function without any parameters also.
The following example illustrates a non-parameterised function.
Example 1: Write a Python program to print a warning message
(Listing2.py)
Let us analyze this program.
The first line of the function is called a header. Here,
The above statement is a function header or function signature. It can be
observed that it starts with a keyword called def, which is a shortform for
define. warning_message() is the function name, and an empty bracket, (),
indicates that the function does not accept any arguments. It ends with a
colon.
The rest is called a body. The function body has a statement. The function
body may have more statements, also called a block. In this example, the
function body has the statement
The statements of a function are not executed until it is called.
warning_message() is the function call of Example 1.
It should be observed that functions should be defined first before they are
used. Otherwise, Python reports an error. As soon as a function call is made,
the Python interpreter executes the function. Also, it should be observed
that indentation in function is a very important issue.
Indentation in Functions
Indentation is an important issue in functions. Every line below the function
header is considered part of the function. As per the PEP8 recommendation,
four spaces are required. It can be provided by either the Tab key or spaces
using the space bar. One should not combine Tab and space bar whites-
paces as Python interpreter may report an error. Consistently, Tab or a space
bar should be used but not both together.
6.4.2 Parameterised Functions
A parameterised function passes information as arguments to the functions.
The following examples illustrate parameterised functions where argument
x is passed and its cube is obtained.
Example 2: Write a Python program to find the cube of a number
(Listing3.py)
It can be observed that the function call is made to the function with the
argument (10) and the function computes the cube and prints it. This
function is a parameterised function as parameters are used.
The following is an example where a kilometre (km) of x is converted to a
mile. The formula to do that is x = x * 0.6214, where x is in km..
Example 3: Write a Python program to convert km to a mile
(Listing4.py)
Passing Multiple Arguments
Similarly, a parameterised function can pass multiple arguments. The
following example illustrates the same.
Example 4: Write a Python program to compute simple interest
(Listing5.py)
Simple interest can be calculated using the formula p*n*r/100, where p is
the principal amount, r is the rate of interest, and n is the number of years.
Example 5: Write a Python program to compute exponential
(Listing6.py)
It can be observed that the function call invokes the function with variables
x and y. The function computes xy and returns the value. The following
program illustrates the Python program for finding xy.
6.5 Void Functions and Fruitful (Value Returning) Functions
There are two types of functions based on the return values: void functions
and fruitful functions. This is shown in Fig. 6.9.
Fig. 6.9: Types of Functions
6.5.1 Void Function
The void function, when called, executes a set of functions and terminates.
A void function does not return a value. A void function may do some work,
but it does not return any value to the function call. It is a special value
called “None”. Sometimes, a void function may not do any work also. The
following script illustrates the same.
Another example of a void function that does not return any value is given
below:
Example 6: Write a Python program to compute some arithmetic
operations (Listing7.py)
Important Facts
It must be observed that the function call compute (a, b) makes the
assignment a=100 and b=200. Then, in the function, the computations are
performed based on the value of a and b. If the order is reversed, say(b, a),
then the values would be different. So, the order of the parameter is
important.
6.5.2 Fruitful Functions
A function can return a value. A function that returns a value is called a
fruitful function. Fruitful functions executes a set of statements given in the
function body and returns a value to the function call. A return function can
be a literal, variable, or expression. One can write a fruitful function for
computing an area of a circle.
Example 7: Write a Python program to compute the area of a circle
(Listing8.py)
Sometimes, a function may return multiple return values. Whenever a return
statement is executed, the statements that are after the return statement are
not executed. It is called a dead code. So, one has to take care that entire
program is covered.
A function can call another function. One can have a function inside another
function. It is called composition. A function can also return a Boolean
value too. A function can return any Python object such as strings, lists, or
tuples. One can even make a function to return another function, and these
are described in Chapter 11 of this book.
Example 8: Write a Python program to return a Boolean value
(Listing9.py)
Return Multiple Values
The following program illustrates the use of a function that returns multiple
values.
Example 9: Write a Python program to return multiplication and
reminder for two numbers (Listing10.py)
The above function can be made to compute both multiplication and
difference.
Return Statement Examples
A function may have one or more return statements. Some of the valid and
invalid return statements are given below.
The unreachable code is called the dead code. The following note illustrates
this.
Dead Code
A function may have multiple return statements. As soon as the first return
statement is encountered, Python immediately ends its execution and the
control is returned to the calling program. All codes after the return
statement are unreachable. This code is often referred dead code as they are
useless and can never be reached.
6.6 Types of Function Arguments
There are five types of functions based on function arguments, as shown in
Fig. 6.10.
Fig. 6.10: Types of Arguments
6.6.1 Functions with No Arguments
This type of function has no arguments. There is no need to pass any
arguments. One of the examples of functions with no arguments is given
below:
Example 10: Write a Python program to illustrate a function with no
arguments (Listing11.py)
It can be observed that there is no exchange of parameters between the
function call and function.
6.6.2 Functions with Positional Arguments
A functional call is required to invoke the functions. As discussed earlier, a
parameterised function can send a set of arguments to the function to invoke
it. These arguments are known by various names such as positional
arguments, required arguments, non-keyword arguments, or
mandatory arguments. These variables are stored in the parameters for
further processing.
The arguments passed must be in the correct positional order, and there
must be a match between the number of arguments of the function call and
the number of function parameters. If there is no match, the Python
interpreter would signal an error.
It should be noted that the argument values are given position-wise and
order-wise. If the positional order changes, the program will give different
results. Hence, it is called positional arguments.
Example 11: Write a Python program to illustrate positional arguments
(Listing12.py)
It can be observed that the swapping of arguments (positions) changes the
values. The following script illustrates the problems when the number of
arguments does not match the number of parameters.
Example 12: Write a Python program to error if the number of
arguments does not match the number of parameters (Listing13.py)
The above functional call is an error as the functional call has only one
argument.
There can be one or more functions in a Python program. Added to the fact
that Python can be executed as part of a module or imported from another
program. Python, when it executes a program creates a special Dunder or
magical variable __name__ that stores the name of the program in it. If the
program is executed as part of the module, it stores the module name. Also,
if one is specific that the order is from the main function, one can execute it
directly as follows.
The following two programs illustrate this usage as these programs have
more than one function and one of them is main().
Let us discuss one more program called GCD. GCD is the greatest common
divisor that finds the integer that divide two accepted integers. The details
of GCD are given in Chapter 2 of this book.
Example 13: Write a Python program to find the GCD of two integer
numbers (Listing14.py)
The output of the GCD program is as given below:
Let us discuss one more program called finding the nth term of the
arithmetic sequence (Listing14.py).
Example 14: Write a Python program to find the Nth term of the
arithmetic sequence (Listing15.py)
The listing is given below.
The output of this program is given below:
6.6.3 Keyword Arguments
Keyword arguments are also known as named arguments. These are the
parameters that are identified by name. One knows that the name is
assigned by the assignment statement. Therefore, one uses an assignment
statement for assigning values.
For example, in the above program, the value can be provided using the
assignment as
Here, a and b are keyword arguments. The assigned values are passed to the
functions from the function call. The advantage of keyword arguments is
that one has more control and the value is provided by named arguments.
For Example 11, the value can be provided as
The advantage is that even if the positional order is changed as
the results would be the same. In other words, the major disadvantage of
positional arguments is negated by keyword arguments as there is no need
to match the order of the parameters that are defined in the function header.
6.6.4 Default Arguments
If the value of the arguments is not given at the time of call, the argument
can be initialised with the default value. A default parameter is a keyword
parameter that has a default value. This is a situation where the value is
already known. For example, if the value of b and c are known already one
can default value as compute(a, b=10, c=30) in functional parameters. So,
there is no need to pass the value for these arguments. The following script
illustrates this:
Example 15: Write a Python program to illustrate default arguments
(Listing16.py)
The values of the default can be overwritten. Only if the values are not
provided, the default values would be used.
Example 16: Write a Python program to illustrate default arguments
(Listing17.py)
It can be observed that both calls result in the same value.
Important Facts
In default arguments, if the default value for the parameter is given, then
there is no need to pass the value. Therefore, default arguments are useful
for:
1. Adding new parameters in the newly designed functions.
2. Useful in combining similar functions into a single function
If the value is not passed, the default value is used. Otherwise, the values
provided by the function call overwrite the default value.
Positional and keyword arguments can be combined but the positional
arguments come first before the keyword argument. For example
compute(a,b=10) is correct but compute(b=10,a) results in error.
6.6.5 Variable Length Arguments
Many times, one may not know the number of arguments in advance. For
example, consider the following functional calls.
add (10,6) # 2 arguments
add (10,7,4) # 3 arguments
add (40,50,60,70) # 4 arguments
add (50,60,70,80,90,90) # 6 arguments
It can be observed that the number of arguments varies or simply variable
positional arguments. These variable arguments are specified as *args and
the comma-separated positional values are gathered in tuples. *args just
indicates that it should be expanded as a sequence.
The syntax of variable-length arguments is given as
Important Facts
In *args, instead of args, any name can be used. Any number of variable
positional arguments can be passed. These variable numbers of positional
arguments can be gathered as a tuple.
Credit: Screenshot of Microsoft Windows PowerShell, © Microsoft 2022
It can be checked that the positional arguments are collected as tuples. All
tuple operations can be done. Or, the tuple can be unpacked and used as a
normal variable in functions. The variables can be accessed using the index
too.
Example 17: Write a Python program to illustrate variable positional
arguments (Listing18.py)
The result indicates the sum of a variable number of values.
6.6.6 Variable Keyword-Based Arguments
One of the major advantages of keyword-based arguments is that there is no
need for positional order. To handle variable keyword arguments, one can
use **kwargs. Any name other than kwargs can be used to collect the
keyword-based variable arguments as a dictionary. **kwargs just indicates
that it should be expanded as a dictionary.
Important Facts
In *kwargs, instead of kwargs, any name can be used. Any number of
variable keyword arguments can be passed. These variable numbers of
keyword arguments can be gathered as a dictionary.
Credit: Screenshot of Microsoft Windows PowerShell, © Microsoft 2022
It can be checked the keyword arguments are collected as a dictionary. All
dictionary operations can be done and the variables can be accessed using
the index too.
Example 18: Write a Python program to illustrate keyword variable
arguments (Listing19.py)
Also, all types of arguments can be combined.
Combining Keyword and Non-Keyword Arguments
One can also combine keyword and non-keyword arguments. The order of
combination is given below.
So, it is formal positional arguments, followed by the variable number of
positional arguments, and the variable number of keyword arguments. If the
order is changed, Python would signal that as an error.
Functional Calls
There can be many functional calls involving all types of arguments. The
following list indicates some of the valid and invalid functional calls.
Assuming a function dummy with some parameters is available, then
6.6.7 Python Anonymous Functions
Python anonymous functions are nameless functions, that is, those functions
are not tied to any name. These functions are also known as Lambda
functions. The syntax of the anonymous functions is given below.
Lambda functions are single-line statements that are specific to the
specialised tasks. These functions are created in runtime and do not require
the def keyword or name. Lambda functions are single-line statements, and
they do not contain multiple statements as in standard python functions.
Example 19: Write a Python program to illustrate lambda functions
(Listing20.py)
Lambda functions are part of functional programming. Functional
programming, along with lambda functions, are discussed in Chapter 11 of
this book.
6.7 Passing of Objects Reference
In most programming languages, there are two types of communication
between the function caller and function definition. They are
1. Pass by value (or call by value)
2. Pass by reference (or call by reference)
In pass by value, a copy of the variable is passed and if any modification is
done to the variables, they will not be reflected outside the function. In
pass-by reference, a memory address of the values is passed, and, here, the
modification carried out by the function is also reflected in the outside
functions. In Python, neither explicit call by value nor call by reference is
there. It is a bit complicated here.
Python passes object reference. Recall from Chapter 3 that all Python
values or variables are objects. So, one can pass two types of objects:
immutable objects and mutable objects.
The pass by value corresponds to the pass-by immutable objects. Recall that
integers, floats, and strings are immutable. To understand immutability,
first, the first concept of assignment should be understood.
Let us consider the following assignment.
This creates a table as shown in Fig. 6.11a.
Here, x is said to refer to the object or bound to the object. Let us make
another variable y assigned to x. This leads to the scenario shown below in
Fig. 6.11b.
Now, let us assign x to something else, say, x=20. This leads to a scenario
as shown below in Fig.6.11c.
Fig. 6.11a-c: State diagram of Variables
It can be observed that the variable y still points to 10. In other words, the
integer is immutable. In pass by value, whatever changes are made in the
function, they are not retained and reflected outside the function.
Example 20: Illustration of immutable objects (Listing21.py)
It can be observed that whatever change is done inside the function is not
reflected outside the function, and one can also observe that the id of x is
different.
Pass By Reference
In pass-by reference, the object reference of mutable objects is passed to the
function definition.
This is called pass-by mutable objects. In other words, the function
definition makes an object alias. If the object gets modified in the function,
a new object is not created. In the following listing, a list is a mutable
object. Therefore, the assignment lst[0]=1, modifies the index of the myList
to 1. The list is modified by the calling program. Therefore, the change is
visible to the outside function too. But there is one caveat—if a new list is
created inside the function instead of modification, the effect is not visible
to the outside. In that case, it behaves like a pass by value.
Consider the following Python segment.
Example 21: Write a Python program to illustrate pass-by reference
(Listing22.py)
Fig. 6.12: State Diagrams
6.8 Namespaces, Global Variables, and Scope
The concept of namespace and scope help to identify variable hierarchy in
the Python context. A namespace is a naming system and it is also known as
context. All python objects have a name that is associated with a
namespace.
A namespace is a directory, mapping names to values. In other words, a
name is a key to the dictionary. It is mapped to values. All these things are
done at the backside and a program is not even aware of it happening. For
example, whenever an assignment to a Python object, say k=6, the
namespace is modified.
When a new function is defined, a new namespace is created. All
namespaces can coexist at the same time but are isolated from each other.
Some of the namespaces are as follows.
1. Global namespace: these are the names of the modules
2. Local namespace: the names in the function or function calls
3. Built-in namespace: all the names of the built-in functions and
exceptions
All the namespaces have a life period. The built-in namespace is active
from the moment when the Python interpreter starts up and it is not deleted
until Python Interpreter quits.
When a module is read, the global namespace is generated. It ends only
when the Python interpreter quits. When a function is invoked, the
functional namespace is alive. It lasts till the function returns the value or
when exceptions are made, which is not dealt with inside that function
These details are summarised in the following Table 6.2 for better
understanding.
Table 6.2: Namespace Details
The scope is closely related to the namespace. A scope can be defined as a
program region where the namespace can directly be accessed without the
namespace prefix. The scope is defined as the part of the program where the
variable is visible or accessible. In other words, scope determines the
accessibility of Python objects. Scopes are static, but it is often used
dynamically.
Example 22: Show the namespaces of these two functions
(Listing23.py)
Fig. 6.13: Function Namespace
Scope of a set of names mapped to objects. To keep track of the variables,
one can use a diagram called a stack diagram. Each function is drawn as a
frame as shown in Fig. 6.13. A frame is a box with the function name. All
parameters and variables are given in the block. The list of functions is
called a traceback. When an error occurs, the Python interpreter shows
exactly where the error occurs. The lifetime of x and y are only func1. If
one tries to print outside the function, then Python gives an error. It gives an
error as a name error as the variable is undefined.
Before that, one needs to know about variables. They are three types of
variables.
1. Local variables
2. Global variables
3. Non-local variables
Local Variables
All variables that are defined inside a function or present in local scope are
called local variables. Therefore, local variables can be accessed only inside
the function. A local variable can share the name with global or names of
another function.
The following segment illustrates the usage of local variable n, where inside
the function, its value is 10 but outside the function, it gives error as n is
printed before it is defined.
Example 23: Illustrate the local variables (Listing24.py)
Global Variables
Global variables are defined outside of any functions. All variables that are
outside the function or in global scope is called global variable. Its life
period starts from the point where they are defined and ends with the
program termination. All functions can access global variables.
Example 24: Write a Python program to illustrate global variables
(Listing25.py)
It can be seen that the value is 10 as the variable is global. If the keyword is
not defined as global, the value of that variable is zero as it becomes a local
variable.
Problems of Global Variables
As one knows that an assignment statement creates a variable. If a variable
is created outside a function, the variable is termed a global variable.
Python does not allow constants, but one can use variables to simulate
constants.
There is no need for the global keyword if the variable is defined outside
the function. In other words, a global variable is accessed by the statement
of any function.
But inside the function, if one attempts to modify it, then the global
keyword is necessary. In other words, the variables inside the function
should have the global word, so that they can be read and written.
The global variable has three problems
1. Global variable makes debugging difficult, as it is difficult to track down
statements that access that variable.
2. Global variable makes functions dependent. Any change in the global
variable is likely to affect the functions.
3. Global variables are very difficult to understand.
Non-local Variables
Nonlocal variables are the newest in Python. The scope of the nonlocal
variables is not defined. This makes the variable neither global nor local. If
the nonlocal variables are modified, they are manifested in the local
variables too.
Conflict Resolution in Scope Using the LEGB Rule
The types of scopes are given below.
1. Built-in: the highest priority is given to the built-in names; it must be
noted that all the keywords are defined in the built-in scope. This is
created as soon as the interactive session is created. It is created as soon
as a script runs. All the keywords, functions, modules, and exceptions
that are built in Python are the contents of this built-in space. One can
access these names anywhere in the program.
2. Global: global scope is the topmost scope. It contains variables that are
defined at the top level of a program or module, and all these names in
the scope are visible everywhere if the program code
3. Enclosing: this is applicable for enclosing functions. In the case of
nested functions, as shown below in Fig. 6.14.
Fig. 6.14: Encompassing Function
Hence Func1 is called an enclosing scope. The enclosing scope is
applicable for function funct2. All the names that are defined for the
outer function have visibility for the enclosing function and the inner
functions.
4. Local: the innermost function is the local or current scope. This is the
current location where the Python interpreter works. It is a code block or
body of a function. Many different local scopes are possible. Each call
will create a new scope. When recursion is used, at every call, a new
scope is created. A local scope is created when a function is invoked.
Inside this, all the names of the functions are present and only these
variables are visible. If anyone attempts to access the variable, it results
in a name error because the local variable is visible only to the function.
Consider the following Python segment.
Example 25: Write a Python program to illustrate scope rules
(Listing26.py)
It can be observed that the value of func() scope is local. It is called local
scope. The code outside the function is called a global scope.
Example 26: Illustration of scope (Listing26.py)
Here, the variable definitions varies as scope changes. LEGB is a name
resolution mechanism and a sort of lookup procedure guiding how to look
out for a name.
The name or scope resolution is done by the LEGB rule.
1. Built-in
2. Global
3. Enclosed
4. Local
LEGB is a rule that the Python interpreter follows. Whenever a name is
encountered, first, it checks the local scope, then the enclosing scope, the
global scope, and at the end built-in scope. Otherwise, the Python
interpreter marks that as a name error.
Consider the above Example 26. In the inner function, the value of x is 4,
and in the outer function, the value of x is ‘Hello’; if nothing is defined, the
value of x is 9. This kind of resolution is called the LEGB rule, which
stands for Local Enclosing Global Built-in scope.
Summary
1. A function is a group of statements that aims to perform a specific task.
2. The basic principle of function is an abstraction. A function is a compact
unit as function variables are known only to it.
3. Functions help in the reuse and modularisation of a program and help in
testing and maintenance.
4. The function has a header and a function body. The function header has a
keyword followed by the function name.
5. Docstring describes the purpose of functions as functional
documentation.
6. Functions are invoked by a function call.
7. Functions are classified as void functions and fruitful functions based on
the return value. Void functions do not return any value, and the return
type is called None.
8. Fruitful functions return one or more values to the function caller.
9. Functions can be invoked either by passing an argument or no without
arguments.
10. Positional arguments are a set of arguments that are identified by the
position. If the order is changed, it gives wrong results.
11. Keyword arguments, or names arguments, are arguments that are given a
keyword name. Keyword-based arguments can be sent without any
positional order.
12. Default arguments are those where the function substitutes a default
value if the value is not passed by the function call.
13. A function can send any number of arguments as *args where these
comma-separated values are treated as tuples. Similarly, any number of
keyword arguments can be collected as **kwargs.
14. Anonymous functions are name-less functions that are created using
running time. These are known as Lambda functions.
15. A scope is defined as the part of the program where the variable is
visible or accessible.
16. Local variables are defined inside the function, and global variables are
defined outside the function.
17. Pass by value is one sort of communication between two functions
where a copy of the argument of the function caller is manipulated. In
Python, it is known as an immutable object.
18. Pass-by reference is one where the variable is passed to the function and
its effects are retained outside the function. In Python, it is known as a
mutable object.
19. The types of scopes are given as built-in scope, global, enclosed, and
local. This kind of resolution is called the LEGB rule.
Glossary
Function - a group of statements that aim to perform a special task. It is
also known as a procedure or subprogram.
Abstraction - removing unnecessary details and concentrating on higher-
level details is called abstraction.
Reuse - The process of using already developed code.
Built-in functions - the functions that are provided by the Python
language itself are called built-in functions.
User-defined functions - the user developed functions for their specific
problem.
Void function - a function that does not return the value or returns None.
Fruitful function - a value-returning function with one or more return
statements.
Arguments - any data passed from a function call to a function. It is
known as actual arguments.
Parameters - the function uses variables as placeholders to receive the
argument from the function call is called parameters.
Block - a set of statements is called a block or suite.
Positional arguments - positional arguments, required arguments, non-
keyword arguments, or mandatory arguments are the same. These
arguments are identified by their positions.
Default argument - if the value of the argument is not given, the
argument can be initialised to a default value.
Variable arguments - if one does not know the number of arguments in
advance, the variable arguments are specified as *args.
Keywords based arguments - a variable keyword-based argument where
arguments are placed in any order. This is also known as named
arguments.
Anonymous functions - nameless functions having a single line
statement for performing a specific task. These are known as Lambda
functions.
Scope - A part of the program where the variable is visible or accessible.
Local variable - variables that are defined inside a function where only in
local scope the variables are visible or accessible.
Global variable - a variable defined outside the function whose life
period starts from where they are defined and ends with the program
termination.
Mutable variable - a Python object whose value will change.
Immutable object - a Python object whose value cannot be modified.
Built-in - The topmost scope where all Python keywords are defined.
Enclosing scope - a one-level above function that encompasses an inner
function.
LEGB - Local Enclosing Global Built-in rule that specifies how
variables are accessed.
Questions
1. What is a function? List out the advantages of functions.
2. What are the components of a function? Illustrate with an example.
3. Distinguish between void functions and fruitful functions.
4. What are the types of functions based on arguments?
5. What is a dead code? How to avoid it?
6. What is a default argument? In what way does it help?
7. What is a keyword argument?
8. How *args handle the variable arguments?
9. What is a Lambda function?
10. Explain the Python scope management mechanism.
11. Explain the call-by-call value and call-by-reference mechanisms.
12. Explain the LEGB rule for scope resolution.
Programming Exercise
1. Write a function for swapping two variables.
2. Write a Python program to print a roll number and name of a student
using functions.
3. Write an iterative version function of the factorial of a number.
4. Write a Python function to compute average for variable length
arguments of numbers.
5. Implement a Python function for bubble sort a set of numbers {10, 2, 7,
24, 32}.
6. Write a Lambda function for finding the multiplication of two numbers.
7. Write a function for finding whether the given number is an Armstrong
number or not.
8. Write a function for checking whether the number is a prime number or
not.
9. Write a function that finds the Nth element of a geometric sequence.
10. Write a program to check whether the number is a perfect number or not.
OceanofPDF.com
Smart data structures and dumb code works a lot better than the other way
around.
— Eric S. Raymond
CHAPTER 7
Lists
A list, also known as a linear sequence, is one of the most common ways to
organize and access a large amount of data. A list is an ordered collection of
items or elements. A single variable identifies each element. This chapter
aims to enlighten the reader on Python list creation, manipulation, and
operations. Finally, it teaches how to use the built-in list functions and
methods in programming.
Learning Objectives
After completing this chapter, the reader will comprehend the context and
its associated questions:
Usage of List
Learn to create, access, and use lists in Python
Use built-in methods and functions to manipulate lists and list indices
Know the typical operations performed on lists
Perform traversals of lists to process items in it.
Handle nested lists
Iterative control with Python’s “loop” statement and how to make the
most of it.
7.1 Introduction to Iterable Object
A variety of lists are used in almost all common operations, including the
grocery shopping lists, to-do lists, books in a library, phone numbers in a
contacts list, a vocabulary list, and browser programs that keep track of all
of the bookmarked websites. Multi-item data storage uses strings, tuples,
and dictionaries. Lists carry sequential data types, and it mostly contains
random objects from the real-world. When data is arranged into collections,
it’s much easier to apply operations to groups of data without repeating
code. Data processing scenarios always handle such sequences. Real-world
situations need handling more than one data type item. Python’s range of
data types makes it easier to manage data sets. Developers call the list of
allocated addresses via an index. In short, lists allow one to deal with many
data items and sequentially process them.
7.2 List
A list in Python is an ordered collection of elements referred to by a single
variable name. A list is a linear data structure. Frequently, lists are referred
to as Python arrays. Lists may have zero or more items. The constituents of
a list are referred to as items or elements. Table 7.1 shows a list of rate of
one kilogram of tomatoes (in rupees) for each day of a given week. Here,
each element in the list is identified by its index value.
Table 7.1: Indexed Data Structure
Primary Properties of Lists
The essential features of Python Lists are as follows:
A list is mutable, meaning the list’s contents may be altered.
Lists in Python use zero-based indexing.
Objects of any kind can be found amongst them.
An index provides quick and easy access to list items.
There is no cap on the number of sub-lists that can be encapsulated in a
single list.
A list can be of a wide range of sizes.
7.2.1 Creating a List
Lists are a vital mechanism in Python for assembling various values.
Python lists are more flexible and more powerful than “classical” arrays.
Fig. 7.1: Creation of List
The list elements don’t have to be of the same type and can be an arbitrary
mixture of elements like numbers, strings, other lists, etc. List objects are
created when the variable elements are enclosed by square brackets and
separated by commas (Refer to Fig. 7.1, Table 7.1).
Ways to Create a List
There are two ways to define and create lists in Python. Python syntax for
creating a list is as given below:
The comma-Python syntax separated characters, items, or elements in the
first method must be placed within the square brackets ([]). Arrange the
series of characters inside either single quotes, double quotes, or triple
quotes, and then allocate it to a variable. An open square bracket represents
a list by a variable named “items,” with the elements separated by commas
and a closing square bracket.
Example 1: Illustration for list creation.
In the first method, the square brackets show that lists 5,6,7,8 and 9 are
getting created. The items are deposited in the given order.
The second method calls the Python list () and passes the items to it.
In both the cases, the output will be the same as shown below:
Notes
7.2.2 Indexing in a LIST
Each cell in a list is assigned an index (Refer to Fig. 7.2). A list is a
sequence of elements. To access the values of elements in a list, pick the
corresponding element from the list by specifying which index is to be used
with the subscript operator ([]). In Python, accessing elements is identical to
accessing strings.
Pass the index values and therefore receive the required values. The
following example shows that to pick the value of the last element in the
list, “-1,” index is specified to be used with the subscript operator ([]). To
access an item or element from the variable “name_of_the_list,” the syntax
is
Fig. 7.2: Indexing of List
Example 2: Illustration for accessing the selected elements of the list
The program below shows the assignment of various languages to the
variable “languages1”; and how to access the particular elements from the
list.
Fig. 7.3: Accessing values of elements in a list
To access an element from a list, type the values of the element to be
selected (Refer to Fig. 7.3).
The above example shows that the fourth element in the list that can be
accessed is Telugu. Similarly, slices of the first, second, fourth, fifth and
sixth elements from the list of languages may be obtained.
7.2.3 List Slicing
Python’s slicing relies heavily on the slice function. Extracting data from a
list of characters is possible; with a list, the values can be accessed as per
the requirement. For example, slicing works well to display only part of a
serial number or a name on a form. The syntax of slicing is given as
follows:
One or more elements are selected at once without a list of options using
the slice notation square brackets. A slice is a list’s truncated subset. It is
possible to quickly select individual items from a list using Python’s syntax.
Python’s slicing technique makes extracting the desired elements from lists
easier. The components of a list can be accessed in a variety of ways. Using
an index operator in conjunction with [] is an example. Slicing may also be
obtained from the list by referring to the list and specifying two indices
(separated by a colon) to reference between. The first index number
identifies the first item in the list, while the second identifies the last item.
The example shows the usage of the method index(). This method gets the
value of the specified element in the list “Greek Alphabets.”
Two or three parameters can be used in the slicing syntax ([start:end] or
[start:end:step])
start: The slice’s beginning index. The default value is Zero.
end: The slice’s final index. The default value is Zero.
step: The items are selected based on the “step” intervals between the
beginning and the finish. In this case, it’s set to 1.
The range of characters obtained with the slicing function if the element’s
index is known can be recalled from earlier portions of this chapter. Lists
can be indexed starting at the zeroth place in Python, and negative indexes
are supported. The last character is represented by the index ‘-1’. Using
‘-2’, the second-to-last item in the list may be retrieved, and so on.
The subscript operator uses a process called slicing to extract desired values
of Python lists using a slicing colon “:”. Python’s subscript operator obtains
a sublist through a process called slicing. To extract a sublist, the
programmer places a colon (:) in the subscript. An integer value can appear
on either side of the colon.
Example 3: Illustration of the slicing operation
The python program for the slicing operation would be as follows:
A list of greek_alphabets is created as follows.
The elements indices from the 0 to 5 (totally, six elements) can be extracted
as sublist using the slicing “[0:6]”. The slicing is executed on the above
created list greek_alphabets as follows.
The above slicing can also be executed to get the same result by using
slicing as “[:6]”, where 0 is not present.
Similar to the above, the elements from 6th index to the end of the list are
extracted using the slicing as “[6:]” and the execution is as follows.
Every second elements in the list can be extracted by slicing as “[::2]” and
execution on the list Greek_alphabets.
The list can be reversed by using the slicing operation as “[::-1]”, where the
“-1” implies the step size of 1 in reverse order and so it extracts the
elements in the reverse order. Thus, the slicing creates a reversed sublist of
the given list.
The slicing operation “[0:6:2]” extracts the elements from 0 to 5 with step
of 2, which corresponds to indices of 0, 2 and 4 and it is executed as
follows.
The slicing operation as “[1:4:2]” extracts the elements with index from 1
and 3. The following execution extracts the index of 1 and 3 from the list
greek_alphabets as follows.
Example 4: Illustration to show the slicing() and substitution in a list
Another python script for the substituting and slicing operation would be as
follows:
7.3 Operations in the List
Lists are simply a collection of heterogeneous items done in a particular
manner; that may be rearranged, transmogrified, and blended. Depending
on the requirement, lists can be manipulated based on the inevitability of a
problem. Most of the time, list manipulation can be done by hand with the
help of operations. Lists are Python’s most widely used data types due to
the variety of operations they can perform. List operations are used to
construct lists and other objects from lists. Below is a list of the various
special operators available in Python.
Table 7.2: Operators in Python
Three operations [+, *, [] ] can operate with any sequences (including
Lists).
Concatenation (+) Operator-It is a binary infix + (plus) operator
regularly overloaded to connote list concatenation. Concatenation of lists
means joining the two operands by linking the lists end-to-end. The syntax
for the concatenation (+) operator is given as :
If two lists are to be concatenated with a plus (+) operator, then a new list
that contains the elements of the first list, followed by the elements of the
second list, will be generated. For example, to join (concatenate) several
lists like the last name, middle name, and first name, together concatenation
operators help to do so.
Example 5: Illustration to show the concatenation operation
Consider two lists that contain the basic elements of German silver. The
python script to demonstrate the concatenation operation would be as
follows:
Concatenating the two results in a new list which contains the full list of
elements for German Silver.
Example 6: Illustration to show the concatenation operation.
One more python program to demonstrate the concatenation operation
would be as follows:
Example 7: Illustration to show the multiplication operation
To get the concatenation of the same list numerous times, the replication
operator (*) is used, with an integer to specify the number of copies the list
is to be generated.
Replicate and Repetition – When dealing with numeric values, the “*”
operator has a different purpose than the multiplication operator. With one
list and one integer, “*” replicates a list as many times as the integer allows.
To put it another way, repetition - generates new lists by appending several
instances of the same list. Given below is an expression for the replication
(*) operator between two lists:
Example 8: Illustration of replication (*) operator
The python script would be as follows:
One of the operands is expected to be a list, and the other is an integer. This
clarification of + and * helps make sense of the connection between
addition and multiplication. The output of the above example shows the
result, as seen in the screen, seven times for the number 5555 and 3 times
for the fastest_dinosaur of the operation, of concatenating three times on the
word ostrich mimic ornithomimids. For example, ‘ostrich mimic
ornithomimids’ *3 times is ‘ostrich mimic ornithomimids’, ‘ostrich mimic
ornithomimids’, ‘ostrich mimic ornithomimid.’
== operator for equality testing -Python allows the comparison of two
lists to see if they are exactly equal, meaning they have the same elements,
in the same order, by using the == operator.
Example 9: Illustration of an equality testing operator
The python program to demonstrate the use of the equality testing operator
would be as follows:
The above execution yields the output as true, indicating that the two lists
are identical.
The second one will give the output false, indicating that the two lists are
different.
Example 10: Illustration to demonstrate the inequality operator !=.
The opposite of == is !=.
The items on both lists should be the same. Both lists must have unique
entries and the same distribution of those elements. Both lists’ component
ordering must be identical, i.e., listed in the same chronological order.
Membership Operators
1. “in” operator - This operator determines whether or not an element is
contained within a list. If an element is present in the list, true is
returned; otherwise, false is returned.
2. “not in” operator - This operator determines whether an element is or is
not present in the list. If the element is not present in the list, true is
returned; otherwise, false is returned.
7.4 Built-in Functions and Methods in List
comma-separated values placed inside the square brackets, compose a list.
Python lists have many uses. Python lists can include text, numbers, and
objects. All mutable Python lists need not be the same. They’re Python’s
best defence. Altering lists is possible in Python. Python calls this
mutability. Python’s changeable lists let developers adjust list size on
demand. List items can’t be edited, but members may. Python’s mutable
lists are sorted and fixed-length. Indexing a list of items follows a sequence.
A list is indexed by starting with zero. The list is hierarchical. List
duplication is allowed. Each list item is valid and relevant. A wide variety
of built-in Python functions can be used to manipulate and operate on lists.
It makes the lists mutable when the Python List’s content can be changed.
Due to the mutable property, the lists return a modified list when these
functions are acted upon.
Notes
These functions and operators apply to all sequence data types, including
string, list, and tuple.
Table 7.3: Built-in functions in list (Just a few)
Readers are encouraged to try out the various other in-built functions.
When it comes to built-in methods, they are defined by the actions that can
be made against them and how they may be changed. When a method is
invoked on a newly generated object, the dot operator is used to accomplish
the task (.). Python’s built-in procedures for modifying a Python list are
numerous and may be found throughout the Python programming
language’s documentation.
Listed further down are some built-in functions and methods.
Table 7.4: Built-In Methods in the List
extend(seq)-The list.extend() method in Python outspreads the initial list
with all the elements added from a second list. The Python list method
extend() keeps adding each element to the list and outspreads the list. The
extended list’s length will be augmented with the magnitude of the iterable
conceded to the extend().
Example 11: Illustration to show the usage of extend().
The python program shows the usage of the method extend(). This method
outspreads the elements present in the two lists.
Note
The sorted() and sort() methods perform an identical operation: sorting the
list’s contents.
While sorted() returns a value, sort() modifies the original list.
reverse or list.reverse() or reversed(iterator) -The list.reverse () method
in Python reverses the order of all the elements in the list by which they are
placed in the iterable.
Example 12: Illustration to show the usage of the method reverse().
(Listing 1.py)
The list can be reversed in any of the following three ways:
1. list.reverse()-
1. While the list is still active and dynamic, the order of the items is
rearranged backwards.
2. This is very fast and efficient, and it takes up little space on the
computer.
3. The list is written backwards; and changed from what it was
originally.
2. List slicing [: :-1]
1. An inverted replica of the list is obtained after it is subjected to the
operation of list slicing [: :-1]
2. This function occupies more memory storage space, and the original
list remains intact
3. reversed()
1. Result is a list that positions the elements in a backward order, and the
original list remains intact.
2. A counter-iterator is returned.
The python script shows the usage of the method reverse (), reversed (), and
List Slicing [: :-1]. This method inverses the elements present in the list
“Extinct_Indian_animals”.
#This is the screenshot of the output for list.reverse() operation
clear()-The list.clear() method in Python dispels all the elements in the list
by clearing all the items from the iterable.
Example 13: Illustration to show the usage of the method clear().
The python program shows the usage of the method clear(). This method
removes the elements present in the list “Extinct_Indian_animals”.
Deleting an element in a list -Certain application situations demand the
deletion of some aspects in a list. Any one of the following ways can delete
the items or elements in a list.
1. Remove an element within a list using the pop method. If the index is
known, it modifies the element in the specified index or removes and
returns the last element. The pop() function returns the value of an
element at a particular index after it has been removed.
2. Eliminate an element within a list using the remove method. Remove() is
a function that removes the first instance of a value in a list.
3. Dispels all the elements in the list by clearing all the items from the
collection iterable, using the clear method.
4. Deleting a single index, a slice of a list, or the entire list is possible using
del(). To remove more than one element, del with a slice index is better.
del[a:b] – The list.del() method in Python deletes the elements specified
within the (), which mentions the start and end points to be deleted from
the iterable.
Example 14: Illustration to exemplify the del()(Listing2.py)
The example shows the usage of the method del(). This method removes the
elements present in the list “Extinct_Indian_animals”.
#This is the screenshot of the output for list.del() operation
7.5 Nested List
It is probable to hold any item in a list with items from another list (sub-
list), and so on. These are denoted as “nested lists,” which are useful when
creating hierarchical data structures.
1. To Create a Nested List- In Python, the most straightforward way to
create a nested list is to establish a list and then layer one or more other
lists inside it. Then, like with ordinary list components, build another
nested list by layering two non-empty lists within another list. Nested
lists are formed by nesting sub-lists within an additional nested list,
which is signalled by using commas to separate the sub-lists within the
parent nested list.
2. To Access the Nested List Items by Index -Finding specific items
within a Nested List may be accomplished using its indexes. In a nested
list (Refer to Fig. 7.4), to retrieve specific items within each list, use a
variety of indexes.
Example 7.15: Illustration of creation of nested lists.
A nested list
A deeply nested list
Another deeply nested list with single quotes.
Example 15: Illustration to access nested list items by Index
Python code illustrating how to access items in a nested list using an
index looks like this:
Work behind the scenes
Because of the limitations of the illustration, the indices for items in a
vertically stacked list are represented as shown in Fig. 7.4.
Fig. 7.4: Indexing of Nested Lists
1. nested_list[0]-prints the contents of the nested list located in zeroth
position which is Apple.
2. nested_list[1]-prints the contents of the nested list located in first
position which is [‘Bananas’, [‘Cherries’, ‘Dragon Fruit’], ‘Eggfruit’,
‘Fig’]
3. nested_list[2]-prints the contents of the nested list located in the
second position, which is Guava.
4. nested_list[3]-prints the contents of the nested list located in the third
position, which is Honeydew Melon
5. nested_list[1][0]-prints the contents of the nested list located in the
zeroeth position and the first element of the first inner loop which is
Bananas
6. nested_list[1][1]-prints the contents of the nested list located in
zeroeth position and the second element of the first inner loop which
is [‘Cherries’, ‘Dragon Fruit’]
7. nested_list[1][2]-prints the contents of the nested list located in the
zeroeth position and third element of the first inner loop which is
Eggfruit
8. nested_list[1][3] -prints the contents of the nested list located in the
zeroeth position and fourth element of the first inner loop which is
Fig
9. nested_list[1][0][0]which is B
10. nested_list[1][1][0]-which is Cherries
11. nested_list[1][2][0]-which is E
12. nested_list[1][3][0]-which is F
3. For Negative List Indexing in a Nested List- With a negative list,
indexing may be accomplished. Negative indexing is yet another means
of acquiring access to the hierarchical list, which is very useful in some
real life situations if used appropriately. Negative indexes count the
number of elements in a list from the beginning to the end, starting at the
beginning and working backwards. Each item on the list is represented
by the letter L[-1], followed by the letter L[-2], then L[-3], and so on,
until all of the things are represented by the letter L[-1].
Example 16: Illustration to access negative nested list items by index
Python code illustrating how to access negative items in a nested list
using an index looks like this:
4. To add items to a Nested list: Depending on the list type being created,
there are various methods for adding items to a Nested list.
1. When adding additional items to a nested list, use the append().
Example 17: Illustration to add new values to the nested list.
As shown below, append() is used in the python script to populate a
nesting list.
2. To include additional items in a nested list, the insert() function is
used. When the insert() function is called, a new item is added to a
nested list at the position specified by the position argument. This
occurs when the function is called.
Example 18: Illustration to alter the value of a list item that is
nested within another list item
The following Python program is used to change the value of a list
item in a nested list:
3. It is possible to combine two lists of items into a single long list of
items that contains a greater number of items with the help of the
extend () function.
Example 19: Illustration to combine two list items into a single
list using extend().
The python program to combine two list items into a single list using
extend() would be as follows:
5. To Remove items from a Nested List
1. Pop() is the function that must remove an item from a list of items.
This method is more efficient when information regarding the
required item to be removed from the list and the index in which it is
located is already known. The program changes the list by the
preceding description, after which it returns the item removed from
the list.
Example 20: Illustration to remove items from the nested list
using pop().
The following python script shows how to use pop() to remove items
from a nested list.
2. If a value is deleted because it is no longer needed or available, it can
be completely removed from a database. The del statement can do
this as shown below.
Example 21: Illustration to remove items from the nested list
using delete.
The following python program shows how to use del()
3. It is possible to delete items from a list using the remove() function
when it is unclear where a specific item is placed within a list of
items. Remove() deletes an item based on its value rather than its
position in the list; when the function is called from a list of items.
Example 22: Illustration to remove items from the nested list
using remove().
The following python program shows how to use remove() to delete
items from a nested list.
6. To modify the value of a list item nested within another list item.
Example 23: Illustration to remove items from the nested list using
remove().
Yet another Python program to use remove() to delete items from a
nested list.
7. To find the Nested List Length - Python’s len() method can determine
the number of items within a nested sublist(see example below). This
method is built into the Python programming language as a pre-
programmed feature.
Example 24: Illustration to find nested list length
Python program to find the nested list length is as follows:
8. To alter the nested list item, value-Parentheses must be used to change
the order of items within a nested list structure. The index number of the
item within the parentheses must be referenced to alter the value of a list
item that is nested within another list item, as demonstrated in the
following example:
Example 25: Illustration to alter the value of a list item in a nested
list.
Python program to demonstrate alteration of the value of a list item is as
follows:
9. To Iterate through a nested list using a simple for-loop -“for-loops”,
are the most straightforward method of iterating through the entries of a
nested list(see example below), and they are commonly used in
programming. As a result, it is the most widely used method of iterating
overall entries in an unstructured data structure.
Example 26: Illustration to iterate through a nested list using a
simple for loop.(Listing3.py)
Python program to demonstrate iteration through a nested list using a
simple for loop is as follows:
#This is the screenshot of the output Iterating through a Nested List
using a simple for loop
7.6 Mutability of List
The elements of mutable lists can be altered at will and can be changed
after a list object is created. Python relies on this. Assigning directly to a list
allows easy access to every item in the collection for editing. The list of
associated names is dynamic, which is updated whenever any defined times
are changed. Mutability is the capacity to modify specific data types
without creating duplicates. “Editing” that list actually creates a new object
in memory. The size of a list can be altered by adding and removing items
from it. So, the lists can be updated on the fly. To modify a single item in a
list, place the index operator (square brackets) on the left side of an
assignment.
Significance of mutability of lists-Lists and dictionaries must be examined
and adjusted regularly; their contents must be enlarged and contracted, and
new and deleted entries must be added and removed, to name a few
requirements. To run programs fast and efficiently, Python requires
mutability. Mutable objects are significant when the size of an object, such
as a list or dict, is to be changed. Pop, Insert, Sort, etc., can be affected on
the list. Once a list has been created:
It is possible to make changes to a list after it has been formed.
Individual values can be substituted.
The elements can be rearranged in any desired arrangement.
Lists in Python use zero-based indexing (i.e., lists have index values 0 … n-
1, where n is the number of elements in the list).
Example 27: Illustration to show mutability of lists
The python script to exemplify the mutability would be as follows:
Note
The internal method id() returns the object’s id in the form of an integer.
The object’s id may be found using the built-in method id(). Python
implementation and platform can affect the value of this number, which is
often used to identify the object’s memory address.
Work Behind the Scenes
Copying a list - List copies are sometimes necessary. Python’s copy()
function can be used to create a copy of a list identical to the original in
terms of content and sorting. The copied list points to a different memory
location than the original list. As a result, updating a single list does not
affect any other lists.
If a new list is created with the = operator, the copy() method makes a
shallow copy of the original list, protecting the original from change. The
actual list is not mentioned anywhere. A new list reference is also compiled.
Use either of the two references to access the most recent version of the list.
Example 28: Illustration to exemplify the copy()
The python program to demonstrate the copy() would be as follows:
The lists petrol_prices_metros and metrocities_petrolprices have elements
arranged in the same order as the output. Based on the need, any element on
the copied list can be modified without affecting the original list.
Shallow and Deep Copy -A deep copy is used for nested lists. With
complicated things, the distinction between shallow and deep copying is
significant.
The duplicated variable has its own distinct memory location in a Deep
Copy. It is safe to make edits to the deeply copied variable because they
will not propagate back to the original. A Shallow Copy is a carbon copy of
the original variable. If the new variable is altered, the previous one is also
modified.
Shallow And Deep Copies
Lists can have shallow copies that are precise replicas of the original list’s
entries. The original list will, thus, reflect any changes made to the entries
in the duplicated new list, and vice versa. This makes it easier to maintain
both lists. The primary list does not update deep copies; it only updates
shallow copies. The list is duplicated in deep copies. The copy module’s
copy operations include copy() and deepcopy(). Either the = operator or the
copy() method can create shallow copies. Users of the list need to be aware
of this behaviour to prevent data modifications.
Program to display the updated original list and shallow copy list.
7.7 Looping with List
Interacting with every item on a list is necessary for real life. Each list item
is handled by Python’s “for loop.” Python’s “for loop” implements process
lists and iterations. Lists are unlimited. Python’s “for loop” iterates lists
best. The list-processing code is the suite in Python. The name in the code
identifies the target. Each iteration of the list gives the target identifier a
new data value. “For loop” runs until the list is empty. The “while loop” can
iterate across code. Simple, useful, and one-line for. “for loop” is made up
of one line. It ensures the execution of all the conditions. They repeat code
at set times. The code in the block is run sequentially. It can be used with
lists, tuples, sets, ranges, and other iterable objects.
Example 29: Illustration of a simple for-else loop iterating over list
(Listing4.py).
Python script to demonstrate a simple for-else loop iterating over list is as
follows:
Work Behind the Scenes
The if statement determines whether or not the item passed in is “Jackfruit”
If this condition holds, it will begin a loop. If it returns false, the control
moves to the else block, where the statements [print(item) and print(‘This is
from the else block’)] are carried out.
#This is the screenshot of the output -for loop condition
Looping with lists
Lists are effectively comparable to arrays in other programming languages.
They do, however, come with the extra advantage of being size variable. In
Python’s data structures, the list is of an ampoule type. The method of
accessing, one-by-one, the elements of a list is called a list traversal. It is
used to store enormous amounts of data unceasingly. Python lists are
ordered and have a fixed number of elements. Python has several list
traversal methods. The following methods are available in Python for
traversing lists or visiting the elements of a list:
1. By using a for-loop
2. By using a while loop
3. Python range () method
4. Python enumerate () method
1. By using a for loop – There are two fundamental ways to visit all list
elements. Either one can loop over the index values and look up each
element, or loop over the elements themselves.
1. Loop traverses all index values- Create a variable x containing five
items. Each value can be obtained using the expression variable[x].
The for-loop below exhibits the index values and their corresponding
elements in the values list.
Since no element corresponds to the value [5], it is appropriate for the
variable x to cycle through the integers 0 through 5.
2. Loop over the elements themselves - Can iterate over each element,
in turn, using a for loop without the need to know their index values
by entering:
While using a loop, the loop body is executed once for each element
in the list values. At the start of each loop iteration, the next element
is allocated to the loop variable element, and the loop body is then
implemented.
2. Using a while loop--it loops over the index values and looks upon each
element in the while loop.
Example 30: Program for list traversal using a while loop
(Listing5.py)
Python script to demonstrate list traversal using a while loop is as
follows:
#The result is shown in the screenshot below:
3. Using for loop and range ()-To use the traditional for loop, which
iterates from number x to number y in a range, look at the example
below.
Example 31: Program for list traversal using for loop and range ()
(Listing6.py)
The python script for list traversal using for loop and range () would be
as follows:
#The result is shown in the screenshot below:
4. using the enumerate () method-In Python, the enumerate() method
keeps track of the elements included in a list, tuple, set, or any other
iterable data structure (known as iterable). It abridges the process of
keeping track of the contents of an iterable object. Instead of a reading
list, execute print(enumerate()) is used to obtain an alphanumeric
memory reference address. This is how enumerate() optimizes memory
use by removing superfluous full-fledged lists.
Example 32: Illustration to demonstrate the usage of enumerate()
(Listing7.py)
Python script to demonstrate the implementation of enumerate()
#This is the screenshot of the output of using enumerate()
7.8 List as a Stack
The notion of LIFO allows the objects to be piled up on top of the other
objects, and then they can be eradicated in the reverse order of influx. Data
structures like the PyStack use a LIFO (Last in, First Out) design pattern.
Objects are piled one on top of the other and then eliminated in the opposite
order of arrival by the LIFO notion. Data structures such as the python
Stack use a LIFO (Last in, First Out) design pattern.
Stacks are helpful to inverse a string, evaluate convert expressions, syntax
and parenthesis checking, Recursive parsing, Backtracking, histograms, tree
traversals, Tower of Hanoi, and topological sorting.
The Stack performs many operations like accumulating or appending
element; erasure or elimination of elements, and navigating or displaying
elements.
Accumulating or appending or adding of element: The insertion of a
new element is denoted as “adding” or “appending” an element. Say, for
instance, to add stack instance 5, add it over 4.
Erasure or elimination of element:
When the stack is vacant, this does not remove any items
When the stack has elements, the top element will be eradicated upon
encountering this operation. By doing so, the size of the stack can be
reduced.
First, delete stack five and then erase stack 4.
Navigating, displaying or iterating over the stack, each element is
displayed on the screen one by one.
Characteristics of stack
In the operation, the order of inserts is to be kept consistent.
Everything in stack may be duplicated as often as possible without
restriction.
The same information may be stored in many locations
This is a complete must-have for anyone who works with data.
As shown in the following example, a stack is implemented using a list
object.
The append() method adds an item or pushes a new item onto the top of
the stack.
The pop () method is used to pop out an item from the top of the stack.
list[-1] is used to access the topmost item on the stack.
Example 33: Illustration to demonstrate the code for List as a stack
The python program would be as follows:
Work Behind the Scenes
The preceding example has been implemented and clearly explains the
stack functions; along with their time complexities.
empty() – Returns whether the stack is empty – Time Complexity: O(1)
size() – Returns the size of the stack – Time Complexity: O(1)
top() – Returns a reference to the topmost element of the stack – Time
Complexity: O(1)
push(a) – Inserts the element ‘a’ at the top of the stack – Time
Complexity: O(1)
pop() – Deletes the topmost element of the stack – Time Complexity:
O(1)
Example 34: Illustration to show the implementation of stacking a list
(Listing8.py)
The python script would be as follows:
#The result is shown in the screenshot below:
7.9 List as Queue
Operations that can be performed on the queue are:
Dequeue - The element to delete in a queue. If the queue is empty, then
it is said to be an underflow condition. It removes an item from the
queue. The items are popped in the same order in which they are pushed.
If the queue is empty, it is said to be an Underflow condition.
Enqueue - The element to add to a queue. If the queue is full, then it is
said to be an overflow condition. It adds an item to the queue. If the
queue is full, it is said to be an overflow condition.
Front - Get the front item from the queue. It gives the front item from
the line.
IsEmpty - Check if the queue is empty or not.
Peek - Retrieve the element from the front end of the queue without
removing it from the queue.
Rear - Get the last item from the queue. It gives the last item from the
queue.
Size - Return the total number of elements present in the queue.
Example 35: Illustration of a queue in a list(Listing9.py)
The python script to demonstrate the implementation of the queue would be
as follows:
#The result is shown in the screenshot below:
Summary
1. A list in Python is an ordered collection of items or elements and is
referred to by a single variable name.
2. Lists in Python use zero-based indexing.
3. A list is said to be mutable, meaning the list’s contents may be altered.
After a list object has been formed, it is possible to change to the
mutable data type in Python. This is a fundamental Python feature.
4. Python provides a rich set of operations, making list processing quite
useful.
1. +-Concatenation- Values on both sides of the operator are added.
2. *-Repetition - Concatenates numerous copies of the same List to
form new Lists.
3. []-Slice- Returns the character at the specified index.
4. [ : ]-Range Slice - Gives the characters from the given range
5. In-Membership - Returns true if a character exists in the given List
6. not in-Membership - Returns true if a character does not exist in the
given List
5. Python has many built-in methods for operating in the list
1. “in” operator - to determine whether or not an element is contained
within a list. If an element is present in the list, true is returned;
otherwise, false is returned.
2. “not in” operator - to determine whether an element is or is not
present in the list. If the element is not present in the list, true is
returned; otherwise, false is returned.
3. sorted(iterable[,key,reverse]) -It is a comparison function; it is
utilized to sort the iterable in the list and arrange it in ascending order
from the start returning a new list of sorted objects.
4. extend(seq)-This function ends the list with the contents of seq. as it
adds multiple items to a list.
5. insert(a, x)-This function places an item obj into a list in a given
position by adding the element.
6. list.reverse() or reversed(iterator)-all of the items in the list are
rearranged, and the iterator’s direction is reversed.
7. count()-The number of entries with the specified value is returned.
(or) returns the number of times obj appears in the list.
8. append or list.append(obj)-Adds one element to a list with the
append() method.
9. clear()-All elements in the list are removed.
10. del[a:b]-When called, this method removes all elements in the range
spanning from index a through index b, as specified by the argument
values passed in during execution.
11. remove Removes the major item from the list or obj from the list.
12. pop or list.pop(obj=list[-1])-This function removes the element
when invoked, and the list’s final object is returned.
13. index or . index(ele, beg, end)-This method returns the first instance
of the given value, or the index at which the obj is found in the list is
returned. After the start and before the end, this function returns the
index of the first occurrence of an element.
6. Stacks are useful to inverse a string, evaluate and convert expressions,
syntax and parenthesis checking, Recursive parsing, Backtracking,
histograms, tree traversals, Tower of Hanoi, topological sorting and so
on.
7. Stack performs many operations like Accumulating or Appending an
Element, Erasure or Elimination of Element, and Navigating or
Displaying elements.
8. Operations that can be performed on the queue are Dequeue, Enqueue,
Front, IsEmpty, Peek, Rear, Size
Glossary
Concatenation-Values on both sides of the operator are added.
Deep Copy-When making a deep copy, the variable being replicated is
given its own separate space in memory. Variables that are deeply copied
will not affect the original.
Dequeue - Eliminating a dequeued item from a queue. When there are
no pending requests in the queue, this is called an underflow situation. In
this instance, it cancels a pending operation. The pieces are popped in
the same order as when they were pushed. If there is no item in the
queue, this is called an underflow condition.
Enqueue-The thing that goes into the line. An overflow situation occurs
when the queue is full. It adds a new item to the backlog of items to be
processed. An overflow circumstance occurs when the queue is
overflowing.
List-Lists are useful for storing various data kinds in a logical sequence.
A list is nothing more than a collection of unrelated objects. Collections
frequently contain data that has been collected from the actual world.
List traversal-A list traversal is the process of going through a list one
item at a time.
Mutable-A list is said to be mutable, meaning the list’s contents may be
altered.
Range Slice-Returns the characters that fall inside the specified range.
Repetition-Creates new Lists by concatenating several copies of the
same List.
Shallow Copy-The original variable is used while making a Shallow
Copy.
Slice-Returns the character at the specified index.
Conceptual Questions
1. Define a list.
2. What are the basic properties of lists?
3. What is a list traversal?
4. What are some of the Python techniques that are available for traversing
lists or viewing the individual items that make up a list?
5. Why is a list said to be mutable?
6. Explain the significance of the mutability of lists
7. What are “nested lists,” and where are they beneficial?
8. How are items added to a nested list?
9. What is a replication or repetition operator?
10. Define the “in” operator.
11. Define the “not in” operator.
12. Explain the three ways by which the list can be reversed.
13. Which built-in function dispels all the elements in the list by clearing all
the items from the iterable?
14. Which built-in function extracts data from a list of characters possible;
with a list, the values can be accessed per the requirement?
15. Explain the ways by which an element in a list can be deleted.
16. What are the operations performed by a stack?
17. Explain the operations performed by a stack.
18. What are the characteristics of a stack?
19. List and explain the operations that can be performed on the queue.
Try Out Questions
What is the output of the following programs?
1. aList = [4, 8, 12, 16]
aList[1:4] = [20, 24, 28]
aList
2. m = [None] * 17
print(len(m))
3. preList = [10, 20, 30, 40]
del preList[0:6]
preList
4. my_list = [“Hello”, "NodeJS"]
print("-".join(my_list))
5. sampleList = [10, 20, 30, 40, 50]
sampleList.append(60)
print(sampleList)
sampleList.append(60)
sampleList
6. sampleList = [10, 20, 30, 40, 50]
sampleList[-2]
sampleList[-4:-1]
7. rose_alloy_items1= [“bismuth”,"lead"]
rose_alloy_items2= ["tin"]
rose_alloy= rose_alloy_items1 + rose_alloy_items2
print(rose_alloy)
print(rose_alloy_items2*3)
8. continental_ breakfast_dishes= [“bagels”,“cereal”,“bread”,
“croissants”,“pastries”,"waffles"]
Fresh_juice= [“Cheesy Scrambled
Eggs”,“orange”,“apple”,“pineapple”,"melon"]
print(continental_breakfast_dishes + Fresh_juice)
9. x=[2,4,5,6,8,10,12,15,19,23]
while x:
print(x [0])
del x[0]
10. list1=[‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’]
for item in list1:
if item==‘E’:
print(‘Item E is left out and other items are printed.)
else:
print(item)
print(‘This sentence is from the else block’)
11. Seashell_types =[“Conch”,“Drupe”,
“Harp”,“Scallops”,“Strombus”,"Turbo"]
for i, m in enumerate (Seashell_types):
print (i,m)
12. print(eval(‘666 == 666’))
print(eval(‘8 ** 4’))
print(eval(“‘Magic’ + ‘Numbers’+’Physics’”))
print(eval("'Magic' *5"))
x = 2+10-18*36+54-86
print(‘Value of x is:’, eval(‘x’))
print(‘Sum of list is:’, eval(‘sum([2,10,18,36,54,86])’))
13. exec(‘print([“Arborio rice”,” Basmati rice”,“Black rice”,“Brown
rice”,“Jasmine rice”,"Red cargo rice"])’)
exec(‘m=54; n=86; print(“Multiplication:”,m*n)’)
x = 1000
exec (‘print(x == 1000)’)
exec (‘print(x / 24)’)
14. print(“len() operation on a sample List containing instruments”)
instruments = [‘altimeter’, ‘barometer’, ‘speedometer’]
x = len(instruments)
print (‘There are’, x, ‘number of elements in the instruments list.’)
15. magic_numbers_physics=[2,10,18,36,54,86]
a=magic_numbers_physics
print(“\nList named magic_numbers_physics is of “, type(a))
acupressure_points_immunity=[‘GB 20’,’Ki 27’, ‘St-36’,’Li4’]
b=acupressure_points_immunity
print(“\nList named acupressure_points_immunity is of “, type(b))
indian_rice_types=[“Arborio rice “,” Basmati rice “, “Black rice” , “
Brown rice “,” Jasmine rice “,” Red cargo rice “]
c=indian_rice_types
print(“\nList named indian_rice_types is of “, type(c))
16. x=[3,7,8,666,“Indian Auroch”,“Asiatic Cheetah”,“Malabar
Civet”,“indian Javan Rhinocéros”,"Asiatic Cheetah"]
y= x.count(666)
print(“\nHow many times the number 666 is present in the list
indian_animals \n”,y)
print()#print an empty line
z= x.count(“Asiatic Cheetah”)
print(“\nHow many times the animal Asiatic Cheetah is present in the list
indian_animals \n”,z)
17. x =[3,7,8,666,“Indian Auroch”,“Malabar Civet”,“Indian Javan
Rhinocéros”,"Asiatic Cheetah"]
print(“\nPrior to remove() operation on list \ n”,x )
x.remove(666)
print(“\nFirst Post - remove(0) operation on list \n”, x)
x.remove(“Indian Auroch”)
print(‘\nSecond Post - remove(“Indian Auroch”) operation on list \n’, x)
18. flowers = [986,108,“Allium “,” Bougainvillea”,”
Chrysanthemum”,“Dahlia”,” Exacum “,” Foxglove"]
print(“First If else Loop is a Program to Search for the number 986”)
if 986 in flowers:
print(“\nReturns TRUE - if 986 exists in the flowers List “)
else:
print(“\nReturns FALSE - if 986 doesnot exists in the list.”)
print()
print(“Second If else Loop is a Program to Search for the word
‘Dahlia’”)
if “986” in flowers:
print(“\nReturns TRUE - ‘986’ exists in the list.”)
else:
print(“\nReturns FALSE - ‘986’ doesnot exists in the list.”)
print()
19. flowers = [986,108,“Allium “,” Bougainvillea”,”
Chrysanthemum”,“Dahlia”,” Exacum “,” Foxglove"] #declared list
print(“Third If else Loop is a Program to Search for the word ‘Dahlia’”)
if “Dahlia” in flowers:
print(“\nReturns TRUE - ‘Dahlia’ exists in the list.”)
else:
print(“\nReturns FALSE - ‘Dahlia’ doesnot exists in the list.”)
print()
print(“Fourth If else Loop is a Program to Search for the word
‘DAHLIA’”)
if “DAHLIA” in flowers:
print(“\nReturns TRUE - ‘DAHLIA’ exists in the list.”)
else:
print(“\nReturns FALSE - ‘DAHLIA’ doesnot exists in the list.”)
20. x =[3,7,8,666,“Indian Auroch”,“Malabar Civet”,“Indian Javan
Rhinocéros”,"Asiatic Cheetah"]
print(‘Index value of 3 in the list is’,x.index(3))
print(‘Index value of 7 in the list is’,x.index(7))
print(‘Index value of 8 in the list is’,x.index(8))
print(‘Index value of 666 in the list is’,x.index(666))
print(‘Index value of “Indian Auroch” in the list is’,x.index(“Indian
Auroch”))
print(‘Index value of “Malabar Civet” in the list is’,x.index(“Malabar
Civet”))
print(‘Index value of “Indian Javan Rhinocéros” in the list
is’,x.index(“Indian Javan Rhinocéros”))
print(‘Index value of “Asiatic Cheetah” in the list is’,x.index(“Asiatic
Cheetah”))
21. magic_numbers_physics=[2,10,18,36,54,86]
print(‘\nList of magic_numbers_physics:’ ,magic_numbers_physics)
print(‘\nExample 1. USING SUM() METHOD’)
x= sum(magic_numbers_physics)
print(‘\nSum of the magic_numbers_physics using SUM() METHOD :
‘,x)
print(‘\nExample 2. USING SUM() METHOD with 986 as A START
VALUE’)
x= sum(magic_numbers_physics, 986)
print(‘\nSum of the magic_numbers_physics using SUM() METHOD
with 986 as the START VALUE: ‘,x)
print(‘\nExample 3. BY USING FOR LOOP’)
y=0
for i in magic_numbers_physics:
y+=i
print(‘\nSum of the magic_numbers_physics using FOR LOOP: ‘,y)
print(‘\nExample 4. BY USING WHILE LOOP’)
z=0
j=0
while j len(magic_numbers_physics):
z= z + magic_numbers_physics[j]
j=j+1
print(‘\nSum of the magic_numbers_physics uisng WHILE LOOP: ‘,z)
Programming Exercises
Use the given two lists for questions 1 to 9.
world_animals=[‘Anaconda’, ‘Platypus’,’Red panda’, ‘Beaver’, ‘Killer
whale’, ‘Platypus’, ‘Camel’, ‘Polar bear’, ‘King penguin’, ‘Snow leopard’,
‘Zebra’, ‘Plains bison’]
world_biomes=[‘Tropical Rainforest’,’Temperate Forest’,’Taiga’,
‘Marine’,’Freshwater’, ‘Desert’,’Arctic Tundra’,’AntarticaTundra’,’Alpine
Tundra’, ‘Tropical Grassland’ ,’ Temperate Grassland ‘]
1. Create a program to calculate and return the length of the given lists.
2. Create a program to count up how many times an item appears in a given
list.
Given:
Platypus in world_animals
Taiga in world_biomes
3. Write a program to find the index of the first matching element for
‘Snow leopard’ in the list of world_animals.
4. Write a program to examine a list for an item.
1. Anaconda in world_animals
2. Forest in world_biomes
5. Write a program to reverse the order of the world_biomes list in Python
6. Write a python script that concatenates the above given two lists into
one.
7. Write a program to insert an item to the world_animals list at a given
position.
Given:
Item is ,"Elephant"
Position is 3
8. Create a program that will remove every item in the given list of
world_animals.
9. Write some code to utilise the zip function to merge the above given two
lists into a single tuple list.
10. Given below are two lists:
Indian_animals =[“Indian Auroch”,“Asiatic Cheetah”,“Malabar
Civet”,“Indian Javan Rhinocéros”,"Asiatic Cheetah"]
Superstitious_numbers=[3,7,8,666]
Show what will happen after using extend(Superstitious_numbers)
operation on the first list .
11. Write a program to reverse a list in Python
Given: list1 = [101, 202, 303, 404, 505]
Expected output: [505, 404, 303, 202, 101]
12. Concatenate two lists index-wise
Write a program to add two lists index-wise. Create a new list containing
the 0th index item from both lists, then the 1st index item, and so on
until the last element. Any leftover items will get added at the end of the
new list.
Given:
common_name = [‘Carrot’,’Garlic’,’Onion’,’Tomato’,’Potato’]
sample2 = botanical_name = [‘Daucus carota’, ‘Allium sativum’,
‘Allium cepa’,’Lycopersican esculentum’,’Solanum tuberosum’]
Expected output:
[‘The botanical name for Carrot is Daucus carota’, ‘The botanical name
for Garlic is Allium sativum’, ‘The botanical name for Onion is Allium
cepa’, ‘The botanical name for Tomato is Lycopersican esculentum’,
‘The botanical name for Potato is Solanum tuberosum’]
13. Write a program to iterate both lists simultaneously and display items
from list1 in original order and items from list2 in reverse order.
Given
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
Expected output:
10 400
20 300
30 200
40 100
14. Write a program that will iterate through a list’s values and indices.
world_animals=[‘Anaconda’, ‘Red panda’, ‘Beaver’, ‘Killer whale’,
‘Platypus’, ‘Camel’, ‘Polar bear’, ‘King penguin’, ‘Snow leopard’,
‘Zebra’, ‘Plains bison’]
15. Write a program to extend it by adding the sublist [“h”, “i”, “j"] in such a
way that it will look like the following list.
Given list:
list1 = [“a”, “b”, [“c”, [“d”, “e”, [“f”, “g“], “k“], “l“], “m”, “n"]
# sub list to add
sub_list = [“h”, “i”, “j"]
Expected Output:
[‘a’, ‘b’, [‘c’, [‘d’, ‘e’, [‘f’, ‘g’, ‘h’, ‘i’, ‘j’], ‘k’], ‘l’], ‘m’, ‘n’]
16. Replace the list’s item with a new value if found
Given a Python list, write a program to find the value D in the list; if it is
present, replace it with XYZ. Only update the first occurrence of an
item.
Given:
list1=[‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’]
Expected output:
[‘A’, ‘B’, ‘C’, ‘XYZ’, ‘E’, ‘F’, ‘G’, ‘H’]
17. Write a program to merge all the items in a list into one long string.
Given:
List1=[“Anaconda”,“is”, “the” ,“animal”, “inhabiting”, “the”,
“Tropical”, “Rainforest” ]
OceanofPDF.com
Data dominates. If you’ve chosen the right data structures and organized
things well, the algorithms will almost always be self-evident. Data
structures, not algorithms, are central to programming.
— Rob Pike
CHAPTER 8
Tuples
The chapter titled “Tuples” introduces the reader to an immutable sequence
of Python objects and the benefits they provide. In the subsequent
subsections, the reader is introduced with the basics of tuples, tuple
operations, and tuple functions and methods.
Learning Objectives
After completing this chapter, the reader will learn to understand the usage
of tuples and answer the questions related to the following
Understand the need for tuple.
Understand the creation of tuples.
Understand the indexing and slicing of tuples.
Know the basic tuple operations.
Understand the built-in tuple functions.
Understand the built-in tuple methods.
8.1 Introduction to Tuples
The built-in data types in Python are quite diverse and include lists, tuples,
dictionaries, sets, and frozen sets, amongst many more. A tuple is used to
store a collection of elements that are arranged in a certain way and cannot
be altered. It is a versatile and powerful data structure since it may carry
components of any arbitrary data type (integer, text, float, list, etc.). For
example, a tuples can be used to store a wide variety of collections,
including, amongst other things, a potpourri of files, music playlists,
browser bookmarks, email messages, and the collection of videos that one
may access through a streaming service.
The term “tuple” refers to a series or sequence of these objects. After they
have been formed, Python items in a tuple cannot be modified in any way.
Tuples in Mathematics
An ordered pair is the grouping of two things. For example, a point in x–y
axis is an ordered pair of (x,y) where “x” “y” are x- and y-coordinates. It is
an ordered pair because swapping of “x” and “y” give different meaning
when x is not equal to y. Similarly, latitude and longitude, which refers to a
location on earth surface, are an example of an ordered pair. A graph can be
referred as an ordered pair of (V,E), where V is the set of vertices and E is
the set of edges.
An ordered pair of three elements is called a triplet (3-tuple), and an
ordered pair of four elements is called quadruple (4-tuples). In general, n-
tuple is an ordered pair of n-elements. In Python, n-tuple is implemented as
tuples. It is basically a data structure that refers to the collection of a finite
number of ordered elements.
The characteristics of a tuple are given below:
1. Tuples are used to store heterogeneous data types.
2. Tuples are immutable and hence can be used as keys for the dictionary.
3. Iteration of tuples, compared to lists, is considerably faster.
4. Tuples are ordered. A tuple is ordered if the objects in it have a specified
order, and that order will not change in the future.
5. Tuples are immutable. Immutability is the hallmark of unchangeable-
Tuples, which means that nothing can be changed, added, or removed
once created.
6. Allow for duplication:Indexed tuples allow the insertion of components
with an identical value that is already in the tuple.
7. Tuples are efficient. Tuples are built on unmodifiable structures, making
them more straight forward regarding memory utilization and
performance than lists. This is because the unmodifiable structures
cannot be changed.
Why are Tuples preferred over Lists?
Lists and tuples are both utilized in contexts that are analogous to one
another, and tuples and lists have many similarities and differences. The
similarities between lists and tuples are given in the following Table 8.1.
Table 8.1: Similarities Between Lists and Tuples
The dissimilarities are given below in Table 8.2.
Table 8.2: Differences Between Lists and Tuples
8.2 Creating a Tuple
Tuples are constructed using parenthesis () to group together all the pieces
(elements). There is no need to use parenthesis; however, it is
recommended.
Even though () is not necessary, it is better to have the brackets. A tuple can
hold any number of elements. Integer, text, string, float, list, other data
types may all be stored in the same data structure, making it enormously
versatile. The tuple elements don’t have to be of the same type, and they
can be an arbitrary mixture of elements like numbers, strings, other lists,
etc.
A tuple can be empty like t1 =(). An empty tuple has an opening and closing
parentheses () to form an Empty tuple.
Example 1: Illustration of the creation of an empty tuple.
A tuple can be with a single element, for example,
t2 = (45,)
A comma is added after each member to let Python recognize it as a tuple.
If the comma is removed, then just (45) results in a class type as “float”
when there is no space between the components; hence a comma plays an
important role.
Therefore, a tuple with a single element has commas after each member to
let Python recognize it as a tuple. This fact is illustrated in the following
example.
Example 2: Illustration of a tuple with a single element
A tuple can have mixed data types. Tuples can hold any form of data. This
characteristic enhances the versatility of tuples.
Example 3: Illustration of a mixed-type tuple.
Example 4: Illustration of a tuple with duplicate elements.
Example 5: Illustration of a tuple of strings with double quotes.
One can use single quotes also instead of double quotes. The results will be
the same.
Tuples can be created from other containers also. The syntax is given as
below:
The sequence can be a list or strings or even a dictionary. A tuple() is a
constructor method in the tuple class. It can be used to construct tuples from
another sequence such as lists and strings. The following program illustrates
how a tuple can be created using lists.
Example 6: Illustration of a empty tuple created using tuple()
constructor.
Example 7: Another example of a tuple created from list of numbers.
Tuple constructors, tuple(), can also be combined with a range function to
create tuples. This is helpful in creating tuples with a large number of
elements.
Example 8: Illustration of a tuple created with a range function.
Example 9: Illustration of a tuple with strings.
A string can also be embedded into a tuple.
Example 10: Illustration of a string embedding.
It can be observed that the string itself is an element. If a string is used, it is
stored as a set of characters. Even though a tuple is immutable, a tuple can
store mutable lists along with immutable objects. For example, the
following tuple is created using lists as well as strings.
Example 11: Illustration of a string and list embedding.
One can create a tuple of tuples as shown below:
Example 12: Illustration of a tuple of tuples.
A tuple can be read from the keyboard as illustrated below:
Example 13: Illustration of a tuple created from user inputs from the
keyboard.
8.3 Indexing and Slicing in a Tuple
Each cell in a tuple has its unique index assigned to it (refer to Fig. 8.1). A
tuple is more than a collection of items compiled in one place. When
accessing the values of items in a tuple, the subscript operator can be used
to define which index should be utilized to get access to those values ([]).
To access an item or element from the variable “name_of_the_tuple,” the
syntax is
Fig. 8.1 Tuples showing the indexing
The example below shows the assignment of various cryptocurrencies to
the variable “cryptocurrency”. It also shows how to access the particular
elements from the tuple.
Example 14: Illustration of selection of an element from a tuple.
The Python script for the selection of an element from a tuple would be as
follows:
The example shows that Ripple’s third element in the tuple is being
accessed. Likewise, slices of the fourth element from the tuple of
cryptocurrency are Zcash. Another example of indexing is given below:
Example 15: Another example of Indexing
If the elements are outside indexing, then it raises an indexError Exception
as shown below:
Example 16: Illustration of IndexError when elements are accessed out
of index.
Tuple slicing is done with the slicing operator. Also, to access a range of
items in a tuple, use the slicing operator - colon “.”. The [start:stop:step]
syntax represents the slicing operator x. The’step’ component is optional. A
slice is a tuple of another tuple itself. This means that the values at start,
start+1, start +2, … , stop – 1 will be sliced into a new tuple.
Note
Some of the Interpretations of Indexing and slicing colon operator
Some of the interpretations are shown in Table 8.3.
Table 8.3: Slicing Operators
Example 17: Illustration of slicing in a tuple containing strings.
The Python script for the slicing operation would be as follows:
8.4 Assignment, Packing, and Unpacking
One of the major uses of tuples is tuple assignment. For example, the
following is a tuple assignment.
x,y = 10,20
The left-hand side is the tuple of variables and right-hand side is the tuple
of expressions or values. Each value is assigned to the respective variables.
If expressions are given then, Python evaluates the expressions before
assigning them. But the condition is that the number of variables on the
right and left has to be the same. For example,
x,y = 10,20,20 results in an error.
The following example shows the swapping of two values
x,y=y,x
This swaps the values of x,y. Here, the assignment is x=y and y=x. Observe
that the variables on the left-hand side are variables associated with tuples.
When a tuple is created, one can assign values to it. It is called packing.
Example 18: Illustration of packing in a tuple
The extraction of values from the tuple to variables is called unpacking. In
the following example, the created tuple is unpacked into the variables.
Example 19: Illustration of unpacking in a tuple.
The above example shows the ways objects are packed and unpacked.
When the number of variables is less than the number of values, then one
can add * to the variables. As a result, multiple values are assigned as a
tuple.
Example 20: Illustration of * operator in unpacking in a tuple.
8.4 Operations in Tuples
Python programming language includes many operations that can process
tuples in various ways. These techniques will be discussed in detail in the
subsequent sections. When used in tuples, the + and * operators act
analogous to concatenation and repetition and the final result is a new tuple.
This is because tuples are ordered sequences of values. Tuples can deliver a
response to a sequence of actions that can be performed as summarized in
Table 8.4.
Table 8.4: Summarization of Tuple Operations
len()
The size of a tuple can be determined with the help of the len() function. It
outputs the number of elements in the tuple. The syntax of len() is given as:
One of the required parameters is an object, which can take the form of a
tuple. The return value is an integer containing the total number of tuple
items. The following code snippet serves as a demonstration of the len()
method.
Example 21: Illustration to find the length of the given tuple
(Listing1.py)
The Python script for the slicing operation would be as follows:
#This is the screenshot of the output to find the length of the string.
Concatenation
Utilizing operators allows users to perform operations such as
concatenating or multiplying tuples. Use the plus sign (+) operator to
conduct concatenation and an asterisk (*) operator to perform
multiplication. Combining two or more tuples into a single new tuple can be
accomplished by using the plus (or plus sign) operator. On the other hand, it
is unable to change an existing tuple. Users can create a new tuple by
assigning its values to two pre-existing tuple.
The binary infix operator + (plus) or plus sign (+) operator is overloaded to
connote tuple concatenation. End-to-end concatenation joins two operands.
Concatenation operators assist in joining tuples. The following is the syntax
for the concatenation (+) operator:
If two tuples are concatenated with a plus (+) operator, a new tuple is
formed with the first tuple’s elements followed by the second tuple’s
elements.
The following example demonstrates the usage of the concatenation
operator. It takes two tuples and combines them to give a new tuple.
Example 22: Illustration of concatenation operation in a tuple.
The following example combines the tuple named vegetable and fruits into
a single tuple named vegetable_fruits as a result of the concatenation
operation.
Example 23: Illustration of concatenation operation in a tuple.
When a tuple is combined with a non-tuple, like a number, it results in an
error. The following example illustrates this.
Example 24: Illustration of concatenation error when a tuple is
combined with a non-tuple
This example demonstrates the usage of the * operator, which is used to
multiply the tuple labelled as fruits by a factor of 4 and the tuple called
vegetables by a factor of 3. * operator is called replication operator that
takes tuples by a number of times and repeats it.
Example 25: Illustration of * operator .
This is another example of a replication operator.
Example 26: Another example of a replication operator.
As a result, users now know how to generate new tuples based on the initial
data sequence displayed in the existing tuples by utilizing the * operator.
One can replicate the tuples by the number of times that users specify.
Membership Operator
A Membership Operator is a Python operator that determines whether a
value is a set member. This operator is used to validate if variables like
tuples, strings, integers, and tuples are members or not. Membership
operators are made up of several various operators. The (in) and (out)
membership logical operators are the two membership logical operators to
check the presence of an element in the given tuple. They are used to see if
a given element is part of a tuple as shown in Table 8.5.
Table 8.5: Membership Operators
Example 27: Illustration of membership operator
Example 28: Another example of “in” operator(Listing2.py)
The Python script to check the presence of a variable would be as follows:
#This is the screenshot of the output for “in” tuple membership
operation
As stated earlier, the “in” operator checks for tuple membership. Returns
True if an element is listed.
Example 29: Illustration of “not in” operator(Listing3.py)
In the example mentioned above, there are three if-else loops checking the
absence of the element.
#This is the screenshot of the output for “in” tuple membership
operation
8.5 Immutability in Tuples
Tuples are immutable. Therefore, users cannot change their values. One
cannot add any element to a tuple. Similarly, there is no append, removing
elements to a tuple. If anyone tries to do that, it results in an error.
Example 30: Illustration of an error when an attempt is made to
update.
So, if anyone wants to change the element, the following procedure can be
used:
1. Convert a tuple to a list.
2. Perform the required operations like append, remove on lists.
3. Convert the list back to the tuple.
The following examples illustrate the ways of changing a tuple to a list in
order to perform the append and removal of items.
Example 31: Illustration of usage of a list in appending a list
(Listing4.py)
The Python script for the slicing operation would be as follows:
Work behind the scenes
A blank tuple is generated and displayed using y=(). Next, ‘y’ is converted
from a tuple into a list, ‘x, using the list() method.
Using the append() function, we can add the items “Sumatran Rhinoceros”
and “South China Tiger” to the list “x."Finally, the tuple() method is used to
convert the list x back into a tuple y.
#This is the screenshot of the output.
Deleting tuple elements using lists
No items in a tuple can be deleted from a tuple, as it is immutable. An
immutable tuple can only be edited or added. For example, look at the
following tuple, which has been transformed into a list; then, the word “
PARROT TULIPS” has been deleted
Example 32: Illustration to demonstrate the removal of values in a
tuple with the help of lists (Listing5.py).
The Python script for the slicing operation would be as follows:
#This is the screenshot of the output for the removal of values in a
tuple.
Deleting a tuple
The del statement can be used to remove an entire tuple. To explicitly
remove the tuple as a whole, use the del statement.
Example 33: Illustration of del in a tuple(Listing6.py).
This example shows the usage of the del statement.
The preceding is the result of the code. A cautionary note has been issued.
8.6 Built-in Function and Tuples
Built-in functions like all(), any(), enumerate(), len(), max(), min(),
sorted(), tuple(), etc. are commonly used with a tuple to perform different
tasks. After using the built-in functions on tuples, the resultant is also a
tuple. The different tasks performed by the built-in functions on tuples are
demonstrated below.
count()-Python’s tuple count() method provides a return result, the
cumulative frequency of the value passed as a parameter. In other words, it
yields how many times the specific instances are present in the tuple?The
following is the syntax of the tuple.count(obj)is given as :
Example 34: Illustration of the count operation in a tuple.
Example 35: Another illustration of the count operation in a
tuple(Listing7.py).
The example shows the usage of the method count(). This method
enumerates the elements present in the tuple “Extinct_fish".
#This is the screenshot of the output for the
tuple.count(element)operation
all()
The function all() is used to check whether all items are True. If so, it
returns a value True. Even if one element is False, the function returns
False.
Example 36: Illustration of all() function.
any()
The function of any()will always return a value. This will only work for
iterables that have values of the True type. any () will return true if any of
the items it checks are true. The default value is false if the condition is not
met. any() finds out whether an iterable object has any elements or none at
all.
Example 37: Program to access the elements in the tuple.
enumerate ()
The built-in function enumerate(iterable, start=0) returns an enumerate
object, as demonstrated in the example below. The index and value of each
item in the tuple are stored here as a pair.
In Python, the enumerate() function in Python is responsible for
maintaining a record of the elements that are contained within a tuple, set,
or any other iterable data structure (known as iterable). Maintaining
awareness of the components that make up an iterable object is simplified.
To acquire an alphabetic memory reference address without using a reading
tuple, an execute print(enumerate()) command is carried out. This is how
the enumerate() function reduces the amount of memory used by
eliminating unused fully fledged tuples.
Example 38: Illustration of the enumerate() method in a tuple.
The Python script for the slicing operation would be as follows:
max()
This function returns the value that is the largest among all of the many
arguments of a tuple. The syntax of max() is given as :
Example 39: Illustration to find the max() in a tuple.
The Python script for the slicing operation would be as follows:
min()
min() function returns the value that is the smallest among the many
arguments of the tuple.
The syntax of min() is given as :
Example 40: Illustration to find the min() in a tuple.
The Python script for the slicing operation would be as follows:
sum()
The function sum() finds the sum of all elements of the tuple. The Python
script for the summing operation would be as follows:
Example 41: Illustration of sum() in a tuple.
Example 42: Illustration of len(), max(), min(), and sum() for a tuple of
numbers.
8.7 Built-in Methods in Tuples
When it comes to built-in methods, they are defined by the actions that can
be made against them and how they may be changed. When a method is
invoked on a newly generated object, the dot operator is used to accomplish
the task (.). Python’s built-in procedures for modifying a Python tuple are
numerous and may be found throughout the Python programming
language’s documentation. Some of the essential methods are shown in
Table 8.6.
Table 8.6: Built-in Methods in Tuple
index()
When a tuple is operated with the built-in index () method, it yields the
index position of the primary presence of the stated item. If the item is not
found in the tuple, it raises a ValueError. The syntax is given as :
In addition to an obligatory element in the index to be searched, there are
two other parameters: an optional start index position (ii) and an optional
end index position that can be used. Index() returns the first occurrence of
the element in the tuple. The example shows the usage of the
method index().
Example 43: Illustration of an index function.
This is another example of an index operation.
Example 44: Illustration of the index() operation in a tuple.
sorted()
The tuple sort () method in Python organizes all the elements in the list
either in an ascending or descending order and are placed in the iterable.
The syntax of ascending order list.sort(obj)is given as:
Example 45: Illustration of the sorted() function.
Sorting can be extended to strings which will be discussed in chapter 10. In
strings, the sorting is done based on the lexicographic sort. The following
examples illustrate sorting as well as the use of keys in a sorted function.
Example 46: Illustration of the sort () method in a tuple of strings.
The example shows the usage of the method sorted(). This method sorts the
elements in the tuple, “Endangered Birds".
The following snippet illustrates how sorting can be done in descending
order.
The following snippet illustrates how sorting can be done using the key
field where the length of the string is used.
8.8 Nested Tuples
The tuples can be nested within each other. The tuple can be nested on as
many levels as desired. This is shown in Table 8.6.
Table 8.6: Examples of Nested Lists.
Example 47: Illustration of nested tuples creation.
One advantage of a nested list is that it can be used for many applications
like the maintenance of employee records. Every employee’s details can be
stored as a tuple, say (employee_id, name, age, address). Tuples facilitates
now the insertion, deletion, and sorting of employee records.
8.9 Selection
The evaluation of tuples in Python is accomplished through relational
operators, which individually compare each character in the tuple. Each
character is mapped to a corresponding numeric value in the computer’s
memory. It is not the case that comparisons of characters follow a one-to-
one structure, such as F being compared to F or h being compared to M.
Comparing the several values that the ASCII codes can represent will
accomplish this. The value of the letter F in ASCII, for example, is 070,
while the value of the letter h is 104, and the value of the letter M is 077.
The values are compared after the comparison, analysis, and reporting of
the results. Therefore, to compare the contents of Tuple objects (or
subTuples), comparison functions must first inspect the contents of Tuple
objects and then compare those contents to the specific characters given by
the function’s arguments.
Example 48: Demonstrate the operator for Equality Testing.
By employing the == operator, the programming language Python enables
the comparison of two tuples to determine if they are exactly equal, which
means that they have the same items in the same order. For instance,
enterthe following at the command prompt:
In consequence, the output is true, indicating that the two tuples are
equivalent.
Next, inputting the following, however, will result in a false output,
indicating that the two tuples are not equivalent.
Example 49a: Another example of equality testing.
The opposite of == is !=.
Example 49b: Demonstration of != operator.
If the output from != is false, then the two tuples are identical, while if it is
true, then the two tuples are not identical.
Example 50:Demonstrate the comparison operation– Equal to and Not
Equal to in a tuple (Listing8.py).
The Python script for the equality comparison operation would be as
follows:
Work behind the scenes for comparison operations:
Compares Honey1 and Honey2 to show that they are equal.
Compares Honey3 and Honey4 to show that they are not equal.
#This is the screenshot of the output for EQUAL TO and NOT EQUAL
TO.
Example 51: Illustration of the Comparison operation like Lesser Than
and Greater than in a tuple(Listing9.py).
Another Python script to illustrate greater than (lesser than) and not greater
than (not lesser than)is as follows:
Work behind the scenes for comparison operations.
It compares the tuples x and y and returns true if they are greater than
(lesser than) or false if they are not.
#This is the screenshot of the output for greater than (lesser than) and
not greater than (not lesser than).
8.10 Looping with Tuples
In real-world situations, the ability to interact with every item on a tuple is
required. Python’s built-in for-loop helps handle each entry in a list.
Python’s for-loop implements process lists and other iterations. There is no
limit on list size. The best way to iterate through a list in Python is to use
the “for Loop”. The Python suite is the list-processing code. The name in
the code identifies the target. Each iteration of the list gives the target
identifier a new data value. The loop runs until the list is empty. Of course,
the while loop can iterate across the code.
For is a single-line statement that is simple, easy, and useful. For Loop is
made up of one line. It ensures the execution of all the conditions. They are
used to execute blocks of code repeatedly at predetermined times. The lines
of code in the block are performed in a sequence. It is used in combo with
iterable objects or sequence types like list, tuple, set, etc.
Example 52: Illustration of a simple for-else loop iterating over a
tuple(Listing10.py).
The Python script for a simple for-else loop iterating over a tuple is:
Declare and define Tamil Nadu’s most endangered species, “x.” With the
optional variable item, the for-loop loops, executing one item from the
iterable object (x) at a time. Next, the if statement checks to see if the input
is “Leopard.” If true, the value of “x” is printed. The else block statements
run if the condition is false. The print function prints the declared variable
item after iterating on the iterable object(x).
#This is the screenshot of the output.
Example 53: Illustration of a simple while loop Iterating overa
tuple(Listing11.py).
After the creation and printing of the tuple(x), script enters the while loop
and iterates.
#This is the screenshot of the output -for the loop condition.
Summary
1. A tuple is a set whose members are fixed in their positions within the set.
Due to its ability to store information of arbitrary types in its constituent
parts, it is a powerful and flexible data structure (integer, text, float, list,
etc.).
2. The enormous flexibility of the tuple data structure comes from the fact
that it can simultaneously store various data types. The elements of a
tuple donot need to be of the same type; they can be a random
assortment of things like numbers, strings, other lists, and so on.
3. An individual index is designated for each cell in a tuple. One’s
requested index values will be returned in exchange for the index values
one supplies.
4. The slicing operator is used to create tuple slices.
5. Tuples in Python cannot be changed from the outside, but their contents
can be modified with the help of lists.
6. There are many ways to process a tuple in Python, owing to the
language’s extensive operations (including + and *).
7. The capabilities and constraints of built-in methods define their scope of
use. The dot operator calls a method on an object that has just been
created.
8. A tuple is immutable, meaning that none of its components can be
changed or removed.
9. In practical applications, itis necessary to be able to manipulate each
element of a tuple. To process each item in a list, use Python’s inbuilt for
a loop. The for-loop in Python is used to iterate over lists and perform
other loops. The maximum list size is unrestricted.
10. Built-in functions like all(), any(), enumerate(), len(), max(), min(),
sorted(), tuple(), etc. are commonly used with a tuple to perform
different tasks. The resultant is also a tuple.
Glossary
Tuple-An immutable sequence of elements or immutable objects.
Ordered-A tuple is ordered if the objects in it have a specified order, and
that order will not change in the future.
Immutability-When none of the elements can be changed, added, or
removed in a tuple.
Unpacking-The extraction of values from the tuple to variables is called
unpacking.
Packing-Assigning multiple values to a tuple is called packing.
Nested tuple-The tuple contains an element of another tuple.
Membership Operator-This operator is used to determine whether or
not an element is contained within a tuple. If an element is present in the
tuple, true is returned; otherwise, false is returned.
count()-The number of entries with the specified value is returned.
index-This method returns the first instance of the given value.
Conceptual Questions
1. Show how to initialize a list of tuples.
2. List the advantages of tuples over lists.
3. Explain the various ways by which tuples can be created.
4. With suitable illustrations, explain the following:
1. traversing mechanisms in tuples
2. immutability of tuples
3. built-in functions in tuples
4. basic operations in tuples
5. Explain the nested tuples in record maintenance.
6. How can lists be merged into a list of tuples?
7. Why can tuples contain mutable items?
8. How to create a list from two tuples of tuples?
9. How to search a list of tuples in Python?
10. Why are tuples needed in Python?
11. How to split a long tuple into a smaller tuple?
12. How to unpack nested tuples?
13. What is the difference between tuples and lists in Python?
Try-Out Questions
What will be the output of the following code snippet?
1. init_tuple = ()
init_tuple
print (len(init_tuple))
tuple = ( “wrtxy”, 79286, 2.23, “R3Corda”, 70.2 )
print(tuple)
tuple = ( “wrtxy”, 79286, 2.23, “R3Corda”, 70.2 )
print (tuple[0])
tuple = ( “wrtxy”, 79286, 2.23, “R3Corda”, 70.2 )
print (tuple[2:])
2. tinytuple = (79289, “Fabric”)
print (tinytuple * 2)
3. What is the output of the Python code for creating tuples in a loop?
tup = (India,)
n = 7 #Number of time loop runs
for i in range(int(n)):
tup = (tup,)
print(tup)
(Listing1_tryout_.py)
4. What is the output of the following Python code?
l = list()
dir(l)
t = tuple()
dir(t)
5. Convert an iterable (list, string, set, dictionary) to a tuple.
t1 = tuple()
print(‘t1=‘, t1)
# creating a tuple from a list
t2 = tuple([1, 4, 6])
print(‘t2=‘, t2)
# creating a tuple from a string
t1 = tuple(‘Python’)
print(‘t1=‘,t1)
# creating a tuple from a dictionary
t1 = tuple({1: ‘one’, 2: ‘two’})
print(‘t1=‘,t1)
6. numbers = [2.5, 3, 4, -5]
# start parameter is not provided
numbersSum = sum(numbers)
print(numbersSum)
# start = 10
numbersSum = sum(numbers, 10)
print(numbersSum)
7. grocery = [‘bread’, ‘milk’, ‘butter’]
enumerateGrocery = enumerate(grocery)
print(type(enumerateGrocery))
# converting to list
print(list(enumerateGrocery))
# changing the default counter
enumerateGrocery = enumerate(grocery, 10)
print(list(enumerateGrocery))
8. # all values true
l = [1, 3, 4, 5]
print(all(l))
# all values false
l = [0, False]
print(all(l))
Programming Questions
1. Write a program to create a tuple of your favourite colours.
2. Write a program to create a tuple with different data types such as street
address.
3. Write a program to convert a tuple to a string?
4. Write a program to find the repeated items of a tuple.
5. Check whether an element exists within a tuple.
6. By writing a program, check if all the items in the tuple are identical.
7. Create a Python program that swaps two tuples.
8. Create a tuple with only one item 12 by writing a program.
9. Develop a program to copy particular elements from one tuple to
another.
10. Write a program to sort tuples by the third item if needed.
11. Show how a tuple can be unpacked into three variables by writing a
program.
12. Write a program to count the elements in a tuple.
13. Write a program to reverse a tuple.
14. Write a Python program to unpack a list of tuples into individual
elements.
OceanofPDF.com
I will, in fact, claim that the difference between a bad programmer and a
good one is whether he considers his code or his data structures more
important. Bad programmers worry about the code. Good programmers
worry about data structures and their relationships.
Linus Torvalds
CHAPTER 9
Sets and Dictionary
This chapter titled “Sets and Dictionary” introduces the reader to an
unordered collection of items and their implementations as sets and
dictionaries. In this chapter, the reader will learn how to create and
manipulate each item in sets and the dictionary.
Learning Objectives
After completing this chapter, the reader will comprehend the following:
Define and use sets in Python
Create, access, update, and delete values in sets.
Understand mathematical set operations.
Understand the basics of a dictionary
Use Built-in dictionary functions and methods
Write simple Python programs using sets and a dictionary
9.1 Introduction to Sets
Lists, tuples, and strings are typical examples of an organized or ordered
collection of linear (sequential) data structures. Each item in an ordered
collection is accessed using its value and its position in the collection. A set
and a dictionary are typical examples of an unorganized collection. No
operation on an unordered collection relies on its position. Instead, it uses
membership functions to check the presence and absence of items. Users
can add, get, or remove items from the unordered collections.
A set in Python is a Python data structure that stores a collection of unique
elements. Python sets are similar to mathematical sets, but they are part of
the Python standard library. A set is an unorganized collection of items or
elements in mathematics. A set object must possess the necessary methods
for iteration over objects also.
The chief characteristics of sets are given below:
1. A Python set is a list of elements that may or may not be ordered. A set is
often called a “Bag” data structure as it can contain various elements.
2. A set may be modified, but the elements of the Set are immutable. Sets
can include many immutable data types, like integers, strings, and tuples.
Python sets, unlike mathematical sets, can contain any hashable data
type. Mutable data types or changeable items (variables) cannot be
included as set elements. Lists can’t be set elements, but tuples can.
3. All set elements are unique. There can’t be any duplicate elements in
sets.
4. Set does not support indexing.
The primary advantage of sets is listed below:
1. Sets have a solid mathematical foundation. Sets can support many
mathematical functions, such as set union and set intersection.
2. Searching for an element in a list takes lots of time. But items can be
searched quickly in sets.
3. A list can contain duplicates that may affect applications, like finding the
count of unique words. Sets are better as there cannot be duplicate
elements in sets.
9.2 Creating a Set
Python sets are constructed and specified by using curly braces. Set object
items, which may or may not be the same type, are separated by commas (,)
and then encircled by curly braces.
Sets contain many immutable data types, including integers, strings, and
tuples. Mutable data types or changeable items (variables) cannot be
included as set elements. Specifically, neither a list nor another set can be
an element of a set.
Creation of an empty set
An empty set can be created using opening and closing curly braces to form
an empty set as shown below:
Example 1: Illustration of an empty set.
The following example illustrates a set with a single element.
Example 2: Illustration of a set with a single element.
The following example illustrates a mixed type of element where all
elements or items are separated by a comma and enclosed by curly braces.
Example 3: Illustration of a set with a mixed type of elements.
A set of strings with double quotes is created in Example 4, where the string
data types are embedded for the set’s elements.
Example 4: Illustration of a set with a set of strings.
Another way of creating a set is by using a set() constructor method. It can
create a set from any iterator, such as lists, tuples, or strings.
The syntax is given as shown below.
The following example illustrates the creation of a set using lists.
Example 5: Illustration of a set created from a list.
Similarly, a set can be created from tuples also, as shown below.
Example 6: Illustration of a set created from tuples.
In the following example, a set is created from strings. One can observe that
all the duplicate characters are removed.
Example 7: Illustration of a set created from strings.
The following example illustrates the difference between string embedding
if the set() function is not used and the usage of the set function for creating
a set of characters.
Example 8: Illustration of a string embedding and generation of set
elements.
In Example 8, it can be observed that the object inside the curly braces is
placed intact while the set () generates the list of elements to be placed in
the set.
A set of larger elements can be created using a range function, as shown
below.
Example 9: Illustration of a set with range() function.
9.3 Basic Operation in Sets
Python provides a rich set of operations for manipulations. The following
sections deal with these operations. The storage of several values in a single
variable can be accomplished with the help of sets. Some of the essential
operations are listed below.
Essential set operations
The length or size of the set is determined by using the built-in len()
function. This function takes one argument, set name, and returns the total
number of elements present in the set. The syntax of length function is
given below:
len() is a standard function that applies to all sequence types, such as lists,
strings, and tuples, and also for sets.
Example 10: Program to demonstrate the len() method.
Membership operator in set
A membership operator is a Python operator that determines whether an
element or value is a set member. Membership operators are in and not in.
These logical operators are used to check the presence or absence of a given
element in the Set, as shown in Table 9.1.
Table 9.1: Membership Operator
Example 11: Program to demonstrate the membership functions.
The following example illustrates not in operator.
Example 12: Program to demonstrate the not in operator
Adding elements to sets
In certain circumstances, it is necessary to construct a set containing either
no items or a certain number of items. This method is also known as
“adding elements,” which is another name for it. The add() function can
include elemental values in the set. The syntax of add or set.add(iterator)is
given as:
The function returns no value. It is added if the specified value does not
exist in the set.
Example 13: Program to demonstrate Set add function.
Another way of adding the elements is by using a set update. An iterable
could be added to the sets under certain cases. The update() method is used
in such instances. A set’s update() function in Python takes an existing
sequence (lists, tuples, strings) as an argument and adds it to the set if it is
not already present.
The syntax of set.update() is given below:
There is just one argument that can be passed into the update() method. One
can use a set, list, tuple, dictionary, or combination as a lone parameter.
Example 14: Program to demonstrate the update operations on a set.
Deletion of elements from a set
Four methods are related to the deletion of elements in sets, as shown below
in Table 9.2.
Table 9.2: Set Deletion Methods
In-depth explanations of each are provided below:
1. Removal of an element in set
When an element is to be removed from the set, the method
remove(element) is called. The method removes the specific element and
returns it. If the specified element is not present in the set, the method raises
an keyError exception.
The syntax of remove is given as:
Example 15: Program to demonstrate the elimination operations on a
set.
It should be observed that all are case-sensitive. If the specified element is
not present in the set, the method raises a keyError exception, as shown
below. The following in the example below:
Example 16: Program to demonstrate the set remove method.
2. Discarding an element in a set
The method discard() is used to discard an element of the set similar to the
remove(), but the difference is that it does not raise an error, and the set
remains unchanged. The syntax of discard is given as follows:
Example 17: Program to demonstrate the set discard method
3. Removing a random element in a set
The method pop() has no input parameters and returns a random element
and the removed element or TypeError if the set is empty. The syntax of the
pop method is given as follows:
Example 18: Program to demonstrate the set pop method
Example 19: Another example of the pop method
4. Deletion of a set
A set deletion can be done using the set clear method. The syntax of the
clear method is given as follows:
The method removes all the elements from the set. This method does not
take any parameters or return any values.
Example 20: Program to demonstrate the membership functions
Set copy
The set.copy() method returns a shallow copy of the set. The result is a
superficial replica of the set. The syntax of copy() is given as follows:
One can use copy method or = operator to copy a set to another set. The
following code illustrates this.
Example 21: Program to demonstrate the copy() on sets.
It can be observed that the results are the same for = as well as copy
method.
9.4 Mathematical Operations of Sets
Sets are collections of items. Sets contain objects collectively known as
‘Elements.’
Some of the set operations are listed below in Table 9.3.
Table 9.3: Set Operations
The set union of two sets X and Y, denoted as X∪Y, is a set whose member
belongs to either set X or Y. The set Union can be done by the operator | or
by the method set1.union(set2).
The syntax is given as
The two sets, X and Y, are jointly given in the resultant set without
duplicates. The resultant Set consists of all members of both sets, X and Y.
The number of elements in X ∪ Y is given by n(X∪Y) = n(X) + n(Y) −
n(X∩Y), where n(X) is the number of elements in set X.
Example 22: Program to demonstrate the set union
Set union can be done using the operator also, as shown below.
Example 23: Program to demonstrate the set Union using the operator.
It can be seen that the results of the method and operators are the same.
Set Intersection, denoted as X∩Y, is a set that has only common elements.
The syntax is given below:
Only the elements common to both sets (X and Y) are alone in the resultant
Set. To find the number of elements in X ∩ Y, use n(X∩Y) = n(X) + n(Y) −
n(X∪Y), where n(X) is the number of elements in set X.
Example 24: Program to demonstrate the Set Intersection using the
method
Example 25: Another example of a Set Intersection using an operator.
Again, one can observe that the results are the same.
The set difference, denoted as X-Y or X difference Y syntax, is given
below.
The set difference done using the set1.difference (set_variable2) or by the
operator - returns a set of all the elements in the X set that are not present in
the set Y. The following example illustrates the set difference.
Example 26: Program to demonstrate the set difference.
The set symmetric difference, denoted as X^Y, the syntax is given below:
The set symmetric_difference() method or the “^” syntax is used to find the
set operation difference of the Python sets. Only the set of all the elements
in the first or second set but not in both are given in the resultant set for the
symmetric difference.
Example 27: Program to demonstrate the set symmetric difference.
Another example is given below:
Example 28: Program to demonstrate the set symmetric difference
using the method.
Equivalent sets
Two sets, X and Y, are equivalent if they have the same elements. Two sets
equivalence can be checked as X == Y. If the sets are equivalent, it returns
True; otherwise, it is False. If two sets need to be checked for not
equivalence, it can be done as X!=Y. This operator returns True if the sets
are not equivalent; otherwise, it is False.
Example 29: Program to demonstrate the Set Equivalence.
Subsets
A set X is said to be a subset of another set Y if all the elements of set X are
part of set Y. If the set X is a subset of set Y, then by converse, B is said to
be a superset of X. The notations are summarized in Table 9.4.
Table 9.4: Subset operations
Example 30: Program to demonstrate the subset operations.
When X < Y is True, if X is the proper subset of Y; otherwise, it is not a
proper subset of Y. Similarly, if X > Y is True, if X is a proper superset of Y;
otherwise, it returns False.
Example 31: Program to demonstrate the superset operations.
Boolean Functions
In addition to the above functions, set has three Boolean functions,
issubset(), issuperset(), and isdisjoint(), to check whether a set is a subset,
superset, or disjoint Set. The syntax is given as follows:
X.issubset(Y) checks if set X is a subset of set Y. The method returns True
if X is a subset of set Y; otherwise, it returns False. X.issuperset(Y) checks
if set X is a superset of Y. It returns True if X is a superset of Y; otherwise,
it returns False.
X.isdisjoint(Y) method checks if a common element exists between sets X
and Y. If there are no common elements, then sets X and Y are set to be
disjoint sets. The method returns the value True if the sets are disjoint;
otherwise, it is False.
Example 31: Program to demonstrate the subset, superset, and disjoint
Set
Set updates using Mathematical Operations
Sets can be updated using the following methods.
1. Symmetric_difference_update
2. Difference_update
3. Intersection_update
4. Update
Symmetric_difference_update():
Symmetric_difference_update() method updates the set by the operation of
set difference. The syntax for the symmetric_difference_update() is as
follows.
In the above syntax, the set_difference_update method of the set
set_variable1 is invoked with the parameter as another set set_variable2.
The set set_variable1 is updated with set_difference operation between
set_variable1 and set_variable2.
Example 33: Illustration for set_difference_update() method and
set_difference() method.
The two sets set_a and vowels are initiated as follows.
The symmetric difference between the sets is executed by the
symmetric_difference() method and stored in set_a_without_inters.
In the above execution, the set_a remains unaltered, and symmetric
difference operations between the set_a and vowels are stored in
set_a_without_inters.
On using the symmetric_difference_update(), the invoking set itself updated
with the set difference operation.
In the above execution, invoking the symmetric_difference_update from the
set set_a updates itself. In simple words, the result is stored back in the set
set_a.
Difference_update():
The invoking of the difference_update() method from a set updates itself
with the set difference operation. The syntax for the difference_update() is
as follows.
Example 34: Illustration for difference_update()
The two sets, set_a and vowels, are initiated as follows.
To perform the set difference operation between the sets, set_a and vowels
are performed as follows. The difference() method returns the set difference
operation between the sets.
The difference_update() method updates the invoking set itself with the
result of the set difference operation between set_a and vowels.
Intersection_update()
The intersection_update() method is invoked to update the invoking set
with the set intersection operation. The syntax for the intersection_update()
is as follows.
In the above syntax, set_variable1 is updated with the intersection of
set_variable1 and set_variable2.
Example 35: Illustration for intersection_update() method.
In the above execution, the set set_a_inters_vowels stores the intersection
of set_a and vowels. The execution of “set_a.intersection_update(vowels)”
updates the set set_a with the intersection of the set set_a and the set
vowels.
9.5 Looping Operations in Sets
A set is a sequence of elements. It is impossible to use an index to retrieve
items in a set because it is an unordered container; hence, there are no
available indexes. The problem is that they’re iterable. With the for
statement, it is possible to iterate over a set’s elements, as shown in the
following example.
Example 36: Illustration of Loop in sets
9.6 Frozen Sets
Python is equipped with a built-in function called frozenset(). A frozen set
is just a Python set object that cannot be manipulated. If an iterable object is
passed to frozenset(), it produces an immutable frozenset object. The
elements of the frozenset are initialized with elements from the iterable. The
syntax of frozenset() is given as:
The similarities and dissimilarities of sets and frozen sets are listed in Table
9.5.
Table 9.5: Differences between set and frozenset
Because of the immutability of frozenset(), it can construct a group of keys
(or other data) that is unalterable by the user. The frozen set methods are
given in Table 9.6.
Table 9.6: Frozenset operators and operations
Example 37: Program to demonstrate frozenset
The iterable’s elements can be collected into a single frozenset by calling
the frozenset() method and printed in the above example.
9.7 Introduction to Dictionary
An associative array is a data structure that collects keys and values. Here
keys should be unique, and one can insert, remove, update, or lookup for an
item. A dictionary is one kind of associative array that is useful for lookup.
A dictionary is a functional data structure that can hold diverse information.
It is a collection of unorganized items. Unlike other sequences, such as lists
or tuples that have only one value as an element, a dictionary has a key-
value pair for an element.
What is a key? A key is a user-defined index or a unique identifier that
helps lookup. The key-related information is called a value. The syntax of
the Python dictionary is given as follows:
A dictionary’s keys are unique to each other. Keys must be of an immutable
data type, such as texts, numbers, or tuples, while dictionary contents can
be of any kind.
1. The value can be of any type. The values may be unique or mutable.
2. Each shall occur only once in the dictionary.
3. The ordering of the words does not matter, and the order in which users
enter the words into the dictionary may not be the order in which the
words appear.
9.8 Creating Dictionaries
Python can be used to create a villa dictionary. Each of the villa’s solitary
residences has its door number. The database is created when a dictionary
of door numbers and residents’ names is compiled as below:
In this case, door numbers are the keys, and the names of persons are the
values. In addition, the dictionary allows users to link a single villa house
number to multiple people.
A dictionary can be created primarily in two ways:
1. By separating some or more key-value pairs enclosed by curly braces
2. By using dict() method
An empty dictionary- opening and closing curly braces are used to form an
empty dictionary.
The items included in a dictionary may not even be known when it is put
together. Later, it can be filled in. It’s thus possible to generate an empty
dictionary either as {} or by using the dict() method.
Example 38: Program to demonstrate an empty dictionary creation
The following code creates a dictionary for dried fruit. Here, integer
numbers are used as keys, and types of dried fruits as keys.
Example 39: Program to demonstrate the dictionary creation
The dictionary can contain any form of data that enhances the dictionary’s
versatility. The following code illustrates this.
Example 40: Program to demonstrate the dictionary of mixed keys
A dictionary with mixed types like lists as values. The following code
illustrates this :
Example 41: Program to demonstrate the dictionary of mixed keys
dict() is a constructor method in a dictionary that can be used to create a
dictionary. The following code illustrates the construction of a dictionary
using a set of tuples.
Example 42: Program to demonstrate the dictionary of tuples
Another way of creating a dictionary using keys and optimal values is using
a function called fromkeys(). The method fromkeys() creates a new
dictionary with keys from sequence and values set to value. The syntax is
given as :
The method fromkeys(), as shown in the example, create a new dictionary
with keys from sequence and values set to value.
Example 43: Program to demonstrate fromkeys()
In case, the value is not given, then it would be None. Order will not remain
the same when printing.
9.9 Basic Dictionary Operations
One of the essential dictionary operations is membership operations. in and
not in operators help check whether a key is present in the dictionary.
Create a small dictionary given to ABC company for its operations.
Once can use membership operators(in and not in) to check whether a
specific key is present.
Example 44: Illustration of in operator
The following example illustrates the use of not in operators to check the
presence of the key. As stated earlier, the “not in” operator checks for
dictionary membership. Returns True if an element is not listed. The “in” or
the “not in” operator is used to examine if an element is contained in a
sequence.
Example 45: Illustration of not in operator
Accessing dictionary elements
The simplest way to get the element is to use the key to retrieve the value.
By employing the appropriate dictionary keys as user-defined indexes, one
can gain access to the values saved by the dictionary. For example, the get
the value, the key can be specified directly, as shown below:
Example 46: Program to demonstrate accessing a dictionary
It can be observed that there is no key called “0”. Therefore, in that case,
Python reports the error. The value associated with the key is returned for
the rest of the cases.
Another way to access the value is to use the get() method. The method
get() returns a value for the given key. If the key is unavailable, it returns
the default value None. The syntax is given as :
Example 47: Program to demonstrate the get() method
The method get() in example 21 returns a value for the given key.
Instead of getting values one by one, one can get all the values in a single
go using the methods listed below. One can do more with a dictionary with
these functions
dict.keys() isolates keys.
dict.values() isolates values.
dict.items() returns items in a list format of (key, value) tuple pairs.
The syntax is given as:
Example 48: Program to show the use of keys()
9.10 Operations on Python Dictionary
Users can add an item, delete an item, and can update the items. This
section focuses on these operations.
Adding an element
Adding a new key/value pair to the dictionary is very simple. Enter the
key’s name between square brackets, and then use the assignment operator
to assign the value to the key. The index key will include the new key’s
name and matching value. The syntax is given as follows:
It is possible to modify the value of any key in a changeable dictionary. In
square brackets, give the dictionary name and the key. Then, in square
brackets, provide the updated value. For example, if ABC company expands
its operations to Bengaluru, the dictionary needs to be updated as follows:
The extra information can be added as
d1[cityId] = 5
d1[city] = Bengaluru
Example 49: Program to demonstrate the addition of elements to the
dictionary
The method update() updates the dictionary. The updation can be done from
another iterator, like lists or another dictionary. The syntax of update() is:
The following illustrates this point.
Example 50: Program to demonstrate the addition of elements to the
dictionary
Remove the elements of the dictionary
Deleting an individual element can be done using pop() method. This
method removes the item with the given key and returns the value.
Example 51: Program to demonstrate the pop() function
It can be observed that key 3 is removed along with its value.
Another method, popitem(), removes an arbitrary item from a dictionary. It
returns an arbitrary element (key, value) pair from the dictionary. The
syntax is given as follows:
Example 52: Program to demonstrate the popitem() function
The del keyword is used to delete an individual item. It is also used to
delete the entire dictionary. The syntax is given as follows:
To explicitly remove an entire dictionary, use the del statement.
Example 53: Program for deleting the elements in a dictionary
It can be observed that in the above program. Del d1 deletes the entire
dictionary and its other reference results in NameError.
The method clear() removes all the items from the dictionary. The syntax is
given as:
Example 54: Program to demonstrate clear()
It can be observed that the clear() removes all the elements of the
dictionary, and further reference to the dictionary results in an empty
dictionary.
Copying the elements of the dictionary
In Python, there are numerous ways to clone a dictionary, as listed below:
Element-by-element dictionary cloning: Every element pointed to a key
is copied to a new dictionary, that was previously declared. A for loop
can transfer the original dictionary’s elements into a new, empty
dictionary. A shallow copy is created using this technique. Changing this
copy of the dictionary will not affect the original dictionary, (which will
remain unchanged).
dictionary.copy(): This Python function copies a dictionary shallowly.
Without any parameters, it returns a copy of the dictionary. The original
dictionary will stay untouched when changes are made to an abridged
version. The iterable object’s exception is present here, as it is in the for
loop method.
The copy() method creates a new dictionary with a copy of the references
from the original dictionary. The original dictionary is recreated each time
the = operator is invoked. The syntax is given as :
Has no parameters.
A shallow copy creates a new compound object that references the
original’s objects. Recursively, the original object’s components are copied
into the new one, creating a new compound object. Aliasing is an issue
because changes to one can affect the other when two variables refer to the
same object. The copy method allows keeping a copy of the original and
modifying the copy of the dictionary.
Example 55: Program to demonstrate the alias
Alias and dict1 refer to the same object; copy refers to a fresh copy of the
same dictionary. On the modification of the alias, dict1 is also changed, as
shown below:
Example 56: Program to demonstrate the change in the original
dictionary
An unordered database is a dictionary. Using the sorted() method, users can
sort the contents of a dictionary in ascending or descending order. The
syntax is given as follows:
For example, the sorted() method sorts the elements of a given iterable in a
specific order either ascending or descending.
Example 57: Program to demonstrate the sorted()
9.11 Nested Dictionary
The idea of a nested dictionary is to stack dictionaries on top of one another.
It’s structured like a dictionary, with keys and values. The nested dictionary
is not arranged in any particular order.
Example 58: Illustration of the nested dictionary (Listing1.py)
#This is the screenshot of the output for the nested dictionary
Example 59: Program to add an element to a Nested Dictionary.
>>>
>>>
>>>
>>>
>>>
>>>
9.12 Looping Over a Dictionary
In a dictionary, there are several entries. A key-value pair denotes each item.
Looping statements and other traversal techniques can iterate over all the
entries in a dictionary. “for loops” are used to iterate over all the values in
nested dictionaries, most of which are nested ones. There are many ways to
loop over a dictionary, such as accessing both key and value deprived of
items() and accessing key using the build.keys().
Access key deprived of using a key(), Iterate through all key, and value
pairs using items(),Iterate through all values using .values(), Print items in
Key-Value in pair. To loop over a dictionary, the common method used is as
given below:
Example 60: Program to show the for looping over a dictionary
Summary
1. A set is a group of items with a predetermined relationship.
2. Python’s built-in sets store unique elements and cannot contain duplicate
items.
3. Set elements can be of any hashable type, including numbers, strings,
and other objects.
4. Sets cannot preserve item order because they do not preserve element
order.
5. Python sets are ordered lists of immutable elements.
6. Indexes can’t be used to access or insert elements in sets.
7. To determine whether an item is present in a set, developers can use the
“in” keyword.
8. The “remove()” and “discard()” methods are used to eliminate items
“add()” method is to add items to the Set.
9. Sets support many mathematical operations, such as union, intersection,
and difference.
10. “Union()” returns all elements from both sets, “intersection()” returns
only the shared elements.
11. “Difference()” returns a set of elements from the first set that are not in
the second Set, and “symmetric difference()” creates a new set with
items from one Set but not the other.
12. Searching for an element in a list takes lots of time. But items can be
searched quickly in sets.
13. To compare sets to other types of collections or to compare sets with
multisets (sets with duplicate elements) such as lists and tuples,
issubset() and issuperset() methods can be used.
14. To check if a set is a subset or superset of another collection, such as a
list, or tuple, the <= and >= operators can also be used.
15. Python’s issubset() method checks if a set’s elements are in another set to
determine its completeness (the set being checked against).
16. The issuperset() method checks if a set contains all of a more extensive
set.
17. To find elements in one set that are not in another, difference() method is
used.
18. To update the set with the difference between its elements and another
set’s elements, difference update() can be used.
19. An immutable set, also known as a frozen set, is a type of pre-existing
Python set whose elements cannot be modified after being added to the
set.
20. Frozen sets are faster than standard sets when the element is large
because they don’t need to create a new object for each addition or
removal.
21. Frozen set hash values can be dictionary keys or set components. Frozen
sets provide thread safety in multi-threaded applications.
22. Frozen sets are immutable, making them ideal for dictionary keys and set
components.
23. A dictionary is a collection or a set of keys and their respective values of
elements that are not ordered in any particular form.
24. The key-value pairs that make up an element are kept in an associative
container known as a dictionary.
25. Dictionary nesting allows you to store dictionaries within dictionaries.
Glossary
Key - A key can look up a specific value in a dictionary. Keys must be
one-of-a-kind and unchangeable. Each key in the dictionary has an
associated value, and the dictionary itself stores the collection of keys
and their respective values.
Value - The information linked to a key is called its “value” in
dictionaries. The values can be of any data type and can be repeated.
Key-value pair: A key-value pair is a dictionary entry comprising a key
and its associated value.
Set - A set lists immutable elements that are not ordered or unordered.
Associative arrays - A special array that can store any kind of key-value
pair.
Items: Key-value pairs are referred to as “items” in dictionaries.
Nesting - Dictionary nesting refers to the practise of creating dictionaries
within dictionaries. Useful for building sophisticated databases.
Iteration - Repeatedly going through a dictionary’s entries, either by key
or by value, is called iteration. Use the items() or keys() built-in methods
in conjunction with a for loop to accomplish this.
Membership test - To perform a membership test is to check if the
specified key or value already exists in the dictionary. The in keyword or
the inbuilt keys() method can be used for this purpose.
Sorting - Putting a dictionary in order by its keys or values is a process
known as sorting. With the items() or keys() method, the sorted() built-in
function can accomplish this.
Frozen set - A frozen set is just a Python set object that cannot be
manipulated.
Set intersection - Set intersection is an operation on two sets that returns
the subset of elements shared by both sets.
Set Union - Union is a set operation that finds all the elements shared by
two sets and returns them.
Set difference - Set difference is an operation of two sets that return the
set of elements shared by both sets but absent from the other set.
Conceptual Questions
1. Lists, tuples, and strings are typical examples of an organized or ordered
collection of linear (sequential) data structures. Comment.
2. What does it mean to be unorganized in the context of a set?
3. What are the contents stored in the sets data structure in Python?
4. What are the benefits of using python sets?
5. Why do we need the set () function in Python?
6. How does Python’s update() method differ from union ()?
7. What is the distinction between set () and {} in Python?
8. In Python, what is the distinction between set.pop() and set.remove() in
Python?
9. How can you create sets?
10. How to gain access to and modify the components of a set?
11. How do you determine the total number of items in a set?
12. Explain the set operations using appropriate examples.
13. Explain advanced set operations with relevant examples.
14. What does the term “frozenset” mean in Python?
15. Show frozenset operations using relevant examples.
16. Compare and contrast the set, list, and tuple.
17. In what way can a list be transformed into a tuple?
18. Define the dictionary and give its syntax.
19. What is the purpose of a Python dictionary?
20. Make a list of the Python dictionary’s features.
21. Define pop(),clear(),copy() method in dictionary
22. Is deletion of individual dictionary components possible?
23. What is the method for accessing and modifying dictionary elements?
24. What are the dictionary copying methods
25. How do you define “nested dictionary"?
26. How does one iterate through a dictionary in a loop? Using a specific
example, explain.
Try-Out Questions
What is the output of the following:
1. What is the output of the following copy operation:
magic_numbers={ 2, 8, 20, 28, 50, 82, 126}
given_set= magic_numbers
replica_set=given_set.copy()
print(‘\n Replica set of magic numbers = \n’, replica_set)
2. set3 = {10, 20, 30, 40,70,80}
set4 = {47, 56, “24”, 66,70,80}
print(“set1 intersection set2= \n”, set3 & set4)
3. set5 = {2,5,10, 20, 30, 40,70,80}
set6 = {20,30,47, 56, “24”, 66,70,80}
print(“\n set5 difference set6= \n”, set5 - set6)
4. set5 = {2,5,10, 20, 30, 40,70,80}
set6 = {20,30,47, 56, “24”, 66,70,80}
print(“\n set5 symmetric difference set6= \n”, set5 ^ set6)
5. What is the output of the following union operation
set1 = {10, 20, 30, 40}
set2 = {47, 56, “24”, 66}
set3 = set1.union(set2)
print(set3)
6. Which outcome do you anticipate from the following code?
# Set of Citrus fruits
Y={‘Dholka’,’ Kandhari’,’Vadki’,’Kabul’,’Muskati Red’,’Paper
Shelled’,’Pomelo’,’Oranges’, ‘Mosambi’,’Bitter Orange’,’Buddha’s
Hand’,’Citron’}
print(len(Y))
7. If you run the following code, what will it produce?
my_dict1 = {"key1":1, “key2":2}
my_dict2 = {"key2":2, “key1":1}
print(my_dict1 == my_dict2)
8. Which outcome do you anticipate from the following code?
set2 = {11, 22, 33, 44, 55,77}
print(“Elements in set2: “,set2)
If 55 in set2:
print(“\nReturns TRUE - if 55 exists in the set2 “) #Returns TRUE as it
exists
else:
print(“\nReturns FALSE - if 55 does not exists in the set2.”) #Returns
FALSE
9. Which outcome do you anticipate from the following code?
set2 = {11, 22, 33, 44, 55,77}
print(“Elements in set2: “,set2)
if 99 in set2:
print(“\nReturns TRUE - if 55 exists in the set2 “) #Returns TRUE as it
exists
else:
print(“\nReturns FALSE - if 55 does not exists in the set2.”) #Returns
FALSE
10. rare_fruits = {“Fingered Citron “,” Star Fruit”,” Mangosteen”,“Wood
Apple”,” Elephant Apple “,” Indian Sherbet Berries”} #declared Set
if 1786 in rare_fruits:
print(“\nReturns TRUE - if 1786 exists in Set “) #Returns TRUE as it
exists
else:
print(“\nReturns FALSE - if 1786 does not exist in the Set.”) #Returns
FALSE
11. rare_fruits = {“Fingered Citron “,” Star Fruit”,” Mangosteen”,“Wood
Apple”,” Elephant Apple “,” Indian Sherbet Berries”} #declared Set
if “Wood Apple” in rare_fruits:
print(“\nReturns TRUE - if ‘Wood Apple’ exists in Set “) #Returns
TRUE
else:
print(“\nReturns FALSE - if ‘Wood Apple’ doesnot exists in the Set.”)
#Returns FALSE as it doesnot exists
12. What is the output of the following dictionary operation
dict1 = {“name“: “Raja”, “marks“: 92}
temp = dict1.pop(“marks”)
print(temp)
13. #create dictionary d with mixed keys
dict1 ={‘AAA’:’aaa’,’CCC’:53}
d = dict1
d
#print dictionary d containing mixed keys
print(‘dict1 dictionary created with mixed keys=\n’, d)
print(“\nValue:%s” %d.values())
14. #create dictionary d with mixed keys
dict1 ={‘AAA’:’aaa’,’CCC’:53}
d = dict1
d
#print dictionary d containing mixed keys
print(‘dict1 dictionary created with mixed keys=\n’, d)
print(“\nValue:%s” %d.items())
15. #create dictionary d with mixed keys
dict1 ={‘AAA’:’aaa’,’CCC’:143}
d = dict1
d
#print dictionary d containing mixed keys
print(‘dict1 dictionary created with mixed keys=\n’, d)
print(“\nValue:%s” %d.values())
print (“Value : %s” % d.setdefault(‘CCC’, None))
print (“Value : %s” % d.setdefault(‘XYZ’, None))
16. # fromkeys()creates a new dictionary with the given keys and values.
keys = [‘name’, ‘temperature’, ‘symptom’]
values = [‘Anamika’, 98.2, ‘headache’]
new_dict = dict(zip(keys, values))
new_dict
17. #keys()returns a list of all keys in the dictionary.
dict_ori = {‘name’: ‘Anamika’, ‘temperature’: 98.2, ‘symptom’:
‘headache’}
keys = dict_ori.keys()
print(keys)
18. ghee_choestrol={‘Cow Ghee’:0.32,’Buffalo Ghee’:0.27}
alias = ghee_choestrol
alias
copy = ghee_choestrol.copy() # Shallow copy
copy
19. ghee_choestrol={‘Cow Ghee’:0.32,’Buffalo Ghee’:0.27}
ghee=ghee_choestrol
print(‘\ndict = ghee_choestrol =\n’, ghee)
print(“\nValue:%s” %ghee.get(‘Buffalo Ghee’))
print(“\nValue:%s” %ghee.get(‘Cow Ghee’,“NIL”))
20. oil_cholestrol=
{‘Coconut’:0.15,’Mustard’:0.28,’Sunflower’:0.42,’Groundnut’:0.71}
x=oil_cholestrol
print (“Start Length of the dictionary oil_cholestrol,BEFORE clear() is :
%x” % len(oil_cholestrol))
oil_cholestrol.clear()
print (“End Length of the dictionary oil_cholestrol ,AFTER clear() is : :
%x” % len(oil_cholestrol))
21. #use copy() method to create a copy of a dictionary
ghee_choestrol={‘Cow Ghee’:0.32,’Buffalo Ghee’:0.27}
ghee=ghee_choestrol
ghee
new_ghee1=ghee.copy()
new_ghee1
22. #use dict()to create a new dictionary from keys & values of another
dictionary
ghee_choestrol={‘Cow Ghee’:0.32,’Buffalo Ghee’:0.27}
new_ghee2=dict(ghee_choestrol)
new_ghee2
Programming Exercises
1. Given a pair of sets, generate a new set by collecting all of the elements
from one Set that are absent from the other Set.
integer_set ={21,12,22,33,65,698}
magic_number_set={2, 8, 20, 28, 50, 82, 126}
2. Given a pair of sets, generate a new set by removing all the elements
from the first set that are also present in the second s+et.
integer_set ={21,12,22,33,65,698}
magic_number_set={ 2, 8, 20, 28, 50, 82, 126}
3. Given a pair of sets, create a new set containing only the elements in
both sets.
set1 ={16,21,12,22,23,32,45,54,33,65,698}
set2={ 2, 8, 20, 28, 45,50,54,33, 82, 126}
4. Write a program to create a new set containing all the elements from the
two sets with no replicas.
set1 ={16,21,12,22,23,32,45,54,33,65,698}
set2={ 2, 8, 20, 28, 45,50,54,33, 82, 126}
5. Write a Python program to eliminate elements (777,9,999) using
remove().
set1={3, 4, 5, 6, 7, 8, 777, 9, 333, 999, 555, 444}
6. Write a program in Python to use discard() to remove the numbers 6, 7,
and 8 from the given Set ().
set1={3, 4, 5, 6, 7, 8, 555, 333, 444}
7. Write a Python program that analyses whether or not two sets can be
considered equivalent to one another by analysing whether or not they
can be considered a superset of one another.
sets = {10,20,30,40,50}
set1 = {11, 22, 33, 44, 55,77}
set2 = {22, 44}
set3 = {22, 44}
8. Write a program to display the growing set1. Use add() to add each new
element to the given set.
Given set:
set1 = {555, 777,999,333,444}
elements to be added are (6, 7, 8,9,5,4,3)
9. Write a program to display the growing set2 as you add multiple
elements to the given Set.
Given set:
set2 = {555, 777,999,333,444}
elements to be added are “6, 7, 8,9,5,4, and 3”
10. Given a pair of sets, write a program for operations of Union,
Intersection, difference, and symmetric difference operations.
prime_numbers={2, 3, 5, 7, 11}
fibonacci_set ={0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
11. For the given set, write a for-loop program to create a new set called
“replica_set” with the same elements as the “given_set."
prime_numbers={2, 3, 5, 7, 11}
12. Write a Python program to sort the given dictionary by its value.
Given :
dict = {6:’Ether’ ,2:’Bitcoin’ ,1:’Gas’ ,9:’Litecoin’ ,7:’Ganache’
,8:’Ethereum’}
13. Construct a dictionary of pulses and their calorie counts from the table
below. Write a program that accesses the matching value at the key.
14. Write a Python script to create and print a dictionary (x, x*x) containing
a number (between 1 and n) in the form (x, x).
15. Find the highest three values of matching dictionary keys using the
Python program.
my_dict = {‘Ether’: 233, ‘Bitcoin’: 4854, ‘Litecoin’: 789, ‘Gaslimit’:
13325}
16. Make a replica of the sample dictionary with the dict() function:
sampledict = {
“brand“: “HP”,
“model“: “Pavilion”,
"year": 2022
}
17. Using Python, write a program that removes all elements from a given
dataset.
set1 = {“Ether”,“Bitcoin”,“Litecoin”}
18. By writing a Python program, check if the two sets have no elements in
common.
19. Remove the Intersection of a second set1 ={16,21,12,22,23,32,65,698}
set2={ 2, 8, 20, 21,28, 45,50,54,33, 82, 126} set from the first set using a
Python program.
20. If you want to construct a dictionary with the relevant fundamental
values: 21-30; 31-40;41-50; and 51-60, write a Python program. Using
the dictionary, get the fifth value of each of the keys.
21. Create a dictionary named “pulses_calories” from the table of pulses and
calories given below.
Write a program that updates the created dictionary with the contents
given below. Print the contents of the dictionary before and after the
update operation.
22. Given myDict={‘FF012’:296,’GG013’:426,’HH014’:683,’LL015’:964}.
Write a program to delete the element ‘GG013’ from the myDict.
23. Using the given sets, write a Python program to determine the equal to
and not equal to operations on Set.
set1={‘Tangerine’,’Pomelo’,’Oranges’, ‘Mosambi’,’Bitter Orange’,’
Buddha’s Hand’,’Citron’} # first Set of Citrus fruits
set2={‘Tangerine’,’Pomelo’,’Oranges’, ‘Mosambi’,’ Bitter Orange’,’
Buddha’s Hand’,’Citron’}
# second Set of Citrus fruits}
# second Set of Citrus fruits
# third Set of Pomegranate fruits
set3={‘Dholka’,’ Kandhari’,’Vadki’,’ Kabul’,’Muskati Red’,’ Paper
Shelled’}
# fourth Set of Mangoes
set4={‘Banganapalli’,’Payri’,’Kesar’, ‘Neelum’,’ Alphonso’,’Dasheri
‘,’Totapuri’,’ Chausa’}
24. Using the given sets, write a Python program to determine the equal to
and not equal to operations on Set.
# third Set of Pomegranate fruits
Set3={‘Dholka’,’ Kandhari’,’Vadki’,’Kabul’,’Muskati Red’,’Paper
Shelled’}
# fourth Set of Mangoes
Set4={‘Banganapalli’,’Payri’,’Kesar’,’ Neelum’,’Alphonso’,’Dasheri ‘,’
Totapuri’,’ Chausa’}
25. Using the given sets, write a Python program to determine the
LESSER THAN OR EQUAL TO and GREATER THAN OR EQUAL
TO
operations on Set.
# third Set of Pomegranate fruits
Set3={‘Dholka’,’ Kandhari’,’Vadki’,’Kabul’,’Muskati Red’,’Paper
Shelled’}
# fourth Set of Mangoes
Set4={‘Banganapalli’,’Payri’,’Kesar’, ‘Neelum’, ‘Alphonso’,’Dasheri
‘,’Totapuri’,’ Chausa’}
26. Using the given sets, write a Python program to determine the
LESSER THAN OR EQUAL TO and GREATER THAN OR EQUAL
TO
operations on Set.
Set5={2,6,18,54,86} # fifth Set
Set6={2,6,18,36,54,86} # sixth Set
27. Write a program to show the addition of elements to a set one by one.
Given
x= varieties of Guava fruit popular in India
x ={“Allahabad Safeda”, “Pant Prabhat”,“Dhareedar”, “Chittidar”}
OceanofPDF.com
It is better to have 100 functions operate on one data structure than to have
10 functions operate on 10 data structures.
Alan Perlis
CHAPTER 10
Strings
The Chapter “Strings” introduces the reader to the concepts of strings and
string operations. The subsequent sections show how to change Python
strings on the fly using string built-in functions, methods, and string
operators.
Learning Objectives
After completing this chapter, the reader will be familiar with the usage
related to:
Know how to access individual characters or values in a string
Acquire a fundamental understanding of how to manipulate string
appearance, order, and contents
Retrieve, search for a substring from a string
Understand how to use string methods to manipulate string using string
operators, methods, and functions
Acquire a fundamental understanding of methods of strings, like count,
split, replace, find; and index, which searches strings for substrings
Know how to format a string and visit each of its characters with a for
loop.
Ability to make use of regular expressions to match patterns in strings
10.1 Introduction to Strings
Strings are an exceptional and distinctive data type in the Python
programming language, and mastery is essential for working on practical
applications. Strings can store anything and everything, accommodating
words, sentences, PAN numbers, mobile phone numbers, or even regular
expressions.
A string is a data type (numbers - an integer and floating-point text) that
comprises a set of characters that can also contain spaces and numbers.
Strings are ordered sets of characters used to denote non-numerical data,
such as works of literature, genetic sequences, etc. In strings, the order is
vital, and this ordering feature differentiates strings from sets of characters.
Strings are convenient for modelling sequential behaviour, and computers
are sequential machines. String management is an essential operation in
many algorithms; it aids in data validation, text parsing, file conversions,
etc., Python strings are a series of Unicode characters that cannot be
changed (immutable property). Strings come in handy for passing data back
and forth between the application and its users. To store data for the
computer, they are less valuable.
Real-Life Applications of Strings
Database interaction: SQL queries are strings.
File manipulation: Strings are used in reading and writing text, CSV, and
JSON files.
Game development: Data in games, such as characters, dialogue, and
level design, uses strings.
Networking: HTTP requests and responses, as well as other messages,
are strings.
Scientific computing: Applications like bioinformatics and data
visualisation.
Text processing: Text Editors, text analysis, and natural language
processing applications.
Web development: Strings are present in HTML, XML, URLs, and in
other internet-essential data.
10.2 Creating a String
To create a string, write the series of characters inside single quotes, double
quotes, or triple quotations, and then assign it to a variable. Strings are at all
times delimited in quotes. A pair of single quotes can enclose the strings
(‘…’), double quotes (“…”), triple single quotes (‘‘‘…’’’), or triple double
quotes (“““…“"”). In Python, a single-quoted string is like a double-quoted
string.
Strings can calculate independently, but the results are displayed in single
quotation marks. First, embed quotations within a string, and re-fix the
embedded quote with the escape symbol. Escape characters such as “Tab”
and “Return” are used to include special characters in a string. Even though
there are no characters, the empty string is still a string, denoted by “ “.
Escape sequences are used to permit special characters in strings. To place a
single quote (‘) inside a single-quoted string, escape sequence \’ is used, as
shown in the output of the example1.
Example 1: Program to show the creation of strings.
As seen above, the string is also created using the single quotes (‘‘). The
strings can also have escape sequences. The following code shows the
usage of the escape sequence in the strings.
In the above code, the variable sample_string contains strings with escape
sequences, the newline (\n), and tabular space (\t). The variable
sample_string is printed using the print() function. The output shows that
the escape sequences are executed while printing the variable.
A string enclosed in single or double quotes cannot extend to the following
line of code. However, strings surrounded by triple quotes can extend across
multiple lines of code as the line breaks are merged into these strings. The
key differences between the single and double quotes in Python are listed in
Table 10.1.
Table 10.1: Key Differences between the Single and Double Quotes in
Python
A triple-single-quoted or triple-double-quoted string spans multiple lines.
There is no need for an escape sequence to place a single/double quote
inside a triple-quoted string. Triple-quoted strings are helpful for multi-line
documentation, HTML, and other codes. In this chapter, the string creation
with iteration loops can be seen.
Example 2: Program to show the creation of a single, double, and
triple-quoted string.
The Python program in the following instance shows the convention of
using single, double, and triple quotes in python.
Unlike single and double quotes, triple quotes could hold multi-line strings.
The following example could explain the case.
The User can notice that the interpreter waits for the next line when the
enter is pressed in the unfinished triple quotes. When the print is called with
that variable, the string stored in the variable is printed as plain text.
10.3 Operations in Strings
The strings in Python are manipulated and accessed using various
operations. The operations on the string are broadly classified into the
following types.
1. Indexing and slicing
2. Concatenation
3. Replication
4. Membership Operations
The above operations are briefly discussed in this section.
1. Indexing and Slicing:
String indexing and slicing are both workable in Python. A string is a
collection of characters accessed through an index. For example, the value
“MOTIVATE” is assigned to the variable var1 as follows.
The positive indexing of the string starts from 0 (from the first character of
the string), whereas the negative indexing starts from -1 (from the last
character of the string). Technically, since the positive indexing starts from
0, the positive index n corresponds to the n+1 th character. The following
Fig. 10.1 shows the indexing in the variable var1.
Fig. 10.1: Indexing for the string “MOTIVATE” stored in var1.
In the above Fig. 10.1, the positive index 3 denotes the letter “I” whereas
the index “-3” denotes the letter “A”. To pick and lift a character(s) at a
specific index within a string is needed on certain occasions in real life.
With a known index, the range of characters can be retrieved using the
slicing feature.
Accessing string elements or values in strings -To extract a single
character from a string, surround it with square brackets ([]) and the index
of the desired character. Indices begin at 0. The Fig. 10.1 displays that the
character at index 3 is ‘I’ and the character at index -3 is ‘A’. The index of
‘-1’ represents the last character of the string. Likewise, using ‘-2’, the
penultimate element of the string can be accessed.
Example 3: Illustration for accessing the strings using indexing.
The following program illustrates the usage of the above-discussed
indexing on a string.
In the above code, the variable string1 is assigned to the string
“MOTIVATE” and the indexing operation is executed as follows.
Positive Indexing: As mentioned above, the positive index starts from 0
(corresponding to the string’s first character (the leftmost character)) and
moves towards the end. The following execution shows the execution of the
positive indexing in the string1.
In the above executions, the Reader can identify that the 0th index
corresponds to the first character of the string “M”, the 2nd index
corresponds to “T” and the 3rd index corresponds to “T".
Negative Indexing: As mentioned above, the negative index starts from the
last character (i.e., the rightmost character with a starting index of -1). The
following execution shows the execution of the negative indexing in the
string1.
In the above execution, the negative index “-1” corresponds to the
rightmost character of the string, which is “E”, then -2 and -3 correspond to
“T” and “A” as shown in Fig. 10.1.
Slicing: In strings, slicing refers to extracting the substring from a string
according to the indexing and step size. The syntax for the slicing operation
is as follows.
Strings slicing has three values
m - start [optional] - An integer number showing slicing starts. The
default setting is 0.
n - end [optional]- An integer denoting the endpoint of the slicing.
step[ optional]-. An integer number denotes the slicing step. The default
setting is 1.
Example 4: Illustration of Slicing in a string.
The string “Dolphin” is stored in the string2 variable, as shown below.
The different slicing is shown in Table 10.2.
Table 10.2: Slicing Operations
2. Concatenation Operation:
Concatenation refers to combining any two or more strings into a single
string. The methods for string concatenation in Python are as follows:
1. Using “+” operator
2. Using % Operator
3. Using f-String
Using the “+” Operator: The operator concatenates two or more strings.
The following example shows the usage of the “+” operator for the
concatenation operation.
Example 5: Program to show the concatenation of strings using the “+”
operator.
The program shows the initialization of two strings, string1 and string2,
with the values “Actions” and “Speak” and then concatenate together with a
space and store it variable “x”.
Using % Operator: Strings can be concatenated by placing “%s” inside
the quotes and providing the strings inside the parenthesis in order after
placing the “%” between them. The syntax for performing concatenation
using “%” is as follows.
In the above syntax, as many %s can be given inside the double quotes
(Users can use single or triple quotes), and following that, the strings to be
placed are given inside the parentheses “()".
Example 6: Illustration of concatenation operation of the strings using
the “%” operator.
The concatenation operations can also be implemented using the “%”
operator.
The line result = “%s %s"%(string1, string2) places the stings stored in
string1 and string2 with space between them and stores the concatenated
string in the variable result.
Executing the above statement in triple quotes, the result would be the same
as above. The execution is as follows.
Using f-Strings: As seen in Chapter 3, the f-strings can also implement
concatenation operations. The following example shows the concatenation
operation using an f-string.
Example 7: Illustration of concatenation using f-strings.
In the above example, the line result = f“{string1} {string2}” places the
stings stored in string1 and string2 with space between them and stores the
concatenated string in the variable result. Strings can be joined using the
string join method also and it is discussed in this chapter in string methods.
3. Replication Operation:
Replication operation is performed by “*” operator. There are two ways of
operating: n * string_variable and string_variable * n, where n denotes the
number of times replicated.
Example 8: Program to show the replication of strings.
The following execution also gives the same result as above.
In the first execution, replication is executed by string1 * 3, and the latter is
by 3*string1.
4. Membership Operations:
A membership operator is a Python operator that determines whether a
value is a string member, as shown in Table 10.3. This operator is used to
validate whether variables like strings, integers, and tuples are members.
Membership operators comprise several operators. The “in” and “not in”
membership logical operators are the two membership logical operators for
string. They are used to see if an element is part of a string.
Table 10.3: Syntax of Membership Operators in Python
Operands for membership operators are:
string_element: Check to see if the string searched for is linked to
another string.
string_variable_name: This is the original string variable, which must
be verified to lie in the string as a substring.
Example 9: Illustration of membership operations in Strings.
10.4 Built-in Methods for Strings
isdigit() and isnumeric() - For each string passed to Python’s isdigit()
method or isnumeric() method, it determines whether a string contains only
numeric characters (0-9). If all the characters are numbers, it returns True;
otherwise, it returns False as a Boolean value. The syntax for isdigit() and
isnumeric() methods are as follows.
Example 10: Program to display the use of isdigit()
Example 11: Program to show the difference between isdigit() and
isnumeric()
isalnum()-For each string passed to Python’s isalnum() method, it checks if
a string is all alphanumeric (i.e., letters and numbers only, no special
characters or spaces). This method returns “True” if the input characters are
alphabetic or decimal digits, and “False” otherwise.
Example 12: Program to display the use of isalnum()
The above example shows the use of the method isalnum(). As it contains
an alphanumeric character(s), it returns true.
isspace()-Python’s Built-in isspace() method checks if all characters in a
string are whitespace (i.e., spaces, tabs, newlines, and other non-printing
characters) and return true if the Python string contains only white space(s).
isspace() returns False if the string is empty or contains non-whitespace
characters like letters, numbers, and special characters.
Example 13: Program to show isspace()
The Python program is as follows:
Example 14: Illustration of a few more Built-in string methods.
Likewise, the built-in functions shown in the examples below returns ‘True’
if the condition is satisfied; otherwise, ‘False.’ Some of the important
methods are shown in Table 10.4.
Table 10.4: Important Built-in methods
upper() - It generates a new string with all the original characters
capitalized.
lower() - It generates a new string with all the original string’s characters
converted to a lowercase.
Example 16: Program to show upper() and lower() string conversion
operations.
title() – Each string passed to title() returns a new string with the first
character of each word capitalized and the leftover characters lowercase.
capitalise()- Each string passed to capitalise() will return a new string with
the first character capitalized and the leftover characters lowercase.
Example 17: Program to show title() and capitalize()
Title() will not split words separated by special characters like ‘_’ or ‘-’; it
only considers whitespace and punctuation as word delimiters.
swapcase()-Each string passed to swapcase() returns a new string with
uppercase characters converted to lowercase and vice versa.
Example 18: Illustration for swapcase() method.
casefold() - This function is used to compare case-insensitive strings. Case
folding is an extreme lower-casing that accommodates script letter
variations.
Example 19: Illustration for casefold() method in strings.
index() method: The index() method associated with the string variable is
used to find the index of the string in the corresponding variable. The
syntax of the index() method is as follows.
The index() method takes a string as an argument and could be of any
length. If the length of the string is 1 (i.e., one letter), the index of the first
occurrence of the letter in the string is returned. If the length of the string is
over 1 (i.e., itself a string or substring), the index of the occurrence of the
first character of the substring in the string_variable is printed. In both
cases, if the string is not found in the string_variable, the ValueError
“ValueError: substring not found”, is initiated.
Example 20: Illustration of the index() method on string.
A string “MOTIVATE” is stored in the variable string1 as follows.
The index of the character “M” in the above string can be extracted using
the following code.
In the above execution, the index of the character “M” is returned as 0, and
it could be verified using an index operation using “[]”. As seen above, the
index() function extracts the index of the first occurrence of the string. The
following execution shows the same.
The readers could identify that the character “T” occurs twice in indices 2
and 6. Since it is an index() function, it returned the index of the first
occurrence, i.e., 2. If the string of length over 1 is given as an input to the
index() function, the index of the first character of the given in string is
returned. The following example shows the finding of the index of the
substring of “ATE” in the string string1.
The above execution shows that the first character “A” of “ATE” occurs at
the index 5 in the string “MOTIVATE”. The following execution gives
more clarity about this.
In the above execution, the first character of the string “TE” is “T”. Though
“T” occurs at two positions, but the sub-string “TE” starts occurring at
index 6 in the string “MOTIVATE".
rindex()-When no identical string is found, the substring’s position in the
string is returned. If no matching string is found, the exploration interval
can be stipulated using the optional parameter [start: end].
The index() and rindex() methods find a substring in a string, but they
handle multiple occurrences differently. index() returns the index of the first
character in a substring. On the other hand, rindex(), finds the last substring
in a string and returns its first character. If the substring is not found,
rfind(), and find(), return -1.
Example 21: Illustration to show index() and rindex()
find()- The find() method is used to find the index of a substring in a string,
in case it is a string. If a Python string is not found, then -1 will be returned.
Example 22: Illustration for find() method in string.
rfind()- finds the last occurrence of the specified value, and it returns -1 if
the value is not found. The syntax of rfind() is given as:
There are three parameters: value (required and is the value to look for),
start, (optional- where to begin the search, 0 is the default), end(optional-
where to end the search. End of the string is the default value.)
Example 23: Illustration of rfind() method in string.
strip() - strip() is a method for removing white space (characters like
spaces, tabs, newlines) from a string object.
Example 24: Illustration of strip() method in strings.
rstrip() – The rstrip() method is used to eliminate the specified character
string end (default is a space) or right of the string.
lstrip()-used to eliminate the specified character string end (default is a
space) or left of the string.
Example 25: Program to show rstrip() and lstrip.
The three strings are printed with white spaces.
The white spaces on the right side are striped and printed.
The white spaces on the left side are striped and printed.
The fourth string is obtained by concatenating the curtailed string1 with
another string3.
The fifth string is obtained by concatenating the curtailed string2 with
another string3.
join(seq) or join() -This function returns a new string that is the
concatenation of the strings in iterable with a string object as a delimiter.
The separator is used to demarcate individual components of the output
string.
Example 26: Illustration of join() method.
The Python program uses the join() method to combine a list of strings into
a new single string like the one below:
Example 27: Illustration of join() method for concatenation operation.
splitlines(num)- used to split the content of the invoked string at each
occurrence of a newline character, i.e., splitlines() returns a list of split
values. Python’s splitlines() method returns a list of strings for each line in a
string. The newline character (‘n’) is the default line delimiter.
split() -Given a string and separator, split() returns a list of substrings.
Example 28: Program to show splitlines() and split()
Using the newline character as the default separator, the splitlines() method
divides the input string into several parts, each of which is returned in its
separate list.
replace(old,new[,count]) – used to replace chunks of a string with new
ones.
Example 29: Program to show replace()
10.5 Built-in Functions for Strings
len() - For each string passed to len(), it will return the number of items
(length) of the string object.
An object is a parameter that is needed, which can be an iterable or string.
The function can have a single argument(s) like a sequence (string, bytes,
tuple, list, range or a collection (dictionary, set, frozen set).
Return Value: The total number of elements or characters is returned as an
integer.
Example 30: Program to find the length of the string
count() –used to calculate the number of occurrences of a substring in a
string. This feature is handy for conversion and finding operations. The
syntax is given:
It has two optional parameters: substring–a string that needs to be counted
and start (Optional) - start index within the string where the search begins.
Example 31: Illustration for showing the count() function in strings.
Example 32: Illustration of string counting using the count() function.
The method str. count(substr [, start [, end]]) also returns the number of
occurrences of Python substr faith.
max()-Returns the largest value from the stated iterable or multiple
arguments.
It has three parameters:
iterable: The iterable can be a list, tuple, set, dictionary, or string.
key: (optional) The built-in or user-defined function to be used for
contrast.
default: (optional) The value to be refunded if the iterable is vacant
min(): The string values are equated lexicographically to get the precise
result. On the inside, the ASCII values of the characters are equated.
Example 33: Program to show min() and max()
The Python program is:
String Comparison - Strings are evaluated by comparing character by
character in Python using relational operators. The computer memory has a
quantity for each character. Character comparisons are not made as
character to character like F to F or h TO M. It is done by comparing the
ASCII codes values. For example, in ASCII, F takes a value of 070, h takes
a value of 104, and M takes a value of 077. Based on the values, they are
compared, analyzed, and then reported. Thus, the comparative functions
compare the contents of string objects (or substrings) with the exact
characters specified by its arguments.
Example 34: Program to show the comparison.
If the results are the same for the ‘string1’ and ‘string2’, true is returned;
otherwise, whether string1 is lexicographically larger or smaller than
string2 produces a positive or negative result.
10.6 Looping with Strings
Many computations require that a string be processed one character at a
time. It sometimes demands using the index value to get every string
element one at a time. The string traversal can loop through string indexes.
It helps to keep track of how many characters have been typed in. It also
aids in spreading out a string by taking every single character from the
string one at a time and then printing it one in a line. They frequently start
at the beginning, pick a character at a time, do something to them, and then
continue until the end. This computational pattern is known as a traversal.
Python calls character-by-character iteration “traversing a string."
Traversing a string entails using the subscript to access all the string’s
elements in rapid succession. An example is shown in Fig. 10.2.
Fig. 10.2: Indexing Operations
Strings based on the index value for the word “WISDOM”
To traverse a string easily, the for loop or while loop comes in handy, as
shown in the following example.
Example 35: Program to traverse a string using a for-loop iteration and
print it.
“for-loop” helps traverse and iterate over a string in many real-life
applications. This is because the string is a sequence of characters and is
easy for us to understand.
Traversal with the “for-loop” with range: The range function is used to
create character indices consistently. The “for-loop” could then iterate over
all these positions. With the indexing operator, these positions could access
individual characters in the string. Iteration by location enables Developers
to change the series of index values to regulate the traversal direction.
Because ranges can be formed to count down along with up, the code below
would print the characters from right to left.
Example 36: Program to show the string traversal using for loop with
range.
The Python program is:
10.7 Regular Expressions
Regular expressions are a well-known topic in computer science. They are
frequently used in programming tasks. Regular expressions can undertake
highly complicated text handling.
Regular expressions are a broad method of matching patterns in text. A
regular expression is also known as regex or regexp. Regex helps search
and identify textual patterns and insights, breaking down a string into
substrings, substituting parts of a string, eradicating digits, removing
punctuation, and removing white spaces from the pattern? A pattern is a
collection of strings. For example, for the pattern “yx*,” strings such as “y,”
“yx,” and “yxxx” are all accepted.
Regex is used in search engines, text processing applications, web scraping,
and pattern-matching applications heavily use the regular expression
paradigm. Google Analytics URL matching algorithm includes the use of
the reserved word re. Besides Google Docs, Sublime Text, Notepad, and
Microsoft Word, it is sometimes used to execute, search and replace
operations with the most popular editors. It can also rename files, run
database queries, and create web directives (Apache).
Every character has a special meaning in regular expressions. So, it is better
to specify it as a raw string.
One such example of the raw string is r’yx*’. The advantage of raw string
is that when working with raw strings, the newline (n), tab (t), and other
unique string literal notations are not interpreted. Raw strings should always
be used when defining regular expression patterns.
To use regular expressions, one has to use a python package called “re
package”. There are many methods available in the “re package” to handle
regular expressions. Some of the most important methods are listed in the
following Table 10.5.
Table 10.5: re Package methods
How to form a regular expression?
One of the most important things in regular expression is forming a pattern.
A pattern is created using literal characters and meta characters. Meta
characters are special characters that impact how a regular expression finds
patterns and are used to define the pattern of search or manipulation.
Some of the literal characters are listed in Table 10.6, which can work with
Regular Expressions. It must be done to pinpoint all instances of a specific
data type needed. To ensure that a piece of text conforms to a particular
format.
Table 10.6: Character Class
Meta characters include opening and closing square brackets ( [ and ] );
backslash ( \ ); caret ( ^ ); dollar sign ( $ ), etc. Some of the important meta
characters are listed in Table 10.7.
Table 10.7: Regular expression with description
Regex utilities such as find all, search, split, sub, and many others are
included in Python’s built-in “re package".
Matching Function
The re.match() method only matches the regex pattern at the start of the
string.
The syntax of re.match() is given as:
Has three parameters pattern, string, and flags.
pattern -This is the regular expression used to match.
string - This is the string that would be investigated to match the pattern
at the start of the string.
Flags–flags are modifiers. It is specified using a bitwise OR.
On success, the re.match function returns a match object; on failure, it
returns None.
Example 37: Program to illustrate re.match() (Listing1.py)
#This is the screenshot of the output re match()
Matching of Full String
The re.fullmatch() method matches the regex pattern to the entire target
string. If the full match of the pattern is present, then the match object is
returned. Otherwise, it is None. The syntax of re.fullmatch() is given as :
Has three parameters, namely, pattern, string, and flags.
pattern -This is the regular expression used to match.
string - This is the string that would be investigated to match the pattern
at the start of the string.
flags – flags are modifiers. It is specified using a bitwise OR.
On success, there. match function returns a match object; on failure, it
returns None.
Example 38: Program to illustrate re.fullmatch() (Listing2.py)
Finding all Occurrences of Match
The re.findall() method matches the regex pattern to the entire target string
and returns a list of all the occurrences.
Example 39: Program to illustrate re.findall() (Listing3.py)
#This is the screenshot of the output for re.findall()
Search Function
The re.search() method only matches the regex pattern within the string.
The syntax of search() is the same as that of re.find(). The syntax of
re.search() is given as :
Has three parameters pattern, string, and flags.
pattern -This is the regular expression used to match.
string - This is the string that would be investigated to match the pattern
at the start of the string.
flags– flags are modifiers. It is specified using a bitwise OR.
On success, there. match function returns a match object; on failure, it
returns None.
Example 10.40: Program to illustrate re.search() (Listing4.py)
#The screenshot of the output for Search()
Substitute or Replacement function
Using this method, one can replace the matched regular expression in the
target string. Here the word sub indicates the substitution or replacement.
The method returns a string in which any occurrences that match are
replaced with the value of the replaced variable. The syntax for re.sub() is:
Example 10.41: Program to illustrate re.sub() (Listing5.py)
#This is the screenshot of the output for re.sub()
re.subn() function is also the same as re.sub() except that it returns the
number of replacements.
Split Function
re.split() is a useful function that can divide the entire string into a list of
tokens according to the pattern. The following code splits the names using
the colon present in the string.
Example 10.42: Program to illustrate re.split() (Listing6.py)
The output of this program is a list of tokens, as shown below:
Summary
1. A string is an immutable sequence of characters.
2. The string data type can be numbers - an integer, floating-point text, and
a set of characters containing spaces and numbers.
3. A pair of single quotes can enclose the strings (‘…’), double quotes
(“…”), triple single quotes (‘‘‘…’’’), or triple double quotes (“““…“"”).
4. Triple-quoted strings can hold strings of multiple lines, typed using
several enter keys. Single and double quotes multiple lines are entered
by placing “\” at the end of every line.
5. A string may contain escape sequences.
6. Concatenation of strings are performed by using “+”, “%”, f-strings and
join method.
7. The collection of characters in a string can be accessed through an index.
8. Substrings of a string can be extracted using indexing with a range
specified, called slicing.
9. To determine whether or not the specified string with Unicode numbers
consists entirely of digits is affected through- isdigit() or isnumeric().
10. Python’s isalnum() string method returns True if all characters in the
string are alphanumeric (letters and digits) and False otherwise.
11. The isalpha() string method checks to see if all characters are letters and
return True if they are, or False if they are not.
12. The built in functions of upper() and lower() are case-sensitive(checks if
they are uppercase and lowercase letters).
13. Each string passed to title() returns a new string with the first character
of each word capitalized and the leftover characters lowercase.
14. Each string passed to capitalise() will return a new string with the first
character capitalized and the leftover characters lowercase.
15. The methods swapcase() and casefold() only change alphabetical
characters, not whitespace or special characters.
16. index() method returns the index of the first character in a substring.
17. rindex() method finds the last substring in a string and returns its first
character.
18. find() method function finds the first occurrence of the substring to the
left (beginning) of the string.
19. rfind() method finds the last occurrence to the right (end). If the
substring is not found, rfind(), and find(), return -1.
20. To remove whitespace(characters like spaces, tabs, newlines) from a
string object, strip() is used.
21. rstrip() is used to eliminate the specified character string end (default is a
space) or right of the string.
22. lstrip() is used to eliminate the specified character string end (default is a
space) or left of the string.
23. To reverse a list of characters, which efficiently reverses a string, the
reverse() method is used.
24. To split the content of the invoked string at each occurrence of a newline
character, splitlines(num) is used.
25. Python’s splitlines() method returns a list of strings for each line in a
string.
26. Regular expression is an advanced way of finding, searching, and
replacing patterns in a string.
Glossary
Strings – Strings are ordered sets of characters used to denote non-
numerical data such as works of literature, genetic sequences.
Immutable - Strings are said to be immutable when their contents
cannot be modified.
Concatenation- Concatenation is an operation of combining two or
more strings into one.
Repetition-Repetition actually concatenates numerous copies of the
same string to form new strings.
Slicing- The subscript operator uses a process called slicing to extract
the desired parts of Python strings using a slicing colon “:” operator.
Traversing - Traversing a string entails using the subscript to access all
of the string’s elements in rapid succession.
Iterable-A string is iterable and can be looped. Each component of a
string iterable is iterated over to check on each value.
Iterator-A Python iterator is an object with a countable number of
iterable elements. They are objects that enable one to navigate a group of
items one at a time. Iterators represent a stream of data and employ the
__next__() function to create data items one at a time.
Expression- An expression is a way to convey a value. Expressions are
merely a representation of value, whereas statements perform actions.
Regular expression-RE are a broad method of matching patterns to
character sequences. A regular expression is shortened regex or regexp.
Pattern-A string of characters characterizes a search pattern, typically
used in pattern matching with strings or string matching, which is both
“find and replace"-like operations.
Text -A huge number of strings where patterns are often searched.
Conceptual Questions
1. What is a string?
2. “String is immutable in Python”. Justify.
3. Compare and contrast the pair of single quotes (‘…’), double quotes
(“…”), triple single quotes (‘‘‘…’’’), or triple double quotes (“““…“"”).
4. What is the primary advantage of immutable strings?
5. How can you access values in strings with a suitable example?
6. Give a list of string modules in summary.
7. How can you slice values in strings with a suitable example?
8. Justify the implications of slicing operations on an array by providing
examples.
9. How to get an array’s elements using the array’s index?
10. Differentiate the rindex() and index().
11. How are find() and rfind() different?
12. When might splitlines() be helpful?
13. What method would you use to verify that two strings are identical?
14. Why do we need Regular Expressions?
15. How can a string be traversed?
16. What is a documentation string?
17. Explain the different string formats available in Python with examples.
18. Which function, isdigit() or isnumeric(), is more appropriate to
determine whether or not the specified string with Unicode numbers
consists entirely of digits?
Try-Out Questions
What is the output for the following?
1. string1="Speaking is important."
string2="Hard work is more important."
string3= “Humility and hard work do wonders."
print(string1)
print(string2)
print(string3)
2. string4=“\“The wealth of the man of rectitude will not perish, but will
bring happiness also to his posterity.\"” # Escape sequence needed
print(string4)
3. string5 = “""city
metro"""
print(string5)
string5 = “""city
metro"""
print(string5)
4. fruit = “Strawberry"
baked_good = “Spinach Salad"
print(fruit + baked_good)
5. string=‘Welcome to the strings exercises of Python.’
print(string.find(‘exer’))
print(string.find(‘o’))
print(string.find(‘o’, 10, 20))
print(string.find(‘o’, 5, 10))
6. string=‘FAITH’
number = ‘1947’
i=0
x =‘‘
while i < len(string):
x = x + string[i] + number[len(number)-i-1]
i=i+1
print(x)
7. import re
longest_dictionary_word =
‘Pneumonoultramicroscopicsilicovolcanoconiosis’
print([(a.start(), a.end()) for a in list(re.finditer(‘is’,
longest_dictionary_word))])
8. string1 = “MOTIVATION"
string2 = “INSPIRATION"
mesh = “".join(i + j for i, j in zip(string1,string2))
print(“mesh:”, mesh)
9. string1 = “CONFIDENCE"
string2 = “FOCUS"
min_len = min(len(string1), len(string2))
mesh = [‘‘] * min_len * 2
mesh[::2] = string1[:min_len]
mesh[1::2] = string2[:min_len]
print(“mesh:”, ‘‘.join(mesh))
10. string1 = “Nurture"
string2 = “Mindfulness"
mesh = ‘‘.join(‘‘.join(item) for item in zip(string1, string2))
print(“mesh:”, mesh)
11. string1 = “Resilience"
string2 = “Adversity"
min_len = min(len(string1), len(string2))
mesh = [‘‘] * min_len * 2
mesh[::2] = string1[:min_len]
mesh[1::2] = string2[:min_len]
mesh += string1[min_len:] + string2[min_len:]
print(“mesh:”, ‘‘.join(mesh))
12. x = “A108"
y = x.isalnum()
print(y)
13. lesson_on_strings = “Angel"
for current_index in lesson_on_strings:
print(current_index,end=‘ ‘)
14. print(‘nostalgia,12’.isalnum())
15. print(‘0xyb’.isdigit())
16. print(‘1789’.isnumeric())
17. print(‘\t’.isspace())
18. Count the occurrences of a particular character in a string
string1 = ‘ out of sight, out of mind.’
print(‘recurrence frequency of o : ‘, string1.count(‘o’))
print(‘recurrence frequency of the : ‘, string1.count(‘of’))
19. def count_distinct_chars(string):
count = 0 #Intialise count
# examine each letter in the string, iteratively.
for ch in string:
# check for character present in the string
if string.find(ch) == string.rfind(ch):
count += 1
return count # Return the number of distinct characters
print(count_distinct_chars(“Distinct”)) # will return 4
print(count_distinct_chars(“hi there”)) # will return 7
Programming Exercises
1. Create your multiline string and show how to print it.
2. Create a program that accepts a string and displays each word’s length
and the length of the string.
3. Write a program that will count the number of lowercase and uppercase
character in a user-supplied string.
4. Create a program that will count the length of the given string without
relying on an in-built function.
Given string:
"Jack and Jill went up the hill. To fetch a pail of water."
5. Write a program to elicit a string from the user and then determine the
total number of characters included in a string.
6. Write a program to elicit a string from the user in lower or small case
letters and then capitalize the initial character of the string.
7. Write a program that obtains a user-supplied string. Use the isnumeric()
to determine whether or not it contains numbers.
8. Write a program that obtains a user-supplied string. Use the isdigits() to
determine whether or not it contains digits.
9. The longest word in the dictionary is “Pneumono
ultramicroscopicsilicovolcanoconiosis."
Write a program that takes the longest given word in the dictionary as an
input and outputs the count of only the distinct characters in it.
10. Given:
"string1=Pneumonoultramicroscopicsilicovolcanoconiosis."
Write a Python program that takes the given string and returns a new
string with all the vowels stripped out.
11. Write a Python for loop program that obtains a user-supplied string and
finds out if the first character is lowercase.
12. Write a Python for loop program that obtains a user-supplied string and
finds out if all the words in the string are in the title case.
13. Write a Python program that takes the given string and finds out if all the
words are in upper case.
Given:
str1 = “STRINGS"
str2 = “Strings"
14. Write a Python program that takes the given string and finds out if the
string includes alphanumeric characters.
Given:
str1 = (“Genius is 1 percent inspiration and 99 percent perspiration”)
15. Write a program to take a string as input and output a string with all the
words in the input string capitalized and lowercase intermittently.
Given:
string1 = “happiness multiplies as we divide it with others."
16. Write a program that, when given a string “anopheles”, will return a new
string containing the same characters but in reverse order.
17. Write a program that, when given a string, gives back the words
arranged in reverse order.
Given
string1 = “A tree starts with a seed."
18. Write a program that, when given two substrings, gives back a combined
string.
substr1 = “An ounce of practice “
substr2 = “ is worth a thousand words"
OceanofPDF.com
Programming is the art of telling another human being what one wants the
computer to do.
Donald Knuth
CHAPTER 11
Advanced Functions
Python supports the functional programming paradigm, where functions are
the fundamental blocks for program development.
Learning Objectives
The reader at the end of the chapter will be able to learn the following
topics.
Understand functions.
Creation of higher-order functions.
Know about the Lambda function.
Application of list comprehensions.
Know about iterations.
Understand how recursion works.
Know about the need for generators.
Design a decorator function.
11.1 Introduction to Functional Programming
Functional programming (FP) is a programming paradigm. A paradigm is a
framework that indicates ways of computational thinking required to solve
a given problem. A framework includes assumptions, computational
thinking, strategies, heuristics, and methodologies to solve a given problem.
Functional programming is a subset of declarative programming where
functions are used to implement program logic for getting solutions.
The core element of functional programming is a function rather than the
statements to solve a given problem. Functional programming starts with
splitting the problem into smaller subproblems. The subproblems would be
split into further subproblems. Finally, the focus is to map the subproblems
to a function. The focus here is to map the subproblems to a function. If the
split-up is improper, it will not lead to solutions in terms of functions. In
that case, the design should be altered to facilitate the solution of the given
problem in terms of functions. This is called functional abstraction. In other
words, functional programming considers functions as the fundamental unit
for program development. Functional programming includes two parts—
functional abstraction, where solutions are subproblems mapped to
functions, and functional application, where functions are applied to
parameters and values to get results.
The notion of “pure functions” is unique in functional programming. The
main requirement of functional programming is that the function should not
have side effects or functions with minimal side effects. A side effect is the
change of state of a variable or object when intentionally or unintentionally
called. A function or expression has a side effect, if it modifies a state
variable outside its environment. The side effect leaves an observable effect
other than its required behaviour of returning a value to the invoked
function call. The main goal of functional programming is to write
functions that do not have any side effects.
Also, functional programming does not maintain state information. There
are two important concepts: state and mutation. For example, the traffic
lights have three states: Green, Red, and Orange. These are called states.
The mutation is the change of state. In functional programming, strictly
speaking, the variables should not change – in other words, they should be
immutable.
Also, the function should produce the same result given the same inputs
making functions deterministic. As the states of the variables are not
maintained, the variables are immutable. This makes functional
programming more predictable.
Characteristics of Functional Programming
Functional programming characteristics are
Use only functions.
Combine functions to form first-class or higher-order functions. Higher-
order functions are used over loops and if statements.
Usage of recursion instead of iteration.
No state information or side effects.
The order of functional calls is not relevant.
Pure functions, where pure functions are defined as functions that have
no side effects.
Getting the same results for the given input under all conditions. This
property is called deterministic.
The value of the variable never changes as there are no assignment
operators in pure functional languages like Haskell. This property is
called immutability. Functional programming favours immutable objects.
Iterators over lists.
The advantages and disadvantages of functional programming are given
below in Table 11.1.
Table 11.1: Advantages and Disadvantages of Functional Programming
11.2 Functions as Objects
Functions are first-class citizens in functional programming. The
terminologies of first-class citizens and higher-order functions are
interchangeable. First-class citizens refer to the tight integration of
functions with the programming language, such as
A function can be stored in a variable.
Function can be passed as a parameter.
Functions can be used to manipulate collections.
Manipulates any data types using functions.
Calling a function with the same inputs should result in the same output. In
Mathematics, this is called idempotent property. In Python, this is done
using the concepts of non-mutable state and higher-order functions. Like
any object, the functions can be stored in a variable. The following example
illustrates this.
Example 1: Illustration of a function stored in a variable(Listing1.py).
The above code is stored as listing.py and executed in Interactive mode.
output of this code:
There can be alias on functions. An alias is a dummy name. The following
example shows the alias where both s and t refer to the same object.
Example 2: Illustration of an alias(listing2.py).
All programs in this chapter are executed in Interactive mode and the
results are presented as code and screenshot of the result.
output of this code:
Similarly, functions can be passed as parameters. The following code
explains how the functions can be passed as parameters to the function.
Example 3: Illustration of a function passed as a
parameter(listing3.py).
Output of this code:
Functions can be returned as return values. The following code illustrates
the usage of functions as return values.
Example 4: Illustration of a function returned as a
parameter(listing4.py).
output of this code:
Here, the function increment() accepts a single argument and returns it by
adding 2.
11.3 Lambda Functions
Lambda Function is a restricted, one-line function in Python. Lambda are
anonymous or name-less functions, as they have no name, unlike ordinary
functions.
Only one instance of these functions should be invoked at a time to perform
a simple task. The history of lambda calculus goes back to the 1930s when
Alonzo Church originally proposed Lambda Calculus to provide a
theoretical basis for computation. This concept is a forerunner before the
advent of algorithms. Lambda Calculus is a cross of functional abstraction
and application of the function. In a way, lambda calculus promotes
Usage of functions only.
No state or side effects.
The order of evaluation is irrelevant.
Lambda functions helps to create a physical expression within other
functions by taking one or more arguments. The greatest usefulness of this
function is to allow it to embed as part of a larger function and it takes less
time.
Syntax of Lambda Function in Python
Python’s anonymous functions must have three components to work: viz.,
lambda as a keyword, parameters (or bound variables), and function body.
lambda: to create a lambda function.
parameters: a lambda function might have a single or several parameters.
Only commas separated arguments following the lambda keyword are
required.
expression: this is the main part of the function. This is where you
specify the operation performed by the function. A lambda function can
have only one expression.
Example 5: Write a Lambda function to return a cube of a number.
Lambda function that accepts only a single argument. This can be
contrasted with Example 3, where Lambda functions are proven to be
compact and effective.
Example 6: Write a Lambda function to return the function (x/30*100
for x).
Lambda functions can have multiple arguments also as shown in the
example 7 to implement an expression 4(a+b) by getting the arguments a
and b.
Example 7: Write a Lambda function to return an expression 4(a+b).
Lambda function that accepts two arguments.
Lambda functions take multiple arguments. It must be noted that the
arguments should be given in the same order as they are defined in the
Lambda function.
Example 8: Lambda function to illustrate multiple arguments for the
expression delta = sqrt(b2–4ac).
If the order is changed, the results will also change.
One can embed the lambda function inside another function. The function
can take a value and return a lambda function.
Example 9: To illustrate a lambda function to embed a function inside
another function (Listing5.py).
Output of this code:
11.4 List Comprehensions
Sequences are common in Python. As discussed in Chapter 3, sequences are
a set of numbers with some pattern or order. Examples of sequences are the
Fibonacci sequence, the sequence of odd numbers, or even numbers.
Sequences are used to model strings, lists, and streams of data. For
example, a string is a sequence of characters. A file is a sequence of lines. A
list is a sequence of values that are separated by a comma.
A sequence is of two types: static and dynamic sequences. Static sequences
are like strings and lists. A sequence can be generated dynamically also. The
term stream is often used to refer dynamically generated sequence of
values.
There are three key Python tools used to process static sequences and
streams. These elegant tools are used to process sequences and streams. The
tools are listed below:
1. List comprehensions
2. Iterators
3. Generators
The need for these Python functionalities arises because of Big Data. Data
management is crucial to proactive data analysis in big data. The steady
influx of data from many sources presents problems for data analysts in the
Big Data age. Doing nothing with the inundated data is fruitless. Large data
storage is costly and time-consuming. Iterators prevent memory exhaustion.
While processing data, iterators reduce the computer’s memory needs,
making it more efficient.
List comprehension is a straightforward, efficient, compact, and elegant
way to construct lists in Python from a string or another list. List
comprehension is a technique of creating a list using existing values. It is an
extremely concise way of building a new list by acting on each item in the
prevailing one. List comprehension is noticeably faster than parsing a list
using them for a loop. Additionally, list comprehension enables you to build
lists while writing cleaner, more readable code. The syntax of list
comprehension is given as follows.
Here, the expression can be any valid Python expression, and iterable can
be any object like
Lists
Strings
Tuple
Dictionary
New lists may be easily created from existing lists due to Python’s list
comprehension feature When compared to traditional list-creation methods
and loops, this method is faster and more effective. List comprehensions are
readable and can easily be communicated to the programmer. Also, list
comprehensions are the short-hand type that makes code simple and tidy.
List comprehensions provide immense documentation value as list
comprehensions are self-documentary.
The following examples illustrate the use of list comprehension.
Example 10: Generate a list of numbers from 1 to 20. The numbers can
be generated traditionally using a range function.
It can be observed that it is a one-line for loop that produces the list which
consists of numbers from 0 to 18.
Example 11: Example to illustrate list comprehension(Listing6.py).
Input code using list comprehension
Output of this code:
Multiple Iteration
The list can be built based on two values. This is illustrated in the following
program.
Example 12: Generate a list of numbers from 1 to 100. The numbers
can be generated using a range function.
Here, j is the inner index, and i is the outer index. This can be extended for
more iterators. Once can include conditions as part of list comprehension.
The following example illustrates this by including a condition to generate
only odd numbers. It can be observed that the condition for checking odd
numbers are added and conditions are one-liners.
Conditions on Iterations
Example 13: Generate a list of odd numbers from 1 to 20 using
conditions.
This generates a set of odd numbers in the range [0-50].
Some examples are given below:
Example 14: Generate a list of even numbers from 1 to 20. The
numbers can be generated using a range function and a condition for
generating even numbers.
Using List Comprehension with Strings
The ensuing example is a list of strings.
Example 15: Example to illustrate list comprehension using strings in
Python (Listing7.py).
Traditional way of computing
The above input code can be rewritten elegantly, like the one given below,
giving the same output (Listing7.py).
Code using list comprehension
Output of this code:
Nested List Comprehension
Nested list comprehensions are sometimes called nested list
comprehensions or multi-dimensional list comprehensions. A list
comprehension nestled inside another list comprehension is analogous to
nested for loops. Nested loops are implemented in the following way.
Example 16: Example to illustrate nested list comprehension in
Python(Listing8.py).
Output of the code:
Set Comprehensions
Python supports set comprehensions similar to list comprehension. The
difference between set comprehension and list comprehension is that set
comprehension uses curly braces instead of square brackets and sets are
collections without duplicates and its results may not have any meaningful
order.
Example 17: Illustration of a set comprehension.
Dictionary Comprehensions
Dictionary comprehensions also use curly braces. It must be noted that
dictionary comprehension does not directly operate on the dictionaries but
with the items() coupled with tuples to manipulate values and keys.
Dictionary inversion is one of the useful applications where the items can
be obtained in the opposite direction.
Also, dictionary comprehension can be used to swap the keys and values in
a dictionary.
List Comprehension in Tuples
Sequences are represented by parenthesis () and commas in a tuple. A tuple
is an ordered list. Tuples are immutable, meaning the tuples cannot be
altered after assigning values. Tuples can store values, such as integers, text,
floats, lists, etc. Python does not support tuple comprehension natively;
however, there are workarounds. No individual elements of a tuple can be
changed, but their values can be changed.
Among the several ways to create a list of tuples, one method is by using
the list comprehension and tuple() method.
Example 18: Example to illustrate tuple comprehension for generating
numbers that are divisible by 5 (Listing9.py).
Output of the code:
11.5 List Comprehension with Sequence Processing
One can perform many computations on sequences. The ability to pass a
parameter and receive it makes such functions higher-order functions. A
higher-order function accepts another function as a parameter. Some of the
helpful higher-order functions are
Map
Filter
Reduce
All these functions accept a function as a parameter and apply the function
to every element of the collections, such as the list. Some of these higher-
order functions are discussed below.
11.5.1 Map Function
map() is a built-in function in Python. It works as a function on all the items
of a supplied iterable (list, tuple, or set). The elements of a Python map
object are looped through since it is an iterator. Using the factory methods,
a map object can be turned into a sequential object, such as a list or tuple.
The map is a function that is used to apply every element of an iterable
object. The function map() takes two arguments.
1. Any Python functions
2. Iterable object
In Python, iterables are lists, tuples, dictionaries, or sets. The function
transforms the elements of the iterable objects to give the resultant.
The syntax of the map() is given as follows.
Parameters have two functions: a required function to pass to map, which
will be applied to all iterator items, and an Iterator, an entity that can be
used to perform a series of repetitive operations repeatedly. One or more of
several different types can be used. The map() function accepts any number
of iterator objects.
To use lambda expression in a computer program, Python permits the
usage of lambda functions in any standard function. This is a handy feature
with high-order functions, which take another function as input.
The following example illustrates the function that accepts two lists, one
currency, and another exchange rate. The aim is to combine two lists to
create a resultant result as shown in Fig. 11.1.
Fig. 11.1: Illustration of Map Function
Example 19: To illustrate the change of case using the Map
function(Listing10.py).
Output of the code:
Example 20: To illustrate the change of case using the map function.
In the above map() syntax, the function str. upper is used as the function,
and the list of iterables all names are used as the list of iterables. Using the
map function, there is no necessity to call “str.upper” on each item in the
given list.
Output of the code:
The following is another example of a map function, where the length of
the words is given in a list.
Example 21: Find the length of the words using the map
function(Listing12.py).
Output of the code:
One can combine map and Lambda functions to create additional functions,
such as combinations of lists. The following example illustrates the function
that accepts two lists, one currency, and another exchange rate. The aim is
to combine two lists to create a resultant result.
11.5.2 Filter Function
The function filter() takes two inputs.
1. Iterable object
2. A condition
It then applies the condition to the elements of the object and returns only
those elements that evaluate as true for the condition.
The first argument in a filter() is that a function is required. The second
parameter is an iterable, say, a list.filter() applies the provided function to
each iterable element. It returns a new iterable with all True components.
Filter() is a function that removes an iterable from a list based on a function.
Example 22: To illustrate the change of case using the filter
function(Listing13.py).
Output of the code:
The following is another example of a filter function to generate even
numbers.
Example 23: To illustrate the filter function to generate even
numbers(Listing14.py).
Output of the code:
Similarly, one can combine the Lambda function and filter() function. This
is illustrated in Fig. 11.2. It can be seen that the filter function applies a
Boolean function even() and finds out whether it is even or not by
evaluating it. The evaluation of a Boolean function results in true or false.
Finally, all the elements that yield true are consolidated, and elements that
result in false are discarded.
Fig. 11.2: Illustration of a Filter Function
11.5.3 reduce Function
reduce function reduces a set of values to a single number. For example,
given a set of numbers, reduce function can reduce to a sum.
reduce function has two arguments.
1. A function
2. A sequence
reduce() apply a function over the series of values and returns a single
value.
Example 24: Illustrate reduce function to find sum(Listing15.py).
To illustrate the reduce function.
Output of the code:
The steps of the reduce function are shown in Fig. 11.3.
Fig. 11.3: Example of Reduced Function
One can also perform comparisons like max or min, as this yield a single
value.
Another illustration of reduce function is given below.
Example 25: To illustrate finding minimum using the reduce
function(Listing16.py).
Output of the code:
The result is shown in Fig. 11.4.
Fig. 11.4: Example of Reduced Function
11.6 Iterators
Normally, for-loops are used for iteration with objects, such as lists, sets,
etc. If used, by default, it would iterate over the whole sequence without
user significant control. The control of this iteration is brought by iterators.
Any Python object that can generate a sequence and can be used as part of a
for-statement is called an iterable object. Examples of iterable objects are
lists, tables, sets, dictionaries, and strings. In Python, all objects are iterable.
Anything that can be inside the loop is called an iterator. When dealing with
Python, an iterator is a collection of iterable objects. An iterator is an
iterable object with a state that keeps its location throughout the iteration.
Iterators usually return a new object when iteration is applied.
The iterators do provide an iterator protocol. It is a protocol that has two
methods, __iter__ (or iter()) and __next__ (or next()). The methods that
start with a double underscore are called dunder methods. When using an
iterator, one must use iterator protocols, such as iter() and next(). Iterators
are stateful and traverse the sequence only once. After that, it is exhausted,
and the rise StopIteration is an exception.
An iterator is iterable, but not the other way around. Strings are iterable, but
not iterators. With the help of the iter function, an iterable may be converted
into an iterator().
The syntax of iter( ) is given as:
Here, there are two parameters namely, an iterable object such as sets,
tuples, etc., and an optional sentinel, which is a unique value that signifies
the conclusion of a series.
Example 26: Example to illustrate an iterator.
The iter object gives access to the next object if it is legally valid. This can
be checked as
This process is continued till all elements in the iterations are exhausted.
The StopIteration exception is thrown when next() is used on an exhausted
iterator.
The following is another example of an iterator function.
Example 27: Construct an iterator to read marks in
physics(Listing17.py).
Output of the code:
The following is another example of an iterator function in strings.
Example 28: To illustrate iterators in strings(Listing18.py).
Python relies heavily on iteration to perform complex computations quickly
and efficiently. Repeating a process can be referred to as “iteration” in
simple words. Iteration is the repeating of a piece of code. Using loops like
the while and for-loops, it is possible to do iterations.
The next function is used to get only one item at a time while traversing
elements in a string or string iterator. When all the elements in the string are
exhausted, it shows the error message, as seen in the output screenshot.
11.7 Generators
Generators are special functions that help to define user-defined iterators.
The generator is a construct that generates a dynamic sequence and it
generates an iterator. A generator is a function that emits one value at a time
when called upon. In short, the result of a generator is eventually a
sequence. The generated object is remembered by the generator. This is
shown in Fig. 11.5.
Fig. 11.5: The scenario of the Generator
The difference between generator and list comprehension is that
1. There is no memory cost involved in the generator.
2. These generators do not compute until they are needed. This is called
lazy evaluation.
Lazy Evaluation
Lazy evaluation is a concept where the computation is delayed till it is
requested. Only on request, the computation is done. Till then, it is only a
described condition. Examples of lazy computation are functions like
map(). Using this concept is potentially endless. A stream of data can be
generated. But the memory kept is low as computational cost involves only
used elements.
Generators are a powerful feature of Python where generators can be used
to generate values infinitely. Therefore, the generator can be used to model
an infinite data sequence with no end. These are stream data. The main
principle involved is a lazy evaluation where results are obtained only when
requested. Therefore, generators are useful to generate and process dynamic
sequences, such as sensor data or mathematical sequences.
For example, a generator can generate values infinitely. Generators, instead
of using return statements, use yield statements. The generator yields a
value using the yield- statement. The syntax of the yield statement is
The following example illustrates the use of a yield statement.
Example 29: To illustrate the generator function(Listing19.py).
The generator can be used as follows:
The above created generator can be used without next() function also as
follows:
Here, generators are iterators to use successive values from the sequence.
StopIteration is an exception when there are no values left out. This was
observed in the last step of the above Python script.
In the above Python segment, the statement x=generate_element(). binds
the generator to the variable x. Then every next() function extracts the value
of the sequence. When all the elements are exhausted, Python raises an
exception called StopIteration.
This statement uses yield to get the next statement of the sequence to print
it. The following example generates odd numbers in the range of 0 to 100.
Example 30: To illustrate the odd number generator
function(listing20.py).
Output of the code:
In other words, the above generator function can be used to simulate the
built-in range function of Python.
Example 31: To illustrate simulation of the range
function(Listing21.py).
Output of the code:
In the same way, generators can generate sequences like Fibonacci or prime
numbers.
Python identifies any function that has a yield statement as a generator
function. The following is another example of the Fibonacci generator. The
code is given below:
Example 32: Example of a Fibonacci generator(listing22.py).
The generator can be interactively invoked in the IDLE as shown below:
The above command generates the first 10 Fibonacci numbers. Therefore, a
generator returns an iterator, and when the iterator is invoked, the function
body is executed. When it reaches the yield statement, the iterator delivers a
value of the expression. Then the execution of the function is suspended
(lazy evaluation!). It is resumed after the next call, and this process is
continued.
11.7.1 Generator Expressions
Generator expressions are the product of list comprehensions and generator
functions. It produces a generator as output lazily as it uses the concept of
lazy evaluation. Generator expressions are used to create generators on the
fly without needing any special methods.
A generator function returns an iterable object. It is done by applying the
operation to every value function of the sequence. The syntax is similar to a
list comprehension, except that generator expressions use parenthesis
instead of square brackets. The output of a generator expression is a
generator.
The syntax of generator expressions is given below.
Example 33: To illustrate the generator expression.
An example of generator expressions is given below.
Once run exhaustively, next time, if executed, generator expressions would
show null, and its yield results in no more new items. Again one has to run
the generator expressions to regenerate the sequence.
An example of a simple count is given as follows.
Example 34: To illustrate the infinite streams of a
counter(Listing23.py).
images
The above generator can be invoked as shown below:
The above sequence would go on infinitely.
11.7.2 Itertools, Special Iterators, and Generators
Itertools is a module. It provides many useful functions and generators with
a wealth of built-in functions, such as enum()and sum(). Itertools take
Python to a new stage by providing many functions for useful advanced
iterators.
In Python, the enumerate() method keeps track of the elements included in a
list, tuple, set, or other iterable data structure (known as iterable). It
abridges the process of keeping track of the contents of an iterable object.
Instead of using a reading list, execute print(enumerate()) is used to obtain
an alphanumeric memory reference address. This is how enumerate()
optimises memory use by removing superfluous full-fledged lists.
Enumerate allows to track the item along with the index. The code segment
is given below.
Example 35: To illustrate enumerate function(listing24.py).
Output of the code:
The following code is another example of enumerate function.
Combining Streams Using Zip Function
Zip allows us to iterate multiple lists simultaneously by zipping the list
together as iterations. The example code is given below.
Example 36: To illustrate the zip function(Listing25.py).
Output of the code:
Transforming Streams with Map
The following is another example of stream processing using the map
function.
Example 37: To illustrate the stream and map function(Listing26.py).
map iterator takes a function and applies it to the values of the given list.
Output of the code:
Transforming Streams with Filter
The following is another example of stream processing using the filter
function.
Example 38: To illustrate the stream and filter function(listing27.py).
Output of the code:
Processing Sequences with Zip/Unzip Functions
The following is another example of stream processing using functional
arguments.
Example 39: To illustrate function arguments(Listing28.py).
Output of the code:
This can be unzipped as follows
Output of the code:
Many specialised iterators are given below.
Pipeline operations use multiple generators. The output of multiple
generators can be pipelined for a specific goal.
Permutations and combinations are other special iterators. The following
example illustrates this:
Example 40: To illustrate the permutation function(Listing29.py).
Output of the code:
11.8 Recursion
Functional programming languages prefer recursion over iteration. Loops
maintain the states and involve mutable variables. Therefore, functional
programming supports recursion over iteration. More than an iteration,
recursion is a useful functional programming technique that solves the
problem using a technique called problem reduction. A problem-reduction
technique takes a problem and reduces its instance by one but retains the
simpler version of the same problem. This procedure is repeated till the
problem becomes so simple that it can be solved in a single go.
A function is called recursive because it calls by itself. As discussed in
Chapter 2, a recursive program is one where recursive functions are used as
an alternative for iteration.
Thus, recursive algorithms have two parts.
1. First part of recursive program is one or more base cases. Base cases
provide trivial solutions and serve as termination conditions. The base
problem is solved directly.
2. The second part is an inductive step. This step reduces the problem to a
smaller version. This is called reducing step.
A good example of recursion is the computation of a factorial. The factorial
of a number (n) is a product of all integers between n and 1. For example,
the factorial of 4! is
The Python code segment for finding the factorial of a number using
recursion is given below.
Example 41: Write a recursive program to find the factorial of a
number(Listing30.py).
Output of the code:
Another good example of recursion is the computation of the power
function. A recursive power function, given base r and exponent n, is given
as follows.
The above function can be written recursively as
Here, r1 is the base condition or the termination condition. The recursive
program can be written as
Example 42: Write a recursive program to find the power
function(Listing31.py).
Output of the code:
The recursive routine can be tested as a print(power(6,5).
Another good example of recursion is checking whether a string is a
palindrome or not. A palindrome is a word that, when reversed, gives the
same word. Excellent examples of palindromes are the words like abba and
Malayalam. It can be observed that when the word is reversed, it gives the
same word. A recursive program can be designed by removing the first and
last letters of the word, if they are the same. The process can be repeated till
a base condition of a single character.
The program for checking palindrome using recursion is given below.
Example 43: Write a recursive program to check whether a word is a
palindrome or not (Listing32.py).
Output of the code:
Recursion is not effective as Python looping because of the overhead
involved. It takes more time and more memory to make a function call. In
Python, the function calls are limited to a depth of 1000. Therefore, the
recursion is limited to processing a recursion tree of two power 1000
elements.
In Python, the recursive call is replaced by a loop, if it is a tail recursion.
Tail recursion is one where the last statement of the function is a function
call that is often associated with the condition beforehand for termination or
function calls.
Memoization
Sometimes, the recursion tree is overlapping and hence the values will be
computed again and again. For example, the factorial of 4! is 4 * 3 * 2 * 1
and factorial of 3! is 3 * 2 * 1. Therefore, it is better to store the
intermediate values in a table so that one does not need to compute
everything from scratch. This technique of storing intermediate results is
called memoization. Memoization is a corrupted word of memorization.
This means the results of the functional calls are cached in the form of a
table so that the computations are reused rather than recomputed from
scratch.
One can give another example for memoization in the Fibonacci sequence
also. One can recollect that the Fibonacci sequence is
So, Fibonacci of F(4) can be calculated and shown in the following Fig.
11.6.
Fig. 11.6: Fibonacci Tree for Fib(4)
It can be observed that Fib(4) computation itself shows multiple redundant
calculations. It is shown as a square in the above Fig. 11.6. There is no point
in recomputation. Instead of precompute, one can use memoization
techniques, where the intermediate computations can be stored in a table
and can be reused. This reduces the running time and greatly improves the
performance of the program. In fact, for large numbers, solving by
conventional means is a difficult problem (NP-Complete problem).
Memoization is provided by a built-in decorator called lru_cache provided
by the module called functools. Decorators are discussed in Section 11.9 of
this chapter. Functools is a standard Python library. The word lru comes
from “Least recently used”. This means that only the recently used items
are available in the cache, and all other older items would be removed.
The syntax of this utility is shown below.
Example 44: To illustrate the memoization technique(listing33.py).
Output of the code:
11.9 Closed Functions and Function Annotators or Decorators
One can add a function into another function. This function is known as an
inner function or embedded function. The other function is known as the
outer function. The advantage of the inner function is that it can have access
to the variables that are defined and available in the outer function
1. The inner functions cannot be accessed by the outside program.
2. The outer function can return the reference of the inner function. Using
the reference, one can execute the inner function.
3. An inner function can serve as a helper function that can help another
function.
The inner function hides the functionality from the outside world. The inner
function cannot be accessed directly. This principle is called encapsulation.
The word closure comes from Mathematics where it is used to indicate
completeness without any loose ends and can be decided. Hence, this word
refers to a complete environment.
Example 45: To illustrate free and bound variables.
Consider the following Lambda expression.
Here, x and y are parameters of the Lambda function. The variable n is not
defined as part of the lambda function. Python takes the definition of n
outside the lambda function and completes the computation. The variable
‘n’ is called the free variable. This combination of a function and the
definition of free variables defined in the contained scope is called function
closure. It must be observed that the tree variables should not change as
they may affect the result. A closure is an inner function and is dynamically
created and returned by another function. The return function can access the
variables or namespaces where it is created.
Function Annotators
PEP 3107 introduced the concept of functional annotators. These are meta-
information that given for the benefit of users especially third-party Python
packages. These annotations are executed only in compile time and not used
for run-time. The following is an example of functional annotator.
Example 46: To illustrate functional annotations(Listing34.py).
Decorator Function
The concept of a decorator is based on the decorator’s design pattern.
Decorators are higher-order function that allows adding new functionality
to the existing function while retaining the original behaviour of the
function. The scenario of a decorator is as follows.
1. A callable entity is defined. A callable entity can be a function, method,
or class.
2. The callable entity passes an argument, as a function. This is called the
decorated function
3. The passed function is decorated by a function by applying a decorator.
This is called the decorator function. The decoration is the additional
behaviour that is added by the decorator function
In decorators, a function can be passed to a new function, and the new
function adds the new functionalities without changing the behaviour of the
original function. For example, if fun() is the function to be decorated, then
the scenario will be as follows.
Then the syntax of the decorator is given below.
The uses of the decorator function are logging, transaction management,
encryption, and data validation.
Let us try to create a wrapper function that adds a borderline to a message.
The message that needs to be sent is ‘this is a sentence”, and the first border
is “************”, and the footer border is “-----------------”. First,
function fun() prints the message “this is the sentence”. Then the decorator
function greet takes this function as an argument and adds the border. This
is given below.
Example 47: To illustrate the simulation of the decorator
function(Listing35.py).
Output of the code:
Anotherway to indicate the decorator is to use the symbol @ as shown
below in Listing36.py.
Example 48: Illustrate the use of @ symbol is decorator(Listing 36.py).
Output of the code:
The function is now sent as an argument to the decorator function, where
the margins are added and sent back to the calling function.
Summary
1. Functions are the fundamental blocks of functional programming.
2. Functions can be stored as a variable, passed, or returned as an argument.
3. Lambda functions are anonymous functions that allow us to create a
physical expression within other functions.
4. List comprehension is a straightforward, efficient, and compact way to
construct lists.
5. Recursion allow us to create iterations in functional programming.
6. Iterator protocols are iter() and next().
7. Iterator uses the concept of lazy evaluation.
8. Map is a function that is used to apply to every element of an iterable
object.
9. Filter takes a function and applies it to every element of the object and
returns only if it evaluates to true.
10. Reduce function reduces a set of values to a style number.
11. Enumerate allows tracking an item with an index.
12. Zip allows iterating multiple lists by zipping the list together.
13. Recursion is an functional programming technique of solving a problem
using the problem-reduction technique.
14. Memoization is a technique of storing intermediate values in a table for
reuse.
15. Generator expression is the same as list comprehensions that use
parenthesis.
16. Generator functions use yield statement.
17. A function that is defined within another function is called an inner
function.
18. Function decorator is a wrapper function that extends or modifies a
function without changing the original function.
Glossary
Functional programming: A paradigm where functions are fundamental
blocks of program development.
First-class citizens: The property of function exactly as an object makes
it first-class citizens. These are also known as higher-order functions.
Lambda function: one-line anonymous function.
Anonymous function: Functions that are not tied to any name.
List comprehension: the ability to create an expression from existing
values.
Nested list comprehension: a list comprehension nested inside another
list.
Iterator: an iterator is a collection of iterable objects.
Iterator protocol: iter() and next().
Iterable: if an object can produce an iterator, it is called iterable.
map: function that is used to apply to every element of a list.
filter: function that is applied to every element and returns only those
elements that evaluate true for the condition.
reduce: function that can reduce a set of values to a single number.
enumerate: function that allows us to track the item along with the index
zip: function that allows iterating multiple lists by combining the lists.
Recursion: a technique of solving problem using the principle of
problem reduction.
Tail recursion: a function call that is often associated with the condition
beforehand for termination or function calls.
Memoization: a technique of storing the intermediate values in a table
for the purpose of reuse.
Generator: function that can generate values.
Generator expression: a function that is the same as a list
comprehension.
Closed function: a function that is defined inside another function.
Function decorator: a function that extends or modifies a function.
Conceptual Questions
1. What is the functional programming paradigm? List out its advantages
and disadvantages.
2. Justify the statement: “functions are first-class citizens” in functional
programming.
3. Why are lambda functions required?
4. Why is list comprehension required?
5. What is the principle of lazy evaluation?
6. What is nested list comprehension?
7. Why is an iterator required?
8. Distinguish between iterator and iterable object.
9. Explain the role of the map, filter, and reduce functions.
10. List out the specialised iterators.
11. What is tail recursion?
12. How recursion is useful in solving problems?
13. What is a memoization technique?
14. Distinguish between generator expressions and generator functions.
Try-Out Questions
1. Show the output of reduce function for the following list [ 1 3 5 6 8] for
the expression x + y.
2. Show the output of the map function for the following list [ 1 3 5 7 9 10
8] for the expression x * y.
3. Show the output of reduce function for the following list [ 1 3 5 7 9 10 8]
for the expression x %2 ==0.
Programming Examples
1. Write a lambda function to find the result for the following expressions.
1. x4
2. 4x + 8
3. x3 + x2
4. x2 + y2
5. x2 + y2 + 18
2. Generate odd numbers in the range 0 – 100 using list comprehension.
3. Generate prime numbers in the range 0 – 50 using list comprehension.
4. Use iterators for printing a list.
Score = [ 80 90 87 76 55 44]
5. Write a map function to convert the following list of string to lowercase
letters
[Hello, India, Tamil, Malayalam, Delhi].
6. Write a map function to sort these words based on the length of the word
[Hello, India, Tamil, Malayalam, Delhi].
7. Write a reduce function for the list [1 2 6 8 10 12 14] for the expression
x + y.
8. Generate permutations in the range (0, 50).
9. Write a recursive program to find the GCD of two integer numbers.
10. Write a recursive program to generate Lucas’s number.
11. Write a generator for prime numbers.
12. Write a decorator function to add a timestamp for a print statement.
OceanofPDF.com
Another trick in software is to avoid rewriting the software by using a piece
that’s already been written, so called component approach which the latest
term for this in the most advanced form is what’s called Object Oriented
Programming.
Bill Gates
CHAPTER 12
Object-Oriented Programming (OOP)
This chapter aims to provide insight into how to work object oriented
programming (OOP) in Python.
Learning Objectives
At the end of the chapter, the reader will be able to:
Know the basics of building blocks of Object Oriented Programming.
Know to define a class in Python.
Create objects for a class.
Learn how to define and access attributes and methods in and outside of
a class.
Know the different scopes of attributes in Object Oriented Programming
and Python.
Understand different kinds of methods and their usage in problem-
solving.
12.1 Introduction to Object Oriented Programming (OOP)
Object Oriented Programming (OOP) is a programming paradigm that
involves designing the solution using objects where attributes and
behaviour are defined. The OOP approach to programming is well-suited
for large, complex programs and actively updated or maintained. The OOP
is used in many applications, such as mobile application development,
neural networking and parallel programming.
Object Oriented Programming facilitates collaborative software
development. Additional benefits of OOP include code reusability,
scalability, and efficiency. In OOP, the programmers should design the
solution in the thought process of interaction between the objects and their
relations.
The basic building blocks of OOP are
Class
Objects
Attributes
Methods
1. Class
Class is defined as the blueprint for the objects that hold definitions for
the attributes (also called data members) and methods. In Python, the
class defines a new type. Defining a class with its attributes and methods
does not make the interpreter allocate the memory.
2. Objects
An object is an instance of the class. Each object possesses states and
behaviours. The state represents the variables, and the class methods
define an object's behaviour. Once the object is created for a class,
memory space for the attributes and methods is allocated. Any number of
objects can be created for a class.
3. Attributes
Attributes are the variables declared inside the class and refer to the
object's state. Class attributes in combination with an object differentiate
the object from others. Each class must define the attributes that will
store the state of each object instantiated from that class. The word "class
variables" and "class attributes" are often used interchangeably.
4. Methods
Methods handle the data and behavior of an object. Every object
instantiated from a class has the methods as defined by the class. In some
cases, objects of the same class could have additional methods apart
from those defined in the class. In such case, the objects of the same
class possess different set of methods. Methods of an object could use
the objects of the different class by passing the objects as parameters.
The internal behaviours are defined using access modifiers discussed
later in this chapter. The concept of building blocks of OOP can be
visualised in Fig. 12.1.
Fig. 12.1: Building Blocks of OOP
The solution for a problem is derived using the building blocks of OOP.
This chapter discusses how to create classes and objects with attributes and
methods in Python. This chapter also looks at how to solve a program using
OOP.
Pillars of OOP
An OOP possesses several additional design principles that help to write a
clean object oriented code. The four pillars of OOP are discussed below.
1. Inheritance
Inheritance is the process of deriving a class from another class or other
classes to possess the attributes and methods of the deriving class. While
designing an OOP, one should acknowledge the commonality between
various classes. The class from which another class derived is called the
parent class or superclass. The derived class is called the subclass.
Superclass contains all the common attributes and methods between
itself and subclass(es).
2. Abstraction
Abstraction identifies only the required attributes and methods and
ignores the irrelevant ones. Abstraction is the process of detailed hiding
which is the actual implementation of data hiding. An abstracted class
can not be accessed unless it is inherited.
3. Encapsulation
Encapsulation is the process of binding the data attributes and methods
associated with the class and the object into a single entity. The
encapsulation leads to restricting access to data and methods with a class
from the outside interventions and it is called as data hiding.
4. Polymorphism
The word polymorphism means "many shapes". In OOP, polymorphism
allows using the operators and functions in different forms. Based on the
characteristics of the availability of data, functions and operators work
distinctly. Specific functionality is chosen based on the characteristics.
Additional Characteristics of OOP
1. Dynamic binding: Dynamic binding is a mechanism where the method
to invoke is determined at runtime and not at the compile time of the
program. The instance methods of the objects execute dynamic binding.
2. Modularity for easier troubleshooting: Identifying the location of
problem in the block of code is made easier because of the principle of
modularity.
3. Reusability of code: Using the concept of abstraction and inheritance in
OOP, certain codes can be reused effectively.
4. Flexibility: The methods in OOP objects can be used for many purposes
using polymorphism.
5. Effective problem solving: OOP takes a huge problem and breaks it
down into small and solvable mini-problems. The bottom-up approach is
an additional advantage for OOP.
6. Security of the data and functionalities: encapsulation enables access
restriction for the attributes and methods for the unprioritised entities.
12.2 Classes and Objects
As seen in the previous section, a class is a user defined blueprint for the
objects and holds attributes and methods. An object is called an instance of
a class. In this section, how to create a class and objects for the class are
discussed briefly.
Defining a Class
In Python, creating a class is regarded as creating a new data type where its
object belongs to this data type. Defining a class does not mean allocating
the memory space needed for the execution of the program. In Python, the
class is defined using the keyword "class". The syntax for defining a class
in Python is as follows.
In the above syntax, the class definition starts with the keyword class and is
followed by the user defined ClassName and colon (':') operator. The
definitions inside the class are given with appropriate indentation. The
uniformity of the indentation should be maintained for all the lines
belonging to the class.
For example, consider a class should be created for a school. Then it can be
executed using the following code in the interpreter.
The name of the class is "School" and is created using the statement "class
School:". Then the interpreter waits for the next input with "…" symbol,
where the programmer continues with the class definition. The class
definition involves method definition and attribute declarations, which are
discussed in the upcoming sections.
In some cases, the class definitions might not be included when defining a
class, and the class definition may be included in the later part of the
program. To handle such cases, the keyword "pass" is used inside the class
definition. Once the interpreter reads the keyword "pass", the control is
given to the next block of code. The syntax for using the keyword "pass" is
shown below.
In the above syntax, the pass is given at once the class is declared. The
keyword pass does not need to be given after the class declaration. There
could be other program statements in between the class declaration and the
pass statement.
Example 1: Illustrating the class creation by creating a class named
Biological Family without definition.
In the code mentioned above, a class with "BiologicalNames" is declared,
but instead of a functional block of the class, the "pass" keyword is used.
This allows the user to define a class without a proper definition. Now, it is
time to look into handling the data by creating objects for the class holds.
Object Creation
An object is an instance of a class that possesses a state, which holds values
stored in attributes and methods in the instance of the class. Each class
instance is associated with the attributes that maintain the object's state.
Meanwhile creating an object, the interpreter allocates the space for the
attributes and methods that are associated with the object. In Python, the
objects that are instances of a class belong to its data type.
There are two types of object creation in Python: without parameters and
with parameters. When an object is instantiated for a class, the constructor
is invoked. The declaration for the object without the parameters does not
demand a user defined constructor. Still, the user can define a constructor
for objects without parameters. For creating an object with parameters, a
user defined constructor with suitable arguments should be defined for the
class. The syntax for creating an object for a defined class without
parameters in Python is as follows.
In the above syntax, the object is created using the assignment operator
where the left-hand side is occupied by the name of the object and the right-
hand side is occupied by the class name followed by parentheses "()". There
are no arguments within the parentheses.
Example 2: Illustrating object creation for a class with pass statement.
As shown in Example 1, the class BiologicalFamily is created as follows.
An object named "cat" is created for the class BiologicalFamily.
Since no parameters are needed, the object cat is created without arguments
for the class BiologicalFamily. As discussed earlier, the data type of an
object is the same as the class from which it is instanced. To check the data
type of the object cat, the type() function is used as follows.
The above execution of the code shows that the object cat belongs to the
data type of the class BiologicalFamily.
Example 3: Illustrating object creation for the class School that holds
details about the school.
To Create a class School without class definition, the pass keyword is
shown below.
The above code creates the class School using the pass statement. Now, let's
see how to create an object without any arguments, as the situation does not
demand now.
The above code creates an object with the name xyz, which possesses its
state (Values in attributes) and methods. Handling the attributes and
methods of a class is discussed in the upcoming section of the chapter.
The object creation with arguments provides arguments to the constructor
of the class, a method in a class, and it is invoked at once after an object is
created. The syntax for the parameterised object creation is shown below.
The above syntax creates the object with arguments inside the parentheses.
This argument is passed as parameters for the constructor of the class. The
concept of the constructor is explained in the upcoming sections of this
chapter.
12.3 Class Attributes
Class attributes are the variables defined inside a class, and all of its objects
share the defined values for the objects inside the class. The values stored in
the attributes are defined as the object's state. The word "state" means
merely the status of the attributes in an object. There is no concept of
tracking the states and using transitions of the object in the OOP paradigm.
Class attributes are usually defined inside the class right after the first
statement of the class declaration. Defining an attribute is as simple as a
variable declaration (as seen in Chapter 3). The syntax for declaring an
attribute inside the class statement is as follows.
In the above syntax, it is assumed that it is placed inside the class definition.
After defining the attributes inside the class, the memory space for the
object and storing of assigned values are considered only when an object is
created for the class. The values stored in these attributes are said to be the
object's state. In Python, the values of variables (attributes) vary from one
object to another, and such variables are referred to as instance variables.
Example 4: Illustration for class creation with a class attribute.
Creating a class BilogicalFamily consisting of the "no_of_species_found "
attribute with the value of 12,00,000 is executed as follows.
In the above code, it could be found that the instance variable
no_of_species_found is declared inside the class BiologicalFamily with the
value 1200000, and the pass statement is given after the attribute
declaration. Here, the attribute no_of_species_found is defined inside the
class and is regarded as a class attribute. The class attribute
no_of_species_found is shared by all the instances of the class
BilogicalFamily. The usage of the pass statement is optional.
The values stored in the object should be accessed. Generally, the scope
access is divided into two types, namely, inside the class and outside of the
class. While accessing a class attribute inside the same class, no object
name or object reference is needed, whereas accessing the attributes outside
of the declared class mandates the use of the object where the values are
stored. The following syntax is used to access the values of the attributes in
an object from outside of the declared class.
In the above syntax, instead of having only the class attribute name, the
object name is prefixed along with the dot "." operator.
In the above snippet, the object new_obj is created for the class
BiologicalFamily, and the class attribute no_of_specieas_found is accessed
using the above syntax as new_obj.no_of_species_found.
Using the syntax, assigning and accessing the attributes can be executed.
The following syntax shows how to assign values for the class attributes.
The above syntax shows that it is similar to the normal assignment
operation, as seen in Chapter 3, but the only difference is that instead of the
variable name alone, the attribute name is associated with the object name
and the dot "." operator. Similarly, Python allows programmers to add
attributes not as a part of the class definition. Such created attributes are
associated with only the specific object. The other objects of the same class
are unaffected because of it.
Instance variables: Instance variables are the attributes that are defined
only for the specific object(s) and are not common to all the class objects.
By the definition of the state of an object, all class attributes are instance
variables but not vice versa. Instance attributes are defined using the
objects. In other words, the attributes defined and created using the object
are called instance variables.
Example 5: Illustration of creating an instance variable for an object
outside the class definition.
Create an object with the cat's name and add an instance variable
"no_of_cat_species_found" with the value of 42 for that object alone.
Similarly, create an object butterfly and add an attribute
"no_of_butterfly_species_found" with the value of 43 for that object alone.
Now, the object cat is created using the following statement, and the new
instance variable no_of_cat_species_found is created and assigned with
value 42 as follows.
The new attribute no_of_cat_species_found, which is not defined in the
class, is added to the object cat using the statement
"cat.no_of_cat_species_found = 42", where the attribute is assigned with
the value 42. The instance variable no_of_cat_species_found is only
assigned for the instance of the class BiologicalFamily , the object cat.
Another object butterfly is created for the class BiologicalFamily, and a
new instance variable no_of_butterfly_species_found is declared for the
object with the value of 43.
Like the object cat, the butterfly is also declared in the above code. Let's try
to access the instance variable no_of_species_found, declared in the class
definition, from both the objects cat and butterfly.
From the above code, the class attribute no_of_species_found is accessed
from both the objects using the object name and the dot "." operator.
Now let us try to access the instance variable no_of_cat_species_found
from both objects.
While accessing the instance variable no_of_cat_species_found from the
object cat, the value 42 is printed as output. Whereas accessing the same
attribute from the object butterfly threw the attribute error. The attributes
created outside the class definition are only for the specific object and not
all other objects of the same class.
Let's try to modify the attribute no_of_species_found from the object cat
and look ahead to the same attribute from the object butterfly.
In the above example, the class attribute no_of_species_found is modified
to 1300000 from the object cat. The class attribute no_of_species_found is
printed from the objects cat and butterfly. It could be identified that the
class attribute in the object butterfly is unaltered. The change in one
attribute of an object does not alter the other objects. Therefore the values in
the attributes are called the state of the object.
Use of setattr():
The values for the class attributes can also be assigned using the "setattr()"
function outside the class. The syntax for the "setattr()" is as follows:
In the above syntax, the "setattr" function is provided with arguments such
as the object's name, the attribute to which the value is assigned (in String
Format) and the value as the last parameter.
In the above Example 4, the value for no_of_cat_species_found in the
object cat can be assigned using the following code.
In the above code, the previous value stored in no_of_cat_species_found in
the object cat is 42 and then using the setattr() function, the value is
changed to 50. The above example shows the declaration of attributes
manually in normal Python statements. The same result can be achieved by
using the __init__ method, which is regarded as the constructor in Python.
The __init__ method creates an object for a class. The parameterised
creation of objects seen in the previous section supplies the arguments to
the constructor of its class. The following example shows how the
constructor defines attributes for a class. Defining the __init__ method is as
similar to defining a function in Python.
Example 5: Illustration for Creating a class with multiple attributes.
Create a class School to maintain records, such as school name, school
address, secretary name, and number of students. As per the motive, the
class School is created as follows.
The above code defines the __init__ method inside the class definition, with
five parameters. The first parameter is the self, which represents the object
itself. The changes in the attributes and methods that are associated with an
object are done using the self keyword. If the self keyword is not used, the
scope of the variables created and used inside the method end within the
method itself. In that case, the object is unaltered if the self keyword is not
used.
In the above code, inside the __init__ method, four variables are created
and assigned with the values passed to the method. For example, the
statement "self.school_name = name" denotes creating an attribute called
school_name for the object and is assigned with the value passed to the
parameter name. If the school_name was already defined, then the
statement alters its value that is associated with the object.
An object xyz is created for the class School by passing the required
arguments to the __init__ method. The following code shows the creation
of the object.
In the above code, values for the parameters should be given that match
with the arguments in the __init__ function itself. The constructor method is
executed asynchronously once the object is created. In other words, the
constructor is called once the object is created without having special
statements or explicit arguments. On executing the above code, the __init__
method is executed successfully. Thereby, the four arguments mentioned
above are created.
The attributes created in the above code are accessed outside the class as
follows using format specifiers in the print statement.
Suppose the user wishes to delete the attribute secretary_name then, the
user would execute it by using the del() function. The following code shows
the execution of deleting the attribute secretary_name from the object xyz.
Types of Attributes
Based on accessibility, class attributes are divided into two categories.
1. Public attributes: the attributes in public scope can be accessed inside
and outside of the defined class.
2. Private attributes: the private-scope attributes can be accessed only
within the class.
Table 12.1 shows a clear picture of the accessibility of attributes under
different scopes. The word "scope" represents where the variables, objects,
and functions can be accessed. The subclass is also another class where the
class is inherited from a parent class. This concept comes under inheritance
and is briefly discussed in detail in Chapter 13.
Table 12.1: Accessibility of Different Type of Attributes
Public Attributes
The public scope of an attribute is the default scope in Python. The kind of
attributes declared, accessed, and modified in the previous section class
attributes come under public attributes. The public attributes can only be
accessed from any part of the program with the object and the dot "."
operator. All the attributes declared and defined in Example 5 and Example
6 are under public accessibility.
Private Variables
The private scope of the class attribute allows access to the class attributes
only inside the class. In Python, the private variables are implemented using
"__" (double underscore) as the prefix of the variable name. The syntax for
defining private members inside a class definition is as follows.
Here, "__" (double underscore) is followed by the name of the class
variable, the assignment operator and the value to be assigned. It is assumed
that the above syntax is used inside the class definition.
Example 6: Illustrating the private attribute definition in the class
School.
In the above example, to add "security_id" as a private variable, the variable
is prefixed with "__" (double underscore). Usage of "__" (double
underscore) as the prefix of a variable leads to name mangling in Python.
Name Mangling
Name mangling is the process of naming the variable using the prefix "__"
(double underscore), which makes the variable can be accessed only by
using the class name prefixed with "_" (single underscore) followed by the
variable name with double underscore.
Situations might demand to access the private variables from the outside of
the class. Here, Python's mangling process comes into the picture. The
syntax for accessing a private variable from outside is,
In the above syntax, for accessing the private variable outside the class, the
name of the object is followed by the "Dot" operator, the class name
prefixed with "_" (single underscore) and the name of the private variable
(with "__" double underscores).
For example, secretary's id is expected to be updated with the number
108108 in the object of "xyz" of the class "School" outside the class, then,
Note
Programmers should avoid using the private variables outside the class
definition
Protected Attributes
In Object Oriented Programming, class attributes have also another scope,
called as protected scope. The protected scope for the class attribute allows
class variables to be accessed by the class itself and its inherited classes.
Python does not contain any special criteria or support for protected scope
for a class attribute. The protect variables can be accessed within the class
and also within the subclass.
12.4 Methods in Class
Methods define the behaviour of the objects. Methods and functions seem
to be the same but they are different in a few aspects and they are tabulated
below.
In this section, various methods in Python with various examples. Methods
in the class are defined using the following syntax.
The above syntax might look like a function definition as seen in Chapter 6,
but two differences can be noticed, which are,
1. The 'self' keyword is the first argument in the parameter list and refers to
the object of the class. Usage of the "self" keyword is an optional
argument.
2. The function (actually, it should be called a 'method') is defined under a
class.
Note
If the "self" keyword is not used in the argument of a method, one cannot
use the data/class attributes/instance variables associated with the object
inside the method.
The "self" keyword in the argument of the method denotes the
corresponding object. Also, it is used to denote the object along with the
"dot" operator for accessing the variables of the corresponding object.
The syntax for calling the methods using the object is,
For invoking the method of an object, the name of the object is followed by
the "dot" notation, the name of the method, and arguments inside the
parathesis. The following example shows how to define a method and
invoking.
Example 7: Defining a method to print the about the school paragraph
for the class School.
As shown in Example 5, the School class is created as follows.
In the above code, the attributes for the class School are created using the
__init__ method. Now, it is time to create a method for printing the about
paragraph of the school. So, without exiting the block of code, the method
is started at this junction with the "…" mark as follows.
The above method about_school is defined with a parameter of the keyword
self. The usage of the self keyword enables the accessing to the properties of
the objects. An instance is created, and the about_school() method is
invoked.
"self" as arguments
The "self" keyword or any corresponding keyword argument is not added
while invoking (in method calling statement) a method because the method
is called through the object (by its name). The object name before the
"dot(.)" notation itself acts as an argument. For example, in the statement
"xyz.about_school()", the object xyz is passed as an argument to the method
about_school.
There are situations where the methods are to be defined outside the class
definition. While assigning those functions to an object, the function is
added as a method to the corresponding object. Such methods are called
instance methods. Instance methods are defined as methods that are
available for specific objects and not as common for all the objects of the
class. The syntax is also similar to defining the attributes out of the class
definition as in Section 12.2.
The syntax for declaring the function outside the class definition is as
follows,
In the above syntax, the declared function "function_name" is added to the
object by using object_name followed by the "dot" operator,
"method_name", and assignment to "function_name". It is not necessary
that "method_name" and "function_name" should be the same. Methods
that are defined outside of the class definition are added to the object and
not to the class definition, i.e., the function is not visible or not available in
the other objects of the same class. Invoking methods defined outside the
class are a little different when the "self" keyword is used, and this case is
explained shortly. Other than using the "self" keyword as an argument,
invoking the method follows the same syntax mentioned above.
Example 8: Illustration for adding a function which is defined outside
the class definition, as method of an object in Example 7.
When the "self" keyword is used as an argument in the function definition,
the object itself should be passed as an argument, although the function is
called from the object. The syntax for such method definition and invoking
process is,
In the above syntax, the function is defined with the keyword "self" as an
argument, and while invoking the function from the object, the argument
list is associated with object_name also, which means that the object is also
passed as an argument to the function irrespective of that the function is
called from the same object.
In the above example, defining a function to print the "school_name"
associated with the object 'xyz' can be executed by the following code.
12.4.1 Private Methods
With the private variables, private methods get access only within the class
and are accompanied by "__" (double underscore) as the prefix. Whenever
the Python interpreter faces "__" (double underscore) as the prefix of the
variable, the name mangling comes into the picture. To invoke the functions
outside the class, the private method should be prefixed '_' followed by the
private method name.
The syntax for defining a private function is as follows.
In the above syntax, the private method is declared and defined as shown in
the previous section, but the only difference is that the method name is
accompanied by "__" (double underscore) as a prefix. The property of
usage of the "self" keyword is similar to the methods in the previous
section.
The method is invokable and can be accessed within the class using the
"self" keyword. To invoke the private method from outside the class
definition, the class name is also used. The syntax for invoking private
functions from outside the class is,
In the above syntax, to invoke the private method, "object_name" followed
by the "dot" operator, the class name with "_" (single underscore), and the
name of the private method with "__" (double underscore) and arguments.
Example 9: Illustration for accessing the private attribute through
private method.
Adding to the class definition of Class School in Example 5, the following
snippet should be defined along with it.
In the above code, the new private method, __put_secretary_id, is defined,
where the double underscore "__" is prefixed such that the method is
converted to a private method. The private method creates a private
attribute, __secretary_id and stores the received parameter.
It's time to create an object xyz with the necessary parameters as follows.
As discussed earlier, to invoke the private method from outside and to
invoke the private method the class name with "_" should be added before
the method name. For invoking the private method __put_secretary_id in
the object xyz, the following code is executed.
In the above line of code, while invoking the _put_secretary_id private
method, the class name School, which is prefixed with "_", is attached
before the method name and after the dot "." operator, used for invoking.
The following Fig. 12.2 shows the process clearly.
Fig. 12.2: Invoking Private Method __put_secretary_id
12.4.2 Constructor
A constructor is a special method of a class that is generally used to
instantiate an object. The constructor allocates the memory space for storing
the class variables and executing the methods of the object of a class.
One of the major tasks of the constructor is to initialise the values of the
class variables (class attributes) when an object of the class is created. In
Python, the constructor method is created using "__init__()" where
"__init__" is a keyword. The syntax for constructor in Python is as follows.
In Python, there are two types of constructors, namely,
1. Default constructor: the default constructors are constructors where
"__init__()" is defined without any arguments except the "self"
argument, which refers to the instance being constructed.
In the above example, suppose the class variable "no_of_students"
variable should be initialised with 0 when the object is created, then,
Example 10: Illustration for default constructor.
For defining the variables outside of a method in a class, the variables
are assigned the value "None". Then, a constructor is defined, and the
value for the instance variable no_of_students is assigned 0.
In the above code, the constructor, which is __init__ method, is
presented only with the self keyword, which says that the corresponding
object is passed itself as a parameter. That makes the changes in their
attributes and methods possible.
In the above code, the constructor for the class School contains
statements to assign 0 to the class variable "no_of_students" using the
"self" keyword and the method get_no_of_students is invoked to print
the instance variable no_of_students which is printed as 0.
2. Parameterized Constructor: the parameterised constructor is the
constructor with arguments and "self" argument, where the data for the
class variables are received when the object of the class is initialised.
In the above example, if all the class variables in the objects should be
assigned with values that are passed as arguments, then,
Example 11: Illustration for parametrized constructor.
In the above code, the parameterized constructor is used and the values for
the class variables are received as parameters when the object is created.
Since class variables "secretary_id" and "no_of_students" are integer types,
they are type converted using the "str()" function to print inside the "print()"
function with string concatenation operator "+".
__new__ is also a constructor
"__init__()" is not only the constructor in Python. Technically, "__new__()"
function in a class is used to allocate the memory, and it is always implicitly
invoked before "__init__()" at once the object is created for a class. The
explanation and discussion about "__new__()" of a class is discussed in
upcoming chapters.
12.4.3 Destructor
Destructors are called when an object gets destroyed. In Python, destructors
are not needed as much as in C++ since Python has a garbage collector that
handles memory management automatically. The __del__() method is a
destructor method in Python. It is called when all references to the object
have been deleted, i.e., when an object is garbage collected. The destructor
method is executed once the object is deleted using the "del()" function.
The "del()" function is used to delete the objects in Python. In previous
chapters, "del()" is used for deleting variables. Formally, the syntax for
deleting the objects of the class is as follows.
In the above syntax, "del(object_name)" deletes the object in the Python
environment. The object would not exist in the current Python environment,
once the statement is executed.
In the above code, after executing "del(xyz)" and then executing
"print(xyz)" shows a "not defined" error.
The syntax for destructor declaration is,
Where the destructor "__del__()" method is called once the
"del(object_name)" is executed.
Example 12: Illustration for defining a destructor for the class School.
From the above example 11, try to print school_name + "School is over" by
the destructor method. The corresponding code is expressed as follows.
The object xyz is created as follows.
To invoke the destructor of the class, the del() method is called with the
object as the argument.
On invoking the del() method, the destructor method __del__ is executed
and "Xyz School is Over" is printed as output.
Garbage Collection
Garbage collection is a memory recovery feature that is used in many
programming languages to free-up memory space that has been allocated to
the variables or objects which are no longer demanded by the program.
Python has a garbage collector that automatically handles the memory
effectively. When an object to the garbage is cleaned, the destructor of the
object is automatically invoked by the built-in garbage collector.
In addition to the automatic garbage collector in Python, a separate module
is used to control the process of garbage collection. Garbage collector (gc)
is a module that provides an interface to the optional garbage collector. This
module provides the ability to disable the collector, tune the collection
frequency, set debugging options, and check the current status of the objects
and their referents. It also provides access to unreachable objects that the
collector found but cannot free. Since the collector supplements the
reference counting already used in Python, the collector can be disabled, if
the user is sure about not creating the reference cycles. Manual garbage
collection is performed on the following basis.
1. Time-based garbage collection: The garbage collector is invoked in
fixed interval of time.
2. Event-based garbage collection: The garbage collector is invoked on
the basis of occurrence of an event.
Automatic collection can be disabled by calling gc.disable(). To debug a
leaking program, call gc.set_debug(gc.DEBUG_LEAK). Notice that this
includes gc.DEBUG_SAVEALL, causing garbage-collected objects to be
saved in gc.garbage for inspection.
The gc module provides the following functions.
Example 13: Illustration for the usage of gc module in Python.
In the above example, "gc" module is imported using "import gc", and the
functions, constants, and variables are accessed using "gc". To get the list of
objects that are available in the environment using "gc.get_objects()" and to
find the length of the available objects list "len(gc.get_objects())". An
empty list "x" is created, and then two elements, 1 and 2, are appended into
the list. While printing the elements in x, "[2,1]" is the output. It could be
identified that the variable refers to the two items 1 and 2. Also, x is
referred to by y. "gc.get_referents(objectName)" gives the list of items that
the variable refers to. While using the same function on x, the result is "
[2,1]". To get the list of referrers of an object,
"gc.get_referrers(objectName)" is used. For "gc.get_referrers(x)", the
output of the referrer of the object x is given as a list. The reference count
for the list created is now two. However, it cannot be reached from inside of
the Python and cannot possibly be used again, since it is considered
garbage. In the current version of Python, this list is never freed. To check
whether the object is finalised or not, "gc.is_finalized(objectName)" is used.
"gc.is_finalized(objectName)" gives bool (true or false) as output.
12.5 Built-ins
When an object is created for a class, there are attributes and methods
created along with it by default.
12.5.1 Built-in Methods in Class
Special methods in the class are a set of built-in methods used to enrich the
classes and are usually prefixed and suffixed with "__" (double score). This
section discusses the following special methods in classes of Python.
1. __repr_(): __repr__ method is used to represent the object of a class as
a string. The __repr__ method is invoked by repr() function with object
as the parameter. Usually, the __repr__ method returns a printable
representation of the objects.
Example 14: Create a representation string for the object of the class
School from the above example.
The class School definition is modified as follows. Additional to the
class attributes and instance variables, the __repr__ method is defined
which returns "school_name School"
In the above code, "print(xyz)" would have returned "<__main__.School
object at 0x0000021B67B86B90>", but now a __repr__ method is
defined for the class School, the statement gives "Xyz School" as the
output. Note that, in the __repr__ method, "string" data is returned. As
stated above, the __repr__ method can also be called using the "repr()"
function.
2. __str__(): __str__ returns the string representation of the object of a
class. If the __str__ method is not defined in a class, the controller
automatically executes the __repr__ method. The __str__ method should
be defined in a way that is easy to read and outputs all the class
members.
Example 15: Create a representation string for the object of the class
School from the above example.
In the above example class School, __str__ method is used for the effective
string representation of class objects.
The class School definition is altered as follows.
The above class definition includes the "__str__" method, which returns the
string, self.school_name + " School located at " + self.address + " has " +
str(self.no_of_students) + " students.".
In the above code, "print(xyz)" would have returned "<__main__.School
object at 0x0000021B67B86B90>", but now a __str__ method is defined
for the class School, the statement gives "Xyz School located at Erode has
1000 students." As the output. Note that, in the __str__ method, "string"
data is returned. As stated above, the __str__ method can also be called
using the "str()" function.
The key differences are shown in the following Table number.
Example 16: Show the difference in how "str()" and "repr()" are used
to call the "__repr__()" method associated with the object of a class.
A class Person is defined with name and age as instance variables
initialised as in the constructor, and __repr__ method is defined, which
returns in 'Person(name, age)'.
An object person1 is initialised with two arguments, "Hari" and 20. The
following code executes the same.
On invoking the repr function, the following output is shown.
Though the __str__() method is not defined, while calling the str() function,
the "__repr__" method is invoked.
In the above example, the "Person" class is initialised and defined with two
attributes, namely "name" and "age". Then "__repr__()" method is also
defined. Then three objects are created with various names and ages, and it
is shown how the "repr()" function is used to call the "__repr__()" method
associated with an object. Since there is no "__str__()" defined in the class,
"str(person3)" invokes the "__repr__()" method.
repr(objectName) shows the memory address and object type as the result,
if it is called without any "__repr__()" method defined in the class.
Example 17: Illustration for invoking __repr__ method when it is not
defined in the class.
The above example shows how "__str__()" is defined inside a class and
how it is called using the "str()" function and print function. Adding to that,
when "repr()" is invoked the "__str__()" method, "object type" and the
memory address are shown printed as the string format, since __repr__
method is not defined in the class Person.
Example 18: Illustration for the usage of "__str__()" and "__repr__()"
methods in a class.
In the above example, invoking "__repr__()" displays the string for the
class, and that could be viewed as a developer view, whereas invoking
"__str__()" displays a string form that is understandable by any new users
that are in layman terms.
12.5.2 Built-in Attributes in Objects
Like built-in class methods, there are a few important built-in class
attributes that are important for writing an effective Python program. Some
attributes are created while creating an object which is referred to as "built-
in class attributes." The built-in attributes that are created while creating an
object for a class are shown in the following table.
The following example shows how the "__dict__", "__class__", and
"__module__" are used in Python.
Example 19: Illustration for the built-in attributes in the class Person.
The class definition is altered with doc strings for the class Person,
__repr__ method and nameDisplay method, as shown below.
An object person1 is created with the input "Hari" and 20 as the parameter
for the constructor.
The "__doc__" gets the doc string of the concerned object, method, and
functions. The following code shows the usage of the __doc__ attribute.
The "__doc__" represents the documentation available for a function, a
class, or a method in a class. In Python, documentation can be given in
string formats (using single quotes (''), double quotes (" "), and triple quotes
(''' ''')) after the first line of class definition or function definition, or method
definition.
The __dict__ attribute gets the dictionary of attributes associated with the
object. The following code shows the output.
The "__module__" attribute associated with the object shows which module
the object belongs to. The above example shows "__main__" as the output
for the "__module__" attribute. In Python, "__main__" has two purposes,
they are,
1. "__main__" denotes the name of the top-level environment of the
program.
2. "__main__.py" is created while creating new packages or modules in
Python, which is discussed in Chapter 15.
Here, "__main__" denotes that the given object is executed in the top-level
environment of the program. Then the "__class__" attribute displays the
module of the program and also the name of the class to which the
corresponding object belongs to the class, "Person".
The "__class__" attribute fetches the name of the class to which the object
belongs to and along with their __module__ name.
The above example shows how the doc strings work with different quotes
and how it conveys the documentation to the end-users. These doc strings
are helpful while writing coding in the development environment.
Summary
1. OOP is based on the objects which contain the data as variables and
performs activities using the associated methods.
2. Class is defined as the blueprint for the objects that hold definitions for
the variables and methods.
3. Attributes are the variables declared inside the class and refer to the state
of the object.
4. Methods encapsulate functions that handle the data and behaviour of an
object.
5. Encapsulation allows different levels of access to the attributes and
methods of the class based on priority.
6. Inheritance is the process of deriving a class from another class or other
classes to possess the attributes and methods of the deriving class.
7. Abstraction is the process of identifying only the required attributes and
methods and ignoring the irrelevant attributes and methods.
8. Polymorphism allows using of the operators and functions in different
forms.
9. Self argument in the method parameters refers to the calling object.
10. Name mangling is the process of naming the variable using the prefix
"__" (double underscore), which makes the variable can be accessed
only by using the class name prefixed with "_" (single underscore)
followed by the variable name with double underscore.
11. "__" (double underscore) is prefixed to a variable or method name to
make it a private variable or method.
12. Constructor is a special method of a class which is generally used to
instantiating an object which also includes memory allocation.
13. "__init__()" is used as a constructor in Python.
14. Destructors are called when an object gets destroyed.
15. "__del__()" is used as the destructor in Python.
16. Garbage collection is a memory recovery feature, which is used in many
programming languages to free-up memory space that has been allocated
to the variables or objects, which are no longer demanded by the
program.
17. __repr__ method is used to represent the object of a class as a string.
18. __str__ method returns the string representation of the object of a class.
Glossary
Design Paradigm: A Paradigm is a framework that encompasses
computation thinking, assumptions, strategies, heuristics and
methodologies.
Object oriented programming: programming based on the objects which
contain the data as variables and perform activities using the associated
methods.
Class: blueprint for the objects that hold definitions for the variables
(also called data members) and methods.
Object: the object is an instance of the class.
Attributes: attributes are the variables declared inside the class and refer
to the state of the object.
Method: the method encapsulates functions that handle the data and
behaviour of an object.
Encapsulation: encapsulation is the process of allowing different levels
of access to the attributes and methods of the class based on priority.
Inheritance: inheritance is the process of deriving a class from another
class or other classes to possess the attributes and methods of the
deriving class.
Abstraction: abstraction is the process of identifying only the required
attributes and methods and ignoring the irrelevant attributes and
methods.
Polymorphism: polymorphism means "many shapes".
Name mangling: the process of naming the variable using the prefix "__"
(double underscore), which makes the variable can be accessed only by
using the class name prefixed with "_" (single underscore) followed by
the variable name with double underscore.
Constructor: a special method of a class that is generally used to
instantiate an object.
Destructor: a destructor is called when an object gets destroyed.
Garbage collection: garbage collection is a memory recovery feature
which is used in many programming languages to free-up memory space
that has been allocated to the variables or objects which are no longer
demanded by the program.
Conceptual Questions
1. What is a class?
2. Explain the building blocks of OOP.
3. What are all the advantages of OOP?
4. What is abstraction?
5. How to print the class name using the object and built-in type?
6. When is the user defined destructor called?
7. Explain the accessibility of variables in class based on the scope?
8. Explain the difference between "__init__" and "__new__".
9. How an attribute is assigned with the value using "setattr()"?
10. State the differences between methods and functions.
11. Explain the usage of the "self" keyword.
12. Explain parametrised constructor in detail.
13. Explain name mangling along with private methods.
14. What is meant by garbage collection, and why it is necessary?
15. Explain various methods used for memory allocation and deallocation in
Python.
16. Explain reference counting with an example.
Programming Questions
1. Write a Program to maintain salary of a worker using OOP. The details
to receive are staff_id, basic_pay, years_of_experience, total_allowance
and total_deduction with the help of Constructor. Using methods
calculate net_pay.
2. Write a program to read, add, display and subtract two matrices using
OOP.
3. Write a program to calculate area, perimeter and height of the triangle
using the concept of classes and objects.
4. Write a static method that check whether all words in the list starts with
vowel.
5. Write a program which receives either basic_pay and total_allowance or
net_pay and print the net_pay using separate method and calculation of
net_pay from basic_pay and allowance should be done using
classmethod.
6. Add a method reflect to class Point, which returns a new point which is
the reflection of the point about the x axis and y axis. For example,
Point(1,2), reflection is Point (-1,-2).
7. Create a Class for Bank which holds few details about the bank and
define destructor which should make all the variables as “None” and
print the message “The Process ends".
8. Create an OOP Program to determine the value and characteristics of the
roots. Roots includes both real and complex numbers.
9. Write an OOP program which reads 2 points and performs following
functions with users choice
1. Distance between 2 points.
2. Slope between 2 points.
3. Checking whether the two points are inside/outside/on the circle x2 +
y2 = 16
10. Write a menu driven program to calculate perimeter and number of
diagonals with number of sides as input.
OceanofPDF.com
Good code is its own best documentation. As you’re about to add a
comment, ask yourself, ‘How can I improve the code so that this comment
isn’t needed?
Steve McConnell
CHAPTER 13
Inheritance
This chapter aims to provide insight into how to work with inheritance in
Python.
Learning Objectives
At the end of the chapter, the reader will be able to:
Know the basics of inheritance in Object Oriented Programming.
Understanding the importance of Unified Modeling Language in Object
Oriented Programming.
Know the types of inheritance.
Understand different kinds of inheritance and their usage in problem-
solving.
13.1 Introduction
The previous chapter discusses the basics of Object Oriented
Programming(OOP). This chapter concentrates on organising the classes
into a hierarchical structure based on the concept of inheritance. In
inheritance, one class is derived from another class, and the properties of
the parent class can be used by the derived class. Inheritance is important
because it allows the programmers to create new classes, which specify a
new implementation while maintaining the same behaviours of the old
class. Inheritance is the process of deriving a class from another existing
class. Inheritance facilitates the reusability of the code and extends the
original properties of the class without modifying it.
Reusability is a vital property of OOP, which enables the programmers to
reuse the existing piece of code and saves time and energy of the
programmer. This property drastically reduces the cost of software products
and enhances the reliability of the developing software.
Motivations for using inheritance while solving a problem are,
1. Code reuse: Because a child class can inherit behaviour from a parent
class, the code does not need to be rewritten for the child. This can
greatly reduce the amount of code needed to develop a new idea.
2. Concept reuse: This occurs when a child's class overrides behaviour
defined by the parent. Although no code is shared between parent and
child, the child and parent share the definition of the method.
In deriving a new class from a class, two classes are defined as parent class
and child class.
Parent class: A parent class is a class from which a class is derived. The
parent class is also called the super class.
Child class: A child class is a class that is derived from another class and is
also called as derived class. Since a child class is derived from a class, it is
also called as subclass. An object of the child class can access the attributes
and methods of the parent class.
Considering an example of creating a school management system, there are
several categories of users, i.e., teaching staff, non-teaching staff, and
students. The readers could identify that all the users possess few common
data and behaviour. Let us dive into designing the parent class, where first
name, last name, address, date of birth, contact number, and aadhaar
number are common attributes for all categories of users. For each attribute,
the assigning and retrieving methods would be there and thus contributes to
common methods for all categories of users. Textually, they are persons and
they are divided into categories. So, while designing, a parent class, called
person, consists of the common attributes and methods, and then, a child
class can be created for the individual categories of users. Considering the
child classes, which are teaching staff, non-teaching class, and students, the
child class, "TeachingStaff", consists of staff Id, department, position,
salary, and class section as its attributes. The list of attributes is limited for
explaining the concepts. Readers could think of more attributes and
methods to their perspectives. Similarly, for "non-teaching" and "students"
child classes, individual attributes can be defined as shown in Fig. 13.1.
Fig. 13.1: Hierarchical Classification of Users in School Management
System
For visualizing the concepts in OOP, Unified Modeling Language (UML)
diagrams are used. The UML is a general-purpose, developmental
modelling language in the field of software engineering that provides a
standard way to visualize the design of a system.
Unified Modeling Language (UML)
The UML diagrams are classified into two types, structural diagrams and
behavioural diagrams. Structural diagrams consist of a class diagram,
component diagram, object diagram, deployment diagram, package
diagram, and composite structure diagram. The behavioural diagram
consists of activity diagram, state machine diagram, interaction diagram,
and use case diagram.
Class diagrams portray the main building block of OOP. It shows the
classes in the system, the attributes, methods of each class, and the
relationship between each class. In most class diagrams, a class has three
parts: the class name at the top attributes in the middle, and methods at the
bottom. In a large system with many related classes, classes are grouped to
create class diagrams. Different relationships between classes are shown by
different types of arrows. Fig. 13.2 shows the class diagram for the above
example, users in the school management system.
Fig. 13.2: Class Diagram for the class User and Student in Fig. 13.1
In UML, for finding the presence of an inheritance relationship in the text
or concepts, is-a test is used.
"is-a" test: If two concepts, namely A and B, are linked by an inheritance
relationship in which A is the super class and B is the child class or derived
class, then the statement "A is a B" should be technically (semantically)
correct.
The "is-a" test is used to check whether two concepts are linked by
inheritance relationships. From the example mentioned above and from Fig.
13.1, "TeachingStaff" is a derived class of "Person", and it could be
identified that the statement "A TeachingStaff is a Person" is sensibly
correct. So, according to the "is-a" test, TeachingStaff and Person are linked
by inheritance relationship.
Positive examples for "is-a" test:
1. Integer is a datatype.
2. Pen is a stationary.
3. Lion is an animal.
4. Giraffe is a mammal.
Negative examples for "is-a" test.
1. A ball is a wall.
2. An animal is a lion.
3. A monitor is a computer.
4. A datatype is an integer.
The readers could go through the positive and negative examples mentioned
above and get more clarity on the "is-a" test.
"is-a" is an indicator for checking the existence of inheritance relationships.
There are occasions where inheritance is applied even though the "is-a" test
fails. Majorly, it is a reliable test for inheritance.
Inheritance in Python
In Python, there are no special keywords while deriving a class from a base
class. For deriving the child class from a parent class, the name of the
parent class should be given as a parameter while defining the child class.
The syntax for inheriting a class from a class is as follows.
In the above syntax, readers could identify that the name of the parent class
is inserted between parenthesis "()" as like placing the parameters for a
function, i.e., "class DerivedClass (ParentClass): ", one could notice that
while defining the class ParentClass is placed as like a parameter. Now, the
Python Interpreter takes DerivedClass as a Child Class of the ParentClass.
Example 1: Derive a class TeachingFaculty from the parent class, Users
for School Management System, as shown in Fig. 13.1 (Listing1.py).
The class diagram for the base class Users and its derived class
TeachingFaculty. The attributes and methods that are present inside the
classes are shown in Fig.
Fig. 13.3: UML Class diagram for the derived class TeachingFaculty
and its base class Users.
The class "User" is created with a constructor, and another method named
"display()" is defined. The constructor of the class user assigns values to the
instance variables first_name, last_name and aadhaar_no. The display
method in the class prints the "Aadhaar number of first_name, last_name is
aadhaar_no". The definition of the class User is shown below.
Another class, "TeachingFaculty" is derived from the class User. Now, the
base class or parent class is "User", and the child class or derived class is
"TeachingFaculty". The deriving of the base class "TeachingFaculty" is
executed by placing the parent class name inside as parameters 13.3.
From the above code, it could be identified that the child class is also
associated with a constructor, which invokes the constructor of the parent
class. To call a method of the parent class, the following two ways are used.
1. Using the parent class name. The parent class name is used in front of
the method name in the function call statement. The syntax for the
method invoking using the class name is as follows.
In the above example, parent class name "Person" is placed before the
method name "__init__". The constructor of the parent class, Person,
demands three arguments, and at the method call, three arguments are
given.
2. The second way is by using the super() function. The super() function
denotes the immediate parent class of the child class. In the example, to
call the display() method of the parent class, the line super().display() is
executed.
In the above code, while defining the method displayItem in the child class
TeachingFaculty, the instance variables of the parents class are used as the
usual way of handling the attributes, as discussed in the previous chapter.
This shows that the child class inherits the properties of its parent class.
Now an object is created for the child class as follows.
When creating an object for the derived class, the memory allocation is also
created for the base class and constituted in the derived class object.
Additionally, note that, in either of the methods in calling a class method,
using class name and super() function, the characteristics of the object are
carried forward through the objects. The execution of the above line
invokes the constructor of the child class, and inside the child class
constructor, the constructor of the parent class is invoked. In case an object
is created for the base class, then indeed the created object for the base class
refers only to the base class and does not possess any reference to the child
class. Simply, the object of the parent class can not access the
characteristics of the derived class, and vice versa is not the same.
Then the method in the child class is invoked using the method call, as
shown in the chapter 12.
The above given code is saved as Listing1.py. The file is executed in the
command prompt, and the following output is shown below.
Additional Observation
Apart from the observations mentioned above, the programmers should
learn the chronological order of execution while an object is created for the
derived class.
Step 1: Memory allocation for the base and derived classes is executed. The
constructor of the derived class is invoked. In the example, while creating
an object "object1" for the class "TeachingFaculty", the constructor of the
class is automatically invoked.
Step 2: After execution, if any mention is there of invoking the constructor
of the base class, the constructor of the base class is executed; otherwise,
the constructor of the base class is not executed. Due to the presence of the
statement, "User.__init__(fname, lname, aadhaar)", the constructor of the
base class "User" is invoked.
Inheritance and Types of Attributes
As seen in Chapter 12, there are two types of attributes in Python, namely,
public and private, attributes. The public attributes are accessible across all
the derived classes. The private attributes are defined and used using the
"__" double underscore operator. Name mangling technique is used while
accessing the private attributes in the case of private attributes.
Types of Inheritance
Based on the characteristics of deriving a class, the inheritance is classified.
Among such classifications, Python programming supports the following
inheritance types.
1. Single inheritance
2. Multiple inheritance
3. Multi-level inheritance
4. Hybrid inheritance
This section describes the above-mentioned types of inheritance in detail,
along with sufficing examples.
13.2 Single Inheritance
When a derived class inherits its properties (attributes and methods) from
one base class, it is called single inheritance. The derived class possesses
the characteristics of the base class and also its properties. The syntax for
the single inheritance is as follows.
In the above syntax, it is shown that "BaseClass" is defined with attributes
and methods, and a derived class is derived using the statement "class
DerivedClass (BaseClass):" where passing BaseClass to a class makes the
class as the derived class to Base Class. The pictorial representation of the
above syntax is shown in Fig. 13.3.
Fig. 13.3: Single Inheritance
Example 1 is a typical example of single inheritance.
Example 2: Illustration for single inheritance by deriving class Apple
from the base class Eatable_Fruits.
Let's create the base class Eatable_Fruits consisting of a class attribute,
no_of_fruit_varieties, with a value of 2000.
A Subclass Apple is derived from the class Eatable_Fruits. Now, the class
Eatable_Fruits is called the parent class, and the class Apple is called as
child class. A class attribute colour is defined inside the class Apple as a set.
The hierarchy of the class Apple and the class Fruit is shown in Fig. 13.4.
Fig. 13.4: The Class Fruit and the Class Apple Hierarchy
An object kashmir_apple is created for the child class Apple. The following
code shows an example of accessing the attributes.
In the above code, the object Kashmir_apple is created for the class Apple,
and it could be noticed that the class attribute no_of_fruit_varieties of the
parent class Fruit is accessible through the object of the child class
Kashmir_apple.
The following code creates an object for the parent class Fruit, and the
attributes are accessed as follows.
In the above code, an object orange is created for the parent class
Eatable_Fruit, and the class attribute no_of_fruit_varieties of the parent
class Fruit is accessed. The class attribute colour is not accessible through
the parent class object.
Example 3: Illustration for single inheritance by deriving a class from
the base class Students in Student Management System, as shown in
the above example (Listing2.py).
Since the student is also a user, this comes under single inheritance. The
class Student is derived from the class User, as shown in Fig. 13.5.
Fig. 13.5: User: Student Single Inheritance
The class User is defined as follows, as shown in Example 1.
Now, the subclass Student is defined as follows with the attributes of
roll_number, class, and section that are assigned in the constructor. An
__str__ method is also defined to represent the object of the class Student.
In the above code, the constructor of the parent class User is called using
the Super() method, as shown above. The __str__ method is also defined to
represent the object.
The above code is stored as Listing2.py and executed in the Command
Prompt. The result is shown below.
13.3 Multiple Inheritance
Multiple inheritance refers to a type of inheritance where the derived class
possesses the characteristics of one or more base classes. In other words, a
subclass is derived from more than one class. The derived class has all the
features of base classes along with the new features that are defined in the
derived class. The syntax for multiple inheritances is similar to that of the
single inheritance as follows.
In the above syntax, on comparing with the syntax of single inheritance,
while defining DerivedClass, the parent classes are included like a
parameter like "class DerivedClass (BaseClass1, BaseClass2,…,
BaseClassN)", which conveys that DerivedClass is derived from
BaseClass1, BaseClass2, …, BaseClassN. This is pictorially represented in
Fig. 13.6.
Fig. 13.6: Multiple Inheritance
In the multiple inheritances, while accessing the attributes and methods,
initially, the interpreter searches in the derived class. If the component is
unavailable in DerivedClass, it will be searched in the parent classes by
Depth-First Search.
Method Resolution Order (MRO): MRO is a concept used in inheritance.
It is the order in which a method is searched for in a class hierarchy and is
especially useful in Python because Python supports multiple inheritances.
Additionally, Python constructs the method resolution order for new-style
classes involving multiple inheritances.
Example 4: Illustration for multiple inheritance by creating a class TF
representing Teaching Faculty and another class Student and then
derive a class called TA from both the class TF and Student.
Creating a class TF, as shown below, with a constructor that prints "Base
class Teaching Faculty" also invokes the constructor from its superclass. It
could be noticed that although the class TF is not a derived class, the
super() function is used. This invokes the constructor of the class of the
same order, which is class Students.
Now another class, Students, is defined where the constructor is defined,
which prints "Base Class Student". The code of the class Students is shown
below.
The class TA is derived from the class TF and the class Students. Here, the
constructor prints "TA Derived Class from TF and Students", and then the
constructor of the parent class is invoked.
The above-created multiple inheritance class is picturized in Fig. 13.7.
Fig. 13.7: Multiple Inheritance of TF and Students and TA
An object for the child class TA is initialized. Consequently, the constructor
of the child class TA is executed. Subsequently, as per the depth-first search,
the constructor of the class TF is executed, and then the constructor of the
class Student is executed.
In the above example, the class "TA" is derived from "TeachingFaculty"
and "Students". One could note that while executing "super().__init__()" in
the class "TA", it could be noticed that as per the MRO, the constructor of
the class "Teaching Faculty" is executed, and then the constructor of the
class "Students" is executed. The syntax for Multi-Level Inheritance is as
follows.
13.4 Multi-Level Inheritance
Multi-level inheritance is the type of inheritance in which a class is derived
from another derived class. Let us take a simple example. Assuming a base
class and there is a derived class named "Derived1" and another class
named "Derived2" which is derived from "Derived1" and thus forms a
three-level of hierarchy. Now, Derived1 exhibits the characters of the
BaseClass as well as its attributes and methods. Whereas, Derived2 exhibits
the characteristics of BaseClass, Derived1, and also its attributes and
methods. While accessing the attributes and methods of the classes, MRO
take resolving processes.
The above syntax can be picturized as the following Fig. 13.8.
Fig. 13.8: Multilevel Inheritance
Example 5: Illustrating the multilevel inheritance by deriving a class
Students from the class User and another class ClassActivities from the
class Students.
Let us consider the above school management system, students are a class
derived from person, and considering ClassActivities is another class that
holds the details about the activities that are performed in Class Sessions.
The following code illustrates how to implement this as a multi-level
inheritance.
The class User is defined below.
The derived class Students from the base class Person is defined as follows.
The class ClassActivities is derived from the derived class students in
which the class activities are stored. The constructor for the class
ClassActivities is defined in which the "activities" is stored in the attribute
activities. The definition of Class Activities is shown below.
The object is created for the class ClassActivities with the parameters
shown below and accessed as follows.
In the above example, the class "ClassActivities" is derived from the class
"Students", which is already a derived class from the class "Person".
13.5 Hybrid Inheritance
Deriving a class from the derived classes which are derived from a same
base class is called hybrid inheritance. This involves multiple inheritance
and multi-level inheritance. Hybrid inheritance shown in above syntax is
represented in Fig. 13.9. The syntax for the hybrid inheritance as follows.
Fig. 13.9: Hybrid Inheritance
In the above figure, the class-derived Class 1 and the class-derived class 2
are inherited from Base Class. The class Derived Class 3 is inherited from
the classes, namely, Derived Class 1 and Derived Class 2. Since the
inheritance is transitive in nature and the parent class of Derived class 1 and
Derived Class 2 is Base Class, so the class Base Class is the indirect base
class for the class Derived Class 3.
Example 6: Illustration for hybrid inheritance by creating classes
University, Course, Branch and Student using hybrid inheritance
(Listing3.py).
From real-world concepts, the course and the branch are the derived class of
the class University. Then, the class Student could be the derived class from
course and branch. This structure is shown in Fig. 13.10.
Fig. 13.10: Hybrid Inheritance Example
The class University is defined as follows: the instance variable univ is
initialized with the value "Anna University". The method is also defined to
display the name of the university.
Now, the derived class course from the parent class University is defined as
follows that stores the course name and also the method function to print
the course name.
Similar to the class Course, the class Branch is also defined as the derived
class from the class University, as shown below.
The class Student is derived from the class Course and the class Branch,
where the constructor calls the constructor of the parent classes. The
constructor of the indirect base class is called in the constructor of the class
Course and so, in turn, the constructor of all the classes is invoked. The
definition of the class Student is as shown below.
An object student1 is created for the child class Student, and the display
function in the class Student is invoked as follows.
The above code is stored as Listing3.py and executed in the command
prompt. The following shows the output of the execution of the file.
Summary
1. Inheritance is the process of deriving a class from another existing class.
2. A parent class is a class from which a subclass is derived. The parent
class is also called the super class.
3. A child class is a class derived from another class and also called a
derived class.
4. "is-a" test: if two concepts, namely A and B, are linked by an inheritance
relationship in which A is the super class, and B is the child class or
derived class, then the statement "A is a B" should be technically
(semantically) correct.
5. super() function denotes the parent class.
6. When a derived class inherits its properties (attributes and methods)
from one base class, it is called single inheritance.
7. Multiple inheritance refers to a type of inheritance where the derived
class possesses the characteristics of one or more base classes.
8. Method resolution order (MRO) is the order in which a method is
searched for in a class hierarchy and is especially useful in Python
because Python supports multiple inheritances.
9. Multi-level inheritance is the type of inheritance in which a class is
derived from another derived class.
10. Deriving a class from the derived classes which are derived from the
same base class is called hybrid inheritance.
Glossary
Inheritance: inheritance is a concept in OOP, which is defined as
organizing the classes into hierarchical structures.
Parent class: parent class is a class from which the subclass is derived.
Super class: parent class is also called super class.
Child class: child class is a class that is derived from another class that
possesses the characteristics of the parent class(es) and its own.
Derived class: child class is also called derived class.
"is-a" test: if two concepts, namely A and B, are linked by an
inheritance relationship in which A is the super class, and B is the child
class or derived class, then the statement "A is a B" should be technically
(semantically) correct.
When a derived class inherits its properties (attributes and methods)
from one base class, it is called single inheritance.
Multiple inheritance refers to a type of inheritance where the derived
class possesses the characteristics of one or more base classes.
Method resolution order (MRO) is the order in which a method is
searched for in a class hierarchy and is especially useful in Python
because Python supports multiple inheritances.
Multi-level inheritance is the type of inheritance in which a class is
derived from another derived class.
Deriving a class from the derived classes which are derived from the
same base class is called hybrid inheritance.
Conceptual Questions
1. What is inheritance?
2. Mention the advantages of inheritance.
3. Differentiate super class and derived class.
4. Give the syntax of multi-level inheritance.
5. List the types of inheritance.
6. Explain the use of "super()" in inheritance.
Programming Exercise
1. Write Python code for a school management system with three user
categories and show all three types of inheritance in it.
2. Write a program that extends the class Employee. Derive a class
Manager from Employee so that it lists all the details of the manager as
well as the details of employees working under that manager.
3. Write a program that extends the employee class so that it stores two
more data members: DOB and Date_of_Hiring. The date must be
defined as a separate class.
4. Write a program that extends the class Result so that, the final result of
the student is evaluated based on the marks obtained in tests, activities,
and sports.
5. Write a program that extends the class shape to calculate the volume of
the cube and the cone.
6. Write a program with a class Person, derive a class BaseballPlayer from
Person and display all the details of a famous baseball player.
7. Write a Python program with a class quadrilateral and derive classes for
Square, Rectangle and Trapezium. While receiving value from the users
for the objects, ensure the correctness of the values using constructs in
child classes and Parent class.
OceanofPDF.com
Computer science education cannot make anybody an expert programmer
any more than studying brushes and pigment can make somebody an expert
painter.
Eric S. Raymond (author of “The Cathedral and the Bazaar”)
CHAPTER 14
Advanced Class
This chapter aims to provide an advanced concept of Object Oriented
Programming (OOP), such as overloading, overriding, static, and class
methods, abstract base class (ABC), and metaclass.
Learning Objectives
At the end of the chapter, the reader will be able to:
Understanding how method overloading is implemented in Python.
Learning to overload an operator in Python.
Learn how to override a method in a class.
Understanding the concept of abstract base class.
Learning about the meta class.
14.1 Overloading
Object Oriented Programming (OOP) allows the programmers to add some
additional functionalities to the operators and methods which have basic
properties. Such a kind of redefining of the entities of the programming
structure is called as polymorphism. In earlier chapters, the readers could
notice that the addition “+” operator is used for the concatenation of strings
inside the print function. This is a kind of polymorphism that is introduced
in earlier chapters. Overloading is a type of polymorphism that adds
additional functionalities for the existing properties of objects (methods and
attributes) or the object itself. Overloading exhibits code reusability and
code readability. Overloading is of two types: method overloading (or
function overloading) and operator overloading.
14.1.1 Method Overloading
Method overloading is adding additional functionalities to a method of a
class by altering the parameters of the methods. In general, method
overloading is implemented by defining multiple methods of the same name
with
1. Varying data types of the parameters.
2. A varying number of parameters.
Fig. 14.1 shows that two methods with the same name, "SampleMethod",
are defined with different parameters. The method "SampleMethod"
definitions have the same length of parameters, but their datatypes are
different. The first definition consists of "int, int, char" and the second
consists of "int, char, int", where the second and third parameters differ.
This is the method overloading with variable datatypes of the parameter.
Fig. 14.1: Method Overloading with Parameters of Varying Types
For implementing the method overloading with variable datatypes, the
dispatch decorator is used to select different method definition
implementations of the same method based on the specified datatypes. The
"dispatch" decorator (@dispatch) from the multi-dispatch module is one of
the famous dispatch decorators used for method overloading. The module
"multi dispatch" is not the default available module in the Python
environment, and it should be installed by executing "pip install multi
dispatch" in the terminal or command prompt.
The syntax for implementing the dispatch operator is shown below.
Before the method definition, @dispatch decorator is used in the above
syntax with parameters consisting of datatypes. To specify the data types of
the parameters, data of the parameters are given as a parameter to the
dispatch decorator. For example,
"@dispatch(int, char, int)
def methodName(i , j, k):
method definition"
The dispatch decorator @dispatch(int, char, int) denotes the parameters of
the following method: i, j, and k are int, char, and int data types,
respectively. Now, the method would accept only the arguments of
datatypes int, char, and int, respectively. So, defining two methods with the
same name and changing the parameters of the dispatch operator
implements the method overloading by varying datatypes of parameters.
The method of overloading implemented using a dispatch decorator is
explained below with an example.
Example 1: Illustration for method overloading by varying data types.
Create a method function1 inside the class Operations where the method
should divide the input value by 2, if the input is an integer and the input
value is multiplied by 2.0, if the input value is the float.
For providing multiple functionalities for a method, method overloading
should be performed. Since the action on the input variable is based on the
data type, the dispatch decorator is used. The dispatch decorator is available
in the package multi-dispatch. For using the dispatch decorator, it is
imported into the namespace using the following code.
The above code imports the dispatch decorator from the multi-dispatch
package. Now, the class Operations is defined with two methods using
dispatch decorators with different parameters, int and float, as shown below.
In the above code, a method function1 with dispatch decorator of parameter
int returns the quotient when the input value is divided by 2. Adding the
statement "@dispatch(int)" at line 6 makes the method defined in lines 7
and 8 receive only the integer data type. Then, another method of the same
name function1 with dispatch decorator with parameter as a float returns the
input float value multiplied by 2.0. The statement "@dispach(float)" makes
the method defined in lines 13 and 14 receive only the float datatype. It
could be identified that the two functions with the same name but with
parameters of different data types.
Now, an object is created for the class Operations, and then the method
function1 is called with 2 and 2.0, which are integer and float datatype,
respectively.
The above code is saved as method_overloading.py and executed in the
terminal or the command prompt.
The above execution shows that if the input is an integer datatype, the
output is the value divided by 2, whereas if the input is an integer and is a
float datatype, the output is the value multiplied by 2.0.
Method Overloading with Parameter of varying length
From Fig. 14.2, one can identify two definitions for the method
"SampleMethod" in the class "ClassName", and those two definitions have
different number of parameters. This is another kind of method of
overloading with varying lengths of parameters.
Fig. 14.2: Method Overloading with Parameters of Varying Number of
Parameters
Another type of implementation of method overloading is done by varying
the number of parameters. Two different functions with the same method
name but different lengths of parameters form the latter kind of method
overloading. The corresponding matching function definition is used
depending on the count of the parameters in the method call statements.
Unfortunately, Python does not support the varying length of parameter
based method overloading. In the case of the implementation of method
overloading based on variable parameter length, the latest defined function
is taken into consideration. The previously defined functions do become
invalid. The following example shows what happens when the programmer
tries varying parameter length based method overloading.
Example 2: Trying the execution of the method overloading with
varying lengths of the parameters.
The class SurfaceArea is defined with two methods of the same name, area
finder but variable parameters as shown below.
On execution of the above code, two methods with the same name,
areaFinder and with different parameters are defined. Now, an object obj is
created for the class SurfaceArea. Then the method is called with the
parameter 8, as shown below.
The above code throws an error while calling the method areaFinder
because of "2 required positional arguments". This shows that the latter
definition of the method overwrites the method defined first.
Now the method is called with the three parameters, 8, 3, and 10, as shown
below.
Execution of the above code, "obj.areaFinder(8,3,10)" shows the output of
268, which is the expected output. Hence, the latest method definition,
areaFinder is executed and the previous definitions are not considered.
14.1.2 Operator Overloading
Operator overloading is an essential process in OOP, which adds multiple
functions to the available operators. Operator overloading is the process of
extending the predefined function of an operator to user defined functions.
For example, the operator + is used to add two integers, join two strings,
and merge two lists. The operator '+' is used for multiple purposes and is
thus called operator overloading.
Example 3: Illustration for operator overloading of operator "+".
Writing a Python Program for adding a date with the number of days and
finding the resulting date using the concept of operator overloading. For
example, if 2 is added with the date "14-July-2022", then "16-July-2022"
should be the result.
For manipulating date and time, datetime module is used. The datetime
module gives flexibility in effectively handling date and time. The module
uses the date method for converting a sequence of inputs into a date. The
timedelta method is used to do arithmetics such as adding and subtracting
the days, months, and years with another date. So, the date function and
timedelta function are imported from the package datetime as follows.
The Class named DateDB is defined with the constructor that receives the
year, month, and day as arguments and stores that as the instance variables.
The method __add__ is defined by adding the date. The day, month, and
year stored in the variable are converted to the date format using the date
method and the addition of date with days count using the timedelta method
where parameter days is assigned with number_of_days parameter. The
__add__ method is called the magic method, which is used for overloading
the operator "+" (Magic method is defined in detail in the upcoming text).
The __add__ is invoked when the "+" operator is used along with the
objects of the class as the first operand. Any kind of functionality can be
added inside the method __add__, and it will be executed when it is called
using the operator "+".
An object, d1, is created for the DateDB class with year, month, and days as
2022, 7, and 14, respectively.
Now, the date stored in the object d1 is to be added with the value 2 using
the operator "+" as follows, and the resulting date is printed as follows.
The "date1 = d1 + 2" is executed where d1 is the object of class DateDB,
and 2 is the numeric constant with the "+" operator in between them. Since
the "__add__" method is defined in the class DateDB and the object d1 is
placed as one of the operands for the operator "+", the "__add__" method is
called, and the operand which is placed next to the operator is passed as a
parameter. Then, the method is executed, and the date where the number is
added with the date that is stored in the object d1. Here, the date stored in
d1 is 14 July, 2022 and it is added with 2 days and the result, 16 July 2022,
is returned. Apart from the numeric addition and the addition between two
similar data structures using the "+" operator, the date is added with the
number of days using the same operator. Thus, it is called operator
overloading.
Magic Method
Magic methods are the methods used for operator overloading, which are
defined as the method inside a class that is invoked when the corresponding
operator is used. Magic methods are also called special functions. There are
different kinds of operator in Python, and for each operator, there are
unique magic methods for overloading them. Whenever the operator is used
with the first operand as the object, the corresponding magic method in its
object is called. The list of operators and their corresponding magic
methods are listed in the table below. While overloading an operator, the
corresponding magic method should be defined with corresponding
parameters and functionality in the corresponding class.
Binary Arithmetic Operators
Comparison Operator
Assignment Operators
Unary Operators
Let us have a few examples of commonly used overloading operators in
detail.
Example 4: Overloading the Binary Operator "-".
Binary operator "-" is commonly used for subtraction operations between
numericals. For example, the operator "-" is now overloaded with the
function of subtracting two different dates, and the difference number is
displayed using operator overloading.
In the above example, DataDB class is created with attributes of 'year',
'month', and 'day', which are initialized using the constructor. Then the
subtraction '-' operator is overloaded using the magic method, "__sub__()".
Two objects are created for the "DateDB" class with values of "1998, 5, 2"
and "2022, 5, 3", representing "2 May 1998" and "3 May 199,8",
respectively. When the statement "diff = date2 – date1" is executed,
"__sub__()" of date2 object is called and the object date1 is passed as
parameter for "__sub__()". Note that once the magic method "__sub__" of
the first operand date2 is called, the second operand date1 is passed as a
parameter to the method. After calling "__sub__()", the date that is stored in
two different objects is subtracted, and the difference date, 8767 days, is
returned.
Example 5: Overloading Binary Exponentiation Operator "**".
As seen in Chapter 3, the binary power Operator "**" is used to find the
power of a number. The "a**b" in Python is mathematically represented as
"ab". For overloading the operator, Find the power of a complex number
with the absolute value of another complex number. For example, “1 + 1j)
** | should execute “(1+1j)**2”, where “
”. The “(1+1j)**2” results in following
steps.
(1+1j)**2=(1+1j)^2 = (1+1j) (1+1j) = 1 + 1j + 1j -1 = 2 j
The following code implements this computation.
In the above example, a class ComplexNo is defined with r and i as the
parameters for the constructors, which are stored in the attributes real and
img of the object, respectively. Then the magic method "__pow__" is
defined for overloading the "**" operator, in which the two complex
numbers are created from the object (From the first operand) and the second
operand and stored in c1 and c2, respectively. Then, the statement "result =
c1 ** abs(c2)" evaluates the desired function, and the evaluated value is
returned. In the example, the complex1 object is created with 1 and 1 as
parameters corresponding to "1 + 1j", and another object, complex2, is with
2 and 1 corresponding to "√2+√2 j". While executing "result = complex1 **
complex2", the magic method "__pow__" of the object complex1 is
invoked, and complex2 is passed as a parameter. The "__pow__" method
returns "2j" as the evaluated value.
Example 6: Overloading binary division "/" operator.
In Python, the binary division "/" operator divides two numbers, which
could be integer or float, and returns the quotient as the float. The division
operator "/" demands that the two operands should be in the same data type.
Let us see an example of dividing all the elements in the list with a number
by overloading the "/" operator.
In the above example, the class ListDivision is defined, and in the
constructor, a list is received as the parameter and stored in the attribute, l,
in the object. Then, the magic method "__truediv__" is defined for
overloading the "/" operator. Inside "__truediv__", using the for loop, the
elements in the list are divided by the number, which is passed as a
parameter. A list with 2,4,6, and 8 is created, and an object, l1, is created for
the ListDivision Class. When the statement, result=l1/2 is executed, the
"__truediv__" is invoked, and the overloaded functionalities are performed
and "[1.0, 2.0, 3.0, 4.0]" where the elements in the given list are divided by
2 and the list is returned.
14.2 Method Overriding
Method overriding is the process of defining a method that is already
defined in their parents' class by modifying the properties of predecessors.
Method overriding is an ability of any OOP language that allows a child
class to provide a specific implementation of a method that is already
provided by one of its parent classes or superclasses. A child class inherited
from parent class(es) needs to possess different properties for the methods
with the same name in the parent class. The property of showing different
characteristics in the child class and parent class is necessary for
implementing complex problems.
When a method in a child class or subclass is said to be overridden, then the
method in the subclass has the same name, same parameters, and same
return type as a method in its superclass. Either the method in the subclass
or parent class can be accessed using the variable, depending on the usage
of the class. If an object of the subclass is used, then the method in the
subclass is executed.
In Fig. 14.3, two classes namely Xth Standard Student and XIIth Standard
Student are derived from the single parent class, which is the Student Class.
The parent class Student" possesses a method called
"total_mark_calculation()" in it, and a similar method is shown in the
children classes also. In contrast, the characteristic of the method is
different in the class "Xth Standard Student" and "XIIth Standard Student"
in which "Xth standard student" involves only five subjects whereas the
"XIIth standard student" needs six subjects. So, the overridden method is
"total_mark_calculation()".
Fig. 14.3: Method Overriding in Student Mark Calculation
This section also discusses how the method overriding is performed in
various inheritances, as discussed in the previous chapter.
Method Overriding in Multiple Inheritance
Multiple inheritance is the process of deriving a class from more than one
class. This section discusses the method overriding in multiple inheritance.
Let us consider the following example.
Example 7: Illustration for method overriding in multiple inheritance.
Class A is defined with the method printMessage as follows.
Class B is defined as follows with the methods PrintMessage and show as
seen for class A (with different functionalities inside).
Class C is derived from class A and class B. Thus class C consists of two
parent classes, so it comes under multiple inheritance. Class C has the
method with the same name as seen in before classes, printMessage. Class
C is defined as follows.
An object for class C is created, and the method show and printMessage are
invoked.
One could observe that printMessage from both parent classes A and B is
overridden in Child class C. An object, obj, is created for class C, and then
the "show" method in parent class B is called, and the expected output,
"Show Message from Class B", is printed, and while calling the
"printMessage" method, the child class method is printed. In the case of
inheritance, as discussed in the previous chapter, method resolution is done
by the Python interpreter while calling ambiguous methods.
Example 8: Illustration for Method overriding in multilevel
inheritance.
Multilevel inheritance is the process of deriving a class from another
derived class. The following example shows how the method overriding is
implemented in multilevel inheritance.
In the above example, class Parent is defined with the method "display,"
and another class Child is derived from Parent with the method show.
Another class, GrandChild is derived from the derived class Child with the
method "show". One could notice that the method "show" is overridden by
the class GrandChild. An object, obj, is created for GrandChild, the method
"show" is invoked, and "This is show function from GrandChild Class" is
printed. Then display method is invoked through the object, obj, and "This
is Display Function from Parent Class" is printed as the output.
14.3 class Method and static Method
The methods in a class are associated with the objects created for it. For
invoking the methods defined inside the method, the presence of an
instance is necessary. The classmethod is a method that is also defined
inside the class but does not need any object to invoke it. A classmethod can
be invoked using the name of the class. For making a method to a class
method, the function decorator "classmethod" receives the class as the
implicit first argument using "cls" argument. This is similar to the methods
receiving object as the first argument using the "self" argument. The syntax
for using classmethod is shown below.
As shown in the above syntax, any method can be made into classmethod
by adding "@classmethod" decorator before the definition of the method.
Classmethod is bound to class but not to objects.
Example 9: Illustration for classmethod
Creating a class "Person" with attributes of name and age. The code is
shown below.
In the above code, the constructor consists of assigning values for attributes
"name" and "age". The function to convert year to age is defined and
converted to classmethod using "@classmethod". The cls() function in the
return statement of the class method from_birth_year invokes the
constructor of the class Person by creating the object. Here, the object name
is taken care by the interpreter and the created object is returned. The
statement "cls(name, date.today().year-year)" creates an object for the class
(which is received as the first argument of the classmethod), and its first
argument corresponds to the name and the second attribute finds the
difference between the current year and the year which is passed on to the
method. The class method returns a cls() method, which is an object of the
class.
Now an object is created for the class Person and called with two
parameters, "Mayank" and 21. The instance variables name and age are
assigned with the values, respectively.
The above code can also be done using the classmethod from_birth_year as
follows.
In the above code, the object for the class Person is not created by the usual
statement as "Person(arg1, arg2)". Here, the method in the class is directly
called using the class name alone (without any object), i.e.,
Person.from_birth_year("Mayank",1998), where the Person is the class
name and from_birth_year is the name of the classmethod. Defining a
method as classmethod enables us to call the method without the object of
the class. In this case, the class method from_birth_year returns an object of
the cls() method, which is the object of the class Person where the instance
variables name and age are assigned with the given value.
Static Method
Static method is similar to a class method, which can be accessed without
an object. A static method is accessible to every object of a class, but
methods defined in an instance are only able to be accessed by that object
of a class. Static methods are not allowed to access the state of the objects
(attributes of the class). The syntax for the static method is as follows.
In the above syntax, the method is converted to static method by having
"@staticmethod" before the method definition. Also, "self" is not associated
with the argument list of the static method.
Example 10: Check whether the given person is an adult (> 18) or not
using static method.
Class Person is defined as follows, with the method from_birth_year as
classmethod and the method is_adult as staticmethod.
In the above code, the "is_adult()" method is defined and converted to a
static method that returns true, if the given argument is greater than 18 or
returns false. Note that the function "is_adult()" does not have any "self"
argument, and also, the function is not defined in such a way that it alters
the state of the object.
Now the object is created for the class Person as follows.
In the above method, the created object with 'Mayank' and 21 shows 'True'
on calling with the method is_adult. Unlike the above, the method is_adult
can be invoked without creating an object.
The above code shows that the method is_adult is called without any object
creation.
The differences between classmethod and staticmethod are shown below.
14.4 Abstract Base Class (ABC)
Abstraction is the process of hiding unwanted information from accessing
the data, and abstract class is considered a blueprint for other classes. It
allows the creation of a set of methods that must be created within any child
classes built from the abstract class. A class that contains one or more
abstract methods is called an abstract class. An abstract method is a class
method that has the declaration in the abstract class but is defined in its
derived class.
If a class is defined as the ABC, no objects can be created for the
corresponding class. An ABC can be inherited from other classes but can
not be a standalone class, and also object can not be created for an ABC.
The properties of the abstract class can be accessed only by inheriting the
class. By defining an abstract class, one can define a common application
program interface (API) for a set of subclasses. In Python, an abstract class
is not available by default. Python uses abstract base class (ABC) module
for implementing abstract classes. Using the ABC module, a method is
made abstract when decorator @abstractmethod is used. The ABC module
works by decorating methods of the base class with decorator
@abstractmethod and then registering concrete classes as implementations
of the abstract base.
Example 11: Illustration for Abstract Base Class (ABC).
Let's have an example of declaring and printing the sides of the polynomials
with the concept of an abstract class.
In the above class Polygon, the class is made to abstract base class by
adding the parameter ABC to the class definition. The method noofsides is
made as abstract method by adding abstractmethod decorator above the
method.
Another class, Triangle, is inherited from the abstract class Polygon as
follows.
In the above code, the method noofsides is defined in the class Triangle
with the functionalities that are applicable to the class Triangle.
Another class Pentagon and class Hexagon are inherited from the abstract
base class Polygon, and the method noofsides is defined inside the classes
as shown below.
Objects for the classes Triangle, Quadrilateral, Pentagon, and Hexagon are
created as follows.
In the above code execution, it could be noted that the method noofsides
execute according to their individual properties. The abstract method
noofsides in the abstract base class Polygon is like a blueprint.
14.5 Meta Class
Meta programming is another programming paradigm where a computer
program can treat another program as its data where reading, generating,
analyzing, and transforming other programs are possible. Meta
programming also involves modifying the program itself. The primary idea
of metaprogramming is to address the new situation without going for the
recompilation of data.
As part of metaprogramming, a metaclass is one of the most important
concepts in Python. A Class in Python defines the functionality of its
objects using attributes and methods. In contrast, a meta class defines the
functionality of the classes, whereas the metaclass treats the other classes as
their data. In OOPs, instances of meta classes are classes. In previous
versions of Python, on executing the command "type(class_object)", "<type
'instance'>" was the output. In the latest versions of Python, the output is "
<class '__main__.Class_Name'>". In the latest versions, the concepts of
class and type are unified, and the output for "type(class_object)" and
"class_object.__class__" are the same.
From the above program, one can notice that the results for "__class__" and
"type()" are the same. For creating a metaclass for a class, the users are
expected to implement the following things.
1. A separate class, which is called a metaclass, should be defined before
defining the normal classes.
2. The metaclass could have an argument called 'type'. This is an optional
procedure to follow. The 'type' denotes it as meta class.
3. When deriving a class from metaclass, the "metaclass" argument should
be defined in the derived class.
Example 12: Illustration for Meta Class.
In the above program, a class name "Meta" (could be other names also)
with the argument of "type" is defined using the code "class Meta (type)".
The metaclass consists of a constructor that holds an attribute of attr with
the value of 100. Then, class X and class Y are derived from the metaclass
"Meta" with the "metaclass = Meta" argument. While executing "X.attr", it
displays the value of 100. The following things can be observed from the
above program.
1. The "Meta" class acts as a Template for the Classes X and Y.
2. Being the template, the attributes are also reflected in classes X and Y,
which is why the attributes are called without declaring any object for
the class.
Summary
1. Method overloading is the process of adding different functionalities to a
method of a class by altering the parameters of the methods.
2. The "dispatch" decorator (@dispatch) is used for implementing the
method overloading with varying data types of parameters.
3. Operator overloading is the process of adding multiple functions to the
operators (in addition to the other prevailing functionalities).
4. Magic methods are the methods used for operator overloading which are
defined as the method inside a class that is invoked when the
corresponding operator is used.
5. While overloading an operator, the magic method of the first operand is
invoked, and the operands (if any) are passed as parameters.
6. Method overriding is an ability of any OOP language that allows a child
class to provide a specific implementation of a method that is already
provided by one of its parent classes or superclasses.
7. When the overridden function is called using the object of the child
class, then the function in the sub-class is called.
8. The overridden function in the parent class is called using objects of the
parent class.
9. Garbage collection is a memory recovery feature that is used in many
programming languages to free-up memory space that has been allocated
to the variables or objects, which are no longer demanded by the
program.
10. For controlling the process of garbage collection, the module gc is used
in Python.
11. Abstraction is the process of hiding unwanted information from
accessing the data and abstract class is considered a blueprint for other
classes.
12. An abstract method is a method that has a declaration but does not have
an implementation.
13. Python uses abstract base class (ABC) module for implementing abstract
classes.
14. Meta programming is another programming paradigm where a computer
program can treat another program as its data where reading, generating,
analyzing, and transforming other programs are possible.
15. A meta class treats the other classes as their input.
16. Instances of meta classes are classes.
Glossary
Overloading: the process of adding additional functionalities for
elements of objects or the object itself.
Method overloading: the process of adding different functionalities to a
method of a class by altering the parameters of the methods.
Operator overloading: an important process in OOP which adds multiple
functions to the operators.
Magic method: in Python, magic methods are the methods used for
operator overloading, which are defined as the method inside a class that
is invoked when the corresponding operator is used.
Method overriding: the process of defining a method, which is already
defined in its parent class for modifying the properties of predecessors.
Garbage collection: a memory recovery feature, which is used in many
programming languages to free-up memory space that has been allocated
to the variables or objects, which are no longer demanded by the
program.
Data abstraction: the process of hiding unwanted information from
accessing the data, and abstract class is considered a blueprint for other
classes.
Meta programming: another programming paradigm where a computer
program has the ability to treat another program as its data where
reading, generating, analyzing, and transforming other programs are
possible.
Conceptual Questions
1. Explain method overloading and its types.
2. Explain how dispatch decorator is used with an example.
3. What happens when a method is overloaded with varying parameter
lengths in Python?
4. Define operator overloading.
5. What is called a magic function in Python?
6. List out the binary operators and their corresponding magic function.
7. What is meant by method overriding?
8. Explain how method overriding is performed in the multilevel
inheritance with an example.
9. Explain the abstract class in detail with an example.
10. What is meant by classmethod in Python?
11. How static methods are implemented in Python? Explain with examples.
12. Differentiate classmethod and staticmethod in Python.
Programming Exercise
1. Write a program to find the increment of all the elements in a list with
value 2 by overloading the unary "+" operator in Python.
2. Using the concept of operator overloading, implement the operator "*"
to perform matrix multiplication for any 3X3 matrices in Python where
matrices should be stored in list format.
3. Using abstract class, implement matrix determinant with two abstract
methods, threeCrossThreeDeterminants and twoCrossTwo Determinants.
4. Write a Python program to overload the relation operator, less than or
equal to the compare two lists, namely, list1 and list2, and print the
number of elements in list1 that are lesser than or equal to the elements
in list2.
5. Write a Python program to print the school name, class, and section
using the method overriding with the help of multilevel inheritance.
6. Write a Python program to evaluate whether the student is overall
passing or not where it has a hierarchy of classes to store the students
and the marks. The passing criteria for a subject are that the student
should have obtained 50 marks. If the student is passed in all subjects,
then he is overall passed or else not.
7. Write a Python program for user management for a school management
system using abstract base class (ABC).
8. Write a Python program to execute the followings.
1. Create a template for the users in a school management system.
2. Derive two classes for teachers and students from the template
created in (a).
3. Store the values in objects of the derived classes.
OceanofPDF.com
“Today, most software exists, not to solve a problem, but to interface with
other software.”
— IO Angell
CHAPTER 15
Modules in Python
Python supports modules and packages. Modules and packages are useful in
the construction of large Python programs. Packages and modules help to
develop Python programs that are reusable and effective. By the end of this
chapter, the readers will understand the following concepts.
Learning Objectives
At the end of this chapter, the readers will be able to learn the following
topics.
Understanding the modules in Python.
Learning methods to import the modules into the Python environment.
Learning various built-in packages, such as sys, os, and math.
Understanding the usage and utilities of command line argument.
Know the basics of packages.
15.1 Introduction to Modules and Packages in Python
A library is a collection of related functions. As discussed in Chapter 6, a
function is a collection of Python statements to accomplish a specific task.
In Python, a library is a set of packages, and every package may have one
or more modules. Each module can have many functions. The reason for
this hierarchical design is to extend the functionality of the Python
language. A module is like a library. Like a library, a module greatly
extends the functionalities of Python.
A module is a simple Python file. This file can be imported into any other
Python program. A module can have
1. Class and their definitions and their corresponding objects.
2. Variables and their values, which in turn are treated as constants after the
module is imported to another file.
A single module can be separated into multiple files. Interactions among the
files can be handled by relative importing techniques of the Python
interpreter. As a Python file, a module file name is also suffixed with .py. In
other words, it can be imported from another file either completely or with
specific definitions available in it. The modules in Python can be stored in
the default directory or the current working directory and the details about
the location of modules are available in the database.
A package is a simple directory that contains modules. A directory can have
many modules and __init__ python file. This file identifies the presence of
a package to the Python interpreter. A package may have many sub-
packages as well. As modules are more, it is quite possible that many
modules share the same name. So, one needs to identify the module from
where the variables and functions need to be imported.
What are the advantages of modules and packages? The advantages of
modules and packages are listed below.
1. Reusability of the code.
2. Simplicity as it reduces the length of the code. This helps in testing and
maintenance.
3. Scoping rules. A module creates a separate namespace, and a namespace
is one place where all the variables are maintained. This avoids the name
confusion of many variables that are present.
Therefore, there is a necessity to understand the modules and packages.
15.2 Creation of a Module
Modules in Python are the collection of functions as single or multiple files.
The modules are created as a usual Python file with the ".py" extension. As
discussed above, a module can be imported into another Python program.
Example 1: Illustrating the creation of module to find the factorial and
permutation. (listing1.py) (store the code as factorial.py)
The factorial of a number n is represented mathematically as follows.
When the 0! = 1 (Where n = 0) and the value of n can not be negative.
The permutation of two numbers n and r defines the number of
arrangements of n objects in r orders. Mathematically, it is expressed as
follows.
Consider creating a module for finding the factorial for a number and
permutation. The Python script for the example is shown below. The
following Python script is saved as factorial.py.
In the above code, a function named factorialForNumber is defined in the
pattern of recursive function as seen in Chapter 11, Advanced Functions.
Notes
To use the modules in another Python program, the module's location needs
to be considered. The module should either be located in the location of
PYTHONHOME or in the current working directory. This can be obtained
as follows.
PYTHONHOME is used to define directories that are part of the Python
installation. If this environment variable is not defined, then a standard
directory structure is used. For
Windows, the standard location is based on the directory into which
Python is installed.
For most Linux environments, Python is installed under /usr/local, and
the libraries can be found there.
For Mac OS, the home directory is under
/Library/Frameworks/Python.framework.
If the Python module factorial.py is stored in the suitable directory, then it
can be imported to the command line interpreter or another corresponding
Python file. Considering that the factorial.py is stored in the current
working directory. Either the complete module or the specific functions in
the module can be imported. The following section describes how to import
a module to the current namespace.
15.3 Importing Modules
The process of importing can be performed for the following activities.
1. Using importing operation, everything inside the module can be
imported.
2. Certain named variables and certain functions can be imported from a
module.
The command import is used for importing the modules. Continuing the
above example, the entire module factorial is imported using the import
keyword. The syntax for importing the entire module is shown below.
In the above syntax, the moduleName which is the file name in which the
modules are used import keyword is used. Once the module is imported, the
functions in the modules are accessed using "." operator which is similar to
accessing the methods in a class.
The import statement performs the following two activities.
1. Find the given module in the directory and follow by loading the module
to the environment and initializing the module. The process of
initialization is not mandatory for all packages and modules.
2. Registering the imported modules and packages in the local namespace
for building the scope.
If the module is imported successfully, it will be made available in the local
namespace in one of the three ways.
If no other name is specified, and the module being imported is a top-
level module, the module's name is bound in the local namespace as a
reference to the imported module. This is the scenario seen in previous
examples of the chapter.
Creating alias: if the module name is followed by the keyword as then the
name following the keyword as is bound directly to the imported module.
In other words, the module name can be replaced with the word following
the keyword as.
Example 2: Illustration for importing with creating an alias.
This program shows how to import the module factorial defined in Example
1.
While importing the factorial module, if the import statement is modified as
"import factorial as fc", then the factorial module would be accessed only
by fc. Now, fc has become the alias for the word factorial. While accessing
the factorialForNumber function, fc.factorialForNumber is used. One could
notice that fc is used instead of factorial, whereas before the statement
looked like factorial.factorialForNumber. The following code shows the
same in detail.
Example 3: Illustrating import using alias in Python.
Importing math module which is discussed in Chapter 3 using alias mt.
To import the math package with the alias of mt, the statement "import math
as mt" is used. The function and variables associated with the math package
can be accessed using the alias mt.
In the above example, the math package is imported with the alias of mt and
the pi variable in the package is accessed using mt.pi. In Chapter 3, it is
seen that the pi variable is accessed using math.pi.
If the module being imported is not a top-level module, then the name of
the top-level package that contains the module is bound in the local
namespace as a reference to the top-level package. The imported module
must be accessed using its full qualified name rather than directly. This is
explained in the importing submodules of a module.
Importing Submodules of a Module
The scope of a module is larger than a simple function and a single class.
For practical purposes, a module could consist of more than one function.
In such cases, Python allows us to import specific submodules using the
following syntax.
The above syntax shows how to import specific functions and submodules
from a module. The system consists of from keyword followed by the
module name import keyword, and the submodule names. Once the
submodule is imported using the above syntax, the usage of the dot (.)
operator is unnecessary. In the above syntax, the "function1" can be altered
with any components in the modules such as objects, classes, variables and
so on.
Example 4: Illustration for importing function from a module.
Considering the above factorial module, then there are two functions
available, namely, factorialForNumber and permutate. The function
factorialForNumber can be imported using the following statement
according to the above syntax.
On executing the above line, the factorialForNumber function is brought to
the namespace and can be invoked without using the name of the module,
whereas the other function, namely, permutate, is not imported to the
namespace.
Let us consider what happens when this command is executed.
Where the factorial is the name of the file in which the functions
factorialForNumber and permutate are defined. One could identify the
functions and other variables available in the module using dir() function.
The two functions factorial and permutate in the module factorial are
imported and other elements in the list are default objects associated with
any imported modules and their uses are explained below.
Table 15.1: Attributes associated with the imported module name
The following example shows how the factorial module is imported and the
attributes that are associated with it.
Example 5: Illustration for attributes associated with the imported
module.
To import the module factorial which is stored as factorial.py in the current
working directory, "import factorial " is executed and the submodules in the
modules can be accessed using "." (dot) operator. "dir(factorial)" displays
the attributes and functions that are associated with the factorial module.
The doc strings of the factorial module are displayed using
factorial.__doc__, where the doc strings are stored in between triple quotes
(''' ''') at the beginning of the factorial module. The directory of the compiled
module is stored in factorial.__cached__. The object of the loader of the
factorial module is stored in factorial.__loader__. The name of the module
which is the file name where the module is present is displayed using
factorial.__name__. As mentioned in the above Table 15.1, the name of the
package where the factorial module is a part of it is stored in
factorial.__package__. Since the factorial package is not a part of any
package, an empty string is stored for the factorial module. The
specification of the factorial module according to the built-in condition of
Python is stored in factorial.__spec__.
This could be visualized by executing dir() in the interpreter. On executing
the command, the following output is displayed.
In the above output, one could notice that factorialForNumber function is
added in the list, which means that the function could be invoked directly
without any additional calling arguments, such as module name and dot
operator. Also, another function permutate is not imported and also, the
module is not imported. The usage of such an imported function is shown
below.
In the above example, after importing the factorialForNumber function
from the module factorial, factorial for 10 is found by directly calling the
function with parameter 10, which is implemented using "tenFactorial =
factorialForNumber(10)" thus resulting in 3628800.
Types of Imports
There are two types of imports in Python, namely, absolute import and
relative import. Absolute import involves importing the entire module from
its root folder and also demands the programmer to utilize the properties of
the modules. The process and methods in absolute import as discussed in
importing the entire module in the previous section.
Relative import specifies an object or module imported from its current
working directory, that is, the location where the import statement resides.
There are two types of relative imports.
1. Implicit relative imports (depreciated by Python 3.x)
2. Explicit relative imports
Since implicit relative imports do not exist in the latest version of Python, it
is not discussed here.
Explicit Relative Imports
Explicit relative import is a type of importing a module based on the current
location of the module or the program that will be present. It is commonly
used while creating new packages and modules.
Consider the following example of packages and modules that are
structured as in Fig. 15.1.
Fig. 15.1: Package and Module Structure for Example of Explicit
Relative Imports
In Fig. 15.1, there are two packages in the project folder, namely, Package1
and Package 2. Inside Package1, there are two modules and inside
Package2 there are two modules and one subpackage, SubPackage1 is also
present. Inside SubPackage1, there is another module. To represent the
hierarchy of the directory, the dot '.' operator is used, and the usage is
shown in Table 15.2.
Table 15.2: Usage of Dot "." Operator in Relative Import
Similar to the usage of dot operator as shown in Table 15.2, the recursive
usage of dot operator mentions previous directories to the current directory.
Consider the following scenarios for better understanding.
Scenario 1: Considering that the programmer is typing module2.py and
module1 has to be imported. Conventionally, this could be done in one of
the following two ways.
1. Implementing the import keyword and using the dot (".") operator is
used for traversal.
As the code shown above, after importing the Project and module3 and
its submodules are accessed by "Project.Package1.module3".
2. Using from keyword and importing the modules.
As the code shown above, the dot (".") operator is used for traversing as
"Project.Package1" and the module is imported. Instead of using the
above method, using the explicit relative importing methodology, the
following command can also be executed to pursue the above result.
In the above code, since module1 is present in the same hierarchy
(present in the current folder), ".module1" is used, and using import, the
required submodule1 is imported.
Scenario 2: Considering that the programmer is typing module5.py and
needs to access module4 in the before hierarchy (in Parent Folder).
Conventionally, this is done by importing the Project and traversing through
the directories using the "." Operators as shown below.
Or
Using the explicit relative import, the dot "." operator is used to
representing the directory level. As discussed above, the single dot "."
operator is used to representing the current folder and ".." operator is used
to represent the parent directory. So, the above conventional code can be
alternatively implemented by the following code.
In the above code, "..module4" represents that module4 is present in the
parent folder to the current location.
importlib Module
Apart from the usual, importlib.import_module() and built-in __import__()
can be used for importing a module or package or library. The importlib
modules provide an API for interacting with the import system of Python. A
direct call to __import__ performs only the module search and if the
module is found, the module is compiled. The drawbacks in using the
components of importing system of Python are as follows.
__import__() performs only the module search and if the module is
found, the compilation process is executed. The name binding operation
is not performed while using __import__().
Once the module is compiled, the recent changes in the modules, which
are done after compiling the module will not be reflected when
importing the modules.
__import__() Function
The syntax for __import__() function is given below.
In the above syntax, five parameters are needed (among them four
parameters are optional) while implementing the __import__ function for
importing a module and the parameters are tabulated below with its
definition in Table 15.3.
Table 15.3: Import Function Flags
Example 6: Illustration for importing a module using importlib.
In the above code, the importlib module is imported using import importlib;
then the factorial.py is imported using abc =
importlib.__import__("factorial"). Here, the 'abc' is as similar to 'abc' in
"import factorial as abc". Since the factorial.py is located in the current
working directory, the file's name is sufficient for importing the module.
The imported module is stored in the variable abc in the namespace of the
current environment. The submodules and functions associated with the
factorial module is present in the variable abc. For accessing the permutate
function, the statement abc.permutate(…) can be used.
15.4 Uses of Standard Modules in Python
When a Python interpreter shell is opened, some built-in functions are
loaded by default. One could notice that, the functions, such as print() and
input(), and number conversion functions, such as int(), float(), and
complex() are available by default and no need for any import operations.
Adding to these built-in functions, many pre-defined functions are also
available as part of the modules bundled with Python distributions, which
are called built-in modules. These are also called standard modules in
Python, and many modules in them are written in C Language for OS
related operations like disc input and output and OS management. There
Also, there are files in the standard library that are written in Python and
stored with.py files.
Among the available standard modules in Python, a few modules are
considered important modules for a Python programmer, and those are
discussed in detail in the upcoming sections.
15.4.1 Math and Sympy Packages
In Chapter 3, the math package is a built-in package widely used for math
related operations. There are the following things to be noticed in Python
math package.
1. math.pi, a constant that is available in the package, is a good
approximation of the mathematical but will lead to some errors when
math.pi is used with other functions that are available in the package.
The following examples discuss the case.
For example, the value of is 0 but the equivalent Python statement using
math package math.sin(math.pi), which results in the closest
approximated value not exact zero , but it is universal truth that sin π = 0.
Similarly, to the above case, math.cos(math.pi/2) which is equivalent to
and its value is 0 but the Python statement gives the result with errors
because of the approximation on math.pi, but it is universal truth that
cos(π/2) = 0.
Contradictory to the above examples, the function math.degrees() which
converts the given radian to the angle, shows the correct result. For
example, the degree equivalent to math.pi which is and the following
code shows the same execution.
Similarly, is equivalent to the Python statement math.pi/2 in
math.degree() function represents the 90°.
2. Similarly, the irrational numbers such as are approximated in a good
way but those approximations leave some errors while using them. For
example, using in math.atan() function and the execution is shown in
the below code.
math.atan(math.sqrt(3)) is equivalent to whose value is 60° but the
above code, gives the result as 59.9999999999o. This is because the
approximation made for .
3. math.remainder() function does not obey the actual remainder
perspective in mathematics where a remainder should lie between 0 and
less than divisor and a remainder should not be remainder. Consider the
following example.
In the above code, math.remainder(7,3) yields the remainder 1, which is
correct with respect to the mathematical law of remainder whereas for
the statement math.remainder(8,3) gives -1 as the result because
according to the definition of the remainder(a, b) function of the package
where a is subtracted from the nearest multiples of b. The nearest
multiple of 3 to the number 8 is 9 and so, 8-9 = -1. math.remainder(9,3)
results in 0 since 9 is the perfect multiple of 3.
To upcoming the above mentioned issues, various upgraded packages are
used. The situations that are presented are based on the before introduced
packages, but practical scenarios demand greater perspectives. For
addressing the above mentioned issues 1 and 2 issues of math packages,
sympy is widely used. There are several alternatives for the built-in math
packages and the programmer should use the respective package based on
various perspectives such as execution time, complexities, and other
comfortability factors. The following code example shows the usage of
sympy.
Example 7: Illustration for the usage of sympy.
The sympy package is imported using the statement import sympy and the
statement sympy.sin(sympy.pi) gives the result as 0 where the statement is
equivalent to the mathematical statement but the math package shows the
different results, which were seen in the previous example. The math.pi
shows the approximated value as 3.141592653589793, whereas the sympy
gives a special class, which is sympy.core.numbers.Pi shown in the above
code.
15.4.2 os Module
The os module provides a handful way of utilizing the operating system
based functionalities, such as file descriptors handling, process handling,
path handling, and so on. The most frequently used and important functions
that are available in the os module are discussed below with suitable
examples.
General os functions
os.name(): os.name() returns the name of the currently working operating
system. In os module, not all operating systems are registered. The
registered candidates in os module are, nt (New Technology), posix, and
java. If a programmer is working in the windows operating system, the
output of the function is "nt".
The above code is executed in a windows environment, so nt is returned as
output.
Process Parameters
os.envrion: os.environ is a class object that consists of all the locations and
is stored in the system environment variables that hold the variable name
and also its values. The value of particular variables is extracted using the
following syntax.
In the above syntax, the variable_name is given as an indexing parameter
(similar to list). The above syntax displays the value stored in the given
variable name in string format. For example, to print the value stored in the
"NUMBER_OF_PROCESSORS" environment variable the statement
os.environ['NUMBER_OF_PROCESSORS'] is executed.
In the above execution, the value stored in
"NUMBER_OF_PROCESSORS" is printed which is 4. The value can be
crosschecked in the environment variables dialogue box in Linux or
Windows.
Files and Directories
os.getcwd(): os.getcwd() returns the current working directory in the string
format. On executing os.getcwd(), the following output is obtained.
In the above code, on executing the os.getcwd() displays the current
working directory of the opened interpreter.
os.chdir("directory"): os.chdir("directory") is used to change the current
working directory of the program to the given directory as the parameter in
string format (within single or double quotes).
In the above example, the current working directory is changed to
"C:\Users\usr\Desktop" using the statement
os.chdir(r"C:\Users\usr\Desktop"). Here, "\\" can be used instead of "\"
because "\" is used for representing the escape sequence, where "\\"
represents "\" and the usage of raw string is not required in this case. To
check the changed directory, again os.getcwd() is used in the above code.
os.chmod(path, mode): os.chmod(path, mode) is used to change the mode
of the path to numeric mode. The parameter path consists of the directory
path or name of the file in the current working directory and the parameter
mode consists of pre-defined access mode constants, which are defined in
stat module and are tabulated below in Table 15.4.
Table 15.4: Stat Modules Constants
os.mkdir("path"): os.mkdir("path") creates a new directory in given path
with numeric mode. If the specified directory is already exists, then
FileExistsError is raised. If the parent directories are not available, then
FileNotFoundError is raised. It is also possible to create temporary
directories, tempfile module is used.
os.scandir("path"): os.scandir("path") returns the iterable scandir object
that holds all the items that are available in the given directory path. The
readers should notice that the scandir objects are iterable but not
subscriptable. While iterating through loops in Python desired actions can
be taken. The entries in the scandir object are nt.DirEntry objects.
os.listdir("path"): os.listdir("path") returns the list that contains the names
of files and folders that are available in the given path. The resulting object
is subscriptable and can be handled like a list as seen in Chapter 7.
os.fchdir(*fd): os.fchdir(*fd) function is used to change the current working
directory to the directory represented by the given file descriptor (fd). A file
descriptor is a small integer value that corresponds to a file or other input or
output streams, such as a pipe or any network sockets.
os.fchmod(*fd, mode): os.fchmod(*fd, mode) converts the mode of the file
given in the file descriptor to the numeric mode. The mode to be changed
can be specified using the second parameter and the values for the
parameter can be one of the entries in the Table 15.5.
os.fspath(*path): os.fspath(*path) returns the file system representation of
the given path.
os.get_exec_path(env = none): os.get_execu_path(env = none) returns the
list of directories that will be searched for a named executable, similar to a
shell, when launching a process. The attribute env, when specified, should
be an environment variable dictionary to look up the PATH in. By default,
when env is None, environ is used.
os.open("path", flags): os.open("path", flag) open the file in the given flag
and returns the file descriptor as the result. The flag parameter in the
function consists of the following Table 15.5.
Table 15.5: Flag Parameters
os.read(fd, n): os.read(fd, n) reads the atmost n bytes from the file
descriptor fd.
os.fdopen(fd, *args, **kwargs): os.fdopen(fd, *args, **kargs) returns an
open file object connected to the file descriptor fd. This is an alias of the
open() function in built-in package and accepts the same argument. The
only difference is that the first argument of fdopen() must always be an
integer.
os.close(fd): os.close(fd) closes the file descriptor fd.
Note: Reading, Opening, and Handling of Files are handled in Chapter 18.
15.4.3 sys Module
The sys module provides system specific parameters and functions that
allow the programmer to access some variables that are maintained by the
interpreter and functions that interact with the interpreter strongly. There are
a lot of functions and attributes associated with the sys module, some
important features that are needed for a programmer are presented below
with examples.
sys.argv: sys.argv is a variable of list data type that stores the command line
arguments that are passed to a module of Python while executing it. The
handling of the command line argument in Python is explained in the
upcoming section.
While executing a code as a script file, the command line arguments are
given as input to the script. The arguments that are given after the name of
the program in the command line shell of the operating system are known
as command line arguments. The general syntax for giving a command line
argument is as follows.
In the above statement (in the Windows Command Prompt), "python3" is
the environment variable and followed by the Python file name (with.py
extension) and then the command line arguments argument1, argument2,
and so on are given.
Python provides various ways of dealing with command line arguments.
Among various ways, using sys.argv is the most commonly used way. The
variable sys.argv is a variable that holds all command line arguments in the
list data structure. The count of elements in the variable sys.argv gives the
number of command line arguments and that can be found by the statement
len(sys.argv). The first element in the variable sys.argv (i.e., sys.argv[0])
gives the name of the Python Script. The following example shows the
usage of sys.argv.
Example 8: Finding the GCD of numbers given as command line
arguments.
In the above example code, for using the command line arguments and gcd
function from the math package, sys and math packages are imported
(Refer lines 2 and 3). In line 3, only gcd function of the math package is
imported using from keyword as seen in earlier this chapter. As discussed
before the total number of arguments is counted by the command
len(sys.argv) and is stored in the variable no_of_arguments as shown in line
6. To print the count of command line arguments a for loop is implemented
across the list sys.argv. As the example intends to print the gcd of the given
numbers and exit the program, a function named exit_function is defined by
printing the "GCD of the given numbers is " then the evaluated GCD value
through string concatenation operator though sys.exit function. Once the
function is executed, the execution of the script is terminated after printing
the given string parameter. For the given list of numbers, the procedure for
finding the GCD is implemented and explained in the given Table 15.6.
Table 15.6: Explanation of Code
The above mentioned code is saved as commandline.py and executed in the
terminal (in case of Linux) or in command prompt (in case of Windows).
The output of the above code is shown in the following table by referring
the cases to above Table 15.7.
Table 15.7: Test Cases
sys.exit(str): sys.exit(str) raises the system exit exception, which makes to
exit the interpreter by printing the given string argument. For example, in
windows using the interpreter from the command prompt, if
sys.exit("Sample Text") is executed, then the interpreter gets exited after
printing the "Sample Text".
Example 9: Illustration for Exiting the interpreter by printing "Exiting
the Interpreter" using the package sys.
In the above code, the Python interpreter is opened using the python
environment variable in the command prompt in windows environment.
Once the interpreter is opened, sys module is imported and the statement
sys.exit("Exiting the Interpreter") is executed. The controller is exits from
the interpreter after printing the text "Exiting the Interpreter".
sys._clear_type_cache(): sys._clear_type_cache() clears the internal type
cache. The type cache is used to speed up attribute and method lookups.
This function is used only to drop unnecessary references during reference
leak debugging.
sys.getrefcount(object): sys.getrefcount(object) returns the reference count
of the object. The count includes the count of temporary references also and
so the count is usually higher than the expectation. For example, consider
getting a reference count for a variable assigned with an integer value.
In the above code, it could be noticed that a variable a is assigned with 10
using a =10, and reference count of the variable is printed using
sys.getrefcount(a) and 12 is the result of the function. The variable a is used
only once but the count is high because the function sys.getrefcount()
includes the count of temporary references also.
sys.getrecursionlimit() and sys.setrecursionlimit(): sys.getrecursionlimit()
gets the recursion limit, which is set by default in the Python interpreter.
This value can be altered by using sys.setrecursionlimit(int) function where
the parameter should be of C int.
Example 10: Illustration for changing the recursion limit of Python.
In the above code, the current recursion count of the interpreter is printed
using the function sys.getrecursioncount() and 1000 is received as an
output. This conveys that the interpreter could go up to the depth of 1000
while solving the recursion problem. This value can be altered using
sys.setrecursionlimit(int) function and it is changed to 12000 by executing
the statement sys.setrecursionlimit(12000). Now the interpreter could
handle the recursion depth of 12000. For rechecking the changed value,
again getreferencecount() function is executed and the altered value 12000
is printed.
Note: The maximum value of the C int is 2,147,483,647. If any value is
given above this value, the interpreter throws an error.
sys.getsizeof(object): sys.getsizeof() returns the size of the given object. It
can be used instead of sizeof() function. For example, getsizeof() function is
executed on a variable assigned with the value of integer datatype.
In the above code, the value 10 is assigned to the variable a and the
statement sys.getsizeof(a) is executed to get the size of the object and it is
returned as 28, which says that the variable a occupies 28 bytes of data and
that is similar to the attribute function __sizeof__().
15.4.4 py_compile
The programmer need not be bothered about the compilation of code,
because whenever a module is imported by another module or another
interpreter environment, the Python script is automatically compiled and
stored in ".pyc" format. The compilation is hidden from the user for a good
reason. Those who are all new to the Python programming environment
wonder sometimes where these ominous files with the.pyc suffix might
come from. If Python has write access for the directory where the Python
program resides, it will store the compiled byte code in a file that ends with
a ".pyc" suffix. The byte code will be produced but discarded when the
program exits. Whenever a Python program is called, Python will check, if
a compiled version with the.pyc suffix exists. This file has to be newer than
the file with the ".pyc" suffix. If such a file exists, Python will load the byte
code, which will speed up the start-up time of the script. If there is no byte
code version, Python will create the byte code before it starts the execution
of the program. Execution of a Python program means the execution of the
byte code on Python.
A Python script is compiled using the py_compile model, either using the
interpreter shell or using the command at shell prompt. Compiled Python
scripts is with the extension of ".pyc". Using the compile function in the
py_compile module, any Python file in the current working directory is
compiled. The syntax for the compile function is as follows.
For example, if the sample.py Python script is to be compiled (assuming
that the sample.py is located in the current working directory), then the
following code shows the execution.
Example 11: Illustration for compiling
In the above code, sample.py is compiled and stored as sample.cpython-
37.pyc in __pycache__ directory.
Compiling Python Script using Shell Prompt (Command prompt or
Terminal)
For compiling the Python scripts outside the interpreter, py_compile is also
used. The syntax for compiling the Python script using the shell prompt is
as follows.
In the above statement, python -m py_compile python_script.py is
responsible for compiling the Python file. For example, if sample.py located
at the current location of the shell needs to be compiled, then the following
code should be executed.
In this case, also, the sample.py is compiled and stored as.pyc file in
__pycache__ directory. All the Python files in the directory can be compiled
by using compileall module. For doing so, enter the following command in
the command prompt or terminal at the desired directory location.
In the above command line, the compileall module is used.
15.5 Packages
A package is a folder that has one or more modules and is differentiated
from other folder by the special method called "__init__.py". This is shown
in Fig. 15.2.
Fig. 15.2: Packages
The differences between packages and modules are as follows as shown in
Table 15.8.
Table 15.8: Differences between Packages and Modules
On many occasions, packages and modules are used interchangeably.
The __init__.py in Package
Packages in Python are always associated with the __init__.py file in the
main directory of the packages and modules. Python provides a comfortable
packaging system, which is simply an extension of the module mechanism
to a directory. The __init__.py file is required to convey the Python
interpreter to treat the directories containing files of packages. Thus, any
directory with the __init__.py file will be treated as a package for Python.
In some cases, the __init__.py file can be an empty file. The different
modules in the package are imported similarly as plain modules, but with a
special behaviour for the __init__.py file, which is used to gather all
package-wide definitions.
Fig. 15.3: __init__.py Structure
As seen in the above Fig. 15.3, the structure of a standard Python module,
the inclusion of the __init__.py file in a directory indicates to the Python
interpreter that the directory should be treated like a Python package. When
a module is imported into a script, that module's __init__.py file will be
sourced and executed. It provides an easy way to group large folders of
many separate Python scripts into a single importable module. If the
__init__.py file is removed, Python will no longer look for submodules
inside that directory, so attempts to import the module will fail. Leaving an
__init__.py file empty is considered normal and even a good practice, if the
package's modules and sub-packages do not need to share any code.
Like other programming languages, Python has an execution entry point,
that is, main program. A module is a file containing Python definitions and
statements. Every module in Python has a special attribute called
__name__. The value of __name__ attribute is set to '__main__' when
module run as main program. When you execute a Python script, it is
treated as the main and its __name__ attribute is set to "__main__". If this
script is imported as a module in another script, the __name__ is set to the
name of the script or module.
Summary
1. A module is a file that contains definitions and statements where the file
name is suffixed with .py.
2. A package is a directory and various sub-directory that contain a
collection of modules and __init__.py in the directory and every sub-
directory.
3. A library is a collection of functions that performs many tasks without
writing separate user defined code.
4. The process of importing involves gaining access of one module from
another module. The import keyword is used for importing a package
into the namespace.
5. The process of importing involves finding the given package in the
directory and registering the found package with the local namespace
along with its location, hence making them available for usage.
6. Relative import specifies an object or module imported from its current
working directory, that is the location where the import statement
resides.
7. There are two types of relative imports, which are implicit and explicit
relative imports where implicit relative import is deprecated in the latest
version of Python.
8. Explicit relative import is a type of importing a module based on the
current location of the module or the program that going to be present.
9. "." operator in explicit relative imports defines the directory and
consecutive usage of dot operator mentions parental directories.
10. The importlib modules provide an API for interacting with the import
system of Python. A direct call to __import__ performs only the module
search and if the module is found, the module is compiled.
11. Functions, such as print(), and input(), and number conversion functions,
such as int(), float(), and complex(), are available by default and no need
for any import operations.
12. The os module provides a handful of the way of utilizing the operating
system based functionalities, such as file descriptors handling, process
handling, path handling, and so on.
13. The inclusion of the __init__.py file in a directory indicates to the
Python interpreter that the directory should be treated like a Python
package.
14. The arguments that are given after the name of the program in the
command line shell of the operating system are known as command line
arguments.
15. sys.argv is a variable that holds all command line arguments in the list
data structure and it is the popular way to handle the command line
arguments.
Glossary
Module: a module is a file that contains definitions and statements
where the file name is suffixed with.py.
Package: a package is a directory and various sub-directory that contain
a collection of modules and __init__.py in the directory and every sub-
directory.
Library: a library is a collection of functions that performs many tasks
without writing separate user-defined code.
Explicit relative import: explicit relative import is a type of importing a
module based on the current location of the module or the program that
going to be present.
os module: the os module provides a handful of ways to utilize the
operating system based functionalities, such as file descriptors handling,
process handling, path handling, and so on.
Command line arguments: the arguments that are given after the name
of the program in the command line shell of the operating system are
known as command line arguments.
Compile-time errors: compile-time errors are errors occurred due to
typing mistakes; if the proper syntax and semantics of the programming
language are not followed, then compile-time errors are thrown by the
compiler.
Run-time errors: run-time errors are the errors that are generated when
the program is in a running state. These types of errors will cause the
program to behave unexpectedly or even stop the program from
executing further.
Exercise
1. Explain different types of imports in Python with examples.
2. Discuss the attributes associated with the imported modules.
3. List the ways how the modules are made available in the current
namespace environment.
4. Explain the syntax and parameters of __import__ function.
5. Explain how the math.isqrt and math.sqrt functions are different from
each other.
6. How __init__.py is important to a package and modules?
7. How the command line arguments are handled using sys package
effectively. Explain with an example.
8. Differentiate compile-time error and run-time errors in Python.
9. Explain different ways of the compiling Python scripts.
10. How compiled Python reduces the start time of Python for a module?
Programming Questions
1. Create a module that holds the functions for finding the number of
different ways the various combinations of words can be given with a
single word (Include repetition of letter cases also).
2. Create a module that handles degree and radian as inputs for finding all
six trigonometric ratios.
3. Construct a module that finds six trigonometric ratios given with two
sides of a right-angled triangle.
4. Write a Python program to change the current working directory to the
downloads folder and change the mode of the directory.
5. Construct a Python script that finds the lcm for the input numbers
through the command line arguments.
6. Construct a module that implements the base rule of logarithm using
natural logarithm function from math library where the input receives
value of argument and base.
Logarithm Base Rule: . Here use c = e.
7. Construct a package that finds lcm and hcf of the given numbers through
two different functions. Use the created package in the interpreter using
importlib.
8. Construct a package for estimating the slope and intercepts of the given
line in the form of ax + by + c = 0. Here a, b and c are the inputs from
the users.
OceanofPDF.com
Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live.
– Martin Golding
CHAPTER 16
Errors and Exceptions
The information provided in the chapter "Errors and Exceptions" enlightens
the reader on errors and how to manage exceptions, which will support
scenarios that can potentially cause the applications to crash. This chapter
makes programs more robust when they encounter with faulty data,
regardless of whether the bad data results from an innocent error or an
intentional attempt to cripple the programs. It is possible to design
applicable, usable, and stable programs by utilizing the abilities learned in
this chapter about handling exceptions.
Learning Objectives
After completing this chapter, the reader will be familiar with the following
concepts:
Understand errors and exceptions.
Understand the standard exceptions
Understand how exception-handling aids to handle run-time exceptions.
Learn to extract information from exceptions, raise an exception finally
statement, and catch exceptions in programs.
Learn to handle exceptions such as using except clauses with no
exceptions, except clauses to specify exception handlers, and using
except clause with multiple exceptions.
To try-finally clause—learn to use the try statement to delimit the code
in which exceptions may occur and use the final clause to release
resources.
Understand the argument of an exception and raise an exception.
Understand the user-defined exceptions.
To create program defined exceptions.
16.1 Introduction to Errors and Exceptions
Programming is error prone . The faults in the programming are referred to
as bugs. Debugging refers to locating and fixing bugs within a computer
program. The bugs in the program can be dealt with in two different ways:
by detecting them and managing them.
1. Anomalies
During the execution of the code, the catch exception method checks for the
presence of any exceptions, and if one is found, the method is required to
take some action. This technique captures all exceptions that can arise due
to abnormal circumstances and then analyses them as they are thrown into
the main flow. Python makes it possible for programmers to deal with
irregularities and helps them work with the so-called Python exception,
which allows them to achieve a clean implementation.
Fig. 16.1: Classification of Anomalies
If an error cannot be remedied, the program should be terminated, or the
user should be prompted to provide an alternative action.
Errors and exceptions both fall under the category of anomalies. Exceptions
can be synchronous or asynchronous, and each type has its characteristics
(Refer to Fig. 16.1).
2. Errors
Errors can occur in a Python application when something unforeseen goes
wrong. For example, if function F1 calls function F2, which, in turn, calls
function F3 and an exception occurs in function F3. If it is not handled in F3,
the exception passes to F2 and F1. If it is not handled, an error notice will be
generated, and the program will abruptly and unexpectedly come to a halt.
Python will generate errors whenever one of the following conditions is
fulfilled: faulty arithmetic, running out of memory, invalid file references,
and inappropriate formatting of Unicode.
Types of Errors
There are many types of errors. This chapter will discuss a few common
errors, as seen in Fig. 16.2. Each of these subcategories shares one specific
set of qualities, which helps to differentiate them from one another.
Fig. 16.2: Types of Errors
Syntax errors: computers use machine code. Compilers generate machine
code from high-level programs. Programming languages have various
syntaxes. Designers talk to the computer in a syntax based language when
they program. The correct syntax should be used. An incorrect syntax
causes syntax errors. A syntax error is reported, if syntax rules are not
followed correctly and are violated. The compiler pinpoints syntactic errors,
making them easier to rectify. The program must repair the compiler's
syntax problem. In interpreted languages, syntax errors are found during
program execution, making them hard to spot. Semicolons, brackets,
undeclared variables, and misspelt keywords are syntax errors.
Example 1: Illustration of SyntaxError - unterminated string literal.
The use of single and double quotation marks in the preceding code causes
an unterminated string literal SyntaxError.
Example 2: Illustration for SyntaxError.
The program suggested error is "Perhaps the user forgot a comma." Include
quotation marks around the statement inside the braces.
The syntax error is likely due to a missing quotation mark(:).
The reader can try other possibilities for this error as follows.
Wrongly spelling a variable name
The declaration of the variables is invalid.
Using a wrong case (upper or lower).
Misplacing a colon in an If statement
Misusing block statements (if-else, loops).
When there are a few missing quotation marks, like missing a
parenthesis ( ) or [ ]
Not correctly indented.
The erroneous use of reserved keywords.
There are not enough spaces between the words.
The equal sign (=) is missing from the expression.
Calling or defining an invalid function.
Semantic errors: the semantic error occurs when the program's semantics
(meaning) is erroneous, or the statements have no meaning. Semantic errors
are not grammar or syntax issues; the program compiles but does not
produce the desired outcomes. A semantic error is when the steps are
syntactically correct and in the proper order, but the program is wrong. The
outcome will be slightly off and utterly incorrect even if the program runs
correctly. Hence, the semantic errors in the program will not produce error
messages, but the software will perform a different task. One must look at
the program's output and figure out what it's doing to uncover semantic
errors. It's time-consuming.
Logical errors: Logical errors are difficult to trace since they are caused by
faulty logical implementation and might happen at any time. A custom
computer program will be written from the ground up to resolve an issue.
To find a solution to this problem, an algorithm is used. A step-by-step
solution to a particular issue is called an algorithm, a way to tackle
problems. Errors are likely to occur due to an algorithm flaw referred to as a
logical mistake. Even if there is not a bug in the software, it will operate
normally, but the outcomes it produces will be incorrect. The fact that no
compiler alerts identify logical errors makes it harder to pinpoint the source
of the issue. No matter how often users attempt to run the application, they
will only obtain accurate results. As a result, the program must carefully
examine each statement to determine where the problem stems. Utilizing
operators in a manner that is not appropriate is an example of a logical
error. This coder committed an error in logic when they used the division
operator (/) instead of the multiplication symbol (*). This error causes a
program to produce incorrect or undesired output.
Table 16.1. Life time examples of the correct and incorrect logics.
Table 16.1: Correct Logic Versus Logical Errors
The first and second sentences lack the adherence to standard norms
typically employed in the real world. Furthermore, the actions mentioned in
these sentences will not lead to the desired outcome and results in logical
errors.
Example 3: Illustration for LogicalError.
'c' in the above code should be 19, but it instead evaluates and displays
25.0. The correct way to find the value of "c" is not "c=a+b/2," but rather
"c=(a+b)/2." As a result, it is a logical error.
The following are some more circumstances under which this type of error
might occur.
Creating an unavoidable loop condition?
Using the correct operator inside a conditional if statement?
Storing the variable data in the correct format (string, integer, etc.)?
Runtime errors: "Runtime errors" occur during an application's execution.
A runtime error (seen in Fig. 16.3) occurs during the execution of a program
after it has been compiled properly. Runtime mistakes are commonly
referred to as "bugs" and are discovered during the testing phase before a
piece of software is released. Developers often provide patches, or minor
updates, after a piece of software has been made available to the general
public to address any issues that may surface during use. Runtime errors
may occur while fixing problems on online platforms that are not expressly
specified in the notice accompanying these errors. Logical, input or output,
and undefined object fallacies are only some of the runtime issues that can
occur. This error occurs during the execution of some illegal operation or
unavailability of execution conditions.
One such runtime error is NameError: when the application cannot
locate a name, it throws the NameError exception. Error messages list the
missing word.
Fig. 16.3: Compile Time Errors and Runtime Errors
Example 4: Illustration for NameError.
The above error [Chennai is undefined in NameError.name] occurs during
the program's execution.
A few instances of the various kinds of Python runtime errors are:
The operation of dividing by zero.
Doing an operation on mismatched data sets.
Attempting to access a resource with an undefined identifier.
An attempt is made to access a non-existent list element, dictionary
value, or object attribute.
Searching for a file that is not there.
Floating point errors.
Modulo operation using a zero based basis.
A form of integer overflow.
Invoke a method on an uninitialized variable (NullPointerException).
Ran out memory (memory leaks) (OutOfMemoryError).
Open a file that does not exist (FileNotFoundException).
Pass arguments to a method that is not within the accepted bounds
(IllegalArgumentException).
Invoke the start() method on a dead thread
(IllegalThreadStateException).
Invoke wait() or notify() on an object deprived of owning the object's
monitor (IllegalMonitorStateException).
Array index out of bounds.
Illegal format conversion.
Example 5: An instance of a program to show the NameError.
In the above example, an error results. It is unable to perform the
calculation a + b / 2 because the inputs string(b='error world') and
data(a=12) are of fundamentally different data types (unsupported operand
type).
Few Types of Errors
Type errors: Python's TypeError is an error when the data type of objects
in the operation is incorrect. When a process or function tries to operate on
an object of the wrong type. The type error occurs when data or value of
unexpected type is passed or input.
ValueError: Python raises the ValueError exception if a function is called
with an argument of the correct type but the wrong value. This error
typically occurs in mathematical operations that require a narrow range of
values.
ZeroDivisionError: If the second input is zero, division or modulo
operations will fail, raising the exception known as ZeroDivisionError.
Zero-division errors are derived from ArithmeticError. ArithmeticError
inherits from Exception.
FileNotFoundError: This exception is generated whenever a piece of
software attempts to access a file or directory that does not currently exist.
NotImplementedError: the NotImplementedError exception is thrown, if
an object is expected to support a particular method but does not. It is not a
good idea to use this error in situations when the specified function is not
designed to take the kind of input argument. A TypeError exception should
be thrown, if one of these conditions is met.
Example 6: Illustration for SyntaxError.
Yet another code to show the syntax error:
Example 7: Illustration of ZeroDivision Error.
Yet another similar code to show the ZeroDivisonError , when a value is
divided by zero.
Table 16.2: Difference between Syntax Errors and Logic Errors
Example 8: The program shows the ZeroDivisionError.
The following code generates a ZeroDivisionError when a value is divided
by zero.
3. Exception
When writing code, programs must look for errors or flaws or mistakes.
Flaws may be because of a broken network or a lack of a necessary
resource; the function might be unable to obtain the required inputs.
Preparing for the unexpected throughout the design phase is key. Programs
can use "exceptions" in Python to help them deal with "strange" or
"unexpected" situations. There is no one-size-fits-all approach to resolving
software issues. With this in mind, servers must be resilient and have an
audit trail that can be traced back to their source. Exceptions are typically
abbreviated as "handling exceptions." Most of Python programming errors
are created via exceptions.
Exceptional conditions: When a system goes through something that is not
typical of the system's behaviour, extenuating factors can be accused of the
event. These are the kinds of circumstances that can affect the outcome of a
case. If these abnormal conditions are not controlled properly, they may
result in the failure or crash of the system.
Exception failures: two-thirds of all system breakdowns and 60 per cent of
all security holes in computer systems are attributed to exception failures.
Exceptions serve as a warning to the program, helping them to avoid a
software crash. In the event of a traceback, this information will be included
in the error message. Two lines of code, "try" and "except," handle and
capture exceptions.
An exception is taken care of by the try: statement. To avoid interrupting a
running program, a developer might occasionally want to perform an action
that could lead to an exception without really doing anything unusual. Try
statements are helpful for "wrapping" code sections to handle exceptions
(see Fig. 16.5). Exceptions that were both caught and uncaught may be an
exception to this general rule. The software will crash, if an exception is
missed and handled by the system.
Fig. 16.4: Python Exception Handling
Difference between an Error and Exception
Non-program errors refer to the misconception that occurs during the
execution of a program as an instance, and the fact that it does not comply
with a general rule is what gives it its name. Error handling is generally
remedied by preserving the state of execution at the time the error occurred,
interrupting the normal flow of the program to carry out a specialized
function or section of code that is referred to as the exception handler, and
restoring the state of execution to its previous state (explained in detail in
the subsequent section). The error handler may be able to "repair" the
problem and allow the program to be resumed with the data that had been
saved before the occurrence of the error. The types of error that may have
occurred are "division by zero," "open file error," and so on. The
distinctions between an error and an exception are compared and contrasted
in Table 16.3.
Table 16.3: The Difference between an Error and an Exception
16.2 Handling Exceptions
The computer systems' software interacts with the actual world's
unpredictability. This software is to be designed and fine-tuned to cope with
the fickleness of the circumstances. This tuning is accomplished by
adjusting the parameters of the software. This software, with tuned
exception handling, delivers fault-tolerant, and fault-avoidance systems.
As a consequence of utilizing exceptions, error handling is streamlined.
Imagine a scenario in which a user writes a lengthy Python application.
Exception handling may help ensure that the program creates an error at
least once. Hence, proactively conceiving, designing, developing, and
implementing systems with exceptional conditions is a precautionary
measure to provide enough protection against disturbances. An error
handling and standard issue programming sandbox will be made available
to users.
Fig. 16.5: Handling Exceptions
How does exception work in Python?
To prevent a program from quitting, Python throws an exception. Python's
exceptions generate errors on their own. Fig. 16.5 depicts the handling of
exceptions. When everything works well and errors do not occur, program
executed normally. When an error occurs, then an exception is raised. In
Python, the exception is raised using the raise exception method, which will
look for the handler. The program control is transferred to the handler, if the
handler is found. If the handler is not found, then the program terminates
and displays an error message. Also, if an exception is thrown in a program
that does not have a try-and-catch mechanism, it causes the program to
terminate.
Rule of Exceptions
To use the exception, a set of rules are needed that need to be adhered to as
follows.
1. If an exception arises but is not always catastrophic, it can be handled.
2. The exception clause in a try statement should include a pointer to a
specific class.
3. A specific object type must exist for an exception to apply.
Listed below are a few examples of an exception.
An exception based on arithmetic.
Attempting to access a file that cannot possibly exist.
Exception to the standard number format.
If the datatype server is inaccessible, the table can be deleted.
The combination of two distinct yet incompatible kinds.
The index of the array is outside the bounds.
Zero cannot be divided by a number.
Advantages of Handling Exceptions
The advantages of Handling Exceptions are as follows.
Decoupling the "Regular" code from the error-handling code.
Aids in grouping and differentiating error types.
Sending errors up the call stack when they occur.
To correct errors that occur during the runtime.
Use events to communicate the right circumstances without transferring
results all over a program by using the notify function.
Benefits of Throwing Exceptions
Throwing and handling the exceptions benefits us from the following.
Better user experience.
More helpful error messages for everyone: from users to fellow
developers.
Reliability: to prevent the application from crashing suddenly by
avoiding potential problems in advance and taking preventative
measures.
An exception occurs when something unexpected happens in the program,
and this causes the program's natural flow to be impeded. The unexpected
happening is the problem that creates an exception. This exception results
in throwing an exception to be handled in the subsequent stages. Fig. 16.6.
shows the sequence of events happening in user-defined exceptions.
Fig. 16.6: The Sequence of Events in Handling Exceptions
The two stages of an exception handling procedure are "raising" the
exception (also known as "throwing" it) and "catching" it, as can be seen in
the Fig. 16.6.
Fig. 16.7: Exception Handling in Python
Python offers a mechanism for managing exceptions that allows control to
be transferred from the point at which an error is detected to a handler that
may address the error. When an error is made, Python's exceptions (Fig.
16.7) will compel the program to print an error message; it will also
abruptly interrupt the process it is now running and send it to the process it
was called from until the fault is handled. If it is not handled, it will crash.
Example 9: Illustration to handle a single value error with a try-except
statement. (Listing1.py).
The python script for handling a single ValueError with a try-except
statement would be as follows:
The try statement with an except clause used to handle the single
ValueError is demonstrated in the following example. The rest of the
statements will be executed, if the not exception got raised in the''try''
statement.
#The output of the python script is shown below:
Example 10: Illustration to handle selected exceptions(without break
statement). (Listing2.py).
The python script for handling selected exceptions with a try-except
statement would be as follows:
The following program shows how to handle selected exceptions. This
program asks the user for input until a valid integer has been entered but
allows the user to interrupt the program (using Control-C or whatever the
operating system supports). Also, note that a user -generated interruption is
signalled by raising the KeyboardInterrupt exception.
#The output of the python script is shown below:
try - except - else
To handle all types of exceptions, the user can define an except clause with
no exceptions. Keep in mind that it's not a good programming practice, nor
does anyone recommend it. It will not help the program find which type of
exception caused the issue. It only aids in understanding the various means
of writing.
Except statements can also be used without specifying exceptions. Defining
except clause with no exceptions enables it to handle all types of
exceptions. The syntax for try-except-else is as follows.
Example 11: Illustration for try-except-else. (Listing3.py).
The python script for try-except-else statement would be as follows:
The above code is stored as Listing3.py and executed as follows.
Example 12: Illustration for except with no exception. (Listing4.py).
The python script would be as follows:
#The output of the python script is shown below:
16.3 Multiple Exceptions
Python programming commonly makes use of an approach centred on
exceptions. Program is alerted to any issues that may occur during the
program's execution using exception handling techniques. In certain
circumstances, a procedure may raise more than one possible exception,
depending on the direction control flows. As a result, the program or
function might have to catch multiple exceptions as a result of this.
Exceptions can be handled in two ways with Python.
1. Using more than one except block: when many exceptions occur, write
exception blocks for each one.
2. Using only one except block: a single except clause can be used to
include numerous exceptions. Multiple exceptions can be caught in a
single except block only if they all have the same behaviour. The
program's time can be saved, if the output of several exceptions is the
same, which avoids unnecessary code duplication. " Python interpreter
will execute code defined within the except clause, if it discovers a
matching exception. The following is a generalized syntax for multiple
exceptions:
Example 13: Illustration of multiple exception using multiple except
statements.
As mentioned above, a try block can have multiple exceptions. Based on
the exception raised, the respective exception block is executed. This
example explains multiple exceptions using multiple except blocks. While
performing arithmetic division operation, there are possibilities of raising
multiple exceptions. Considering the ZeroDivisionError and TypeError
exceptions, the following function arithmetic_division is written.
In the above function, ZeroDivisionError and TypeError are addressed in
different except blocks and each has different statements inside them to
execute. The above function is executed as following.
Example 14: Illustration of multiple exception using single except
block.
The function in the Example 13 is altered as follows to implement multiple
exceptions using single except block. The arithmetic division is performed
using the function, arithmetic_division as shown below.
In the above execution, the single exception block inside the function
arithmetic_function handles two types of errors namely, ZeroDivisionError
and TypeError which are given as tuples.
While invoking the function with values (6,3), gives the result as 2.0 which
is the perfect execution of the arithmetic of 6 upon 3. While invoking the
function with parameters (6,'three') and (6,0), the TypeError and
ZeroDivisionError are raised and the single except block is executed. The
following output shows the raising of multiple exceptions.
One could notice that for both errors, single exception block is executed. By
knowing which exception raised, execution can be altered using conditional
statements as shown in the following example.
Example 15: Illustration of Multiple Exception using additional
features. (Listing7.py)
The arithmetic_function in Example 14 is altered as follows.
In the above function, to identify the name of the exception class, the
attribute "__class__.__name__" is used with exception alias "err" ("err" is
user-defined, can be varied from user to user). The conditional statement
decides which block should be executed on comparing with the exception
class. The following output shows the difference from the output of
Example 14.
16.4 Raising Exceptions
Python's "raise" statement is used to throw an exception explicitly. Using
the "raise" statement, an exception object is created, and the expected
program execution path is immediately abandoned in search of a matching
except clause in the surrounding try statements. If no matching except
clause could be located to handle the exception, the raise statement aborts
execution and puts an end to the program. Raise will cause an exception to
be thrown, so the program's execution will be halted, if it is not handled.
The syntax for the raise statement is given by
Where,
The exception is the name of the exception.
The "args" is optional and represents the value of the exception
argument.
The final argument, traceback, is optional and, if present, is the traceback
object used for the exception.
The message string attaches a meaningful error message to the exception
object. Additional attributes are connected to the exception owing to the
string. Two types of raise statements exist.
raise exceptionClass<, value >: in the first type, the raise statement
uses an exception class name. The optional parameter is the extra value
contained in the exception. Usually, this is a string. Here is an example
of the raise statement.
This statement raises the built-in exception ValueError. The string here is
"Oh My God". Always ensure that the parameter string focuses on the
error condition while using a built-in exception.
raise < exception >: the second type of the raise statement uses an
object constructor to construct the exception object.
Here is a variation on the second form in which additional attributes are
provided for the exception.
Example 16: Illustration on how to raise an Exception. (Listing5.py).
The python script to raise an exception would be as follows:
#The output of the python script is shown below:
In this example, when the ValueError exception is thrown, the text " You are
not eligible for voting in India." gets included in the message. This value is
used to define the thrown exception and is passed on.
16.5 Exception Chaining
Like Nested if statements, nested loops and nested class, the concept of
nested is also extended to try - except as well. Reader could identify that the
try block raises the respective exception either by interpreter or user-defined
exception raise, when an error is occurring and the except block handles the
raised exception. What happens when another exception is raised in the
except block? There comes Exception chaining into the picture.
The process of exception chaining involves introducing a new try – except
block into a except block or, a new exception is raised inside the except
block.
Example 17: Illustration for Exception Chaining.
The aim of the example is to write the computed value using
arithmetic_division function (like seen in Example 15) into a file.
The function arithmetic_division involves the following code.
In the above code, on raising any exception, the error is written into a file
using writing_to_file function. The function writing_to_file is defined as
follows where the function performs opening and writing into a file.
On the whole, the readers could see the possibility of raising
ZeroDivisionError or TypeError exception inside the function
arithmetic_division and also the possibility of raising the IOError exception
in the function writing_to_file which is invoked inside the except block in
the arithmetic_division. This kind of the placing an exception another
exception is called exception chaining and since there is not explicit
mentioning of exception raising statement, it is called as implicit exception
chaining.
The execution of the above code is shown below.
16.6 Built-In Exceptions
Types of Exceptions
Python supports two diverse kinds of exceptions (Fig. 16.7).
Built-in exceptions
User-defined exceptions
Fig. 16.7: Types of Exceptions
Python exceptions must derive from BaseException. A try statement with an
except clause refers to a specific class that handles any derived exception
classes (but not exception classes from which it is derived). Even if two
exception classes have the same name but are not related via subclassing,
they are never the same.
The built-in exceptions listed below can be created by the interpreter or by
built-in functions. Built-in exceptions are contained in the exception's
module, which is always loaded before the execution of any program.
Exceptions are defined as classes. Except in those cases, they have an
"associated value" that identifies the specific problem and its root cause.
Several pieces of data can be combined into a string or a tuple in this case
(e.g., an error code and a string explaining the code). An exception class's
function Object() { [native code] } typically receives this value as an
argument.
Exceptions can be raised by user code. To test an exception handler or to
report an error condition "exactly like" the case in which an interpreter
raises the same exception, be aware that there is nothing to prohibit user
code from raising an inappropriate error.
To create new exceptions, programs are urged to at least derive them from
the exception class and not the BaseException class, which is the default
exception type. Python's tutorial provides more details on defining
exceptions under the heading. In most cases, these exceptions are the
building blocks for further exceptions.
Fig. 16.8: Most Relevant Exception Classes
Just an outline of the exception base classes, predefined exception classes,
and built-in exceptions is shown below.
Exception Base Classes: the exception base classes and descriptions are
listed in the following table.
Table 16.4: Base Class Exception Name
Predefined exception classes: the predefined exception classes and
descriptions are listed in the following table.
Table 16.5: Predefined Exception Classes Name
The built-in exceptions and causes are listed in the following table.
Table 16.6: Python Built-In Exceptions and Their Classes
16.7 User-Defined Exceptions
Python detects runtime faults, on compilation and all vital errors are logged.
Errors stop the program and throw exceptions. Sometimes, there is a
necessity to restrict program variables or avoid an undesired condition.
These situations need user-defined exceptions. The user-defined exceptions
feature in Python allows developers to limit the range of possible values for
variables. Exception classes allow users to define their own custom errors.
User defined exceptions are defined by classes.
Subclasses of Exception can be either explicitly coded or derived from the
parent class. Users can create exceptions, called Custom Exception, or User
Defined Exceptions by developing classes from the standard built-in
Exception class.
Most exception classes end with "Error" in their names. By defining a new
Exception Class, it's possible to consolidate several distinct error conditions
into a single construct.
The canonical syntax for user-defined exceptions is given as follows.
Once the class is defined, one can throw an exception.
Example 18: Illustration on KeyError Exception (Listing6.py).
The python script would be as follows:
Once the class is defined, one can throw an exception.
#The output of the python script is shown below:
16.8 Clean-Up Actions
Clean-up actions are always executed program statements. Even if there is
an error in the program, these statements are performed. When using
exception handling, these statements will be executed as well. It is safe to
assume that clean-up actions are used when an application's specific section
has to execute at all times.
It is natural to aspire to race through a piece of code, regardless of the
repercussions of coding it. Finally, statements in try-and-except statements
allow us to accomplish this activity in Python. Once the program has been
executed several times, the finally keyword tells Python where to go next.
As a result, every single line of code under this one is being cleaned up.
Even if there is an exception in the try-and-except statements, any action in
the Finally clause will be executed. However, if an exception cannot be
handled by the except clause, then the error is raised as the default during
the execution. Clean-up actions refer to the overall procedure.
Code can have three distinct scenarios as follows.
Code normally runs: the developed code operating in this instance has
no difficulty. There is no mistake in either of these places. As a result,
the clean-up is done at the end of the process.
Error handling is done in the except clause: similarly, to the preceding
situation, the compiler outputs the statements in the finally clause at the
end of the code.
Error without any except clause: finally clause statements are printed
first when the compiler encounters this condition. The compiler prints an
error message after the finally clause.
Regardless of whether or not it has issued an error, users frequently need
the program to carry out a specific activity at all times. Try and except
blocks are commonly used to capture any issues or exceptions. An optional
statement indicating 'clean-up operations,' which must be performed
regardless of the circumstances, should be included in the "try" statement.
Try-Finally Clause
Fig. 16.9: Try Finally Clause
Fig. 16.9. above depicts that any code the user wants to be executed
regardless of whether an exception occurs can be inserted inside the finally
block, if that code exists. Regardless of the exception, the finally block
always runs.
Example 19: Illustration on clean-up actions processing.
The python script would be as follows:
The finally block in the preceding example is executed. Because an
exception was not handled, the program's execution was halted.
Summary
1. Programming is error-prone. The faults in the programming are referred
to as bugs.
2. Debugging refers to locating and fixing faults within a computer
program.
3. Errors and exceptions both fall under the category of anomalies.
4. Exceptions can be synchronous or asynchronous, and each type has its
characteristics.
5. Errors can occur in a Python application when something unforeseen
goes wrong, such as faulty arithmetic, running out of memory, invalid
file references, and inappropriate formatting of Unicode.
6. A syntax error is reported, if syntax rules are not followed correctly and
are violated. The compiler pinpoints syntactic errors, making them easier
to rectify.
7. A semantic error is when the steps are syntactically correct and in the
proper order, but the program is wrong. The outcome will be slightly off
and utterly incorrect even if the program runs correctly.
8. Logical errors are difficult to trace since they are caused by faulty logical
implementation and might happen at any time.
9. "Runtime errors" occur during an application's execution.
10. When the application cannot locate a name, it throws the NameError
exception.
11. "ValueError" refers to errors reported when function arguments have the
right type but an invalid value.
12. If the second input is zero, division or modulo operations will fail,
raising the exception known as ZeroDivisionError.
13. FileNotFound exception is generated whenever a piece of software
attempts to access a file or directory that does not currently exist.
14. When a system goes through something that is not typical of the system's
behaviour, extenuating factors can be accused of the event. These are the
kinds of circumstances that can affect the outcome of a case. If these
abnormal conditions are not controlled properly, they may result in the
failure or crash of the system.
15. Exceptions are the errors that happen when the program is being
executed. An occurrence that occurs during the execution of a program
and impedes the usual flow of the instructions contained inside that
program is referred to as an exception.
16. The try statement has four separate clauses—or parts—introduced by
the keywords try, except, else, and finally. All clauses but the try can be
omitted.
17. To handle all types of exceptions, the user can define an except clause
with no exceptions. Python try-except block can handle all kinds of
exceptions.
18. In certain circumstances, a procedure may raise more than one possible
exception, depending on the direction control flows. As a result, the
program or function might have to catch multiple exceptions as a result
of this.
19. Python's "raise" statement is used to throw an exception explicitly. Using
the "raise" statement, an exception object is created, and the expected
program execution path is immediately abandoned in search of a
matching except clause in the surrounding try statements.
20. A chained exception is implicitly raised when another exception is
thrown within an except block.
21. One can stipulate which exceptions the except clause should catch and
which need not be handled. A try clause can handle all except clauses
differently, but only one will be executed, if an exception occurs. To
specify multiple exceptions in an except clause, use a tuple of values.
22. Python supports two diverse kinds of exceptions, namely, built-in
exceptions and user-defined exceptions.
23. Python detects runtime faults, on compilation and all vital errors are
logged. Errors stop the program and throw exceptions. ArithmeticError,
AttributeError, ImportError, IOError, and FileNotFoundError are
frequent exceptions. There is a necessity to restrict program variables or
avoid an undesired condition. These situations allow user-defined
exceptions.
24. To create a user-defined exception, derive a class from the built-in
exception class. Users can create exceptions, called custom exceptions,
or user-defined exceptions by developing classes from the standard
built-in exceptions.
Glossary
Built-in exceptions: Python contains built-in exceptions that can cause
an error to be logged. An exception is a term for a problem that arises
while the program runs.
Clean-up actions: clean-up program statements always run. These
statements are executed despite a programming fault. Using exception
handling will execute these statements. Always clean an app's
specialized part.
Except block: except block catches the exception object and ensures it
is appropriately handled before continuing.
Exception chaining: an implicit chained exception occurs when another
exception is raised within an except block.
Exception failures: two-thirds of all system breakdowns and 60 per cent
of all security holes in computer systems are attributed to exception
failures.
Exceptional conditions: a system's anomalous behaviour can be
attributed to mitigating circumstances. Case outcomes can be influenced
by a wide range of factors, including the ones listed below. If these
unusual conditions are not adequately regulated, the system could go
down or fail.
Exceptions: programmes can use "exceptions" in Python to help them
deal with "strange" or "unexpected" situations.
FileNotFoundError: FileNotFoundError exception is generated
whenever a piece of software attempts to access a file or directory that
does not currently exist.
Logical errors: logical errors occur when the programme runs without
crashing but produces an incorrect result.
NameError: when the application cannot locate a name, it throws the
NameError exception. Error messages list the missing name.
NotImplementedError: the NotImplementedError exception is thrown,
if an object is expected to support a particular method but does not.
Python handling exceptions: as a result, the program's normal flow is
disrupted, and an exception occurs. An exception is created whenever a
problem arises. An exception is thrown due to this error and will be dealt
with in the following stages.
Raise: Python's "raise" statement throws exceptions. "raise" statements
produce an exception object and require the computer to check the
surrounding try statements. Suppose no unless clause matches, the raise
statement aborts. Unhandled raise exceptions stop program execution.
Runtime errors: a runtime error occurs during the execution of a
program after it has been compiled properly. Runtime mistakes are
commonly referred to as "bugs" and are discovered during the testing
phase before a piece of software is released.
Semantics errors: The semantic error occurs when statements have no
meaning. Semantic errors in the program will not produce error
messages, but the software will perform a different task.
Syntax errors: a syntax error is reported, if syntax rules are not
followed correctly, causing the compiler to object. Syntax errors are
caused by incorrect syntax, and errors are easily caught when the
program is built-in preparation for execution by the compiler.
Try block: try block detects and throws as an exception object anything
that it comes into contact with throughout the execution of the try block.
Try statements: try statements are helpful for "wrapping" sections of
code to handle exceptions.
Type errors Python's TypeError is an error that happens when the data
type of objects in the operation is incorrect.
User-defined exceptions to create a user-defined exception, one must
derive a class from the built-in Exception class. Users can create
exceptions, called custom exceptions, by developing classes from the
standard built-in exceptions.
ValueError The term "ValueError" refers to errors reported when
function arguments have the right type but an invalid value.
ZeroDivisionError If the second input is zero, division or modulo
operations will fail, raising the exception known as ZeroDivisionError.
Conceptual Questions
1. Define the term "exception."
2. In Python, how are exceptions handled?
3. How anomalies are categorized?
4. List and describe the various types of errors.
5. Bring out the distinction between a syntax fault and a logic error,with a
suitable example.
6. What is the difference between an error and a warning?
7. Distinguish between error and exception.
8. What are the set of rules need that need to be adhered, to use exception?
9. List the benefits of throwing Exceptions?
10. With a suitable illustration explain the built-in exceptions that may arise.
11. Give an example of using the catch clause.
12. What are the most common exceptions?
13. What is the distinction between handling an exception and raising one?
14. Explain multiple exceptions? With an example, declare Multiple
Exceptions in one single block.
15. Explain "raise an exception" with an example.
16. With a suitable exemplification explain exception chaining.
17. What happens if an exception is lacking an except clause that matches?
18. Differentiate thrown exceptions and exception handling.
19. Why the final clause required in a try /except block is and what does it
do?
20. Justify the necessity of the except and finally blocks in a program.
21. What is meant by "argument of exception"?
22. Provide an example of when an exception is warranted.
23. What are the two diverse kinds of exceptions?
24. What are "User-Defined Exceptions?" Why are they significant?
25. Explain the try except block with a suitable illustration.
26. Depict the process of making custom exceptions for the user, with a
suitable example.
27. Give an in-depth description of handling exceptions together with a
sample program that demonstrates the concept in action.
28. Elaborate on the "try-finally" clause.
29. When does the finally block get executed?
30. How proactively python handles an error as it arises?
Try-Out Questions
What is the output of the code shown below?
1. def f(x):
sample x+1
print(“ABC”)
yield x+2
g=f(111)
print(next(g))
print(next(g))
2. def Z():
try:
f(X, 108)
finally:
print(‘after f’)
print(‘after f?’)
Z()
3. try:
if ‘1’ != 1:
raise “WatchoutError"
else:
print(“WatchoutError has not occurred”)
except “WatchoutError":
print (“WatchoutError has occurred”)
4. a=3456
b=234
assert a>b, ‘A is too small.’
5. def f(x):
yield a+109
print(“Sample test”)
yield a+2
g=f(9)
6. def f(x):
ffor i in range(12):
yield i
a=f(5)
print(list(5))
7. x = (i for i in range(9))
type(x)
8. def getMonth(x):
if x<1 or x>12:
raise ValueError(“Invalid”)
print(x)
getMonth(9)
Identify the type of error in the codes given below.
9. lst = [102, 215, 367]
lst[3]
10. for x in fruits:
if (x != o):
print( x )
11. int(‘65.43’)
12. Print(“Silence Is Golden”)
print(“Silence is Golden”)
13. What will happen if you try to accomplish this?
def func():
return func()
• func()
14. Is the following code valid?
try:
# Statement
except:
# Statement
finally:
# Statement
15. What is the error message that appears while running the code provided
below:
import itertools
l1=(34, 45, 56)
l2=[67, 78, 89]
l=itertools.chain(l1, l2)
print(next(l1))
Programming Exercise
1. Write a program that asks for input from the user and incorporate value
error exception handling into it.
2. Develop a program to demonstrate raising exceptions for a
ZeroDivisionError.
3. Write a python program that access the elements of a list using range
function and loop which handles index error.
4. Create a Python Program which has a class BiologicalFamily and tries to
access attribute using try and exception which handles Attribute Error.
5. Given :
citizenship_details = {'city': 'Chennai',
'age': 22,
'country': 'India'
'nationality': 'Indian'}
user_input = input('What field of data you need on citizenship ? ').
Construct a Python script that accepts user input and returns results
related to citizenship. Use a missing data field ("place of study and
degree")to trigger a KeyError in Python and display the resulting error.
6. Clearly show using the KeyError Exception class in Python how to deal
with the KeyError that was thrown by the preceding code.
7. Write a Python script to handle a
1. ArithmeticError with a try-except statement for (67+89j)/0
2. ZeroDivionError with a try-except -else statement for (491*362)/0
8. Include a finally block and rewrite the python script (example 16.11) to
handle a single value error with a try-except statement.
9. Write a Python script for handling Multiple Exceptions in one single
block
Given:
city= 'Delhi'
city+= 1
x = x + '295'
10. Write a Python script for handling FileNotFoundError Exceptions.
Given:
x = open("sample.txt")
11. Show how an IndexError can be easily resolved with a brief Python
script.
Given:
spices ['Saffron','Turmeric','Cumin','
Nutmeg','Cardamom']
print(spices[8])
12. Create a Python script that handles multiple exceptions with separate
blocks of code.
13. Write a Python user-defined exceptions to display the following output?
Hints:
Elicit input from user using the pharse, "Enter the consensus mechanism
used in ethereum:"
Raise an exception using –
"raise(BlockchainError(Blockchain_consenus))"
Except block- "except BlockchainError as error:"
OceanofPDF.com
Code without tests is bad code. It doesn’t matter how well written it is; it
doesn’t matter how pretty or object-oriented or well-encapsulated it is. With
tests, we can change the behavior of our code quickly and verifiably.
Without them, we really don’t know if our code is getting better or worse.
— Michael C. Feathers
CHAPTER 17
Debugging and Testing
The focus of this chapter is to inculcate the skills necessary for debugging,
testing, and profiling Python programs. Removing the errors in the program
is called debugging. Testing is to check whether a Python program gives the
expected results. Profiling is to find the bottlenecks of a program that affect
its efficiency.
Learning Objectives
By the end of this chapter, the reader would be able to
Understand Python debugger (Pdb).
Apply Pdb to remove bugs in Python programs.
Know the basics of testing.
Know the details of doctest.
Acquire knowledge of unittest and pytest.
Know about the basics of profiling.
Understand profiler tools like cProfile and pstats.
17.1 Introduction to Python Debugger
In programs, errors are common, and one cannot avoid them. It is almost
impossible to write a program with zero errors. The bugs would crop up as
program requirements change. When changes occur, more bugs would crop
up. Sometimes, the bugs would be small and get rectified immediately.
Sometimes, removing the bugs would be costly, if not detected early. So, it
is essential to catch up on the bugs early to reduce the project cost and
provide ease of maintenance.
Sometimes, the errors are related to the syntax of the programming
language and sometimes related to the logic. The removal of software errors
is called debugging. Removing errors is one of the toughest challenges
posed by programmers and one of the toughest skills that need to be
acquired. Many Python editors offer 'fancy GUI' debuggers. Python
provides native non-GUI debuggers.
The early methods of removing the bugs are code tracing, ducky debugging,
and the use of print statements to track the variables. These techniques are
discussed below.
Code Tracing
A brute force technique for debugging is using paper and pencil. Assuming
oneself as a computer, one repeatedly examines the entire program variables
and asks whether that is correct, and tracks the variable. If unexpectedly
this happens, then it is a likely source of error.
Ducky Debugging
Another way is to pretend that you are explaining the code to another
person, for example, a rubber duck doll, and tracing it. This is called rubber
ducky debugging. This imaginary way of explaining the code to another can
be a helpful practice.
Print Statements
One of the commonest techniques is to use print statements. print
statements can be useful in printing intermediate results of variables. This
approach is quite useful for small programs. But this approach will not
work for larger programs. In those circumstances, a skill in Python
debugger is quite useful.
17.1.1 Python Debugger (pdb)
Python provides a native debugger called Python debugger (Pdb). It is one
of the default debuggers that come bundled with Python. What is the use of
Pdb? Using pdb, one can pause a program and verify the variables at
specific program locations. One can also review the code at various points
of the program. One can invoke a Python debugger from an interactive
environment and command line mode.
Start pdb from REPL
Let us have a simple code to illustrate the concepts of debugging, as shown
below in Listing1.py.
Example 1: Illustration of a buggy code (Listing1.py)
It can be checked the output is an error obviously as shown below.
To invoke Pdb from the command line, one has to import the Python
program that needs to be debugged and Pdb, as shown below in Example 2.
Python responds with the prompt (Pdb). This shows the success of the
import of Pdb.
Example 2: Illustrate to invoke Pdb from the interactive shell.
One can observe that the line pdb.run() helps to run the Python script that
needs to be debugged. There are various commands available on Pdb. The
complete commands list that can be used in a Pdb environment can be seen
by using the command help().
To quit from the Pdb, one uses the command exit or quit, as shown below.
Pdb through Python Command Line Interpreter
Pdb can use the command-line interface (CLI) so that a Python program can
be run from the terminal. The way to invoke pdb through CLI is to use the
command.
Here, sample.py is the Python program. -m is the flag that notifies that the
Python program needs to be imported. It can be observed that the flag is
followed by 'pdb' and the filename in the Pdb environment gives the
directory of the Python file and enters its Pdb prompt, as shown below.
If the program accepts any input parameters, then those parameters also
should be given in the CLI along with the command like:
To quit the pdb environment, one can enter quit or exit in the pdb
environment as
Running through Pdb
Python program of Listing1 is imported for the pdb environment. First, it is
necessary to know the lines of the Python code. So, list commands are
useful in a pdb environment to list the lines. Some of the useful list
commands are given below in Table 17.1
Table 17.1: List Commands in pdb
It can be observed that the effect of the list 3,6 command is shown below.
Example 3: Illustration of usage of list commands.
It can be observed that only the first 11 lines would be shown on the screen.
One can specifically list the lines by giving the line number also as list
12,13 for listing 12 and 13th line of the program.
The command step (or s) does the stepping through the Python code and the
next (or n) commands. Using this command, one can step through the code.
The usage of this is as given below.
The difference between the step and the next is given in Table 17.2.
Table 17.2: Differences between Step and Next
Stepping through the code line by line can be a boring task. It is better to
have a facility to go straight to a particular piece of code for verification.
Breakpoint helps to skip codes.
What is a breakpoint? A breakpoint is a location where the debugger pauses
and permits us to inspect the execution. It is a spot where the Pdb stops
execution and allows us to inspect the value. Technically, it allows us to
inspect the call stack. A call stack has all the declared variables and function
arguments.
Art of Debugging
The first task in debugging is to guess the section where a potential bug is
present. This is the primary and foremost point. Then one can use step or
next by using breakpoints judicially.
Pdb allows tracking of variables and shows the potential bug that is present.
Then the correction is made. The above steps are repeated till all the bugs
are removed from the code.
There are various ways using which the breakpoint can be set. Some ways
of creating breaking points are given in Table 17.3.
Table 17.3: Setup of Breakpoints
Once a breakpoint is set, it can be manipulated, as shown in Table 17.4.
Table 17.4: Manipulation of Breakpoints
Once a breakpoint is set, then the command "continue" can execute the
Python code until a breakpoint or finish. This is done as follows.
One can enter, continue, or 'c' to run the code.
The command continue would force execution till the finish, or till the
declared breaking point or any unexpected error occurs.
Example 3: Demonstrate how the debugging is carried out on the
buggy code (Listing2.py)
A part of the screenshot of Pdb debugging is given below.
One can observe the reason for the error. Everything is fine until the
number is got for which the square needs to be computed. The function
instead finds the square root and, therefore, one can spot that the potential
error is in the return statement having a different function than the expected
one. So, correcting that would solve the problem.
17.1.2 pdb through Primitive GUI Mode in IDLE
Pdb through CLI is difficult. Modern Python IDE provides a lot of support
by offering GUI for debugging purposes. IDLE support a primitive GUI
facility for debugging.
For doing that, one has to set the debug options ON in IDLE.
IDLE responds like this!
Then run the program (Run -> Run Module F5). Then the following screen
appears to facilitate debugging graphically.
Credit: Copyright ©2001-2023. Python Software Foundation
Fig. 17.1: GUI of Debug Control Window
The debug control window is shown in Fig. 17.1. It can be observed that the
code would appear at the top of the window. The stack panel lists various
things as _ _ annotation _ _, _ _ built in _ _, and so on. These are interval
variables of Python. The five buttons are listed below in Table 17.5.
Table 17.5: Debugging GUI Buttons
As the buttons are pressed, simultaneously Python interactive demonstrates
the trace of the program, as shown below in Fig. 17.2.
Fig. 17.2: Interactive Shell Showing the Trace of the Program
17.2 Testing in Python
Software testing is executing a program to check, if any errors are present.
Testing is a necessary activity to check whether the program behaves in an
expected manner. All the individual components of a program are called a
unit. A unit can be an object, function, or module. Every unit can be tested
independently.
One can automate the execution of tests. This is called test automation, and
there are various tools available. There are various advantages to automated
testing.
1. Manual testing is difficult.
2. Automated tests are effective.
3. Manual testing is a boring job. Automated testing reduces the effort.
4. Automated tests can detect errors easily.
5. Many tools are available for unit testing.
Testing is an important skill set one should possess. A mistake in a code is
inevitable. So, testing a code to remove it is an important skill. During
testing, a lot of test cases are given to the Python program to check whether
the behaviour of the systems is as expected. If there is a deviation, it is
called an error. This is illustrated below in Fig. 17.3.
Fig. 17.3: Testing Process
A testing bed or test oracle is a mechanism that provides the correct result.
Test cases reveal errors. An ideal test case uncovers the errors that are
present in the program. The characteristics of an ideal test case are.
1. Reliability
2. Validity
If many test cases satisfy criteria that detect the same error is called
reliability. A criterion is valid if criteria, when satisfied by an error, will be
revealed. Some strategies that are used are given below.
1. Black-box testing
2. White-box testing
Black-Box Testing
In the black-box test, the functionality of a Python program is tested.
Python program is treated as a black box. There is no attention paid to the
internal structure of a program. The tester knows only the test cases that are
given to the program and the system output. If there is a match between the
input and output, the program works correctly, otherwise, the program
behaviour is improper.
But, how to test the test cases? One way of testing a program is to test
exhaustively by giving all input valid inputs. While this is ideal, it is
impossible to test all inputs and check them in a reasonable amount of time.
So some sort of strategy is important.
Test Case Design in Black-Box Testing
One way to design a test case is to go for partitioning. Suppose, if a
program tests for input, for example, from 0 <= x <= max, then according
to the partitioning equivalence testing, the test cases would be designed as
Often, a program fails at the boundary conditions. With boundary value
analysis, the boundary will be considered for a test case analysis. Suppose,
if the range of x is
Then, the test case would be to check the value of x as 0, 5, -1.0, and 5.1.
{-1 for x < 0, 0 and 5 for boundaries and 5.1 for x > 5}
White-Box Testing
In the white-box testing, the implementation and non-functionality are
tested. It is also known as structural, and control-flow testing.
Test Case Design in White-Box Testing
In a sample case, the control fl ow can be tested as shown below in Fig.
17.4.
Fig. 17.4: Structural Testing
For example, a test case can be designed in such a way that both parts of the
condition are tested through a proper test case. A test case would be x > 6
and x < 6. It would be interesting to check how the program functions when
x = 6.
Testing in Python
An automated test is a program that takes another Python program and tests
it. If the Python program is correct, the test is passed. Otherwise, the
automated test catches the error and informs the programmer. The
automated test goes by different names like unit tests and integration tests.
Unittest is like JUnit in Java, and PHPUnit in PHP. Python unit test is based
on Junit3. The environment in which the tests are carried out is called test-
driven development (TDD). It should be noted that in TDD, the first test is
written before the actual code. This differs from the conventional wisdom
of code followed by tests. TDD has become immensely popular now.
Assert Statement
The assertion is done to ensure that the input and output of the function are
both accurate. When Python finds an assertion statement, it evaluates the
following presumably correct expression. Python throws an assertion error,
if the expression is incorrect. The syntax for assert is
The assertion statement starts with the keyword assert, followed by an
expression. The expression is evaluated. If the expression is evaluated to a
true, non-zero value, non-empty string, or different from none, then nothing
will happen. Otherwise, an exception is raised. The raising of exception
shows that the assertion is failed.
Example 5: Illustrate the use of an assert statement(Listing3.py).
Assert uses a variable called _ _ debug _ _. This is always true. If this
variable is made false, assert would print nothing.
Example 6: Potential problem of __debug__ variable.
So, the statement assert is not a complete solution. Two testing libraries are
provided by Python. They are listed below.
1. Doctest
2. Unit test
17.2.1 Doctest
Doctest or doctest is one of the simplest ways of testing a Python program.
Doctest is useful for
1. Check if the docstring is updated.
2. To write documentation.
3. To test modules.
A docstring is a string literal. The difference between a docstring and a
comment is that the Python interpreter keeps a docstring in a parsed code
tree and is, hence, available at the run time always. Doctest is a lightweight
unit test framework. Doctest uses a docstring for automated testing. It is
part of the Python standard library. PEP 206 explains the philosophy of
these extra facilities.
The advantages and disadvantages of doctest are listed below.
1. To verify the documentation whether it is up to date.
2. To provide more examples.
3. To assist in regression testing.
4. To provide exclusive documentation.
The disadvantages of doctests are
1. It is not good for API testing.
2. Doctests are static and cannot be parameterized.
Doctests fail because of faulty logic, faulty input to the code, and faulty test
cases.
Doctest Using Terminal
One can run doctest from the terminal. Let us try to write a function to
check the doctest. One can run doctest using the -v command. The flag -v
stands for "verbose". Doctest can be done as follows:
One can doctest in the code itself.
Example 7: Illustration of doctest(listing4.py).
Therefore, doctest is an important skill that one programmer should have.
Separate Test File
It is better to write a test as a separate file and the code separately. For this,
a text file is created as below:
Example 8: Illustration of loading the program in the interactive shell -
(listing4.py from Example 7)
The test can be run as
It must be observed that the test applies to the text file and not to the Python
program.
Some extensions of the doctest are shown in Table 17.6.
Table 17.6: Extensions of Doctest
Example 9: To illustrate the usage of doctest extensions.
It can be observed that the whitespaces are irrelevant.
The advantages of the doctest are its simplicity and its ease of use. The
major disadvantage of a doctest is that the doctest is not suitable for all
situations and the code is bulky.
17.2.2 Unittest
Unit test is another testing library. Doctest is primitive. When a Python
program is lengthy and has various functions, one can use the unittest.
Pytest is originally called a pyunit. It is modelled on Java's Junit to provide
comprehensive testing.
Some Technical Jargons
These are some of the technical jargons that are associated with unittest.
Test case: a test case is an assert statement that is the actual test.
TestCase is an object that creates the tests.
Test Suite: a collection of tests is called a test suite.
Test runner: a runner controls and monitors the test cases.
Test fixture: setup a test before a function, such as creating additional
variables, test databases, and also tears down after the function is
executed. Tears down function clean all temporarily created memory
spaces.
There are two parts to the unit test. One is writing the test suite and the
other part is testing the module with the test suite. Pytest can provide a
single and a group of tests. The syntax of the unittest is given below.
If there are more tests, a test suite can be created. A test runner runs a test
suite. The syntax of this is given below.
Assert Statement in Unittest
Table 17.7: Types of Assertion Statements
The following code illustrates how a small test suite can be done for testing
square root function.
Example 10: Illustrate the use of unittest(Listing5.py).
It can be observed that a unit test is a series of test cases where the actual
and expected values are checked whether there is a match. If there is a
match, the module is considered a 'pass'. Otherwise, the test is considered a
'fail'.
One can separate the test file from the Python file that needs to be tested as
below.
Example 11: Illustration of the use of Unitest(Listing6a.py)
The unit test can be invoked now.
Illustration of Unittest(listing6b.py)
The output is given.
The first dot indicates that the test is successful. Let us change that 10 to 9,
then one can see that the test fails.
The first dot of the result (when is_even(10)) shows that the test is
successful. When the input is change from 10 to 9, the test fails and it raises
an error.
17.2.3 Pytest
An advanced unit test framework is called the Pytest. Pytest offers more
functionality than unittest and is also actively supported by the Python user
community.
Pytest Debugging
The Python environment does not provide the native pytest.
It should be installed. pytest can be installed as below in Command line
using pip.
pip install pytest
pytest expects python programs to have test either as prefix or suffix. Then
pytest can be run as follows:
This can be used in a verbose mode:
The difference between pytest and doctests is given in Table 17.8.
Table 17.8: Differences between Doctest and Pytest
Table 17.9: Differences between Pytest and Unittest
Example 12: Illustration of pytest(Listing8 - stored as test_sample.py).
The above script can be stored as test_sample.py. For this section, the
Anaconda edition (Appendix 2) is used to test the pytest. The script is
executed in the Anaconda environment command line and the result is
shown below.
It can be observed that the program passed the test in 0.02 seconds.
Multiple test cases also can be designed. A package of tests across multiple
Python files can be created and can be combined as a package.
The syntax test_sample.py is used to create multiple tests.
pytest.raises()
If any test raises an execution, then the test is passed. Otherwise, the test is
considered a failure.
Example 13: Illustrate the use of pytest exceptions (Listing9.py) stored
as - test_sample_pytest.py
Here, the test case raises an exception.
17.3 Introduction to Profiling
The requirement of good programming is to create efficient programs. The
characteristic of efficiency is time. A program is supposed to run faster.
Chapter 2 introduced the concept of time complexity. There are two ways of
finding complexity. One way is to use asymptotic analysis. Another way is
to use empirical analysis. In the empirical analysis, a code is written, and
the code is executed using benchmark data. This is called profiling.
A profiling activity finds outs the bottlenecks in the code. By eliminating
the bottlenecks, one can improve the performance of the program.
17.3.1 Python timeit()
At various times, one needs to measure the execution time. Python module
timeit is used to measure the execution time. The syntax of timeit is given
below.
The statement (s) can be single or multiple statements separated by a semi-
colon. Setup is the details that need to be executed before the statement.
The default is pass. The timer is a default value and the default number is
10 × 106. This implies that Python executes a statement one million times
and finds the minimum value.
Example 14: To illustrate the usage of timeit().
It can be observed that the module timeit is imported. Then timeit is used to
measure the execution time.
The program can be run from the terminal as
Various statements can be given separated by semi-colons. This is
illustrated in the following example as well.
Example 15: To illustrate the usage of timeit for multiple statements.
The following example illustrates the profiling of modules. The test code is
written with Python statements, the module can be imported using setup.
Example 16: To illustrate the usage of timeit for modules(Listing10.py).
Instead of timing the statements, functions, and modules, Python has two
profiling tools for profiling the entire code. The popular profilers are.
1. profile
2. cProfile
Both tools are used to analyze CPU cycles that are required to run the
Python script. Both profiling modules hold a common interface.
Some of the other popular profilers are:
1. Line profiler: find the individual line execution time.
2. Memory profiler: discover which line takes more memory.
3. Profile hooks: help profile using decorators.
4. Snakeviz: a graphical profile viewer.
17.3.2 cProfile
cProfile is popular compared to profile because of its lesser overhead. Any
module can be imported using cProfile. Let us try to find a profile of simple
statement math.sqrt(144).
Example 17: Illustrate the use of the cProfile tool.
Example 18: Illustrate the use of the cProfile tool(Listing11.py)
The output shows the profiling of the above two functions. Calling profiler
from the command line.
The profiler can be run from the terminal.
It can be observed that so much statistical details are present.
It can be observed that the following important information is present in the
screenshot shown above.
Important statistics are given in Table 17.10.
Table 17.10: List of Metrics
Python comes with many reporting tools. pstats is a library that is used to
analyze the output of the profiler. It filters and sorts the data of the profiler
and helps to understand better. It can take the dump of data of cProfile,
apply many filtering options, and can generate reports that can be easily
read by humans.
Summary
1. Errors are common in a program. The removal of an error is called
debugging.
2. Code tracing, ducky debugging, and print statements are the primitive
ways of debugging.
3. Pdb is an inbuilt Python debugger.
4. One can import pdb from the interactive prompt and the terminal.
5. The commands step and next help us to examine a Python program.
6. Pdb allows us to pause the program, verify its variables, and identify the
errors in the program.
7. A breakpoint is a spot where the program stops its execution and allows
us to inspect the values.
8. Profiling helps us to profile a program.
9. cProfile is a common profiler to find function performance assessment.
10. pstats is a library that is used to analyze the output of a profiler.
11. Testing is executing a program to check whether the program gives the
expected results.
12. Doctest is the simplest test for testing a program.
13. Unittest is one of the simplest test tool.
14. Pytest is one of the simplest ways of testing a program.
Glossary
Debugging: the removal of software errors is called debugging.
Code tracing: a brute force technique for debugging using paper and
pencil.
Ducky debugging: a brute force technique for explaining code to
uncover errors.
Continue: a command that allows execution of the debugger till the
finish or breakpoint or exception.
Breakpoint: a spot where the pdb stops its execution and allows us to
inspect variables.
Call stack: a call stack is one that has all declared variables and function
arguments.
Profiling: finding the bottlenecks of a program by assessing it.
Total time: total time taken by the function.
Per call: total call divided by the call count is called per call.
Cumulative time: time is taken by functions and their sub-functions.
pstats: a library that is used to analyze the output of a profiler.
Testing: an activity of removing errors and checking whether the
program gives the expected results.
Test automation: a program that tests another Python program.
Test driven environment: TDD is an environment where test cases are
written before the coding is done.
Doctest: the simplest tool for testing a program.
Unittest: a testing tool for testing a Python object.
Assert: a command to assert a condition.
Conceptual Questions
1. What is meant by code tracing?
2. Explain the role of print statements in finding errors in a program.
3. Explain briefly how pdb can be invoked.
4. Distinguish between the step and the next statement.
5. What is a breakpoint? How a breakpoint can be created?
6. List out at least six commands for manipulating a breakpoint.
7. Explain how cProfile can be invoked.
8. Explain the role of empirical algorithm analysis.
9. List out the metrics available when a profiler is used.
10. Explain the role of pstats in report generation.
11. What is test-driven development?
12. What are black-box and white-box testing?
13. What is an assertion statement?
14. Explain the doctest.
15. What is a pytest? How is it different from the unittest?
16. What is the unittest? List out the advantages and disadvantages of
unittest.
Programming Exercise
1. Write a function for finding a prime number. Introduce a known bug.
Use Pdb to debug it.
2. Write a function for finding a perfect number and use Pdb to debug it.
3. Write a program for finding linear and binary searches. Determine the
profiling of both functions and justify the better search method based on
profiling results.
4. Apply a profiler for a Python program that finds a perfect number.
5. Write a function for multiplication and division. Apply to a doctest to
verify it.
6. Use unittest for testing a multiplication program.
7. Write test cases for finding a prime number.
8. Write test cases for finding the perfect number.
9. Use unittest for checking whether a number is a perfect number or not.
10. Use the pytest to verify whether a number is a prime number or not.
OceanofPDF.com
Good design adds value faster than it adds cost.
– Unknown
CHAPTER 18
File Handling
This chapter provides an the overview of files and file handling in Python.
This chapter gently introduces the concepts of files, types of files and file
handling in Python. This chapter also assists in recognizing the differences
between the processing of prepared and raw data files, complicated
serializing objects for storage, and constructing a transaction processing
program using random-access file processing.
Learning Objectives
After completing this chapter, the reader will be able to:
Create, read, write, and update files.
Become familiar with sequential-access file processing.
Understand text processing in Python.
Differentiate between a text file and a binary file.
Open a text file for input/output and write strings or numbers to the file
Learn about the binary files and Excel files
Learn about context managers
18.1 Introduction to Files
Python programs are written or coded to assist in storing, retrieving, and
manipulating data or information in a file. It also possible to use stored data
to carry out numerous operations in the future. For example, in student
admission system, a student registers for a course, and gets admitted later
into the course. The student submits personal information, which is stored
in files or databases. The student might use these stored details later
academic activities.
Data input is typically required whenever Python is asked to perform a task.
When the task is finished, the results (say calculations) are returned to the
user in the desired output format. There are obligatory mechanisms and
tools for processing the instructions and data. Data can be read, written,
manipulated, and stored in various ways in Python, just as in any other
programming language. The computer keyboard and the input data file are
the two most common methods of input that concern us. The output data
file and the computer screen are the same. With the keyboard and computer
screen as input and output, it is started. Users need not import a library to
read and write files using Python.
A file is a specific area on a computer’s hard drive designated for storing
data and a collection of bytes. This device can permanently store data in
non-volatile memory (e.g., hard disk). Storing files in these locations
ensures that they remain accessible even after the computer has been shut
down. Most of the current file systems include three primary parts.
Data: the file’s contents, as written by the author or editor, are called
data.
End of file (EOF): special character used to denote the end of a file
(such as ‘EOF’).
Header: the file’s header contains information about the file’s contents
(file name, size, type, and so on)
File Access Modes
File access mode defines what operations can be performed. There are
many access modes in Python,namely,
1. Read Mode,
2. Write Mode and
3. Append Mode.
Read Access Modes
1. Read only (‘r’): when a text file is opened for reading, its handle is
placed at the beginning. An I/O error is thrown, if the file does not exist.
This is the default mode.
2. Read only in binary format (‘rb’): this opens the file for reading
exclusively in its native binary format. It is helpful in working with
binary file types.
3. Read and write (‘r+’): the handle jumps to the file’s beginning when
it’s opened for reading or writing. Unexisting files cause I/O errors.
4. Read and write only in binary format (‘rb+’): opens a file in the
binary format, allowing reading and writing functions to take place.
Write Access Modes
1. Write only (‘w’): a handle is attached after opening a text file for
writing (‘w’). A new file is created if it doesn’t exist. Existing files are
truncated and overwritten.
2. Write only in binary format (‘wb’): the binary file format is the only
one that can be used to open the file so it can be written in.
3. Write and read (‘w+’): ‘w+’ moves the handle to the start of a read-
write text file. Data are clipped off and replaced as long as the file exists.
4. Write and read-only in binary format (‘wb+’): this feature enables
users to read from and write to files formatted in binary.
18.2 Types of Files
How many distinct types of data files are there in Python?
Python files can be divided into two categories: (i) files in human readable
format, and (ii) files in machine readable format, and (iii) files in Comma
Separated Values (CSV) format.
Text Files
The human-readable text files are used to store characters or strings of data.
Text files store data in ASCII and Unicode character sets. Text files are
arranged linearly by the number of lines. An array of characters is used to
hold data in each line. This is what is usually seen as the code or the syntax.
End of Line (EoL) characters are used to mark the end of a line. The comma
or a newline character is often employed among the many forms. It signals
to the interpreter that a new line has begun and ends the current one. The
backslash represents the beginning of a new line. The interpreter
understands that after the backslash, a new line will be started, Text files are
saved with the .txt extension. The file is the default extension, if the file is
saved without any mention. Text files are made up of bytes. Hence, they
might also be considered binary files. Text files are human-readable and use
Unicode character sets. For example, handlefiles.txt. Some more examples
of text files are configuration (like cfg, ini, and reg), documents (like txt,
tex, and RTF), source code (like c, app, js, py, and java), tabular data (like
CSV, and tsv ), and web standards (like HTML, XML, CSS, and JSON).
Binary Files
The computer stores data or information as binary files in its memory.
Binary files are non-text files and have a sequence of zeroes(0s) and
ones(1s). A binary file saves its contents in the binary format, zeroes(0’s)
and ones(1’s). The information contained in a binary file is represented
entirely by its byte count. The essence of binary files mandates that they
may be processed by an application that is knowledgeable of or fully
understands the file’s structure. A line in binary file does not have a
terminator or delimiter of any kind. Text files necessitate translators
because machines rarely comprehend the format of a text file. Machines can
read binary format text; and hence do not need a translator to translate those
files. Examples of binary files are archive files (like .zip, .rar, .iso, and .7z),
audio files (like .mp3, .wav, .mka, and .aac), database files (like .mdb,
.accde, .frm, and .sqlite), document files (like .pdf, .doc, and .xls),
executable files (like .exe, .dll, and .class ), image files (like .png, .jpg, .gif,
and .bmp), and video files (like .mp4, .3gp, .mkv, and .avi).
CSV Files
Plain text files only contain printable ASCII or Unicode characters. In
Python, accessing excel files is a common activity for various purposes.
One such important file format used in excel is Comma-Separated Value
(CSV) format. Commas are used to separate data values in CSV files.
Appropriate formatting is needed to organize CSV tabular data successfully.
This is how it appears to be:
Example 1: The appearance of CSV files.
#This is the screenshot of the output
Most spreadsheets start with the name of a data column. Though
widespread, the comma is not the only delimiter. Delimiters also include the
tab (t), colon (:), and semi-colon (;). CSVs are easy to program. CSV files
can be used in any language that accepts text files and manipulates strings.
CSV file can be analyzed and be used to produce graphs or reports.
18.3 Input and Output Operations
At runtime, the input() and print() functions allow users to enter data while
programs run and display the program’s output on a screen.
Steps Involved in the File Input/Output Operations
To tackle real-time challenges, data or information must be inputted and
outputted in the form of files. The three general steps involved in file
input/output operations are :
(i). File opening, (ii). read or write (operate), and (iii). File closing
For a better comprehension of the input/output operations that Python
carries out, it is vital to have an in-depth description of the three general
steps that make up the process.
1. To open a file: Use Python’s built-in open function or open() command
to acquire and open a file object. The open function returns a file object
when used. File objects have methods and attribute to collect/manipulate
file information. This function returns a file object and is also called a
handle (because it reads or modifies files). The syntax to open a file is
as shown below.
To open a file in text mode or binary mode, choose whether to read “r”
or write “w” or append “a” to the file when doing so. Text mode
reading is the default mode for reading a file; in this mode, strings are
returned as they are read. The binary mode returns bytes for non-text
files, such as images or executables, which is why this mode is most
commonly employed. Specifying the encoding type while working with
text files is highly recommended. The modes of opening a file is listed in
Table 18.1 and the important attributes available with the file_object is
listed in Table 18.2.
Table 18.1: Modes of opening the file and their Description
Note: Basically, a file can be opened in read, write or append modes. In
above table "+" is used with "a", "w" and "r" to add additional
functionalities either reading or writing.
Table 18.2: Attributes associated with File Object and its Description.
2. To close a file: A file must be properly closed when all activities have
been completed. The Python close() frees up resources that a file object
had previously held. The close() method does not delete the object
(deletion of variable in Python environment) and I/O operations are not
permitted on the closed file but basic information (Attributes and its
values) is available with the File Object.
The syntax to close a file is given below:
Here is a list of all attributes related to the file object.
3. To read from a file: A file can also be read in Python using the open
function. Calling open returns the file object(). To begin, enter the file’s
name to be read from and then assign the access_mode to the second
argument, as shown in the syntax below. The syntax to read a file.
In the above syntax,the parameter size is optional. In the access mode,
“r” is used for reading. Access mode “r” is optional. The read(size)
method can be used to read data of a specific size. If the size parameter
is left blank, it will read and return the entire contents stored in the file.
The other ways of reading a file in Python is listed in table 18.3.
4. To write into a file: To write a file, open the file in write (‘w’), append
(‘a’), or exclusive creation (‘x’) mode to write into it. Be cautious while
using the ‘w’ mode, as it overwrites any existing data in the file. write()
method returns the number of characters written to the file and can write
a string or bytes (for binary files). Remember to close the file handle at
the end of each session, especially when writing to a file.
The following table 18.3. contains a complete listing of all the methods
accessible when working with a file object.
Table 18.3: File Methods in Python
In the following subsection, an examination of the input and output
operations relevant to the various types of files is carried out.
18.4 File Handling Operations in Text Files
1. To open a text file: Python’s open function(open ()) creates text file
objects and opens the file. File objects can modify file data using methods
and attributes. This function can handle the file by reading or modifying
the text files. The syntax to open a text file as follows.
Note:
The upcoming examples are used with ‘sample.txt’ which is a text file
stored in the current working directory of the Python Instance. The content
of the file is as follows:
‘This is an example for opening of an text file in Python. File Handling in
Python is very easy. Python could handle any type of files with necessary
libraries available.
In this chapter, file handling is covered effectively. ‘
Example 2: The program exhibits the opening of a text file.
Users can open the text file in text mode.
Strings are returned as they are read in the text mode of reading, which is
the most common way of reading a file.
2. To close a text file: after completing all operations to the file, it is
necessary to close it correctly because the files’ resources will be freed up
using the Python close() method. The syntax to close a text file as follows.
Example 3: The program exhibits the opening and closing a text file.
3. To read from the text file: The open function reads a Python file. The
open() is used to return the file object called file handle.
There are three ways to read text from a text file using the file object.
read(size = n) is a function that may be used to read the entire contents
of a file and output them as a string. It is useful for editing a small file’s
text. The parameter size is an optional arguments for the function read().
The value ‘n’ for the argument size, represents the size of the data to be
read in bytes. This method comes in handy for reading a file to the new
line. read() returns ‘\n’ for newline. When the file ends, an empty string
is obtained.
readline() is a function that allows for the line-by-line reading of a text
file. It returns a string representation of each line it reads and produces
this representation for each line it reads. This method reads a file till the
newline, including the newline character. A file can be read line by line
using a for loop, and it is both efficient and fast.
readlines() is a function used to read a complete text file and returns a
list of strings that match each line. This method is efficient and fast and
may be used to read a file line by line. As a final result of the readlines()
method, users will get a list of all remaining lines in the file. When the
EOF is reached, all these reading methods return empty results.
Example 4: The program shows to generate a text file.
First, generate a text file. To save a text file,use the same directory as the
program. Assume the naming of the text file as sample.txt by the
application. To read files, open the file in reading mode. There are
numerous methods available for reading files. Use the read(size) method to
read in size number of data. If the size parameter is unspecified, it reads and
returns to the end of the file.
Example 5: The program shows to print a text file.
Example 6: Illustration on how to read a text file using readlines().
(Listing1.py)
#This is the screenshot of the output
Example 7: Illustration on how to read a text file using readline().
(Listing2.py)
#This is the screenshot of the output
4. To write into the text file: writing to a file is easy. The file handle
object’s write() method is used to write the data. The syntax for the write()
method associated with the file object is as follows.
The str in the above syntax represents the string that needs to be inserted in
the file. The write() and writelines() methods write strings to text files, and
they are the two ways to write content to a text file using the file object.
write(): Text is written to a file using the write() method.
writelines(): Write a string list to a file using the writelines() method.
Rather than only accepting a list, the writelines() method also takes
different iterable objects.
Example 8: The program shows how to open and write to a text file.
#This is the screenshot of the output file
This program creates a new file named ‘sample2.txt’, if it does not exist. If
it does exist, it is overwritten. The newline characters are introduced to
distinguish different lines. Never, ever, ever forget to close the file handle
again when writing to a file.
Example 9: The program shows how to append contents to the
sample2.txt file.
To append contents to the sample2.txt file, open the document in append
mode first.
#This is the screenshot of the output file.
The lines that are typed in by the user is added to the existing three lines.
There are still more programming exercises for the reader to try out on
write data, write multiple data, read data, read multiple data, appending
data, update data, search data, and delete data to a text file.
18.5 File Handling Operations in Binary Files
A binary file can be anything, including binary document formats (Word
and PDF), binary image, and audio files, such as JPEG, GIF, or MP3. The
format of binary file format does not contain readable characters.
Example 10: Illustration to open,write and close a binary file
Curate a list, “magicnumbers_physics”, of numbers with the values 2, 8, 20,
28, 50, 82, and 126. In the next step, the bytes() function is used to
transform the list into bytes, as shown below.
For writing the bytes-converted list, a new file called “binary.txt” is opened
in “bw” mode, and after that, the file object is closed, as shown below.
To read the above stored binary file, the file is opened in “r” mode and then
read using “read()” function as shown below.
18.5.1 Pickle
Converting the structures (lists, dictionaries, etc.) into a byte stream just
before writing to the file is called “object serialization”. “object
marshaling”, or “object flattening”. The pickle module serializes and
deserializes Python objects. “Pickling” converts a Python object hierarchy
into a byte stream, while “unpickling” converts a byte stream into an object
hierarchy. To execute an object serialization procedure on the binary files,
use the dump() method. Bytes are returned as a result of the dump()
method. dump() serializes an object hierarchy. Pickling saves integers,
strings, tuples, lists, and dictionaries as binary files. The syntax for pickling
in Python.
To read data from a binary file or object, the pickle module uses the load()
function. Load() de-serializes data streams. The syntax for unpickling in
Python.
Write multiple data: operating system built-in routines cannot write
various data sets to a binary file. Writing a binary file in the “wb”
mode overwrites existing files of the same name before creating a new one.
Example 11: Illustration on how to write multiple data to the binary
files. (Listing3.py)
Import Pickle. Crete and open a file, named “binary.bytes” for writing in
binary mode. Form an empty list for patient records. Enter the while loop.
Initialize all user inputs. To the empty list add patient records with various
attributes. In the while loop(), start collecting patient data.
The user is prompted to provide more details like Doctors name, patients
name, age, symptoms, and body temperature. All data is delivered to the
binary file using dump(). Execution of this code is continued until the
while() loop condition is met. If the user selects ‘ press 0 or else any key0,’
the loop ends. Dump() function is used to output a sequence of bytes. At
last the file is closed using close().
The load() function is utilised, by the pickle module to read data from the
binary file.
Close() file after completing operations, or they would not commit. Also,
binary file data is not human-readable; consequently, use load() to read it.
The learner is advised to work on some more file handling programming
exercises like- write data, write multiple data, read data, read multiple data,
append data, update data, search data, and delete data to a binary file.
18.6 File Handling Operations in Excel Files
Programmer can do tasks like reading and writing to an Excel sheet file
(i.e., Excel Files [xlsx, xlsm, xls]) or CSV Files using Python’s built-in
support for this. Programmers can carry out these activities owing to a
variety of libraries that are accessible.
Example 12: Illustration on how to read an excel file with openpyxl.
(Listing9.py)
To read and write the CSV file: CSV is the most used import/export
format for spreadsheets and databases (CSVs). RFC 4180 tried to
standardize CSV’s description. Due to a lack of a standard, different
applications produce and consume data with subtle differences. These
differences make processing CSV files from diverse sources difficult.
Delimiters and quote characters can vary, but the general syntax is uniform
enough that a single module can quickly process such data. Write this data
in Excel’s preferred format” or “Read data from an Excel file” can be said
without knowing Excel’s format. Programmers can define their CSV forms
and other applications’ formats. The CSV module has data reader and writer
objects. DictReader and DictWriter read and write dictionary data.
Example 13: Illustration of on how to read a CSV file.
Example 14: Illustration on how to write a CSV file.(Listing4.py)
The above code is stored as Listing4.py and executed as follows.
On above execution, the university.csv file looks like below.
18.7 Python Context Manager
Consider a program that utilizes sensitive resources, such as a file, database
connection, or both. File operations and database connections are common
computer language resource uses. Files, locks, and network connections are
external resources. Due to their scarcity, these resources need to be
judiciously managed by the developers. Context managers in Python help
us deal with these sorts of problems.
The key challenge in managing resources is returning utilized resources
to their original states. They will leak resources and slow or crash the
system, if not free. Leaks can also occur, if the developer forgets to free
up the resources.
A prevalent issue is creating new database connections but never closing
or reusing the old ones. When it happens, the database server might stop
processing new connections. This may require a database administrator
to log in and end dead connections.
Coders have trouble editing files. Buffering is used for file based text
writing. When the write() method of a file is called, data are not written
directly to the file but rather to a buffer. Developers risk losing data, if
they do not check-in. If the buffer is not full, close() it.
Errors and exceptions can derail the resource releasing function.
The developers also need to construct a lifecycle for these scarce
resources, including initialization, resource release, and so on. This is a
hassle for developers.
The solution to all the above issues and users wanting an automatic
resource allocation and release system makes use of the Context manager.
Context managers are like generators but only produce a single value. A
context manager creates, manages, and destroys an execution context. The
context manager can either be a method or a class. Context managers
allocate and release valuable resources exactly when the necessity arises.
Python’s context managers help manage resources. It has two phases,
namely the setup phase and a teardown phase.
General strategies usually adapted for resource management are:
1. A try-finally structure will be dealt with in detail in Chapter 18: errors
and exceptions.
2. A structure using with.
3. Using generators.
4. Using class.
With the generator, class will be dealt with in this section.
Context manager using WITH: a context manager in Python is often
used as an expression in the “with” statement to aid in the automatic
management of resources. The frequently used method to invoke a
context manager is using a with statement. Common with statement
syntax is as follows.
This can also be restated as.
for better understanding.
Context managers sandwich code between the start and end code blocks.
They use with block.
Upon bumping into a with statement, Python establishes a new context.
Optionally, context returns an object.
Python cleanses up after bumping into the with block.
The “x” scope is the same as with, because “x” can be accessed before
and after the with statement.
Example 15: Illustration of the automatic management of resources,
using the context manager - “with” statement.
Write mode is activated on a text file that has been given the name
“sample.txt.” By utilising the ‘with’ command, it is possible to efficiently
allocate the “sample.txt” file and release its contents. The data can be
written into the file that opened by utilizing the “opened file.write()”
method.
Reading contents from a file using Context Manager
Read mode is activated on the “sample.txt.” text file. The data contained in
the opened file is read by utilizing the “opened file.read()".
Example 16: Illustration of the decorator @contextmanager creating a
new generator out of the wrapped function and returning it.
The contextlib module of the Python standard library defines the
ContextManager class, whose object effectively manages a program’s
resources. Contextmanager API is called with the function name (openfile)
as its argument. Python decorators aid to modify function or class
behaviour. In the ensuing example the decorator @contextmanager wraps
the function into a new generator and then returns it. Decorators thus helps
to simplify code and promote logic reuse.
Using the returned Generator - The ContextManager can support Python’s
“with” keyword. The file is opened, and then some information is written to
it. The “.seek()” function is then utilised in order to advance to the initial
point of zero. After that, the contents are read using the .read().
Context manager using CLASS: a class File(object) is created with
three functions, namely
__init__ method to open the file.
__enter__method to open the file and return it.
__exit__method to close the file.
Example 17: Illustration of the Context Manager as CLASS.
(Listing5.py)
The file is opened, and then some information is written to it. The “.seek()”
function is then utilised in order to advance to the initial point of zero. After
that, the contents of the class file are read using the .read().
Summary
Having read this chapter, the reader should have a firmer grasp of files and
how to work with them in Python. Reading, writing, updating text, binary,
raw, and csv files, and understanding Python text processing along with
File, and folder management were covered.
1. A file is a collection of bytes.
2. File Access Mode defines what operations can be performed. The
position of the File Handle determines the extent to which the file can be
read (Read Access Modes) or written(Write Access Modes ).
3. Files can be divided into two categories: (i) files in human readable
format, and (ii) files in machine readable format.
4. Python carries out the file input/output operations, in three steps, namely,
(i). Open a file, (ii). Read or write (operate) and (iii). Close the file
5. Use Python’s built-in open function or open() command to acquire and
open a file object.
6. A file must be properly closed when all activities have been completed.
The Python close()frees up resources that a file had previously held.
7. The seek() method can be used to modify the current file cursor
(position) in a file (in several bytes). This method is efficient and fast
and may be used to read a file line-by-line.
8. A file’s lines can be read using the readline() technique.
9. To read and write to an Excel sheet file i.e., Excel Files (xlsx, xlsm, xls)
or CSV Files (Comma separated values) use Python’s built-in support.
10. Python’s context managers help judiciously manage resources.
Glossary
Binary Files - Binary files are non-text files with zeroes(0s) and
ones(1s). This file saves its contents in binary format, 1 or 0. The
computer stores memory in binary.
CSV Files - (Comma Separated Values) file names reveal its structure.
Commas are used to separate data values in CSV files.
Data-The file’s contents, as written by the author or editor, are called
data.
File access mode-File access mode defines what operations can be
performed. The position of the File Handle determines the extent to
which the file can be read or written.
Object serialization: Converting the structures (lists, dictionaries, etc.)
into a byte stream just before writing to the file is called “Object
serialization,” “ Object marshalling,” or “ Object flattening.”
Openpyxl is a Python library that reads and writes to an Excel file.
Pickling converts a Python object hierarchy into a byte stream. Pickling
saves integers, strings, tuples, lists, and dictionaries as binary files.
Text Files – Human-readable text files are used to store characters or
strings of data. Text files are made up of bytes.
The dump() function is used to serialize an object hierarchy.
The load() function is used to deserialize a data stream. To govern
serialization and de-serialization, construct a Pickler or Unpickler object.
Unpickling-In Python, “unpickling” is the process of converting a binary
file or bytes-like object into a byte stream.
Conceptual Questions
1. What are file access modes?
2. What is meant by opening and closing files?
3. Write the syntax and parameter details of open file in Python.
4. List out the different modes of opening a file.
5. What is meant by file handling?
6. List out the attributes of the file object.
7. Give syntax along with the details of their parameters.
1. To open a file.
2. To close a file.
3. To read from a file).
4. To write to a file.
8. Mention the difference between read and write methods?
9. Which are the two built-in functions to read a line of text from standard
input that comes by default from the keyboard?
10. What is the use of seek() method in files?
11. What is pickling?
12. What is unpickling?
Try-Out Questions
1. What is the output for the following code:
import pickle
def write():
file=open(“binary.txt”,’wb’)
records=[]
while True:
data=input(“Enter your ID number: “)
records.append(data)
ch=input(“add more reacords: “)
if ch== ‘n’:
break
pickle.dump(records,file)
file.close()
write()
2. What is the output of this program?
age_of_familymembers = [55,45,28,29,26,2]
x = age_of_familymembers
print(“Age of family members are = “, x)
#Converting Integer to Byte Array
y = bytes(x)
print(y)
fd = open(“binary.bytes”, “wb”)
fd.write(y)
fd = open(“binary.bytes”,’rb’)
print(fd.read())
3. Given, contents to be stored in the sample.txt file is
"Time management is life management."
fopen = open(“sample.txt”, “r+”)
y = fopen.read(29);
print (“Read String is : “, y)
4. What is the output of this program,when python1.txt is given below:
Python1.txt
Fintech, a combination of the terms “financial” and “technology,” refers
to businesses that use technology to enhance or automate financial
services and processes.
fopen = open(“python1.txt”, “r+”)
z = fopen.read(19);
print (“Read String is : “, z)
position = fopen.tell();
print (“Current file position : “, position)
z = fopen.read(17);
print (“Again read String before reposition is : “, z)
position = fopen.seek(0, 0);
z = fopen.read(36);
print (“Read String after reposition at the beginning : “, z)
5. What is the output of this program?
fsource = open(“example.txt”, “w”)
fsource.write(“In this manner we can write into the file\nIsn’t it
interesting!\n”)
fsource.close()
Programming Exercises
Use the following text files for the programming exercise.
specimen1.txt
Fintech, a combination of the terms “financial” and technology,” refers to
businesses that use technology to enhance or automate financial services
and processes.
specimen2.txt
Python is one of the famous programming language.
Python is used in the Financial Analyses and other processing.
specimen3.txt
Python is useful in storing and retrieving data into a file including database.
The data analysis is made easier in Python which make add fuels to its
popularity.
Python adopts to any upcoming trend in IT industry.
1. Create a text file and then display it using a Python script.
2. Create a Python program that generates a file containing a list of all
English alphabet letters arranged in a specific order.
3. Create a Python program that reads the named specimen2.txt file line-
by-line and count the number of lines.
4. Create a Python program that reads a random line from a file
(specimen2.txt file)and outputs it.
5. Create a Python program to merge each line from the first
file(specimen1.txt file) with the equivalent line from the second file
(specimen3.txt file).
6. Create a Python script to count the number of words in a file.(
specimen2.txt file)
7. Find the longest words in the specimen3.txt file by writing a Python
program.
8. Write a program that reads the first n lines of a given file.
OceanofPDF.com
“Design is not just what it looks like and feels like. Design is how it works.”
- Steve Jobs.
CHAPTER 19
Introduction to Graphical User Interface
This chapter discusses the Graphical User Interface (GUI) and its designing
process in Python, along with various widgets.
Learning Objectives
At the end of this chapter, the readers will be able to learn the following
topics.
Understand the basics of a Graphical User Interface.
Learn the steps involved developing a GUI in Python.
Learn frequently used widgets in Python for GUI.
Learn how to process the data through the widgets in GUI in Python.
19.1 Introduction
A user interface (UI) is a software component used for communication
between users and computers. A Graphical User Interface (GUI) is a type of
UI that uses graphics, such as icons or widgets for interactions. The icons
comprise small images. Various types of devices, such as stylus pens touch
screens, and conventional screens, are used for interactions with computers.
Tasks associated with GUI components are executed based on users’
interaction. This makes the GUI program keep track of the state of the
processes and responds to the interactions without fail. GUI is used to
develop mobile applications, games, and human-machine interfaces (such
as kiosks, vending machines, and industries), and so on. Python is one of
the most friendly programming languages that is used for GUI
programming. Python provides many packages and libraries for the
development of GUI. The widely used GUI packages in Python are Tkinter,
QT5, PyGUI, and PySlide2. Among GUI packages, Tkinter has a wide
range of applications. This chapter deals with the development of GUI
using the Tkinter package.
19.2 Tkinter Module
In Python, the package tkinter is used to develop the GUI using Tcl/Tk
toolkit. The package tkinter is a default package in Python. Tool Command
Language (TCL) is a dynamic interpreted programming language that is
commonly embedded into C as an interface to Tk Toolkit. Tk is a package
developed in the TCL package and it is implemented in C Language. The
Tk is used to create and manipulate GUI widgets. Each Tk object embeds
its own TCL interpreted instances with Tk loaded into it. There is a new
family called Ttk, which is themed Tk that provides a much better
appearance than classical Tk. The TCL, tk, and Ttk are available as classes
in the tkinter package, such as tkinter. TCL, tkinter.Tk, and tkinter.ttk,
respectively.
Using tkinter, most of the GUIs are developed. The steps to be followed for
importing the package tkinter while creating GUI are as follows.
1. Creation of main window: The main window of the application holds
other components and widgets that are going to be added after the
statement. This is executed by creating an object for the class Tk
available in the package Tkinter, which constructs the top-level Tk
widget. Each object of the class Tk is associated with the Tcl interpreter.
The syntax for creating the main window by creating an object for the
class Tk is as follows.
2. Adding widgets to the main window: Widgets are the elements of GUI
for user interaction, for example, text box, button, and so on. Various
widgets are added using the functions from the class Ttk in the package
tkinter.
3. Applying event triggers: Various events that are needed to be
associated with the widgets are added. For example, consider an example
of getting values for x and y, after clicking on the calculate button in the
widows, the event of the addition of the two numbers x and y should be
displayed.
4. Initiating mainloop(): The mainloop() method, which is associated with
the instance of the class Tk, is used when the application is in the state of
ready to run. The main loop() function makes the application run in an
infinite loop, executes the responses to the events, and remains the
window to open unless the close button is clicked. The syntax for the
main loop() function is as follows.
19.2.1 Display Text with Label Widgets
The label widget refers to the display box that displays either texts or
images. Usually, the labels are used to display information and instructions,
identify the controls, and feedback. The syntax for inserting the label
widget into the main window is as follows.
In the above syntax, an instance is created for the class Label in the module
ttk that contains arguments parent_window, which is the object of the class
Tk and another argument text comprises the text to be printed. The
following simple example illustrates the use.
Example 1: Create a simple window that displays “Hello, Welcome to
GUI programming".
As the initial stage, the package tkinter is imported as follows.
In the above execution, the statement “from tkinter import *” imports the
functions and classes in the packages from the package tkinter. The module
ttk is imported using the statement “from tkinter import ttk”. After
importing, the main window is created as follows.
In the above execution, the object main_window for the class Tk is created.
The title for the main window is set using the method title associated with
object main_window with the parameter as the string “Example 1”. The
label is created using the Label class from the module ttk, and the result is
stored in the variable label_widget with the arguments main_window
(instance of the class Tk) and the text argument of “Hello, Welcome to GUI
Programming”. After creating the label_widget, the pack method associated
with the label_widget is executed to implement the change in the window.
After adding the label widget, the mainloop() method associated with the
main window is invoked as follows.
On executing the above code, the dialog box would be displayed as shown
in Fig. 19.1. In Fig. 19.1, the title of the dialog box is “Example 1” as the
argument given for the method title() and the text is the label displayed in
the dialog box.
Fig. 19.1: The Output Dialog Box for Example 1
Example 2: Illustration for having an image as a label. (Listing1.py)
To insert an image as the label, the following Python code is used.
In the above code, lines 1–3 are used to import tkinter and the module ttk.
Lines 6 and 7 are used to create the main window. Lines 10–13 are used to
create a label widget for the main window. In line 10, an instance
label_widget for the class Lable in the module ttk is created with the
argument as main_window. The function PhotoImage from the package
tkinter is used with argument file = “CompanyLogo.png” to read the image
from the source and is assigned to the variable image_for_label (line 11).
The read image is assigned to the image attribute of the variable
label_widget as shown in line 12. Then the label variable is executed with
the method pack(). Then, the method mainloop associated with the object
main_window is executed as shown in line 16. The above code is stored as
Listing1.py and executed as follows, and Fig. 19.2 shows the resulting GUI.
The above code would display the dialog box as shown in Fig. 19.2 where
the text “CompanyLOGO” is an image file (refer line 11).
Fig. 19.2: Output for Listing 1.
Example 3: Illustration for styling in Labels (Listing2.py).
Create a dialog box with the text “Labels with Styles” in red colour and a
background in blue colour.
The following code creates a dialog box with text in red colour and a
background in blue colour.
In the above Python code, the variable style has been defined for the
function Style() associated with the module ttk. The configure method is
associated with the variable style where the arguments of the name of the
style as “BW.TLabel”, the foreground attribute as “red” and the background
as “blue” as in line 11. The style is given as an attribute with the Label
function in the Module ttk, as shown in line 13. The above code is saved as
Listing2.py and executed as follows.
On the above execution, the dialog box, as shown in Fig. 19.3, would be
displayed.
Fig. 19.3: The Output for Listing 2.
19.2.2 Button Widgets
Buttons are one of the most widely used in UI for interacting with the user.
The package tkinter allows the button to be composed of text, image, or a
composite of both as well. The syntax for creating a button is as follows.
In the above syntax, the instance is created for the button class from ttk
module with three parameters (the first parameter is mandatory and the
other two are optional), where the first one is the Tk() object that
corresponds to the parent window, the text attributes contains the string that
should be displayed in the button, and the command argument points the
function to be invoked at once the button is pressed.
Example 4: Create a dialog box that contains an exit button to close the
opened dialog box.
In the above execution, the assignment of button_variable is new, and it is
an instance for class Button with arguments as main_window (instance of
Tk()). The text attribute contains the text to be printed in the button, and
command attributes contain the function to be invoked on clicking the
button. Here, it is main_window.destroy, which is the method responsible
for closing the window. The above code would display the dialog box, as
shown in Fig. 19.4, in which the close button is present inside the dialog
box. When the close button is pressed, the dialog box is closed.
Fig. 19.4: The Output Dialog Box for Example 4
Example 5: Illustration for Changing the foreground color of a button
when the cursor is placed on it. (Listing3.py)
Create a dialog box with a button that changes its foreground colour when
the cursor is placed on it.
In the above code, the button function that is available in tkinter is used,
and in which the attribute active foreground is assigned with the value “red”
that makes the text colour red when the button is clicked. The above code is
saved as Listing3.py and executed as follows.
On executing the above, the following dialog box (as shown in Fig. 19.5
(a)) would appear as an output. In the above dialog box, the colour of the
text is changed when the okay button is clicked. The result of changing
colour when the button “okay” is clicked is shown in Fig. 19.5 (b).
Fig. 19.5: Dialog Box for the Program Listing 19.3.
Fig. 19.5 Dialog Box Showing When the Button Okay is Clicked, the Text
is Changed to Red Colour.
19.2.2.1 Radio Buttons
The radio buttons are used to choose one of many options that are available
for the users. The syntax for creating radio buttons is as follows.
In the above syntax, an instance for the class RadioButton in ttk module is
created with one mandatory parameter, parent_window, which is the
window variable of the parent window and the command points to the
function that should be called once the button is clicked.
Example 6: Illustration for Radio Button. (Listing4.py)
In the above code, line 9 starts a variable called value_of_radiobutton of
IntVar(), which is a function in the package tkinter and is used to store
varying sizes of Integer. In lines 10 and 12, the radio button is created with
arguments of main_window, text (which text to be printed, variable where
the value of the selected radio button is stored, and the attribute value have
the value for the radio button). The pack() method is executed once the
radio button is created. In line 15, the value stored in the variable
value_of_radiobuttons is extracted using the get method associated with it.
The above code is saved as Listing4.py and executed as follows.
On the above execution, the output dialog box is shown in Fig. 19.6.
Fig. 19.6: The Output Dialog Box on the Execution of Listing 4.
After clicking the second radio button and closing the dialog box, the
following output is printed in the command prompt or terminal where the
program is executed.
In the above output, one could notice that the value from the Radio Button
is 0.
19.2.2.2 Check Boxes
In Python, the checkboxes are treated as Check Buttons, which are used to
select multiple choices from the list of choices that are presented to the
users. The syntax for Check Buttons is as follows.
In the above syntax, an instance is created for the class CheckButton from
the ttk module. Two arguments, onvalue and offvalue, are new to the
discussion. the onvalue represents the value when the check box is ticked,
and the offvalue represents the value when the checkbox is not ticked by the
user.
Example 7: Illustration for Check Boxes. (Listing5.py)
The following code shows an example for checkboxes where two check
boxes are displayed which are labelled as “Number 1” and “Number 2".
In the above code line 11, the check button with the label Number 1 is
stored with the value 1 if it is ticked and if not, with the value 0. In the
above code line 13, the check button with the label Number 2 is stored with
the value 2 if it is ticked and if not, with the value 0. Finally, in lines 16 and
17, the values stored in the variable value_of_check_button1 and
value_of_check_button2 are printed.
The above code is saved as Listing5.py and executed as follows.
The above execution would display a dialog box as shown in Fig. 19.7.
Fig. 19.7: Dialog box on Executing Listing 5 (Clicked Number 2)
The displayed dialog box is closed after checking the second check box, as
shown in Fig. 19.7. After closing this above dialog box, the following
statements are printed.
19.2.3 Frame Widget
Frame widget comprises a collection of other widgets like a container.
Frame widget is used for grouping and organizing the widgets in the
window. The syntax for the frame widget is as follows.
In the above syntax, an instance is created for the class Frame in the module
ttk to create a frame in the window and it contains the mandatory argument
that is the parent window variable. Other optional parameters shall be
visited on the demand.
Example 8: Illustration for Frames with label and radio button.
(Listing6.py)
The following code displays an example for the frame.
In the above code, when the frame_variable is created before the widget
creation and other widgets that are needed to be placed inside the frame,
then their main_window argument is changed to the frame_variable, refer to
lines 14, 19, and 21.
The above code is saved as Listing6.py and executed as follows.
The following Fig. 19.8. represents the dialog box would be displayed on
the execution of the above code.
Fig. 19.8: Dialog box on Executing Listing 6.
After closing the above dialog box by selecting the radio button
corresponding to Food, the following statements are printed.
19.2.4 Input Text Box
The input text box is named as “Entry” in the tkinter package where the text
box is created, and it is used to receive a single-line text entry from the user.
The syntax for Entry is shown below.
In the above syntax, an instance for the class Entry in the ttk module creates
a text box in the parent_window.
The text boxes can also be created using the text method associated with the
class Tk. The syntax for the text method is as follows.
In the above syntax, the following arguments are used.
1. parent_window: The instance of the class Tk represents the parent
window.
2. width: The width of the textbox.
3. height: The height of the textbox.
4. xscrollcommand: It makes the text box horizontally scrollable.
5. yscrollcommnad: It makes the text box vertically scrollable.
The get method associated with the textbox_variable is used to receive the
user-entered text in the text box. The syntax for the get method is as
follows.
In the above syntax, the start_index corresponds to the starting index from
where the input should be read, usually, the value for the start_index is
“1.0” and for the end_index is “end-1c”. The constant “end” corresponds to
the end of the input that is available in tkinter package but the Text()
function method always adds “\n” at the end of the text box input and so
does not cover the last character “-1c” that is sufficed with the constant
“end".
Example 9: Illustration for text boxes in GUI. (Listing7.py)
The following code creates a dialog box with a text box.
In the above code, the textbox_variable is created with Text() from tkinter
which is from the tk module with parameters as main_window and width
and height as 8 and 5, respectively.
The above code is saved as Listing7.py and executed as follows.
The dialog box shown in Fig. 19.9. would display by the above execution.
Fig. 19.9: Dialog Box with a Text Box for Listing 7.
Example 10: Illsutration for reading data from Text Boxes.
(Listing8.py)
The data in the text boxes are read using the get method associated with the
textbox variable as follows.
In the above code, the text_extraction function is used to extract the text
from text_variable and printed in another label.
The above code is stored as Listing8.py and executed as follows.
The above execution would display the dialog box as shown in Fig. 19.10.
Fig. 19.10: The Dialog Box for Listing 8.
After the display, the statement “Python is User Friendly” is given as input
and the corresponding statement is printed at the bottom, as shown in Fig
19.11
Fig. 19.11: Displaying the Text in the Text Box
19.2.5 Grid
Aligning the widgets is mandatory to show a decent and attractive GUI to
the users that also contribute to increasing the UX of the application. Most
of the widgets are associated with the grid method. The grid for a window
in GUI is divided into rows and columns, as shown in Fig. 19.12.
Fig. 19.12: Grid Template
Fig. 19.12 shows a sample of how a window is divided into 5X5 where five
rows and five columns are there. The indexing of the column and row starts
from 0. In Fig. 19.13, each cell is marked with “(m, n)”, which represents
the mth row and nth column, and variables “N”, “S”, “E”, and “W”
correspond to North, South, East, and West, respectively (shown top right of
the Fig. 19.13) is used to locate the content in a cell. The syntax for the grid
method is as follows.
In the above syntax, the following list of attributes is used:
1. row: the attribute row corresponds to the row number where the widget
should be placed.
2. column: the attribute column corresponds to the column number where
the widget should be placed.
3. sticky: the attribute sticky is used to locate the text in the cell where the
values “N”, “S”, “E”, and “W” are used.
4. columnspan: the attribute columnspan is used to represent the number
of columns to be merged and the column placed.
5. rowspan: the attribute rowspan is used to represent the number of rows
to be merged and the text is placed.
6. padx: the attribute padx corresponds to the padding (the horizontal space
between the neighbouring widget.
7. pady: the attribute pady corresponds to the padding (the vertical space
between the neighbouring widget.
Example 11: An example for illustrating the concept of a grid as shown
in Fig. 19.13 (Listing9.py).
Fig. 19.13: Grid for Example 11.
The above code is saved as “Listing9.py” and executed as follows.
The above execution would display the dialog box as shown in Fig. 19.14.
Fig. 19.14: Dialog Box for Example 9
Summary
1. A user interface (UI) is a software component used for communication
between users and computers.
2. A graphic user interface (GUI) is a type of UI that uses graphics, such as
icons or widgets, for interactions.
3. Four steps are involved in GUI development: creation of the main
window, adding widgets to the main window, applying event triggers,
and initiating mainloop.
4. The label widget refers to the display box that displays either texts or
images.
5. Buttons are one of the most widely used in UI for interacting with the
user.
6. To get the values from Checkboxes, the onvalue and offvalue attributes
are used.
7. Frame widget comprises a collection of other widgets like a container.
8. Frame widget is used for grouping and organizing the widgets in the
window.
9. The grid for a window in GUI is divided into rows and columns.
10. Each cell is marked with “(m, n)”, which represents the mth row and nth
column, and variables “N”, “S”, “E”, and “W” correspond to North,
South, East, and West, respectively.
Glossary
User interface: a user interface (UI) is a software component used for
communication between users and computers.
Graphical user interface: a graphic user interface (GUI) is a type of UI
that uses graphics, such as icons or widgets, for interactions.
Frame widget: a frame widget comprises a collection of other widgets
like a container.
Padding: padding refers to the space between the before widgets or
times.
Conceptual Questions
1. What is meant by an user interface and user experience?
2. What is the syntax for creating a grid for the window?
3. How can the values inserted in the text boxes be extracted? Explain with
an example.
4. Explain how does the grid is works in tkinter.
5. Explain various packages associated with the tkinter package.
6. Explain with an example how data values are handled in check boxes.
Programming Exercise
1. Create a GUI based window to enter data into the database of at least
four attributes recursively.
2. Create a simple GUI comprising two text boxes to receive input for the
value of n and r and display the value of nCr in the Window itself.
3. Create a simple GUI with four buttons with different foreground colours
and background colours.
4. Create a simple GUI containing a text area enabled with horizontal
scrolling.
5. Create a simple GUI to receive value of height and radius of a cylinder
and calculate its volume.
6. Create a user registration form and store the details as an object of User
Class. The attributes of the User Class are first_name, last_name,
address, email, contact_number and aadhaar_number.
7. Create a GUI to Say whether the given number is a prime number or not.
Note: Use Text box and use label to display the result.
8. Write a Python program to receive a text from the user and print the
reversed text in the form label after clicking the reverse button in the
GUI. For example, if “xyz” is given as input and “zyx” is displayed as
the output.
OceanofPDF.com
“The goal is to turn data into information, and information into insight.”
– Carly Fiorina, former CEO, Hewlett-Packard Co.
CHAPTER 20
Introduction to Data Analytics with Python
Data analytics is one of the most emerging areas in computer science.
Python has become one of the most used languages in data analytics now.
This chapter introduces the basics of data analytics.
Learning Objectives
By the end of the chapter, the reader would be able to:
Know the basics of data analytics.
Understand the basics of array processing.
Know about Numpy.
Basics of Pandas.
Acquire knowledge about Data visualization with matplotlib.
Basics of scikit-Learn.
Understand regression basics and its implementation.
20.1 Introduction to Data Analysis
Business organizations use huge amounts of data. Modern technology
makes it easier to capture, process, store, distribute, and transmit digital
information. The data are available in a variety of forms such as flat files,
databases, records, and digital archives. Most of these data are useful for
making decisions.
Data have to be converted to information, which is processed data.
Information includes patterns, associations, or relationships among data.
For example, the sales data can be analyzed to extract information like
popularity of a product launched by an organization in the market and
which product needs to phased out from the market.
Data analytics is an activity that takes the data and generates useful insights
for assisting decision-making in business organizations. The latest areas in
computer science, such as machine learning, data analytics, and big data are
all having data analysis at their core. Data analysis and Data analytics are
terms that are used interchangeably to refer to the same concept. Data
science is an “umbrella term” used to denote everything from the data
capture to the deployment of an application. But there is a subtle difference.
Data analytics is a general term and data analysis is a part of it. Data
analytics refers to the process of data collection, pre-processing, and
analysis of such data. The steps of data analytics are given below:
Fig. 20.1: Steps of Data Analytics
Data analytics starts with a question or hypothesis. It is about the
identification of a problem and, collecting the relevant data. Then comes the
step of data cleaning and data analysis. Data analysis involves collecting
historical data and creating a model. The model can be used for prediction.
Finally, the model results are interpreted for taking business decisions. The
types of data analytics are given in Table 20.1.
Descriptive analytics is about describing the main features of the data.
Descriptive analytics only focuses on the description part of the data and
not the inference part. Diagnostic analytics deals with the question
—’Why?”. This is also known as causal analysis as it aims to find out the
cause and effect of the events. Predict analytics deals with the future. It
deals with the question – ‘What will happen in the future given this data?”.
Prescriptive analytics is about finding the best course of action for the
business. Prescriptive analytics goes beyond prediction and helps in
decision-making by giving a set of actions.
Table 20.1: Types of Data Analytics
20.2 Dataset and Data Analysis
A dataset is collection of data objects. The data objects may have many
attributes. An attribute can be defined as the property or characteristics of
an object. For example, consider the following student dataset, as shown in
Table 20.2. The columns are the attributes, and every row is a record (Data
Object). Data analytics takes structured data like this and generates
information.
Table 20.2: Sample Student Dataset
Every attribute should be associated with a value. The measurement process
measures the values and it associates every attribute with a value. The value
determines the data types. These are often referred to as measurement scale
types.
Types of Variables
A variable is one whose value is changing. A variable can have two types of
values: qualitative data and quantitative data. Qualitative data (or
categorical data) are non-numerical data. For example, a designation is not
numerical data. Quantitative data are measured data that is a number. For
example, the number of cars in a household is qualitative data.
Quantitative data can be continuous data or discrete data. Continuous data
are decimal or fractional numbers that represent a number in a specific
range. Discrete data, on the other hand, are decimal whole numbers and
cannot be fractions. It is countable. For example, the number of cars in a
household is discrete data.
There are four measurement scales for qualitative and quantitative data.
These are listed in Table 20.3.
The categorical data can be divided into two types. They are nominal type
and ordinal type.
Table 20.3: Types of Measurement Scales
Data Analytic Process
Cross Industry Standard Process–Data Mining (DM) model is the popular
model that is used for data analytics. This model involves six steps. The
steps are listed below.
1. Understanding the Business
This step involves understanding the objectives and requirements of a
business organization. For example, an organization may have a lot of
historical data and may wish to explore whether the new product will be
successful or not. This involves a precise problem statement and its
formulation.
2. Understanding the Data
The problem statement may involve the collection of data. Data can be
structured data or unstructured data. Structured data is in the form of a table
or a database. A non-structured data is in the form of text, images, or video.
So, one has to understand the characteristic of the data for data collection.
This stage involves the steps like data collection, understanding the
characteristics of the data, and formulation of the hypothesis.
3. Preparation of Data and Data Preprocessing
The collected data may be “bad data”. For example, collected data may
have missing values, inconsistent data, and noisy data. These data cause
problems in data analytics as these data may cause inaccurate results. This
problem is solved by preprocessing techniques that focus on cleaning the
data.
Good Data Versus Bad Data
In the real world, the available data is “dirty or bad”. By the word “dirty”, it
means that the data may have the following kinds of problems:
The detection and removal of errors in data are called data cleaning. Some
of the data errors are typographical errors or may be due to measurement
processes and structural mistakes like improper data formats. Data errors
also can arise from omission, missing, and duplication of the attribute. A
“Bad” dataset is given in Table 20.4.
Table 20.4: Illustration Table for ‘Bad’ Data
It can be observed that data like –100 for maths marks may be due to
typographical mistakes or collection of data mistakes. It can be observed
that many marks are missing. Age of Andre is 92 and it cannot be an age of
a student. Inconsistent data are data due to problems in conversions,
inconsistent formats, and differences in units. The primary data cleaning
process is missing data analysis. Data cleaning routines clean up the data by
solving the problems of missing data by filling it up, removing noise (by
smoothening), removing outliers, and solving inconsistencies associated
with the data.
4. Modelling
This step involves the application of a data analytics algorithm for data
analysis. The aim may be to get some statistical details or to obtain a model
or pattern. A model may be a formula or procedure that takes test data and
produces the results.
5. Evaluation of a Model
This step involves the evaluation of the models using statistical analysis and
visualization methods. The performance of the constructed model is
evaluated using performance measures.
6. Deployment
This step involves the deployment of the model on the customer site and
checking it in real-time mode. The results and feedback of the model can be
used to improve the existing model for better performance.
Data analytics is an important task for many organizations. Python has
many third-party packages that assist data analytics. Some of the essential
python packages for data analytics are given in Table 20.5.
Table 20.5: Python Packages for Data Analytics
Python and Anaconda
It is better to use a third-party IDE for Python data science projects.
Installing packages like NumPy, Pandas, and scikit-learn and their
dependencies in the “Core” IDLE environment takes time and effort. So, it
is better to use Anaconda for data science projects as it comes pre-installed.
But for the sake of consistency, only IDLE is used for this chapter. But
Anaconda is recommended for new users.
20.3 Arrays in Python
Most programming languages provide a data structure called arrays. In
Python, array is a package and it is different from “core” python lists.
Arrays are similar to lists and the differences between arrays lists are given
in Table 20.6.
Table 20.6: Differences between Array and List
20.3.1 Importing and Creating an Array
Example 1: Create an array with odd elements from 1 to 13 and print
that array.
The array can have only one data type and arrays are mutable. They change
dynamically. Here, d represents the data type of the elements. Some of the
popular data types are shown in Table 20.7.
Table 20.7: Flags and Datatypes
The syntax for creating an array.
20.3.2 Array Operations
1. Arrays are mutable
The array is a container in Python that stores objects of different types. It is
also a fundamental data structure that is useful for processing data. It works
similarly to lists and stores objects of similar types. The possible indexes
are given below in Table 20.8.
Table 20.8: Array Index in Python
The elements can be changed as follows.
Example 2: Illustration of assignment of an element using index
It can be observed that the first element is changed now to 60 and it shows
that the values of the array can be changed, or in other words, arrays are
mutable.
Length of the Array
len() method built-in function. This function returns the length of the array.
Example 3: Illustration of len()
Adding/Removing an Element
An element can be added to an array. The commands are explained in Table
20.9.
Table 20.9: Inserting Elements into an Array
Opposite to that of append, an element can be deleted using the command
del. These commands are listed in Table 20.10.
Table 20.10: Deletion of Elements in Array
20.4 Introduction to NumPy
NumPy is a library of Python, and it is a shorthand form of numerical
Python. NumPy provides an array data structure and helps in numerical
analysis. NumPy is used to manipulate arrays. The manipulation includes
mathematical and logical operations. The differences between NumPy and
arrays are shown in Table 20.11.
Table 20.11: Differences between Numpy Arrays and Arrays
20.4.1 Array Creation in NumPy
One can create a NumPy array of elements 1,3,7,9,12,15 as follows.
The elements of the created array x can be displayed by typing the name of
the array.
Immediately, the command would display the elements of the array x and
the command,
There is another way of creating an array using the command arange. The
following command creates an array with elements in the range of 1–10.
The range would be divided by 5 and two elements would be displayed.
Another way to create a NumPy array is to use the command linspace. It
creates a range with the starting value to the ending value with exactly
specified elements. The following command creates a range as follows.
Creating Predefined Arrays
Array creation using predefined methods is shown in Table 20.12.
Table 20.12: Python Predefined Methods for Creating an Array
Creating Arrays from Another Array
Creating arrays from another array is shown in Table 20.13.
Table 20.13: Creating Arrays from Another Arrays
20.4.2 NumPy Properties
The important characteristics of defining a NumPy array are listed below.
Data type
Item size
Shape–dimensions
Data
Data types are integers, uint, float, and complex; other data types are
Boolean, string, date time, and Python objects. Item size is the memory
requirement of data elements in bytes. The shape is the dimension of the
array. Data are the elements of a NumPy array. The shape operations are
shown in Table 20.14.
Table 20.14: Shape Operations
Example 4: Illustration of selected Numpy Commands
20.4.3 Indexing and Slicing Operations
Indexing and slicing operations are used to access the elements of the array.
The indexing operations are given using the colon operator.
Index in array starts with 0. For example, x[3] indicates the index of the
third element of the array x. The following examples illustrate the
application of index.
The index is given as [2,4]. The result would be
Similarly, the concept of the binary mask can be applied. For example, the
above value can be obtained using the mask as
[False,False,True,False,True].
The above can be illustrated in the following example.
Slicing is also constructed by specifying start, stop, and step parameters.
For example, consider the program segment.
Slicing produces the original view of the NumPy array and its change
modifies the original array as these are mutable.
20.4.4 Arithmetic Operations on NumPy
One can create an array and apply the following commands to perform
statistical operations. Array operations are shown in Table 20.15.
Table 20.15: Array Operations of Numpy
Example 5: Illustrate creation of two arrays, x = [1,2,3,4,5,6] and y =
[5,6,7,8,9,10], and illustrate some of the arithmetic operations.
Example 6: Finding the square root of an np array y =[10,20,30,40,50].
Numpy can perform matrix operations also. Some of the matrix operations
are shown in Table 20.16.
Table 20.16: Matrix Operations of NumPy
20.4.5 Data Analysis Using NumPy
Descriptive analytics is about describing the main features of the data.
Descriptive analytics only focuses on the description part of the data and
not the inference part. Some of the descriptive statistics are given in Table
20.17.
Table 20.17: Statistics Operations of NumPy
Example 7: Show how descriptive statistics can be performed in
NumPy.
20.5 Pandas
Pandas is a name from “panel data” and was designed by Wes McKinney in
2008. Pandas is used for data manipulation and analysis. The core of pandas
is their data structures. It provides three data structures.
1. series 1D (Column)
2. data frame 2D (Single Sheet)
3. Panel 3D (Multiple Sheets)
A panel may have multiple sheets (df) and every df may have many
columns (series). Series and df are very common and this section focuses on
them.
20.5.1 Series in Pandas
one-dimensional series can be created as shown below:
Example 8: Create a Pandas Series (Listing1.py).
The output of this would be
The shape operations are shown in Table 20.18.
Table 20.18: Shape Operations in Pandas
For the above created myDat, the above some of the commands are applied
and the results are shown below.
Alternatively, a series can be created with an index as shown below.
20.5.2 CRUD Operations with Pandas
Let us create a small dataset for physics marks as shown in Table 20.19.
Table 20.19: Physics Marks
Example 9: Create the above dataset of physics marks with Pandas
(Listing2.py).
Read the Operation of Series with Pandas
One can use the index to retrieve the data
Updating of Series with Pandas
One can update a value using an index. The update can be made using a
standard assignment statement. One example is given below.
Deleting of an Element with Series
The deletion of an element can be carried out using the del command.
del() is not usual. If there are multiple indexes, the del command may cause
a problem. It is better to filter the result as follows and continue further
operations.
Example 10: Illustrate some of the CRUD operations using Pandas
(listing3.py).
20.5.3 Series Indexing
The first item is indexed at 0. The last item index is -1. The item can be
accessed by specifying the index.
Data can be accessed through also
iloc
loc
Iloc is label based and similar to the Python list. Its example is given below.
One can slice more items using this also
.loc is a position-based index method
.ix is another method of combining both and is rather confusing
20.5.4 Series Statistical Methods
The values of the series can be printed as usual. The sample script is
given below.
Series provides many statistical operations to support data analysis. Some
of the statistical operations are given below in Table 20.20.
Table 20.20: Array Operations of Numpy
Example 11: Illustrate descriptive statistics with one-dimensional
series.
20.5.5 Data Frames with Pandas
A data frame is an object that is similar to a record structure. It consists of
rows and columns. A data frame can be a file or an excel sheet. A data
frame can be constructed using tuples and dictionary also. In data analysis,
often a data frame is constructed first. Once a data frame is constructed then
any operations can be applied to it.
Creating a Data Frame from the Excel Sheet
A data frame can be constructed from a .csv file. csv is a comma-separated
file that can be created in an excel environment. During the saving of an
excel sheet, one can save the sheet as a .csv file. The snapshot of the .csv
file is shown in Fig. 20.3. The .csv file can be read as follows.
The following example explains the reading of the excel sheet.
Fig. 20.2: Snapshot of Excel Sheet
Example 12: Create a data frame of the above Excel dataset using
Pandas (Listing4.py).
It can be observed that the column name is in the square bracket.
Data frame is a 2D structure that can be used for creating datasets. For
example, the following dataset can be created using data frame.
Python and Excel
A record can be created using an excel sheet as shown below: it must be
recollected that all excel files are stored in the format .xlsx.
Python pandas can read a .csv file as follows.
Import pandas as pd
One can index it as follows.
One can create a header name as follows.
One can skip the rows, say 3, using the following command
Similarly, Pandas can read from different sources from data as
Constructing a Data Frame Using Tuples
A dataset can be constructed using tuples also. A sample dataset shown in
Table 20.21.
Table 20.21: StudentDat Dataset
Example 13: Illustrate data frame creation of the above StudentDat
using tuples (listing5.py).
Construction Using Dictionary
Example 14: Illustrate data frame creation of the above StudentDat
using a dictionary (Listing6.py).
20.5.6 Indexing in Data Frames
The index operation can be retrieved as follows.
The index column is generated by default. Instead of a default column, an
index column can be created as follows.
Now, the physics column is the new index column using which the records
can be accessed.
If the original frame itself needs to be modified and set as the index column,
one should ass ‘inplace=True’ as follows.
Operations on the Data Frame
The information about the rows and columns can be obtained using the
command shape().
The rows can be retrieved using the command head(). By default, the head()
command retrieves five rows.
If one wants to display only three rows, then the default behaviour of the
command can be changed as follows.
The range of rows that can be retrieved using the colon command. For
example, the rows 2–3 can be retrieved as follows.
Alternate rows can be accessed using the command df[0::2] or df[::2]. The
rows can be accessed in the reverse manner also
Accessing Columns
The column data can be retried as
df.table
or
df[‘column’]
Similarly, two columns can be retrieved as
Rows and columns can be deleted using the axis information.
It should be observed that the row number and axis number are mentioned.
The accessing of an element of the created dataframe in pandas is done
through the commands iloc and loc. The rows of the dataframe can be
accessed using the iloc method. For example, df.iloc[0] returns the first row
of the dataframe.
Example 15: Illustrate indexing operations in dataframe (Listing7.py).
The details are given in Table 20.22.
Table 20.22: Access Operations in Pandas
20.5.7 Data Manipulation in Data Frames
A new column total can be added as follows.
Ascending/Descending
Sorting can be specified using sort_values(). The following command can
be used to sort the values of the dataframe.
The same record can be sorted in descending order using the command
Grouping and Aggregation
One can group using these commands.
the first command does grouping by key and the second command does
grouping using key1 and key2.
Missing Values
Pandas can handle missing values. The “bad” dataset is given in Table
20.23.
Table 20.23: Missing Data Table
The missing data are represented by Not a Number (NaN). The command
fillna() can be used to replace NaN by a specific value. For example, a
missing value can be replaced by zero as
Similarly, the missing name can be made as “missing” as follows.
The missed rows can be removed using the command dropna() as follows.
20.5.8 Data Analysis Using Pandas
One can retrieve the data by giving the query to the database. For example,
the students who scored marks more than 50 in physics can be retrieved as
follows.
The statistics can be created as follows in Table 20.24.
Table 20.24: Statistical Operations
Example 16: Illustrate statistical operations using dataframe
(Listing8.py).
Example 17: Illustrate descriptive analytics in dataframe (Listing9.py).
20.6 Data Visualization Using Pandas and Matplotlib
The process of visualizing data to identify patterns is called data
visualization. A pattern is a trend or repetition of some data. By visualizing,
one can observe some trends that may be helpful in business decision
making.
For example, by observing the trend, one can take some important
decisions. Data visualization is useful in many domains such as
1. Data science
2. Machine learning
3. Data mining
4. Data analytics
Matplotlib is one of the most important libraries. One can import the pyplot
module from Matplotlib import library. Let us discuss the ways of using
some plots using Matplotlib.
Pandas and Matplotlib Data Visualization
Pandas can be used for data visualization also. Pandas can read comma-
separated values (CSV), Excel and Tab-Separated values 0 files into
dataframe. The first requirement of Pandas visualization is that the data files
need to be imported into dataframe. Let us assume that the following
dataset is available for data.
This is followed by the dot operator. This is followed by the name of the
plot. For example, the histogram of salesJan in the above table can be done
as follows.
Different types of plots are tabulated in Table 20.25.
Table 20.25: Plot Commands in Pandas
Examples of Visualization
Example 18: Illustrate exponential function plot using Pandas and
Matplotlib (Listing10.py).
The resultant plot is given in Fig. 20.5.
Fig. 20.5: Simple Plot
The title can be created as follows in Table 20.5.
Table 20.26: Labels and Markers
Example 19: Illustrate line plot using Pandas and Matplotlib
(Listing11.py).
Fig. 20.6: Line Chart
The resultant plot is given in Fig. 20.6.
Example 20: Illustrate scatter plot using Pandas and Matplotlib
(Listing12.py).
Fig. 20.7: Scatter Plot
The resultant plot is given in Fig. 20.7.
The function hist can be used to plot histograms. It groups the data into
“bins” and shows the data distribution.
Example 21: Illustrate histogram plot using Pandas and Matplotlib
(Listing13.py).
Fig. 20.8: Simple Plot
The resultant plot is given in Fig. 20.8.
Bin is a parameter. The entire data can be divided into a specific number of
bins. The colour can be specified by the parameter colour.
Example 22: Illustrate a pie chart using Pandas and Matplotlib
(Listing14.py).
Fig. 20.9: Pie Chart
The resultant plot is given in Fig. 20.9.
Multiple Plots
Matplotlib facilitates the plotting of multiple plots. The function subplot() is
used for multiple plots.
This function takes two parameters- rows and columns. The number of rows
and columns creates rows x columns cells. For example, subplot (2,2)
creates 4 cells. Each cell can be accessed as subplot (2,2,1), subplot (2,2,2),
and so on
Example 23: Illustrate multiplots (Listing15.py).
The output of this program would be
Fig. 20.10: Multiplot
The resultant plot is given in Fig. 20.10.
20.7 Scikit-Learn and Data Analysis
Regression analysis is used to model the relationship between one or more
independent variables and a dependent variable. Regression analysis
discovers the relation between the variables. In the simplest form, the model
can be created as
Here, a0 is the intercept that represents the bias, and a1 represents the slope.
These are called regression coefficients. This specifies the Y-Intercept and
slope of the line. The values of estimates of a and b are given as follows.
Example 24: Let us consider an example for regression. A sample
dataset is shown in Table 20.27.
Table 20.27: A Sample Dataset
Apply the regression analysis and find the relationship between the number
of hours the shop worked and units sold.
Table 20.28: Regression Table
Let us compute the denominator now.
Let us model the relationship as
Suppose, if the shop works for 10 hours, the number of units likely to be
sold are
In other words, the prediction is if the shop opened for 10 hours, it is likely
that the number of units sold would be 162.
Scikit-Learn for Regression
The above calculations can be done by scikit-learn. Scikit-learn is a third-
party Python package that provides the routines for regression. The
following is the program for linear regression.
Example 25: Illustrate linear regression (Listing16.py).
It can be verified that the results are matching with our manual
computations.
Summary
1. Data are a raw fact.
2. Data analysis is the processing of raw data.
3. Array is the basic module in Python. Array modules can be imported into
Python for array processing.
4. Array is a collection of objects that are of the same type. It is a built-in
module.
5. Array is more compact and consumes less memory.
6. Any element of an array can be accessed using indexes.
7. Any sub-part of an array can be accessed by the slicing operation.
8. Any element can be added, removed, and can be processed.
9. NumPy is a package that can be imported into Python for array
processing.
10. NumPy has properties such as data type, item size, and shape.
11. Data analysis can be done using NumPy statistical operations.
12. Pandas is used for data manipulation and analysis.
13. Pandas support 1D and 2D arrays such as data frame.
14. Pandas facilitate adding, removing, and indexing of data frames.
15. Data analysis and data visualization can be done using Pandas very
effectively.
16. Matplotlib is a Python data visualization tool.
17. Any charts like line charts, histograms, or pie charts can be done using
Matplotlib.
18. Scikit-learn can be used to facilitate data analysis.
19. Regression analysis can be performed using Scikit-learn to find
relationships among variables.
Glossary
Mutable array: array value whose values change.
Index: any element can be accessed using the index in the array.
Slicing: accessing sub-arrays of an array is called slicing.
Shape: the dimensions of the array.
Series: one-dimensional array is called a series in Pandas.
Data frame: a 2D dataset is called a data frame.
Aggregation: the grouping of elements is called aggregation.
Correlation: the relationships among the variables are called correlation.
Model: a model can be a formula or procedure that can be extracted
automatically from data is called a model.
Regression: the relationship among variables that indicates how the
variables are interrelated.
Conceptual Questions
1. What is an array, and how is it different from a list?
2. How an array can be imported?
3. Explain the ways using which an array can be created?
4. Explain the concept of array slicing?
5. Explain the way of constructing a data frame using tuples and a
dictionary.
6. Explain how data frame can be imported from an Excel file.
7. List five examples of how statistical operations can be performed using
Pandas.
8. Explain the role of data visualization in data analysis.
9. List out the steps of data analysis.
10. Explain the advantages of regression analysis.
Try out Questions
1. Apply regression analysis and predict the sales of items in the 25th and
60th week.
2. Apply regression analysis and predict the marks likely to be scored if the
working hours are increased to 12 and 14 hours.
Programming Exercise
1. Find the odd and even elements in an array.
2. Find the sum of an array.
3. Find the duplicate elements in an array.
4. Using NumPy, find the five-point summary of the following list of
numbers.
A = [ 10 40 60 100 120 140]
5. Find the matrix multiplication of these two matrices using arrays and
NumPy.
6. Find the determinant of the following matrices using NumPy.
7. Consider the following data frame.
Create a data frame using tuples and a dictionary.
8. Write a Python program using Q7 and find descriptive statistics.
9. Draw the following charts for this data.
1. Line plot between student ID and marks.
2. Bar chart student ID versus marks.
3. Pie chart for marks
10. Use Scikit-learn and write a Python program to solve the regression
problems given in the problem section.
OceanofPDF.com
Bibliography
1. Ana Bell, Get Programming: Learn to Code with Python, manning
Publishing Company, 2009, New York.
2. Brian Overland, Python Without Fear, Pearson Education, 2018.
3. Budd, Timothy. Introduction to object-oriented programming. Pearson
Education India, 2008.
4. Cay Horstmann, Rance Necaise, Python for Everyone, 3rd Edition, John
Wiley & Sons, New York.
5. Charles Severance, Python for Everybody: Exploring Data in Python 3,
Shroff Publishers & Distributers Pvt Ltd, 2009, New Delhi.
6. Daniel Zingaro, Learning to Code by Solving Problems: A Python
Programming Primer, No Startch Press, 2021.
7. David Amos, Dan Bader, Joanna Jablonski, Fletcher Heisler, Python
Basics: A practical Introduction to Python 3, Fourth Edition, Real Python,
2021.
8. Eric Matthes, Python Crash Course: A hands-on, project-based
Introduction to Programming, 2nd Edition.
9. Haythem Balti, Job Ready Python, John Wiley and Sons, New Jersey.
10. Lacey, Nichola. Python by Example: Learning to Program in 150
Challenges. Cambridge University Press, 2019.
11. Weisfeld, Matt. The object-oriented thought process. Pearson Education,
2008.
12. Michael Driscall, Python 101, 2nd Edition, Learnpub.
13. Michael Driscall, Python 201, Learnpub.
14. Mohammad Asif, Python for Geeks, Packt Publications, 2021,
Birmingham – Mumbai.
15. Naomi Ceder, The Quick Python Book, Third Edition, Manning
Publications Company, New York.
16. Nageswara Rao, R, Core Python Programming, Dreamtech Press, New
Delhi.
17. Paul Deitel, Harvey Deitel, Introduction to Python for Computer
Science and Data Science, Pearson Education, 2020.
18. Shaw, Zed A. Learn Python the hard way: A very simple introduction to
the terrifyingly beautiful world of computers and code. Addison-Wesley,
2013.
19. Sridhar S, Design and Analysis of Algorithms, Oxford University Press,
2014, New Delhi.
20. Sridhar S, Digital Image Processing, Oxford University Press, 2016,
New Delhi.
21. Sridhar S, Vijayalakshmi, M, Machine Learning, Oxford University
Press, 2021, New Delhi.
22. Stuart Reges, Marty Stepp, Allison Obourn, Building Python Programs,
Pearson India Education Services Pvt. Ltd, New Delhi.
23. Sweigart, Al. Automate the boring stuff with Python: practical
programming for total beginners. No Starch Press, 2019.
24. Sweigart Al, Cracking codes with Python, No Starch Press, 2018.
OceanofPDF.com
Index
Abnormal loop termination, 140
Abstract base class (ABC), 472
Abstraction, 397
Acceptance test, 31
Accessing dictionary elements, 301
Accessing string elements, 325
Adding elements to sets, 281
Adding widgets to the main window, 596
Additional observation, 439
Advanced class, 453–476
Advanced functions, 357–391
Algorithm, 3, 29, 107
analysis of, 47
analysis, 48
branching, 107
decision, 107
euclid, 41
for finding the area of a circle, 38
for finding the largest among three numbers, 39
for subtraction of two numbers, 37
iteration, 107
loop, 107
repetition, 107
selection, 107
sequence, 107
Algorithm analysis, 48
mathematical framework for, 48
Algorithm design, 30, 33
Algorithm design strategies, 32
Algorithmic complexity theory, 47
Algorithms design, 28
Anaconda edition, 562
Analysis of algorithms, 47
Anatomy of a user-defined function, 161
Anomalies, 508
Anonymous functions, 125
Append mode, 572
Applying event triggers, 596
Arguments, 161
default, 176
keyword, 175
mandatory, 172
multiple, 167
non-keyword, 172
positional, 172
required, 172
variable keyword-Based, 179
variable length, 177
Arithmetic error, 529
Arithmetic operations on NumPy, 629
Arithmetic operations, 48
Arithmetic operators and expressions, 78
Array creation in NumPy, 625
Array operations, 622
Arrays
creating arrays from another, 626
creating predefined, 625
creating, 626
creation in NumPy, 625
in python, 620
operations, 622
properties, 627
Ascending, 646
ASCII, 57
Assembly languages, 3
Assert statement, 553
AssertionError, 530
Assignment, 247
Assignment operations, 48
Assignment operators, 461
Assignment statement, 66
Associativity, 80
left-to-right, 80
right-to-left, 80
Asymptotic notations, 51
Atom, 18
AttributeError, 530
Attributes, 70¸396
class, 401
public, 406
types of, 406
types of, 439
autocorr(series), 649
Bar plot, 652
Basic dictionary operations, 300
Basic operation in sets, 281
Basic operations, 48
Basics of programming language, 1–2
Best-case complexity, 51
Binary arithmetic operators, 461
Binary digits, 3
Binary files, 573
Binary infix operator, 199
Bitwise operators, 84
Black-box testing, 552
Block, 166
Boolean expressions, 83
Boolean functions, 292
Boolean literals, 60
Boolean operations, 81
Boolean types, 72
Boolean variable, 108
conditions using, 108
Bottom-up approach, 33
Branching, 107
decision, 107
iteration, 107
loop, 107
repetition, 107
break statement, 140, 147
Built-in attributes in objects, 427
Built-in exceptions, 528
Built-in functions, 99, 125, 160, 202
Built-in function and tuples, 257
Built-in methods, 94
for strings, 330
in class, 422
in tuples, 263
Built-in namespace, 183
Built-in operations, 73
Built-ins, 422
Button widgets, 600
Byte arrays, 76
Byte code, 9
Bytes, 76
Camel case, 65
capitalise(), 334
casefold(), 334
Chained conditions using logical operations, 109
Check boxes, 603
Child class, 434
Class, 396, 398
advanced, 453–476
built-in methods in, 422
child, 434
methods in, 408
parent, 434
Class attributes, 401
Class method, 469
Classification of flow controls, 124
Clean-Up actions, 533
Close a file, 574
close(), 576
Closed functions, 388
Code inspections, 31
Code reuse, 433
Code tracing, 542
Coding mechanism, 2
Coding, 2
Collection data types, 73
Columnspan, 610
Combining streams using zip function, 382
Command line interpreter (CLI), 17
Comma-separated value (CSV), 573, 651
Comments in python, 63
Common python syntax colours, 16
Comparison operations, 48
Comparison operator, 461
Compile time, 48
Compiler, 8
Complex numbers, 71
Compositional approach, 33
Compound statements, 56
Computational skill, 28
Computational thinking, 27
Computer software, 2
Concatenation, 250
Concatenation operation, 327
Concatenation operator, 199
Concept reuse, 433
Condition loop, 124
Conditions on iterations, 365
Conditions using a boolean variable, 108
Console user interface (CUI), 17
Console, 17
Construction using dictionary, 641
Constructor, 414
Context manager using WITH, 588
continue statement, 143, 147
Copying the elements of the dictionary, 305
count(), 257, 342
count-controlled loop, 135
Counter loop, 124
cProfile, 565
Creating a data frame from the excel sheet, 638
Creating a string, 322
Creating a tuple, 241
Creating alias, 482
Creating arrays from another array, 626
Creating dictionaries, 298
Creating predefined arrays, 625
Creating set, 278
Creation of a module, 480
Creation of main window, 596
Cross-platform language, 5
CRUD operations with pandas, 633
CSV files, 573
cumsum (), 649
Data, 572
interval, 618
nominal, 618
ordinal, 618
panel, 631
preparation of, 619
preprocessing, 619
qualitative, 618
quantitative, 618
ratio, 618
Data analysis, 615
dataset and, 617
introduction to, 615
scikit-Learn and, 660
types of, 616
using pandas, 648
Data analysis using NumPy, 630
Data analytic process, 618
Data conversion, 88
Data frames
data manipulation in, 646
indexing in, 641
operations on, 641
with pandas, 638
Data manipulation in data frames, 646
Data mining (DM), 618
Data preprocessing, 619
Data type(s), 70, 322
boolean types, 72
collection, 73
complex numbers, 71
float number, 71
integers, 71
none type, 72
Data visualization using pandas and matplotlib, 651
Dataset and data analysis, 617
Debugging, 31
Debugging and testing, 30, 541–567
Decision structures, 107–122
need for, 107–108
Decisions using relational operators, 109
Decomposition, 28
Decorator function, 389
Decorators, 388
Deep copy, 219
Default arguments, 176
Default constructor, 414
Deletion of a set, 286
Deletion of elements from a set, 284
Delimiters, 61
Dequeue, 230
Descending, 646
Descriptive analytics, 616
Destructor, 416
df.corr (series), 649
df.count(), 649
df.cumprod (), 649
df.describe(include=‘all’), 649
df.diff (), 649
df.kurt (), 649
df.mad (), 649
df.max (), 649
df.mean (), 649
df.min (), 649
df.mode(), 649
df.skew (), 649
df.std (), 649
df.sum(), 649
df.var (), 649
Diagnostic analytics, 616
Dictionary, 76, 297
copying the elements of, 305
creating, 298
introduction to, 297
looping over, 310
nested, 307
remove the elements of, 303
Dictionary comprehensions, 367
Difference_update(), 294
Discarding an element in a set, 285
Display text with label widgets, 596
Docstring, 161, 164
Doctest using terminal, 555
Doctest, 554
Ducky debugging, 542
Dunder variables, 164
Dynamic programming language, 4
Dynamic type checking, 67
Efficiency of the algorithms, 52
Elementary operations, 48
Elements of computational skill, 28
Elements of python language, 58
Encapsulation, 397
End of file (EOF), 572
Enqueue, 230
Entry-controlled loop, 125
enumerate (), 260
EnvironmentError, 529
EOFError, 530
Equivalent sets, 291
Error, 31
Errors, 508
assertion, 530
attribute, 530
environment, 529
EOFError, 530
fileNotFound, 513
floatingPoint, 530
import, 530
indentation, 530
index, 530
IO, 530
key, 530
logical, 510
lookup, 530
memory, 531
name, 530
notImplemented, 513, 530
OS, 531
overflow, 530
reference, 531
runtime, 511, 530
semantic, 510
standard, 530
syntax, 509, 531
system, 531
tab, 531
type, 513, 531
types of, 508
unicode, 531
unicodeDecode, 531
unicodeEncode, 531
unicodeTranslate, 531
value, 513
zeroDivision, 513
zeroDivision, 531
Errors and exceptions, 507–535
Essential set operations, 281
Euclid algorithm, 41
Evaluation of a model, 620
Event-based garbage collection, 418
Exception chaining, 527
Exception failures, 515
Exceptional conditions, 515
Executable, 8
Exit-controlled loops, 125
Explicit conversion, 88
Exponential time, 52
extend(seq), 204
Factorial of a number, 40
Failure, 31
Fault, 31
File access modes, 572
File handling, 571–591
File handling operations
in binary files, 582
in excel files, 585
in text files, 577
File(s), 571
access modes, 572
binary, 573
close a, 574
CSV, 573
handling operations in binary files, 582
handling operations in excel files, 585
handling operations in text files, 577
handling, 571–591
introduction to, 571
open a, 574
read a, 576
text, 573
types of, 573
write a, 576
FileNotFoundError, 513
Files and directories, 493
Filter function, 370
find(), 337
Finding all occurrences of match, 349
Finite loops, 124
Float number, 71
FloatingPointError, 530
Flow charts, 35
Flow of execution, 162
for loop with else, 136
for loop, 133
Formatted string literals, 98
Formatting using print statement, 94
Forming conditions, 108
Frame widget, 604
Frozen sets, 77, 296
Fruitful functions, 168, 169
f-string, 98
Function annotators, 388, 389
Function arguments, 171
types of, 171
Function body, 162
Function call, 162
Function signature, 161
Function(s), 125
advanced, 357–391
anatomy of a user-defined, 161
annotators, 388
anonymous, 125
arguments, 171
as objects, 359
body, 162
built-in, 125, 160, 202
call, 162
closed, 388
decorator, 389
definition, 161
filter, 370
fruitful, 168, 169
general OS, 492
inbuilt looping, 125
indentation in, 166
introduction to, 157–188
lambda, 125, 361
map, 368
non-parameterised, 165
parameterised, 166
python anonymous, 180
range (), 125
recursive, 125
reduce, 372
replacement, 350
search, 350
signature, 161
split, 351
substitute, 350
types of, 160, 165
user-defined, 125
value returning, 168
void, 168
with no arguments, 171
with positional arguments, 172
zip, 382
Functional call, 172
Functional programming, 357
introduction to, 357
Functional programming, 5
Functions as objects, 359
Functions with no arguments, 171
Functions with positional arguments, 172
G
Garbage collection, 418
event-based, 418
time-based, 418
General OS functions, 492
General-purpose programming language, 4
Generator expressions, 380
GeneratorExit, 531
Generators, 376, 381
Getting help in python, 19
Global namespace, 183
Global variables, 183, 184, 185
Graphical user interface (GUI), 7, 595
introduction to, 595–612
Greatest common divisor (GCD), 42
Grid, 609
Grouping and aggregation, 647
Handling exceptions, 516
Hash symbol, 63
Header, 572
Hexadecimal digits, 3
High-level languages, 3
Histogram, 652
Hybrid inheritance, 448
Identifiers, 58
Identity and membership operators, 85
Identity, 68
if-elif statement, 117–119
if-else statement, 112–116
if-statement, 110–112
Immutability in tuples, 254
Implicit conversion, 88
ImportError, 530
Importing modules, 482
Importing submodules of a module, 483
importlib module, 488
Imports, 486
explicit relative, 487
types of, 486
Inbuilt looping functions, 125
Indentation, 61
IndentationError, 530
index(), 263
IndexError, 530
Indexing and slicing, 324
in a tuple, 245
operations, 628
Indexing in data frames, 641
Infinite loops, 124
Inheritance, 397, 433–450
hybrid, 448
introduction, 433
multi-Level, 445
multiple, 443
single, 439
types of, 439
Initiating mainloop(), 596
Inline-if-statement, 116–117
Input and output operations, 574
Input text box, 606
Insertion sort, 45
Instance variables, 402
Integers, 71
Integrated development environment (IDE), 10
Integration test, 31
Interactive development environment (IDLE), 10
Interpreter, 8
Intersection_update(), 295
Interval data, 618
Introduction to
data analytics with python, 615–662
dictionary, 297
functions, 157–188
modules and packages in python, 479
profiling, 563
python, 55–101
IOError, 530
isalnum(), 331
isdigit(), 330
IsEmpty, 230
isnumeric(), 330
isspace(), 331
Iterable object, 191
Iteration, 35
Iterator, 369
Iterators, 374
Itertools, 381
J
join(), 340
join(seq), 340
Jump statements, 140
KeyboardInterrupt, 530
KeyError, 530
Keyword arguments, 175
Keywords, 58
Lambda functions, 125, 361
syntax of, 361
Languages
assembley, 3
high-level, 3
low-level, 3
machine, 3
Lazy evaluation, 376
Learning to code, 2
Left-to-right associativity, 80
LEGB rule, 186, 188
Libraries, 4
LIFO design pattern, 225
Line plot, 652
Linear search, 43
Linear sequence, 191
List comprehensions, 363
in tuples, 367
with sequence processing, 368
List slicing, 195
List traversal, 223
List(s), 74–75, 191–232
as queue, 230
as stack, 225
built-in functions and methods in, 202–209
creating, 192
indexing in a list, 194
looping with, 221
mutability of, 217
nested, 209
operations in, 199
primary properties of, 192
significance of mutability of, 217
slicing, 195
list.reverse(), 205
Literals, 59
boolean, 60
numerical, 60
string, 59
Local namespace, 183
Local variables, 184
Logical errors, 510
Logical operations, 48, 109
LookupError, 530
Loop over the elements themselves, 223
Loop traverses all index values, 223
Loop(s), 124
classification, 124
condition, 124
condition-controlled, 128
count-controlled, 135
counter, 124
entry-controlled, 125
exit-controlled, 125
finite, 124
for, 133
infinite, 124
nested, 138
outer, 138
pre-checking, 125
pre-test, 125
purpose of, 124
statements, 127–137
Looping
introduction to, 124
python built-in functions for, 125–126
statements, 123–150
with list, 221
with tuples, 271
Looping operations in sets, 296
Looping over a dictionary, 310
Looping with strings, 344–345
Loop’s body, 124
Low-level languages, 3
Machine languages, 3
Magic method, 460
Magic variables, 164
Mandatory arguments, 172
Map function, 368
Matching function, 348
Matching of full string, 348
Math packages, 490
Mathematical framework for algorithm analysis, 48
Mathematical operations of sets, 286
Matplotlib, 620
max(), 261, 343
Membership operations, 73, 329
Membership operator, 202, 252
in set, 281
Memoization, 387
MemoryError, 531
Meta class, 475
Method overloading, 454, 465
Methods, 396
class, 469
enumerate (), 225
in class, 408
magic, 460
method overriding, 465
overloading, 454
private, 412
series statistical, 636
static, 470
Methods in class, 408
min(), 261, 343
Modelling, 620
Modules
and packages, 479
importing, 482
in python, 479–504
uses of standard, 490
Modules in python, 479–504
Multi-level inheritance, 445
Multiple arguments, 167
Multiple exceptions, 521
Multiple inheritance, 443
Multiple iteration, 365
Mutability of list, 217
Name mangling, 407
NameError, 530
Names, 69
Namespaces, 68, 183
built-in, 183
global, 183
local, 183
Nested dictionary, 308
Nested list, 209
Nested list comprehension, 366
Nested loops, 138
need for, 138
syntax of, 138
Nested tuples, 266
Nested-If conditions, 120
Nominal data, 618
None type, 72
Non-keyword arguments, 172
Non-local variables, 184, 186
Non-parameterised function, 165
No-operation statement, 147
Notepad++, 17
NotImplementedError, 513
NotImplementedError, 530
Null statement, 147
Numerical literals, 60
NumPy, 620, 624
arithmetic operations on, 629
array creation in, 625
data analysis using, 630
introduction to, 624
Object code, 8
Object creation, 399
Object flattening, 583
Object marshaling, 583
Object oriented programming (OOP), 5, 395–429
characteristics of, 397
introduction to, 395–396
pillars of, 397
Object serialization, 583
Objects, 396, 398
Octothorpe symbol, 63
Old style formatting, 95
Open a file, 574
Open-source, 4
Operands for membership operators, 329
Operations, 77
Operations in strings, 324
Operations in tuples, 249
Operations on python dictionary, 302
Operations on the data frame, 641
Operator
assignment, 461
binary arithmetic, 461
binary infix, 199
comparison, 461
concatenation, 199
for equality testing, 201
in, 202
membership, 202, 252
not in, 202
replication, 201
unary, 462
Operator associativity, 80
Operator for equality testing, 201
Operator overloading, 459
Operator precedence of all operators, 85
Operator precedence, 79
Ordinal data, 618
OS module, 492
os.envrion, 492
OSError, 531
Outer loop, 138
OverflowError, 530
Overloading, 453
method, 454
operator, 459
Packages, 502
Packing, 247
Padx, 610
Pady, 610
Pandas, 620, 631
and matplotlib data visualization, 651
CRUD operations with, 633
data analysis using, 648
data frames with, 638
data visualization using, 651
plot commands in, 652
series in, 632
Panel data, 631
Paradigm in programming, 5
Paradigm, 5
Parameterised functions, 166
Parameterized constructor, 415
Parameters., 162
Parent class, 434
Pascal’s case, 66
Pass by reference, 182
pass statement, 146, 147
Passing of objects reference, 181
Pattern generalisation and abstraction, 28
Pattern matching, 28
Pdb through primitive GUI mode in IDLE, 549
Pdb through python command line interpreter, 544
Pickle module, 583
Pickling, 583
Pie chart, 652
Pillars of OOP, 397
Plot commands in pandas, 652
Plot, 652
bar, 652
line, 652
scatter, 652
Polymorphism, 397
Positional arguments, 172
Post-checking loops, 125
Pre-checking loops, 125
Predictive analytics, 616
Preparation of data, 619
Prescriptive analytics, 616
Pre-test loops, 125
Primary properties of lists, 192
Print statements, 542
print() statement, 92
Private methods, 412
Private variables, 406
Problem reduction, 33
Problem-solving, 1
and algorithms, 25–52
and computational skills, 25
Procedural programming, 5
Process parameters, 492
Profile, 565
Profiling, 563
Program, 3, 55
for deleting the elements in a dictionary, 304
to add an element to a nested dictionary, 309
to demonstrate accessing a dictionary, 301
to demonstrate an empty dictionary creation, 298
to demonstrate clear(), 305
to demonstrate fromkeys(), 300
to demonstrate frozenset, 297
to demonstrate set add function, 282
to demonstrate the addition of elements to the dictionary, 303
to demonstrate the copy() on sets, 286
to demonstrate the dictionary creation, 299
to demonstrate the dictionary of mixed keys, 299
to demonstrate the dictionary of tuples, 299
to demonstrate the elimination operations on a set, 284
to demonstrate the get() method, 301
to demonstrate the len() method, 281
to demonstrate the membership functions, 281
to demonstrate the not in operator, 282
to demonstrate the pop() function, 304
to demonstrate the popitem() function, 304
to demonstrate the set difference, 290
to demonstrate the set discard method, 285
to demonstrate the set equivalence, 291
to demonstrate the set intersection using the method, 290
to demonstrate the set pop method, 285
to demonstrate the set remove method, 284
to demonstrate the set symmetric difference using the method, 291
to demonstrate the set symmetric difference, 291
to demonstrate the set union using the operator, 289
to demonstrate the set union, 289
to demonstrate the sorted(), 306
to demonstrate the subset operations, 292
to demonstrate the subset, superset, and disjoint set, 293
to demonstrate the superset operations, 292
to demonstrate the update operations on a set, 283
to display the use of isdigit(), 330
to find GCD, 174
to illustrate global variables, 185
to illustrate lambda functions, 180
to illustrate pass-by-reference, 182
to illustrate re.findall() (Listing3.py), 349
to illustrate scope rules, 187
to show the concatenation of strings using the “+” operator, 327
to show the creation of a single, double, and triple-quoted string, 323
to show the creation of strings, 323
to show the for looping over a dictionary, 310
to show the replication of strings, 328
to show the string traversal using for loop with range, 345
to show the use of keys(), 302
Program maintenance, 31
Programming, 2
introduction to, 1–19
mechanism, 2
various resources for python, 17
Programming language, 3
Programming paradigm, 5
Pseudocode, 34
Public attributes, 406
Punctuators, 61
Pure functions, 358
Purpose of loops, 124
py_compile, 501
PyCharm, 18
Pytest, 561
pytest.raises(), 563
Python
anonymous functions, 180
arithmetic operators and expressions, 78
arrays in, 620
assignment statement, 66
comments in, 63
context manager, 587
data types in, 70
debugger, 541
dictionary, 297
editor, 14
elements of, 58
getting help in, 19
IDLE editor, 18
implementations using, 7
in interactive mode, 10
in script mode, 13
interpreter, 9
introduction to, 55–101
introduction to data analytics with, 615–662
introduction to modules and packages in, 479
lEGB rule, 186
limitations of, 7
modules in, 479–504
program, 55
reading numbers from the keyboard, 90
simple input/output statements, 90
syntax colours, 16
syntax of lambda function in, 361
testing in, 551
uses of standard modules in, 490
virtual machine, 10
Python anonymous functions, 180
Python block structure, 61
Python context manager, 587
Python debugger (PDB), 541, 542
introduction to, 541
running through, 545
Python editor, 14
Python IDLE editor, 18
Python objects, 68
Python package index (PyPI), 5
Python programming language, 3
characteristics of, 4
Python timeit(), 563
Python virtual machine (PVM), 10
Qualitative data, 618
Quantitative data, 618
Radio buttons, 602
Raising exceptions, 525
range() function, 125
Rapid edit-test-debug cycle, 7
Ratio data, 618
Read a file, 576
Read access modes, 572
Read and write (‘r+’), 572
Read and write only in binary format (‘rb+’), 572
Read mode, 572
Read only (‘r’), 572
Read only in binary format (‘rb’), 572
read(n), 576
read(size = n), 578
read(size), 578
Read-eval-print loop (REPL), 10
Reading numbers from the keyboard, 90
readline(), 578
readlines(), 578
Recursion, 384
Recursive functions, 125
Reduce function, 372
ReferenceError, 531
Regular expressions, 346
Relational operators, 83, 109
Removal of an element in set, 284
Removing a random element in a set, 285
Repetition, 35
REPL, 56
Replacement function, 350
Replicate and repetition, 201
Replication operation, 328
Replication operator, 201
Required arguments, 172
Return value, 342
Right-to-left associativity, 80
rindex(), 336
Row, 610
Rowspan, 610
rstrip(), 338
Rule of exceptions, 517
Run time, 48
Running through pdb, 545
Runtime errors, 511
RuntimeError, 530
Scatter plot, 652
Scikit-Learn, 620
and data analysis, 660
for regression, 661
Scope, 183
Screaming snake case, 65
Script, 3, 55
Scripting, 7
Search function, 350
Selection, 35
Semantic errors, 510
Semantics, 3, 8
Separate test file, 556
Sequence, 34
Sequential search, 140
Series in pandas, 632
Series indexing, 636
Series statistical methods, 636
Set comprehensions, 367
Set copy, 286
Set updates using mathematical operations, 293
setattr(), 404
Sets, 77
adding elements to, 282
and dictionary, 277–311
basic operation in, 281
copy, 286
creating, 278
deletion of elements from, 284
deletion of, 286
discarding an element in, 285
equivalent, 291
frozen, 296
introduction to, 277
looping operations in, 296
mathematical operations of, 286
membership operator in, 281
removal of an element in, 284
removing a random element in, 285
Shallow copy, 220
Shell, 17
Significance of mutability of lists, 217
Simple input/output statements, 90
Simple statements, 56
Single inheritance, 439
Slicing in a tuple, 245
Slicing operations, 73
Snake case, 65
Software development cycle, 29
sort_values(), 646
sorted(), 265
Sorting, 646
Source code, 8
Special iterators, 381
Split function, 351
split(), 341
splitlines(num), 340
Spyder, 18
Stack diagram, 184
StandardError, 530
StandardError, 530
Start pdb from REPL, 542
Statements, 56
compound, 56
jump, 140
print, 542
simple, 56
Static analysis, 31
Static method, 470
Steps involved in the file input/Output operations, 574
Stepwise refinement, 32
Sticky, 610
StopIteration, 531
String comparison, 343
String formatting, 96
String interpolation, 95
String literals, 59
String representation, 73
String(s), 73–74, 321–351
built-in functions for, 342
built-in methods for, 330
capitalise(), 334
casefold(), 334
comparison, 343
count(), 342
creating, 322
find(), 337
index() method, 335
introduction to, 322
isalnum(), 331
isdigit(), 330
isnumeric(), 330
isspace(), 331
join(), 340
join(seq), 340
looping with, 344–345
matching of full, 348
max(), 343
min(), 343
operations in, 324
rindex(), 336
rstrip(), 338
split(), 341
splitlines(num), 340
strip(), 338
swapcase(), 334
using list comprehension with, 365
strip(), 338
Structured programming, 34
Subset operations, 292
Subsets, 291
Substitute function, 350
sum(), 261
swapcase(), 334
Sympy packages, 490
Syntax, 3, 8
Syntax errors, 509
Syntax of continue, 144
Syntax of nested loops, 138
SyntaxError, 531
Synthesis, 33
Sys module, 495
System test, 31
SystemError, 531
SystemExit, 531
TabError, 531
Test-driven development (TDD), 31, 553
Testing, 31
black-box, 552
in python, 551, 553
white-box, 552
Testing process, 31
Tests
acceptance, 31
integration, 31
system, 31
unit, 31
Text files, 573
Thonny, 18
Time-based garbage collection, 418
Tkinter module, 596
Tool command language (TCL), 596
Top-down design, 32
Transforming streams with map, 382
Translator, 3
try - except - else, 520
Try-Finally clause, 534
Tuple, 75–76
Tuples, 239–273
built-in function and, 257
built-in methods in, 263
creating, 241
immutability in, 254
indexing in, 245
introduction to, 239
list comprehension in, 367
looping with, 271
nested, 266
operations in, 249
slicing in, 245
Type conversions, 88
Type errors, 513
TypeError, 531
Types of files, 573
Types of imports, 486
Unary operators, 462
UNICODE, 57
UnicodeDecodeError, 531
UnicodeEncodeError, 531
UnicodeError, 531
UnicodeTranslateError, 531
Unified modeling language (UML), 434
Unit tests, 31
Unittest, 557
Unpacking, 247
Unpickling, 583
Use of setattr(), 404
User-Defined exceptions, 531
User-Defined functions (UDF), 125
Uses of standard modules in python, 490
Using list comprehension with strings, 365
Value returning functions, 168
ValueError, 513
Variable keyword-based arguments, 179
Variable length arguments, 177
Variables, 65
global, 184
local, 184
non-local, 184
Various resources for python programming, 17
Visual studio code, 18
Void functions, 168
Ways to create a list, 193
while loop, 128
while loop with else, 130
White-box testing, 552
Widgets
adding, 596
display text with label, 596
Write a file, 576
Write access modes, 572
Write and read (‘w+’), 572
Write and read-only in binary format (‘wb+’), 573
Write mode, 572
Write multiple data, 583
Write only (‘w’), 572
Write only in binary format (‘wb’), 572
write(), 576, 580
writelines(), 580
ZeroDivisionError, 513, 531
Zip function, 382
combining streams using, 382
OceanofPDF.com
Copyright © 2023 Pearson India Education Services Pvt. Ltd
Published by Pearson India Education Services Pvt. Ltd, CIN:
U72200TN2005PTC057128.
No part of this eBook may be used or reproduced in any manner whatsoever
without the publisher’s prior written consent.
This eBook may or may not include all assets that were part of the print
version.
The publisher reserves the right to remove any material in this eBook at any
time.
ISBN 978-93-560-6933-6
eISBN 978-93-570-5330-3
Head Office: 1st Floor, Berger Tower, Plot No. C-001A/2, Sector 16B,
Noida - 201 301, Uttar Pradesh, India.
Registered Office: Featherlite, ‘The Address’ 5th Floor, Survey No
203/10B, 200 Ft MMRD Road,
Zamin Pallavaram, Chennai – 600 044, Tamilnadu, India.
Phone: 044-66540100
OceanofPDF.com