SlideShare a Scribd company logo
1 of 9
Download to read offline
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
PROBLEM SOLVING AND PYTHON PROGRAMMING
First Edition : November 2021
ISBN 13 : 978-81-939626-9-5
Published by
M AGNUS PUBL I CAT I ONS
© All rights reserved. No part of this publication may be reproduced in any form without the
prior written permission of the publisher.
Price Rs.515/-
Copies can be had from
M AGNUS PUBL I CAT I ONS
Plot No.7, Balaji Nagar 2nd Street,
Vanagaram, Chennai-600 095.
Cell : 9840788450, 9840482641
Phone: +91 44 2476 5688, 2476 5344
Email: magnuspublication@yahoo.co.in
ii
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
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
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
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
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
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
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

More Related Content

Similar to Python-content-1.pdf

Teaching of computer programming
Teaching of  computer programmingTeaching of  computer programming
Teaching of computer programmingmarpasha
 
IRJET - Pseudocode to Python Translation using Machine Learning
IRJET - Pseudocode to Python Translation using Machine LearningIRJET - Pseudocode to Python Translation using Machine Learning
IRJET - Pseudocode to Python Translation using Machine LearningIRJET Journal
 
ilovepdf_merged pdfggjhfgyutertyuiuytrsj
ilovepdf_merged pdfggjhfgyutertyuiuytrsjilovepdf_merged pdfggjhfgyutertyuiuytrsj
ilovepdf_merged pdfggjhfgyutertyuiuytrsjgautamkumar88905
 
Be cs-course-structure-2015-onwards
Be cs-course-structure-2015-onwardsBe cs-course-structure-2015-onwards
Be cs-course-structure-2015-onwardsAug Ment
 
Isolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationIsolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationeSAT Publishing House
 
Isolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationIsolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationeSAT Journals
 
[EN] PLC programs development guidelines
[EN] PLC programs development guidelines[EN] PLC programs development guidelines
[EN] PLC programs development guidelinesItris Automation Square
 
chapter-04(Computational Thinking).pptx
chapter-04(Computational Thinking).pptxchapter-04(Computational Thinking).pptx
chapter-04(Computational Thinking).pptxssuserf60ff6
 
IRJET- Voice to Code Editor using Speech Recognition
IRJET- Voice to Code Editor using Speech RecognitionIRJET- Voice to Code Editor using Speech Recognition
IRJET- Voice to Code Editor using Speech RecognitionIRJET Journal
 
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSULTHAN BASHA
 

Similar to Python-content-1.pdf (20)

Cd lab manual
Cd lab manualCd lab manual
Cd lab manual
 
Teaching of computer programming
Teaching of  computer programmingTeaching of  computer programming
Teaching of computer programming
 
GE3151_PSPP_UNIT_5_Notes
GE3151_PSPP_UNIT_5_NotesGE3151_PSPP_UNIT_5_Notes
GE3151_PSPP_UNIT_5_Notes
 
OOP ppt.pdf
OOP ppt.pdfOOP ppt.pdf
OOP ppt.pdf
 
IRJET - Pseudocode to Python Translation using Machine Learning
IRJET - Pseudocode to Python Translation using Machine LearningIRJET - Pseudocode to Python Translation using Machine Learning
IRJET - Pseudocode to Python Translation using Machine Learning
 
ilovepdf_merged pdfggjhfgyutertyuiuytrsj
ilovepdf_merged pdfggjhfgyutertyuiuytrsjilovepdf_merged pdfggjhfgyutertyuiuytrsj
ilovepdf_merged pdfggjhfgyutertyuiuytrsj
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
Be cs-course-structure-2015-onwards
Be cs-course-structure-2015-onwardsBe cs-course-structure-2015-onwards
Be cs-course-structure-2015-onwards
 
Arm lab
Arm labArm lab
Arm lab
 
Programming
ProgrammingProgramming
Programming
 
Isolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationIsolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantization
 
Isolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantizationIsolated word recognition using lpc & vector quantization
Isolated word recognition using lpc & vector quantization
 
[EN] PLC programs development guidelines
[EN] PLC programs development guidelines[EN] PLC programs development guidelines
[EN] PLC programs development guidelines
 
chapter-04(Computational Thinking).pptx
chapter-04(Computational Thinking).pptxchapter-04(Computational Thinking).pptx
chapter-04(Computational Thinking).pptx
 
PP ECE A Sec UNIT-1.pptx
PP ECE A Sec UNIT-1.pptxPP ECE A Sec UNIT-1.pptx
PP ECE A Sec UNIT-1.pptx
 
2nd sem
2nd sem2nd sem
2nd sem
 
2nd sem
2nd sem2nd sem
2nd sem
 
3350703
33507033350703
3350703
 
IRJET- Voice to Code Editor using Speech Recognition
IRJET- Voice to Code Editor using Speech RecognitionIRJET- Voice to Code Editor using Speech Recognition
IRJET- Voice to Code Editor using Speech Recognition
 
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
 

Recently uploaded

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Recently uploaded (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
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
  • 2. PROBLEM SOLVING AND PYTHON PROGRAMMING First Edition : November 2021 ISBN 13 : 978-81-939626-9-5 Published by M AGNUS PUBL I CAT I ONS © All rights reserved. No part of this publication may be reproduced in any form without the prior written permission of the publisher. Price Rs.515/- Copies can be had from M AGNUS PUBL I CAT I ONS Plot No.7, Balaji Nagar 2nd Street, Vanagaram, Chennai-600 095. Cell : 9840788450, 9840482641 Phone: +91 44 2476 5688, 2476 5344 Email: magnuspublication@yahoo.co.in ii
  • 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