Why Teams call analytics are critical to your entire business
Python-content-1.pdf
1. PROBLEM SOLVING
AND PYTHON
PROGRAMMING
Dr.A.HEMLATHADHEVI, M.E, PhD.,
Associate professor
Department of Computer Science and Engineering
PANIMALAR ENGINEERING COLLEGE
Bangalore Trunk Road,Varadharajapuram,Nasarathpettai,
Poonamallee,Chennai –600 123
Mrs. D.R. THIRUPURASUNDARI, M.E., (PhD.,)
Department of Computer Science and Engineering
Mrs. D. SUDHA, M.E., (PhD.,)
Department of Computer Science and Engineering
MEENAKSHI COLLEGE OF ENGINEERING
West K.K Nagar, Chennai - 600 078.
M AGNUS PUBLICAT IONS
3. PREFACE
Today, the amount of computing power and the volume of data available to us is
staggering. The field of machine learning is just beginning to harness this power and data
and is transforming the field of computer science. At one level, machine learning, allows
programs to do for themselves what programmers have had to do for years, which is to
recognize complex patterns in the data. Machine learning algorithms are now routinely
doing better than humans in tasks like reading X-rays, or recommending a restaurant to
try. This book is about exploring the use of computer programs to solve these kinds of
problems, whatever your area of interest and major might be.To do so, this book will
teach you how to understand and create computer programs in Python.
There are three facets to a programming language: how you write it, what it means,
and the additional practical considerations that make a program useful, this book will
cover the pragmatic considerations. This book uses plain language to explain the basics
of this topic. This book provides a logical way to explain different complex concepts and
a step-by-step way to explain important topics. Each chapter is well supported with
illustrations required and issues resolved. This makes the understanding of this subject
more clear and interesting. All of the chapters in the book are organized in the correct
order so that each topic can be constructed based on previous research. Everything is
done so that the pupils easily understand the basic concepts of the subject. It’s aimed at
basic programmers, but even if you’ve written programs before and just want to add
Python to your list of languages, Problem Solving and Python Programming will get you
started.
Introduction
Python reflects a number of growing trends in software development, putting it at
or near the leading edge of good programming languages. It is a very simple language
surrounded by a vast library of add-on modules. It is an open source project, supported by
many individuals. It is an object-oriented language, binding data and processing into
class definitions. It is a platform-independent, scripted language, with complete access
to operating system API’s. It supports integration of complex solutions from pre-built
components. It is a dynamic language, which avoids many of the complexities and
overheads of compiled languages.
Python is a programming language that is in wide use both for professional software
development and in education. In the professional world it is used for anything, from
creating small scripts that rename files in a folder, to developing full web applications
such as YouTube, SnapChat and DropBox. In the world of education, Python is a popular
language because of its relatively simple syntax, its robust set of built-in functionality
and its beginner-friendly error messages. For all these reasons and more, Python has
become widespread in the world of data science and machine learning.
Authors
Mrs.THIRUPURASUNDARI D.R.
Dr. A.HEMLATHADHEVI
Mrs. D. SUDHA
iii
4. SYLLABUS
UNIT I COMPUTATIONAL THINKING AND PROBLEM SOLVING
Fundamentals of Computing - Identification of Computational Problems -
Algorithms, building blocks of algorithms (statements, state, control flow,
functions), notation (pseudo code, flow chart, programming language),
algorithmic problem solving, simple strategies for developing algorithms
(iteration, recursion). Illustrative problems: find minimum in a list, insert
a card in a list of sorted cards, guess an integer number in a range, Towers
of Hanoi.
UNIT II DATA TYPES, EXPRESSIONS, STATEMENTS
Python interpreter and interactive mode, debugging; values and types: int,
float, boolean, string, and list; variables, expressions, statements, tuple
assignment, precedence of operators, comments; Illustrative programs:
exchange the values of two variables, circulate the values of n variables,
distance between two points.
UNIT III CONTROL FLOW, FUNCTIONS, STRINGS
Conditionals: Boolean values and operators, conditional (if), alternative
(if-else), chained conditional (if-elif-else); Iteration: state, while, for, break,
continue, pass; Fruitful functions: return values, parameters, local and
global scope, function composition, recursion; Strings: string slices,
immutability, string functions and methods, string module; Lists as arrays.
Illustrative programs: square root, gcd, exponentiation, sum an array of
numbers, linear search, binary search.
UNIT IV LISTS, TUPLES, DICTIONARIES
Lists: list operations, list slices, list methods, list loop, mutability, aliasing,
cloning lists, list parameters; Tuples: tuple assignment, tuple as return
value; Dictionaries: operations and methods; advanced list processing -
list comprehension; Illustrative programs: simple sorting, histogram,
Students marks statement, Retail bill preparation.
UNIT V FILES, MODULES, PACKAGES
Files and exception: text files, reading and writing files, format operator;
command line arguments, errors and exceptions, handling exceptions,
modules, packages; Illustrative programs: word count, copy file, Voter's
age validation, Marks range validation (0-100).
iv
5. CONTENTS
Unit-I COMPUTATIONAL THINKING AND PROBLEM SOLVING
1.1 Fundamentals of Computing 1.1
1.1.1 Identification of Computational Problems 1.1
1.1.2 What is Computation? 1.3
1.1.3 What Is Programming? 1.3
1.2 Algorithms 1.4
1.2.1 Characteristics of an Algorithm 1.6
1.2.2 Building Blocks of Algorithms 1.8
1.2.2.1 Sequence 1.9
1.2.2.2 Control Flow or Decision Statements 1.10
1.2.2.3 Repetition 1.12
1.2.2.4 Functions 1.15
1.3 Notations - Pseudo Code 1.19
1.3.1 Keywords that are common while writing pseudo-code 1.20
1.3.2 Types of Control Structures in Pseudo code 1.21
1.4 Flowchart 1.27
1.4.1 The symbols used in a flowchart 1.27
1.4.2 Advantages of Using Flowcharts 1.27
1.4.3 Disadvantages of using Flowcharts: 1.28
1.4.4 Significance of Flowcharts 1.28
1.4.5 Flowchart Symbols 1.28
1.4.6 Using nested loops in flow charts 1.34
1.4.7 Using multiway selection in flow charts 1.37
1.4.8 Programming Language 1.39a
1.5 Algorithm Development Process 1.39g
1. 6 Algorithmic Problem Solving 1.43
1.7 Simple Strategies for developing algorithms 1.47
1.7.1 Iteration vs. Recursion 1.49
1.8 Illustrative Problems 1.50
1.8.1 Finding Minimum in a List 1.50
1.8.2 Insert a card in a list of sorted cards 1.52
1.8.3 Guess an integer number in a range 1.54
1.8.4 Towers of Hanoi 1.55
1.9 Additional Problems 1.62
Two Marks Question and Answers 1.80
v
6. 16 Marks Questions 1.91
UNIT II DATA, EXPRESSION, STATEMENTS
2.1 Python – Introduction 2.1
2.2 Data Types 2.11
2.2.1 Python Numbers: 2.13
2.2.2 Sequence Types (Strings, Lists and Tuples) 2.16
2.2.3 Python Dictionary / Mapping Type 2.19
2.2.4 Conversion between data types 2.21
2.3 Input and Print Statement 2.22
2.3.1 Input Statement 2.22
2.3.2 Print Statement 2.23
2.4 OPERATORS 2.25
2.4.1 Precedence 2.31
2.4.2 Associativity 2.32
2.4.3 Functions as operators 2.33
2.4.4 Summary of Operators 2.34
2.5 Functions 2.41
2.5.1 Function with Arguments 2.45
2.5.2 Nested Function 2.49
2.5.3 Anonymous/Lambda Function 2.50
2.6 Modules 2.51
2.7 Command Line Arguments 2.63
Programs 2.66
Two Marks Question and Answers 2.74
16 Marks Questions 2.85
UNIT III CONTROL FLOW, FUNCTIONS
3.1 Branching Programs 3.2
3.1.1 Python if…Statement 3.3
3.1.2 Python if...else Statement 3.4
3.1.3 Python if...elif...else 3.6
3.1.4 Python Nested if loop statements 3.8
3.2 Looping Statement 3.10
3.2.1 for Loop 3.10
3.2.2 while Loop 3.13
3.2.3 break, continue and pass 3.17
vi
7. 3.2.4 The infinite loop 3.22
3.2.5 Types of Loop 3.23
3.3 Testing Conditional Statements 3.26
3.3.1 Testing While Loops 3.27
3.4 Function Scope, Recursion 3.39
3.4.1 Scope and Lifetime of variables 3.39
3.4.2 Recursion 3.42
3.4.3 Testing Functions 3.45
3.4.4 Functions are values 3.47
3.4.5 Programs in Function 3.48
3.5 String 3.54
3.5.1 string functions and methods 3.59
3.5.2 Sample Programs in String 3.71
3.6 List as Arrays 3.76
3.7 Programs 3.81
Two Marks Question and Answers 3.90
16 Marks Questions 3.96
UNIT IV LISTS, TUPLES, DICTIONARIES
4.1 Lists 4.1
4.1.1 Accessing Values in Lists 4.1
4.1.2 Updating Lists 4.2
4.1.3 Deleting List Elements 4.2
4.1.4 Traversing List Elements 4.3
4.2 List Operations 4.3
4.3 List Slices 4.4
4.4 List Methods 4.5
4.5 List Loop 4.7
4.6 Mutablity 4.9
4.7 Aliasing 4.11
4.8 Cloning List 4.12
4.9 List Parameters 4.14
4.9 Tuples 4.16
4.9.1 Tuple Assignment 4.18
4.9.2 Tuple as Return Values 4.19
4.10 Dictionaries: Operations and Methods 4.20
4.10.1 Operations on Dictionaries 4.20
4.10.2 Dictionary Methods 4.24
vii
8. 4.10.3 Built-in Functions with Dictionary 4.31
4.11 Advanced List Processing: List Comprehension 4.33
4.12 Illustrative Programs 4.34
4.12.1 Selection Sort 4.35
4.12.2 Insertion Sort 4.36
4.12.3 Merge Sort 4.39
4.12.4 Quick Sort 4.42
4.13 Students marks statement 4.45
4.15 Retail bill preparation 4.46
Two Marks Question and Answers 4.47
16 Mark Questions 4.53
UNIT V FILES, MODULES, PACKAGES
5.1 Files and Exceptions 5.1
5.2 Text Files 5.1
5.2.1 Opening a file 5.2
5.2.2 Closing a file 5.4
5.2.3 Reading and Writing files 5.5
5.2.3.1 read() 5.5
5.2.3.2 write() 5.5
5.3 Format Operator 5.6
5.4 Command Line Arguments 5.9
5.4.1 Parsing Command-Line Arguments 5.10
5.4.1.1 getopt.getopt method 5.10
5.4.1.2 Exception getopt.GetoptError 5.11
5.5 Errors and Exceptions 5.11
5.5.1 Syntax errors 5.11
5.5.2 Exceptions 5.12
5.6 Handling Exceptions 5.14
5.6.1 Exception with arguments 5.18
5.6.2 User defined exceptions 5.19
5.7 Modules 5.21
5.7.1 import 5.21
5.7.2 The from...import Statement 5.22
5.7.3 The from...import * statement: 5.22
5.7.4 Locating Modules 5.22
5.7.5 The dir( ) Function 5.23
viii
9. 5.7.6 The reload() Function 5.23
5.7.7 Standard Modules 5.24
5.8 Packages 5.24
5.8.1 Packages in Multiple Directories 5.25
5.9 Illustrative Programs 5.26
5.9.1 Word Count 5.26
5.9.2 Copy file5.26
5.10 Additional Programs 5.27
5.11 Voter’s age validation 5.35
5.12 Marks range validation (1-100) 5.35
Two Marks Question and Answers 5.36
16 Mark Questions 5.42
Appendix
Content Beyond Syllabus 1-13
Problem Solving and Python Programming Laboratory 14-48
List of Mini Projects 49
University Question Papers 50-56
Python Installation 57-63
Python Quick Reference Sheet 64-75
ix