22
Sample Copy. Not For Distribution.

Sample Copy. Not For Distribution.TABLE OF CONTENTS CHAPTER PAGE UNIT – I 1 ALGORITHMIC PROBLEM SOLVING 1 1.1 Algorithms 2 1.1.1 Characteristics of Algorithm 3 1.1.2 Method for Developing

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Sample Copy. Not For Distribution.

i

Problem Solving and Python Programming

Sample Copy. Not For Distribution.

ii

Publishing-in-support-of,

EDUCREATION PUBLISHING

RZ 94, Sector - 6, Dwarka, New Delhi - 110075

Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001

Website: www.educreation.in

________________________________________________________________

© Copyright, 2018, Authors

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer.

ISBN: 978-93-88719-51-3

Price: ₹ 265.00

The opinions/ contents expressed in this book are solely of the authors and do not represent the opinions/ standings/ thoughts of Educreation or the Editors . The book is released by using the services of self-publishing house.

Printed in India

Sample Copy. Not For Distribution.

iii

Problem Solving and

Python Programming

Dr.N. Dhasarathan, K.Saraswathi,

Dr.R. Reka, Dr.S.R.Boselin Prabhu

EDUCREATION PUBLISHING (Since 2011)

www.educreation.in

Sample Copy. Not For Distribution.

iv

Sample Copy. Not For Distribution.

v

PROBLEM SOLVING AND PYTHON

PROGRAMMING

B.E., Semester-I

According to the revised (2017), Syllabus of Anna University,

Chennai.

Dr. N. DHASARATHAN

Professor, Department of Electronics & Communication

Engineering,

BVC Engineering College, Amalapuram, East Godavari.

Mrs.K.SARASWATHI

Assistant Professor, Department of Computer Science,

Nehru Memorial College, Puthanampatti, Trichy.

Dr. R. REKA

Professor & Head, Department of Computer Science and

Engineering,

Annai Mathammal Sheela Engineering College, Erumapatty,

Namakkal.

Dr. S.R. BOSELIN PRABHU

Associate Professor, Department of Electronics & Communication

Engineering,

VSB College of Engineering Technical Campus, Coimbatore,

Tamilnadu.

Sample Copy. Not For Distribution.

vi

PREFACE

This book is meant for Python beginners. We can learn python

programming language well with the practice of applications in

that particular programming language. The purpose of this book is

to learn python easily with the variety of applications. This book

makes the reader to get familiar with Python. It mainly focuses on

problem solving using python.

Unit 1 covers algorithms, building blocks of algorithms, notation,

algorithmic problem solving and simple strategies for developing

algorithms. This unit also gives the solutions to find minimum in a

list, insert a card in a list of sorted cards and guess an integer

number in a range and Towers of Hanoi.

Unit 2 covers python interpreter, basics of python, statements,

operators, modules, functions and flow of execution statements.

This unit also provides the solution to exchange the values of two

variables, circulate the values of n variables and distance between

two points.

Unit 3 covers if types, looping, break, continue and pass

statements. This unit also covers fruitful functions, variable scope,

string operations, string functions, methods and string module. The

solutions are given to find square root, gcd, exponentiation, sum an

array of numbers, linear search and binary search.

Unit 4 covers list, tuple, dictionary operations, functions and

methods. This unit also provides the solution for selection sort,

insertion sort, merge sort and histogram.

Unit 5 covers the concepts of files, exception, modules and

packages. This unit also provides the solution to word count and

copy file.

Sample Copy. Not For Distribution.

vii

ACKNOWLEDGEMENT

We are thankful to Almighty God, without God’s grace nothing is

possible for anyone.

First and foremost, we would like to express our deep gratitude to

the Management of Annai Mathammal Sheela Engineering

College, Erumapatty, Namakkal for their continuous guidance,

encouragement, patience and giving us the opportunity to complete

this book successfully.

We would like to express our sincere thanks to the Management,

Principal, Dean and Faculty Members of BVC Engineering

College, Amalapuram, Andhra Pradesh for constant inspiration and

encouragement.

We would like to extend our bottomless gratitude and appreciation

to Thiru.M.Ponnambalam, Chairman, Nehru Memorial College,

Puthanampatti for his valuable guidance, support and persistence

for giving us the opportunity to do this book successfully.

We would like to express our special gratitude to Educreation

Publishing for bringing this book in excellent form within a short

span of time.

Last but not least, we thank our family members for their support,

love & affection. This book would never have been completed

successfully without their support & encouragement.

Dr. N. DHASARATHAN

Mrs.K.SARASWATHI

Dr. R. REKA

Dr.S.R.BOSELIN PRABHU

Sample Copy. Not For Distribution.

viii

Dedicated To

My Father Mr. V. Nagarajan, My Mother Mrs. N. Gajalakshmi,

My Wife Mrs. Meenakumari, My Daughter

D. M.Samukta Meeraja

& All my Family Members

-Dr. N. Dhasarathan

Dedicated To

My Father Mr. N.Karuppannan, My Mother Mrs. K.Vasantha,

My Spouse Mr. T.Murugan, My Son M.Shanmuga Priyan and

My Daughter M.Nandhana Shri

-Mrs.K.SARASWATHI

Dedicated To

My Father Mr. R. Rajagopal, My Mother Mrs. R. Meera,

My Husband Mr. R. Satheesh Kumar, My Lovable Daughter

S. R. Himanika, My Mother-in-law Mrs. R. Maniroja &

All my Family Members

-Dr. R. Reka

Dedicated To

Loving Student Friends, Family Members & Research Team

Members from Malaysia, Iraq, Ghana and Russia.

-Dr. S.R.Boselin Prabhu

Sample Copy. Not For Distribution.

ix

AUTHOR BIOGRAPHIES

Dr.N.DHASARATHAN is working as a

Professor of Electronics and Communication

Engineering in BVC Engineering College,

Amalapuram, East Godavari, Andhra

Pradesh. He is having 10 years of experience in

teaching and research. He has published around

20 papers in various International Journals &

Conferences. He is a Reviewer and an Editorial Board Member of

various National and International Journals. He has authored two

books which includes C Programming & Python Programming.

Mrs.K.SARASWATHI is working as an

Assistant Professor in Nehru Memorial

College, Puthanampatti. She has more than 15

years of experience in teaching and more than 4

years of experience in research. She is currently

pursuing her Doctorate Degree at Bharathiar

University & submitted her synopsis,

Coimbatore. She has published more than 21 papers in various

National, International Journals and Conferences. She has written

one of the book chapters in “Advanced Engineering Research and

Applications”. She has served as Resource Person for workshop of

Computer Science and delivered a special technical lecture in

various colleges.

Dr.R.REKA is working as Professor & Head,

Department of Computer Science and

Engineering at Annai Mathammal Sheela

Engineering College, Erumapatty,

Namakkal. She has above 15 years of

extensive working experience in Teaching and

Research. She got a Gold Medal in Master Degree. She has

published papers more than 36 in various National, International

Journals & Conferences. She is a Reviewer and an Editorial Board

Sample Copy. Not For Distribution.

x

Member of various National and International Journals. She was a

chair person in National and International Conferences in various

Institutions. She has authored about more than five books for

Engineering Students which includes Python Programming, Java

Programming, Advanced Java Programming, Data Structures and

C Programming, etc.

Dr. BOSELIN PRABHU S.R. obtained his

doctorate (Ph.D) from Anna University Chennai,

India. He has 9 years of experience in teaching

and research. He has published 165 research

articles in International Journals. He is an editorial

board member, advisory board member and

reviewer of 300 International Journals both

Scopus and SCI Indexed. He is an elected fellow member

FUAMAE, FISECE, FISRD, FUAAMP, FISQEM, SIESRP,

FUACEE and FISEEE. He has attained Google scholar citations-

1116 and h-index-16. He is the recipient of awards like Excellent

Professional Achievement Award Winner from Society of

Professional Engineers, Biography Included in Marquis Who’s

who in the World (Academic Year 2015, 2016 & 2018), Dedicated

Professional Engineer Award Winner from Society of Engineers &

Technicians, Albert Nelson Marquis Lifetime Achievement Award

Winner (2017 & 2018), Best Young Scientist from Association of

Scientists, Developers and Faculties (2017), Best Young

Researcher from Association of Scientists, Developers and

Faculties (2017) and Bright Educator Award from International

Institute of Organized Research (2018). He has written 14 books

and two monographs for students.

Sample Copy. Not For Distribution.

xi

Sample Copy. Not For Distribution.

xii

TABLE OF CONTENTS

CHAPTER PAGE

UNIT – I 1

ALGORITHMIC PROBLEM SOLVING 1

1.1 Algorithms 2

1.1.1 Characteristics of Algorithm 3

1.1.2 Method for Developing an Algorithm 3

1.1.3 Qualities of a Good Algorithm 4

1.1.4 Representation of Algorithms 5

1.2. Building blocks of Algorithms

(statements,state,control flow, functions)

5

1.2.1 Statements 6

1.2.2. State 6

1.2.3. Control Flow 6

1.2.3.1. Sequence 7

1.2.3.2. Selection 10

1.2.3.3. Iteration 11

1.3. Notations 17

1.3.1. Flowchart Symbols 18

1.3.2. Guidelines for developing Flowcharts 18

1.3.3. Advantages of Flowcharts 19

1.3.4. Limitations of Flowchart 20

1.3.5. Rules for Drawing a Flowchart 20

1.3.5. Additional Flowchart Symbols 20

1.3.6. Flowchart Types 21

1.3.7. Additional Flowchart Symbols 22

1.3.8. Sample Flowcharts 24

1.4. Pseudo Code 28

1.4.1. Common keywords used in Pseudo Code 29

1.5. Programming Languages 30

1.6. Algorithmic Problem Solving 42

1.7. Simple Strategies for developing Algorithms 46

Sample Copy. Not For Distribution.

xiii

1.7.1. Iteration 46

1.7.2. Recursion 47

1.8. Illustrative Problems 48

1.8.1. Find minimum and maximum number from

the List.

48

1.8.2. Insert a card in a list of sorted cards 48

1.8.3. Guess an integer number in a range 49

1.8.4. Towers of Hanoi 49

UNIT-II 52

DATA, EXPRESSIONS, STATEMENTS 52

2.1. Introduction to Python 53

2.1.1. History of Python 54

2.1.2. Python Features 54

2.2. Python Interpreter and Interactive Mode 55

2.2.1. Interactive Mode Programming 55

2.2.2. Script Mode Programming 55

2.3. Values and Types 56

2.3.1 Numbers 56

2.3.2 Strings 57

2.3.3 Lists 57

2.3.4 Boolean 57

2.4. Variables 58

2.4.1. Assigning Values 58

2.4.2. Multiple Assignments 58

2.5. Expressions 59

2.5.1. Types of Expressions 59

2.6. Statements 60

2.6.1. Multiple Statements on a Single Line 60

2.6.2. Multiple Statement Groups as Suites 60

2.7. Tuple Assignment 61

2.8. Precedence of an Operators 61

2.9. Comments 62

2.10. Modules 62

2.10.1. Math Module 62

2.10.2. Random Module 63

2.10.3. OS Module 64

2.10.4. Sys Module 65

Sample Copy. Not For Distribution.

xiv

2.10.5. Datetime Module 65

2.11. Function Definition and Use 66

2.11.1. Defining a Function 66

2.11.2. Calling a Function 67

2.11.3. Flow of Execution 67

2.12 Parameters and Arguments 68

2.12.1. Types of Function Arguments 68

2.12.1.1. Required Arguments 68

2.12.1.2. Keyword Arguments 69

2.12.1.3. Default Arguments 69

2.12.1.4. Variable-length Arguments 70

2.13 Illustrative Programs 71

2.13.1. Exchange the values of two variables 71

2.13. 2.Circulate the values of n variables 71

2.13.3. Distance between two points 72

UNIT-III 74

CONTROL FLOW, FUNCTIONS 74

3.1. Operators 75

3.1.1. Types of Operators 75

3.2. Conditionals 80

3.2.1. Simple If 80

3.2.2. If….Else 81

3.2.3. Elif 82

3.2.4 Nested If 83

3.3 Iteration 84

3.3.1. While 84

3.3.2. For 85

3.3.3. Continue 86

3.3.4. Break 87

3.3.5. Pass 88

3.4 Fruitful Functions 88

3.4.1. Local and Global Scope 89

3.4.2. Function Composition 90

3.4.3. Recursion 90

3.5. Strings 91

3.5.1. String Immutability 92

3.5.2. String Functions & Methods 93

Sample Copy. Not For Distribution.

xv

3.5.3 String Methods 96

3.5.4. String Modules 98

3.6. Lists as arrays 99

3.7. Illustrative programs 104

3.7.1. Square root 104

3.7.2. Find Gcd 105

3.7.3. Find Exponentiation 106

3.7.4. Sum an array of n numbers 106

3.7.5. Linear Search 107

3.7.6. Binary Search 107

UNIT-IV 109

LISTS, TUPLES, DICTIONARIES 109

4.1. Lists 109

4.1.1. List Operations and List Slices 109

4.1.2. List Methods and its Description 113

4.1.3 List Loop 115

4.1.4. Mutability 117

4.1.5. Aliasing 119

4.1.6. Cloning Lists 119

4.1.7. List Parameters 120

4.2. Tuples 121

4.2.1. Tuple Assignment 121

4.2.2. Tuple as Return Value 122

4.3. Dictionaries 122

4.3.1. Dictionary Operations 123

4.3.2. Dictionary Functions & Methods 124

4.4. Advanced List Processing 126

4.5. Illustrative Programs 128

4.5.1. Selection Sort 128

4.5.2. Insertion Sort 129

4.5.3. Merge Sort 130

4.5.4. Histogram 131

UNIT-V 132

FILES, MODULES, PACKAGES 132

5.1. Files: Text Files, Reading and Writing files 132

Sample Copy. Not For Distribution.

xvi

5.2. Format operator 138

5.3. Command Line Arguments 140

5.4. Exception: Errors and Exceptions, Handling

Exception

141

5.5. Modules 151

5.6. Packages 152

5.7. Illustrative Programs 154

5.7.1. Word Count 154

5.7.2. Copy File 155

Sample Copy. Not For Distribution.

Problem Solving and Python Programming

1

UNIT I

ALGORITHMIC PROBLEM SOLVING

1.1 Algorithms

1.1.1. Characteristics of Algorithms

1.1.2. Method for Developing an Algorithm

1.1.3. Qualities of a Good Algorithm

1.1.4. Representation of Algorithms

1.2 Building Blocks of Algorithms (Statements, State, Control

flow, Functions)

1.2.1 Statements

1.2.2. State

1.2.3. Control Flow

1.2.3.1. Sequence

1.2.3.2. Selection

1.2.3.3. Iteration

1.3. Notations

1.3.1. Flowchart Symbols

1.3.2. Guidelines for Developing Flowcharts

1.3.3. Advantages of Flowcharts

1.3.4. Limitations of Flowchart

1.3.5. Rules for Drawing a Flowchart

1.3.5. Additional Flowchart Symbols

1.3.6. Flowchart Types

1.3.7. Additional Flowchart Symbols

1.3.8. Sample Flowcharts

1.4. Pseudo Code

1.4.1. Common Keywords used in Pseudo Code

1.5. Programming Languages

1.6. Algorithmic Problem Solving

1.7. Simple Strategies for Developing Algorithms

1.7.1. Iteration

1.7.2. Recursion

1.8. Illustrative Problems

Sample Copy. Not For Distribution.

2

1.8.1. Find Minimum and Maximum Number from the List.

1.8.2. Insert a Card in a List of sorted cards

1.8.3. Guess an Integer Number in a Range

1.8.4. Towers of Hanoi

1.1. ALGORITHMS

• An algorithm is a collection of definite, unambiguous and

effectively computable instructions, if execute it will return the

proper output.

• After finished a suitable plan for developing the program by its

logic, the correct sequence or technique of instructions required

carrying out the task, then the algorithm is often used to refer to

the logic of the program.

An algorithm must be

• Finite: An algorithm must always terminate after a finite

number of steps (although this number can be arbitrarily

large or even unknown).

• Definite: Each step of an algorithm must be unambiguous.

• Effective: Each step of the algorithm must be possible within a

finite period of time. An algorithm is an unambiguous

procedure for performing some task in a finite number of steps.

Definition

• It is defined as a “sequence of instructions designed in such a

way that if the instructions are executed in the specified

sequence, the desired results will be obtained”, thus the

algorithm should precise and unambiguous in practice and the

results should obtained after a finite number of steps.

• It is also defined as “if any problem whose solution can be

expressed in a list of executable instruction”. By executing we

mean an instruction which is an independent executor can

actually perform, or carry out, in a step-by-step manner.

• An algorithm is a technique or formula for solving a problem.

An Algorithm is a step by step process designed to perform an

Sample Copy. Not For Distribution.

Problem Solving and Python Programming

3

operation, and which (like a flowchart or pictorial

representation or map) resolve to the required result if followed

correctly.

1.1.1. Characteristics of Algorithm

In order to qualify an algorithm is a sequence of instructions, must

possess the following characteristics.

• It should have an end point.

• The algorithm in which each and every instruction should be

precise.

• The algorithm in which each and every instruction should be

unambiguous.

• The instruction in an algorithm should not be repeated

infinitely.

• Ensure that the algorithm will ultimately terminate.

• The algorithm should be written in sequence.

• Algorithm should complete after a finite number of steps.

• It looks like normal English.

• The desired result should be obtained only after the algorithm

terminates

• Derived results should be conquered only after the algorithm

terminates.

• Well-ordered: the steps are in a clear order

• Unambiguous: the operations described are understood by a

computing agent without further simplification

• Effectively computable: the computing agent can actually carry

out the operation

1.1.2. Method for Developing an Algorithm

Define the problem: State the problem you are trying to solve in

clear and concise terms.

• List the inputs (information needed to solve the problem)

and the outputs (what the algorithm will produce as a

result)

Sample Copy. Not For Distribution.

Get Complete Book At Educreation Store

www.educreation.in

Sample Copy. Not For Distribution.

Sample Copy. Not For Distribution.