Buy Ebook Programming and Problem Solving With Python 1st Edition - Ebook PDF Cheap Price
Buy Ebook Programming and Problem Solving With Python 1st Edition - Ebook PDF Cheap Price
com
https://ebooksecure.com/download/programming-and-
problem-solving-with-python-ebook-pdf/
https://ebooksecure.com/download/problem-solving-and-python-
programming-ebook-pdf/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-programming-problem-solving-
and-abstraction-with-c/
ebooksecure.com
https://ebooksecure.com/download/programming-for-problem-solving-
ebook-pdf/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-language-development-from-
theory-to-practice-3rd-edition/
ebooksecure.com
(eBook PDF) American Courts and the Judicial Process 2nd
Edition
https://ebooksecure.com/product/ebook-pdf-american-courts-and-the-
judicial-process-2nd-edition/
ebooksecure.com
https://ebooksecure.com/download/corporate-finance-ebook-pdf/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-crime-and-criminology-an-
introduction-to-theory-third-canadian-edition/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-gardners-art-through-the-
ages-a-concise-history-of-western-art-3rd-edition/
ebooksecure.com
Photo-induced corrosion or protection: Determining the
charge transfer in the semiconductor-metal heterojunction
- eBook PDF
https://ebooksecure.com/download/photo-induced-corrosion-or-
protection-determining-the-charge-transfer-in-the-semiconductor-metal-
heterojunction-ebook-pdf/
ebooksecure.com
PROGRAMMING AND PROBLEM SOLVING
WITH
PYTHON
About the Authors
PYTHON
Ashok Namdev Kamthane
Retired Associate Professor
Department of Electronics and Telecommunication Engineering
Shri Guru Gobind Singhji Institute of Engineering and Technology, Nanded
Maharashtra, India
1 2 3 4 5 6 7 8 9 D103074 22 21 20 19 18
Printed and bound in India.
Print Edition
ISBN (13): 978-93-87067-57-8
ISBN (10): 93-87067-57-2
e-Book Edition
ISBN (13): 978-93-87067-58-5
ISBN (10): 93-87067-58-0
Managing Director: Kaushik Bellani
Director—Science & Engineering Portfolio: Vibha Mahajan
Senior Portfolio Manager—Science & Engineering: Hemant K Jha
Associate Portfolio Manager—Science & Engineering: Mohammad Salman Khurshid
Senior Manager—Content Development: Shalini Jha
Content Developer: Ranjana Chaube
Production Head: Satinder S Baveja
Assistant Manager—Production: Jagriti Kundu
General Manager—Production: Rajender P Ghansela
Manager—Production: Reji Kumar
Information contained in this work has been obtained by McGraw Hill Education (India), from sources believed to be reliable.
However, neither McGraw Hill Education (India) nor its authors guarantee the accuracy or completeness of any information
published herein, and neither McGraw Hill Education (India) nor its authors shall be responsible for any errors, omissions, or
damages arising out of use of this information. This work is published with the understanding that McGraw Hill Education
(India) and its authors are supplying information but are not attempting to render engineering or other professional services. If
such services are required, the assistance of an appropriate professional should be sought.
Typeset at APS Compugraphics, 4G, PKT 2, Mayur Vihar Phase-III, Delhi 96, and printed at
Cover Printer:
It gives us immense pleasure to bring the book ‘Programming and Problem Solving with Python’. The
book is intended for the students in initial years of engineering and mathematics who can use this
high-level programming language as an effective tool in mathematical problem solving. Python is
used to develop applications of any stream and it is not restricted only to computer science.
We believe that anyone who has basic knowledge of computer and ability of logical thinking can
learn programming. With this motivation, we have written this book in a lucid manner. Once you
go through the book, you will know how simple the programming language is and at the same
time you will learn the basics of python programming. You will feel motivated enough to develop
applications using python.
Since this book has been written with consideration that reader has no prior knowledge of
python programming, before going through all the chapters, reader should know what are the
benefits of learning python programming. Following are some of the reasons why one should
learn python language.
• Python language is simple and easy to learn. For example, it has simple syntax compared to
other programming languages.
• Python is an object-oriented programming language. It is used to develop desktop, standalone
and scripting applications.
• Python is also an example of free open source software. Due to its open nature one can write
programs and can deploy on any of platform, i.e., (Windows, Linux, Ubuntu and Mac OS),
without changing the original program.
Thus, due to the features enlisted above, python has become the most popular language and is
widely used among programmers.
• To develop GUI-based applications, cryptography and network security and many more
applications
Electronics and Telecommunication Engineering and Electrical Engineering
• Image processing applications can be developed by using python’s ‘scikit-image’ library
• Widely used in developing embedded applications
• Develop IOT applications using Arduino and Raspberry pi
Python can also be used in other engineering streams such as mechanical, chemical, and
bioinformatics to perform complex calculations by making use of numpy, scipy, and pandas library.
Thus, the end user of this book can be anyone who wants to learn basics of python programming.
To learn the basics, the student can be of any stream/any engineering/Diploma/BCA/MCA
background and interested to develop applications using python.
In the end, we would like to express gratitude to all our well-wishers and readers, whose
unstinted support and encouragement has kept us going as a teacher and author of this book. Any
suggestion regarding the improvement of the book will be highly appreciated.
Publisher’s Note
McGraw-Hill Education (India) invites suggestions and comments from you, all of which can be
sent to [email protected] (kindly mention the title and author name in the subject line).
Piracy-related issues may also be reported.
Visual W
All chapters within the book have been structured into the following important pedagogical
components:
Decision Statements
• Learning Outcomes give a clear idea to the 4
students and programmers on what they will
learn in each chapter. After completion of 4.1 Introduction
CHAPTER OUTLINE
4.6 Boolean Expressions and Relational
4.2 Boolean Type Operators
chapter, they will able to comprehend and apply 4.3
4.4
Boolean Operators
Using Numbers with Boolean Operators
4.7 Decision Making Statements
4.8 Conditional Expressions
all the objectives of the chapter. 4.5 Using String with Boolean Operators
LEARNING OUTCOMES
• Introduction explains the basics of each topic After completing this chapter, students will be able to:
bool
and familiarizes the reader to the concept being Boolean Relational > <,>= <= !=
if
dealt with. if
if else
if-elif-else
conditional expressions
4.1 INTRODUCTION
Write a program to create a list with elements 1,2,3,4 and 5. Display even elements of the list
PROGRAM 8.1 using list comprehension.
List1=[1,2,3,4,5]
print(“Content of List1”)
print(List1)
List1=[x for x in List1 if x%2==0]
print(“Even elements from the List1”)
print(List1)
• Programs are the highlighting
Output Generate 50 random numbers within a range 500 to 1000 and write them to file
feature of the chapters. Ample
PROGRAM 13.3
Content of List1
[1, 2, 3, 4, 5]
WriteNumRandom.txt.
Output File
Visual Walkthrough xi
penup(), pendown(),
forward(), right(), goto(), color(), shape(), speed() left()
Note: The del operator uses index to access the elements of a list. It gives a run time error if the index
• Notes have been inserted in each chapter
is out of range.
Example: to provide valuable insights based on
>>> del Lst[4]
Traceback (most recent call last):
programming concepts. Notes shall
File “<pyshell#37>”, line 1, in <module>
del Lst[4]
also act as precautionary statements for
IndexError: list assignment index out of range readers to solve programming problems
effectively.
SUMMARY
• A concise Summary has been listed at
chapter-end to reiterate vital points and
describes in short, the complex concepts
covered within the chapter.
x = 10
def f():
x= x + 10
print(x)
f()
Acknowledgements
We would like to express deep sense of gratitude to Professor B. M. Naik, former Principal of
S. G. G. S. College of Engineering and Technology, Nanded, who constantly praised and inspired
us to write books on technical subjects and whose enthusiasm and guidance led us to write this
book.
Special thanks are also due to Dr. L. M. Waghmare, Director, S. G. G. S. Institute of Engineering
and Technology, Professor Dr. U. V. Kulkarni, HOD, CSE and Professor P. S. Nalawade of S. G. G. S.
Institute of Engineering and Technology Nanded for encouraging us to write this book on Python.
We are grateful to Professor Dr. Mrs. S. A. Itkar, HOD, CSE and Professor Mrs. Deipali V. Gore
of P. E. S. Modern College of Engineering Pune, for supporting us while writing the book. We
are also thankful to the staff members (Santosh Nagargoje, Nilesh Deshmukh, Kunnal Khadake,
Digvijay Patil and Sujeet Deshpande) of P. E. S. Modern College of Engineering for their valuable
suggestions.
Furthermore, we would like to thank our friends—ShriKumar P. Ugale and Navneet Agrawal—
for giving valuable inputs while writing the book. Also, we would like to thank our students—
Suraj K, Pranav C, and Prajyot Gurav—who offered comments, suggestions and praise while
writing the book.
We are thankful to the following reviewers for providing useful feedback and critical suggestions
during the development of the manuscript.
Lastly, we are indebted to our family members—Mrs. Surekha Kamthane (mother of Amit
Kamthane), Amol, Swarupa, Aditya, Santosh Chidrawar, Sangita Chidrawar, Sakshi and Sartak for
their love, support and encouragement.
At such close quarters Bomba could not use his bow, and he dared
not fire the revolver lest it attract the attention of lurking foes.
Rising into the air, he came down with both feet on his enemy’s
head. Then he stamped the head into the mud and ooze till the
savage lay still.
Whether the man breathed or not, Bomba did not stop to inquire. It
was enough that he had been put out of action. The noise of the
struggle, muffled as it had been, might already be drawing others to
the scene. Bomba must act swiftly, if he were to leave the spot alive.
One of his precious minutes he gave to the search for his machete.
With its aid he might still win through to Casson at the hut of Pipina.
By a stroke of good fortune he found the weapon where it had stuck
in the trunk of a tree.
With a smothered cry of elation, Bomba leaped upon it and
wrenched it from its hold. Again and again that knife had saved his
life, and it might do it again before the night was over.
Bomba’s body was bruised, he was dead tired, but his spirit was
unhurt. The thirst of battle was still in him. His blood was hot with it.
Twice to-night he had outwitted his enemies. Nascanora and his half-
brother Tocarora would again realize that he, Bomba, was as
slippery as the cooanaradi and as deadly.
He wasted no time. He set his feet in the direction of the cabin of
Pipina, the squaw, and went stealthily yet swiftly through the jungle.
The storm had felled great trees across his path. Some of these he
climbed over, while he took the smaller ones with a leap. Where the
ground was impassable he swung himself along from creeper to
creeper and branch to branch. No inhabitant of the jungle save the
monkeys were as skilled in this method of progress as Bomba, and
he made his way with amazing celerity. Never had that
accomplishment stood him in better stead.
His eyes and ears were alert for the slightest sight or sound that
might forebode danger. But this did not prevent his mind from being
in a tumult of varied emotions.
His most anxious thought was of Casson, Casson alone in the jungle
hut save for Pipina. Again the headhunters sought the life of Casson.
Again was Bomba hunted like the veriest wild beast.
Bitterness welled up in the heart of the lad against these savages,
whom he had never injured except in self-defense. Why was he
doomed to spend his life among these people so alien to him?
Bomba was white. All his yearnings were toward those of his own
race.
Who were his parents? He thought of the picture of the beautiful
woman that had hung in the little back room of Sobrinini’s hut on the
island of snakes. That face had stirred his heart as no other had ever
done. Was the beautiful woman his mother?
Who was he? What had happened to his parents and why had he
become at so early an age the sole companion of old Cody Casson?
He reviewed the strange behavior of the half-mad old woman,
Sobrinini, she who had once been the operatic idol of Europe, she
who had had kings at her feet. Why had she not finished the story of
the man named Bartow, his wife, Laura, and the child they called
Bonny?
Sobrinini had called him, Bomba, by the name of Bartow. She had
thought in her poor twisted mind that Bomba was Bartow. Was it
possible that Bomba was the boy who had once been called Bonny?
Bomba heaved a heavy sigh. Questions, questions always, and no
answers. Cody Casson had the key to the mystery. But poor Casson
must first find the key to that closed door in his mind beyond which
he could not go.
His mind in a whirl of unrest and longing, Bomba at last reached the
river which he must cross to reach the hut of Pipina.
The storm had now entirely died away. Only the heavy dripping of
moisture from the foliage betrayed its recent passage. The jungle
was still again with an unearthly stillness. The slight swish made by
Bomba as he swung himself from branch to branch was the only
sound that broke the silence.
Suddenly he paused and hung motionless, arms and legs entwined
about a bunch of creepers. His quick ear had caught a sound other
than the dripping of water on the sodden earth.
It was a slight sound, but Bomba knew at once what had caused it. It
was the faint dip of paddles in the water. The Indians were traveling
upstream. The headhunters of Nascanora were on their way to the
hut of Pipina to spread terror and death. Fortunate if death were all!
Far worse would be the tortures of any captives who might be
carried off alive to make a holiday for the savages who had been left
at home and who would revel in the screams of their victims.
Bomba had been carrying his machete between his teeth. Now he
dropped lightly to the ground, and, with the double-edged knife held
firmly in his grasp, ran swiftly toward the river.
Upon the banks of the stream he paused, listening. Still the dip, dip
of paddles coming upstream. So faint and stealthy was the sound
that it would have been inaudible to most ears other than those of
Bomba.
The lad wasted not an instant, but slipped from the steep bank until
he was waist deep in the sluggish water. The dense foliage of the
jungle trees grew down to the very edge of the stream, flinging its
rank growth out over the water.
Bomba had a canoe of his own concealed in the bushes some
distance up the stream. Had there been time, he would have made
for that, for he well knew the danger of making the river crossing by
fording or swimming. The deadly alligator, or cayman, infested all the
waters of the jungle, and any daring person that ventured to cross
knew that he might pay for the venture with his life.
But time was everything to Bomba now. The headhunters were more
to be feared than the cayman. The former were awake. The latter
might be asleep. At all costs, he must make the venture. He must
make haste, if he were to save the life of Casson and that of Pipina.
Bomba had let himself go so gently into the water as scarcely to
make a ripple, and he moved on noiselessly, wading where he could,
but soon reaching the deeper channel where he had to swim. Then
most of the time he swam under water lest his presence be declared
to prying eyes. He was almost as much at home in the water as on
land, and only at long intervals had to come to the surface for air.
But swiftly as he swam, the Indians could paddle more swiftly. And a
terrible fear gripped the lad’s heart as the sound of the paddles grew
ever fainter in the distance.
They would reach the hut first. They would find it undefended and
might attack at once. The worst might have happened before Bomba
could reach the only place he called home.
What he would do when he got there he had not figured out. He
would act as the occasion suggested. He would be but one against
many; but he had been in that position more than once and yet won
the victory.
He swam on swiftly until he was arrested by a sight that brought a
growl of fury to his lips.
Turning a bend in the river, a light assailed Bomba’s eyes, a fearful
light, a light such as the native of the jungle dreads above all others.
It was a dull glow, brightening now and then to a vivid red as the
flames swept skyward.
Bomba groaned and his teeth gritted against each other as he
plunged madly forward. For he knew all too well what had caused
the glare. The hut of Pipina was ablaze!
This was the work of Nascanora’s bucks, their revenge upon a
broken, demented old man who had never harmed any one in his
life!
Was Casson in that blazing hut? Was poor Pipina, faithful friend,
caught in that flaming inferno?
Scarcely daring to put these questions to himself, Bomba swam
madly upstream, his one thought now of revenge. He was consumed
by rage. His one desire was to feel the throat of Nascanora beneath
his fingers.
The light was brighter now. The whole jungle was bathed in the
fiendish glow.
Bomba turned toward the bank, but paused abruptly and trod water.
Between him and the shore, blocking his path, was a monster
alligator!
CHAPTER IV
TERRIBLE JAWS
Bomba strained his ears and again heard the thing that had startled
him. It was a faint cry, rising and falling like a wail somewhere in the
bushes.
“Help!” came the voice, eerie as that of a banshee in the darkness.
“Help, Bomba! Help!”
Into Bomba’s heart sprang a great joy. This was the voice of Pipina,
the squaw—the voice that he had never expected to hear again. And
where Pipina was, must be Casson!
He was off like a deer in the direction from which the cry had come.
“Bomba hears you,” he called softly. “Bomba is coming.”
“Help!” came the feeble voice again. “Pipina is caught and cannot
get loose. Come quickly.”
Bomba wondered why he did not hear Casson’s voice, if Casson still
lived. But he said nothing and hurried on, hacking a passage through
the undergrowth.
He came nearer and nearer to the wailing woman until, pushing
aside a tangle of vines, he saw her. The moon, following close on the
heels of the tropical storm, was now riding high in the heavens and
shedding a soft luster over the jungle. By its light, Bomba caught
sight of Pipina as she stood holding out helpless hands to him.
She had been caught in a thorn thicket that had cruelly scratched her
hands and arms as she had struggled to free herself. Her wrinkled
face was drawn with pain.
By the deft use of his machete Bomba cleared away the clutching
branches and released her. The old squaw staggered dizzily, and the
lad put this arms about her shoulders to support her.
“Casson!” muttered Bomba hoarsely. “Tell me, Pipina! Tell me quick!
Where is Casson?”
The old woman drooped her head and stood there like a bowed
statue of grief, but said nothing until Bomba, mad with anxiety, shook
her gently by the shoulders.
“Do you hear, Pipina? Where is the good white man, Cody Casson,
my friend?”
Then the old woman raised her hands above her head and gave
vent to a wailing, desolate cry.
“Pipina no can tell. Casson her friend, too, good friend. He is gone.”
Bomba’s face darkened and again his heart contracted under the
cold hand of anguish.
“Tell me, Pipina,” he commanded. “Where has he gone? What has
become of him?”
“We sit down and I will tell you,” returned the squaw. “Pipina weak,
sick—”
For answer, Bomba cleared a space and, taking the old woman,
placed her as comfortably as he could with her back resting against
a giant tree.
He sat down opposite her, his arms folded, his glance full upon her
face.
“Now, Pipina, tell Bomba all,” he urged.
The old woman looked about her and shuddered. She wrapped her
skinny arms about her as though they were a garment and had
power to ward off the chill of the night.
“Headhunters—they gone?” she asked fearfully.
“Gone,” said Bomba tersely. “Where is Casson?”
“Bomba make them go away all by himself,” continued the squaw
admiringly. “Bomba great man some day—”
Bomba bent toward her.
“Do not talk foolishly, Pipina. Bomba not care about himself. Pipina
tell about Casson.”
The old woman gave her wailing cry and rocked herself back and
forth drearily.
“We have bad time, Casson, Pipina,” she said. “We all alone in hut,
wishing Bomba come. Storm come, but not Bomba. Thunder like
roar of pumas, many pumas.”
“Bomba caught in storm,” explained the lad. “No could come till
storm stopped.”
“Pipina listen for sound of Bomba’s feet,” went on the squaw. “Pipina
afraid. She think danger near. Wish Bomba would come quick.”
She said this, leaning forward, in a quick, hissing whisper. Now she
relaxed against the tree and stared gloomily into the heavy shadows
of the jungle.
“Casson not too good,” she muttered. “Pipina worry about Casson.
Worry hard.”
“What was wrong with Casson?” cried Bomba, exasperated beyond
measure by the slowness with which Pipina got on with her story.
“He very sick,” returned the squaw. “He not right.” She touched her
forehead significantly. “He walk back and forth, back and forth, and
talk to himself. He say: ‘Laura, Laura, dear sweet Laura. Must tell
Bomba. Bartow and Laura and little boy—’”
Bomba caught the arm of the old woman in an eager grip.
“Go on,” he commended. “What else did Casson say? Tell Bomba.”
But Pipina shook her head.
“He not say more,” she said. “Only those words he say again and
again. Then he stop, listen at door of hut, listen and then walk up
and down, up and down.”
“Go on,” cried Bomba.
“Then we hear things. We think you come. We happy. We sing. We
dance. But no, Bomba not come. It is the headhunters that come to
try to kill Casson and Pipina—”
Bomba gave a low growl like that of an animal and ground his teeth
together.
“They come.” The voice of the old woman rose again in eerie wailing.
“Casson, Pipina, we close door, push bolt, as Bomba tell us. We
heap things against door. Casson he take down old gun, but it not
work. He put fire stick through hole in hut. He think frighten bucks of
Nascanora.”
Bomba groaned as he saw the picture of old Cody Casson, brave to
the last, defying death, his only weapon a “fire stick” that would not
work.
“It happen quick,” went on Pipina with a helpless shake of her head.
“One, two, three—like that,” with a snap of her bony fingers. “The
headhunters come. They have heads, fresh heads, women, children
heads, on string at waists. They want more heads, Casson’s head,
Pipina’s head. They beat on door. They say: ‘Open. No hurt.
Nascanora friend of Casson.’”
Again came that growl as of an angry jaguar from the clenched teeth
of Bomba.
“Forked tongues! Black hearts!” he snarled. The woman nodded.
“Casson no open door,” she resumed. “He know Nascanora. He say
things. Make big chief mad. He beat more hard on door. He shout:
‘Casson witch doctor. He put a spell on sick people of our tribe.
Nascanora burn Casson and hut of Casson with him.’”
A smoldering fire was in Bomba’s eyes that boded no good to the
chief of the headhunters.
“Then Nascanora bring fire to the hut of Pipina,” went on the squaw.
“His bucks come with heaps of vines and leaves. They wet and not
burn at first. But after they burn, burn hot, and the hut of Pipina begin
to burn too.”
“But you got away, Pipina!” burst in Bomba eagerly. “You got away
from the headhunters and the fire. That was good. But how did you
do it? Tell Bomba. Do not make much words.”
The old woman shrugged her shoulders and there was a touch of
pride in her tone as she replied:
“Beneath the hut of Pipina there is a hole, and this hole it lead under
the ground out into the jungle.”
Bomba stared at her.
“A hole!” he exclaimed. “A passage! Why you not tell Bomba?”
The squaw smiled inscrutably.
“None know but Pipina.”
Bomba was listening with the most intense interest and wonder.
“Go on,” he cried, as Pipina paused.
“Pipina take up board in floor of hut,” went on the old woman. “Then
get down and crawl through hole. Casson come too. Long time to
creep through hole. Then come to end. Out into jungle where wet
and cool.”
“Then Casson got out safely?” cried Bomba.
The squaw nodded, and Bomba gratefully took her old wrinkled hand
in his.
“Pipina has saved the life of Casson,” the lad said gravely. “For this
Bomba thanks Pipina. He will never forget.”
The old woman threw her hands above her head, rocking herself
back and forth.
“Ayah, ayah!” she wailed. “Pipina save the life of Casson, but she
lose him after. For when Pipina look around Casson is gone!”