20% found this document useful (10 votes)
4K views

TAMING PYTHON by PROGRAMMING

python book

Uploaded by

sanchi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
20% found this document useful (10 votes)
4K views

TAMING PYTHON by PROGRAMMING

python book

Uploaded by

sanchi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TAMING PYTHON

by
PROGRAMMING

Dr. Jeeva Jose

KHANNA BOOK PUBLISHING CO. (P) LTD.


::: Publisher of Engineering and Computer Books :::
Head Office : 4C/4344, Ansari Road, Darya Ganj, New Delhi-110002
Phone : 011-23244447/48 Fax : 011-23244447
Regd. Office : 1694-95, Nai Sadak, Delhi-110006
Phone : 011-23283211 Fax : 011-23256658
E-mail : [email protected]
Website : www.khannabooks.com, www.khannapublishers.com
Price : 325.00

TAMING PYTHON BY PROGRAMMING


Dr. Jeeva Jose

Copyright © Khanna Book Publishing Co. (P) Ltd.


This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, resold, hired
out, or otherwise circulated without the publisher’s prior consent in any form of binding or cover other than
that in which it is published and without a similar condition including this condition being imposed on the
subsequent purchaser and without limiting the rights under copyright reserved above, no part of this publication
may be reproduced, stored in or introduced into retrieval system, or transmitted any form or by any means
(electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of both
the copyright owner and the above mentioned publisher of this book.

ISBN: 978-93-86173-34-8

Edition: 2017

Published by:
KHANNA BOOK PUBLISHING CO. (P) LTD.
4C/4344, Ansari Road, Darya Ganj, New Delhi-110 002
Phone: 011-23244447-48 Mobile: +91-9910909320
E-mail: [email protected]

Printed in India by:


India Book Printers & Binders, Delhi
Preface

Python is a general-purpose, high-level programming language. It is an Open Source


Software and its source code is available with a license in which the Copyright holder
provides the rights to study, change, and distribute the software to anyone and for any
purpose. This programming language was developed in late 1980s and its implementation
was started in December 1989 by Guido van Rossum at Centrum Wiskunde & Informatica
which is a research center in Netherlands. The non-profit organization Python Software
Foundation fosters the development of Python community and is responsible for various
processes within the Python community which includes developing Python projects,
distribution, managing intellectual rights, developer conferences including PyCon, and
raising funds.
Python is easy to learn for a first time programmer or a person experienced in other
programming languages. It can be read like English language. Python can run on
any hardware platform (PC, Mac, Sun Sparc, etc.) or software platform (Linux,
MacOS, Unix, Windows, etc.). Its design philosophy emphasizes code readability and its
syntax allow programmers to express concepts in least lines of code than in languages
like C++ or Java. Python supports multiple programming paradigms, including object-
oriented, imperative and functional programming or procedural styles. It features
a dynamic type system, automatic memory management and has a large
comprehensive standard library. It is powerful, fast and has the ability to play with other
programs.
Python is an interpreted, interactive, object-oriented programming language. It
incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and
classes. Python is a scripting language like PHP, Perl, Ruby and can be
used for Web programming (Django, Zope, Google App Engine and much more). It also can
be used for desktop applications (Blender 3D, pygame). Python can also be translated into
byte code like Java.
Python is great for data analysis, artificial intelligence and scientific computing.
Developers can use Python to build prototypes, productivity tools and games. The softwares
like YouTube, DropBox, Instagram, etc. are to name a few which is written in Python.
Python is used in many application domains. Python’s standard library supports many
Internet Protocols such as HTML, XML, JSON, E-mail processing, FTP, IMAP etc.
Government is utilizing this for Administration, Homeland Security, Public Safety, Traffic
Control, Urban Infrastructure etc. In Business, Python is using in domains such as
Consumer Goods Industry, Aviation, Medical, Industrial, Financial services, GIS &
Mapping, Marine and Lighting. Python is used in areas of Customer Relationship
Management (CRM), Content & Document Management, Energy Conservation, E-
Commerce, Enterprise Resource Planning (ERP), Knowledge Management, Manufacturing,
Product Development, Project Management, Quality Control, Online Analytical Processing,
Risk Management, Simulation etc. In Network Programming, Python is used to control
Firmware updates. In Software, Python plays a role in Computer Graphics, Cross-platform
Development, Data Mining, Documentation Development, Embedded Systems etc.
Based on the application, many packages and libraries are developed in Python. SciPy
is a collection of packages for Mathematics, Science, and Engineering. Pandas is a data
analysis and modeling library. IPython is a powerful interactive shell that features easy
editing and recording of a work session, and supports visualizations and parallel computing.
This book takes you from basics of Python to advanced areas smoothly. This book is
suitable for Python enthusiasts, students and researchers.
Chapter 1 of this book introduce you to Python, its features, programming constructs
like identifiers, reserved keywords, variables and various operators. All the data types in
Python which includes numbers, strings, list, tuple, set and dictionary are covered in Chapter
2. Chapter 3 explains various types of decision making and loops. While moving to Chapter
4, the book covers a detailed explanation of functions. Chapter 5 explains about modules
and packages. Concepts and operations of file handling are explained in Chapter 6. Object
Oriented Programming concepts are explained in Chapter 7. Chapter 8 covers Exception
Handling techniques. Regular Expressions are covered in Chapter 9. Chapter 10 explains
how databases can be connected from Python. All operations including creation of tables,
insert, delete, update and SQL statements are explained in detail.
Advanced topics like the concept of iterators, generators and decorators in Python are
provided in Chapter 11. These topics will be new to many other programmers using C or
C++. Chapter 12 explains about GUI programming using tkinter and various widgets
used in Python. Chapter 13 explains the concept of multithreading. Different types of threads
in Python, the thread module and threading module is explained. Chapter 14 covers a
detailed description of CGI programming. Various HTTP headers and environment variables
are explained in detail. Chapter 15 covers the concept of socket or network programming.
The socket module, various methods associated with client sockets and server sockets are
illustrated. How to connect to a server, making a server and making a client are illustrated
with example programs.
All Chapters of this book have worked out programs, illustrations, review and frequently
asked interview questions. More than 450 solved lab exercises available in this book is
tested in Python 3.4.3 version for Windows.

–Author
Table of Contents

Chapter 1 Introduction to Python 1 – 16

1.1 Features of Python 1


1.2 How to Run Python 2
1.3 Identifiers 3
1.4 Reserved Keywords 4
1.5 Variables 4
1.6 Comments in Python 5
1.7 Indentation in Python 5
1.8 Multi-Line Statements 6
1.9 Multiple Statement Group (Suite) 6
1.10 Quotes in Python 7
1.11 Input, Output and Import Functions 7
1.11.1 Displaying the Output 7
1.11.2 Reading the Input 8
1.11.3 Import function 8
1.12 Operators 9
1.12.1 Arithmetic Operators 9
1.12.2 Comparison Operators 10
1.12.3 Assignment Operators 11
1.12.4 Bitwise Operators 12
1.12.5 Logical Operators 13
1.12.6 Membership Operators 14
1.12.7 Identity Operators 14
1.12.8 Operator Precedence 15
1.13 Conclusion 15
1.14 Review Questions 15

(v)
(vi)

Chapter 2 Data Types and Operations 17 – 65

2.1 Numbers 17
2.1.1 Mathematical Functions 18
2.1.2 Trigonometric Functions 20
2.1.3 Random Number Functions 21
2.2 Strings 21
2.2.1 Escape Characters 22
2.2.2 String Formatting Operator 22
2.2.3 String Formatting Functions 24
2.3 List 33
2.3.1 Built-in List Functions 34
2.3.2 Built-in List Methods 35
2.3.3 Using List as Stacks 38
2.3.4 Using List as Queues 39
2.4 Tuple 39
2.4.1 Built-in Tuple Functions 40
2.5 Set 41
2.5.1 Built-in Set Functions 42
2.5.2 Built-in Set Methods 44
2.5.3 Frozenset 48
2.6 Dictionary 49
2.6.1 Built-in Dictionary Functions 51
2.6.2 Built-in Dictionary Methods 52
2.7 Mutable and Immutable Objects 55
2.8 Data Type Conversion 56
2.9 Solved Lab Exercises 58
2.10 Conclusion 63
2.11 Review Questions 63

Chapter 3 Flow Control 66 – 101

3.1 Decision Making 66


3.1.1 if statement 66
3.1.2 if….else statement 67
3.1.3 if...elif...else statement 68
3.1.4 Nested if statement 70
3.2 Loops 70
3.2.1 for loop 70
(vii)
3.2.2 range() function 72
3.2.3 enumerate(iterable,start=0)function 73
3.2.4 for loop with else statement 73
3.2.5 while loop 73
3.2.6 while loop with else statement 75
3.3 Nested Loops 76
3.4 Control Statements 77
3.4.1 break statement 78
3.4.2 continue statement 79
3.4.3 pass statement 79
3.5 Types of Loops 80
3.5.1 Infinite Loop 80
3.5.2 Loops with condition at the top 80
3.5.3 Loop with condition in the middle 80
3.5.4 Loop with condition at the bottom 81
3.6 List Comprehensions 82
3.6.1 Nested List 83
3.6.2 Nested List Comprehensions 83
3.7 Set Comprehensions 84
3.8 Dictionary Comprehensions 84
3.9 Nested Dictionaries 84
3.10 Solved Lab Exercises 86
3.11 Conclusion 101
3.12 Review Questions 101

Chapter 4 Functions 102–118

4.1 Function Definition 102


4.2 Function Calling 103
4.3 Function Arguments 104
4.3.1 Required Arguments 104
4.3.2 Keyword Arguments 105
4.3.3 Default Arguments 105
4.3.4 Variable-Length Arguments 106
4.4 Anonymous Functions (Lambda Functions) 106
4.4.1 filter() Function 108
4.4.2 reduce() Function 108
4.5 Recursive Functions 109
4.6 Function with more than one return value 110
(viii)

4.7 Solved Lab Exercises 111


4.8 Conclusion 118
4.9 Review Questions 118

Chapter 5 Modules and Packages 119 – 152

5.1 Built-in Modules 119


5.2 Creating Modules 126
5.3 import Statement 126
5.3.1 import with renaming 126
5.3.2 from…import statement 127
5.3.3 import all names 127
5.4 Locating Modules 128
5.4.1 PYTHONPATH variable 128
5.5 Namespaces and Scope 128
5.6 The dir() function 129
5.7 The reload() function 130
5.8 Packages in Python 131
5.8.1 Importing modules from a Package 131
5.9 Date and Time Modules 132
5.9.1 The time module 132
5.9.2 The calendar Module 134
5.9.3 The datetime Module 138
5.10 Solved Lab Exercises 142
5.11 Conclusion 152
5.12 Review Questions 152

Chapter 6 File Handling 153 – 169

6.1 Opening a File 153


6.1.1 Modes for Opening a File 154
6.1.2 Attributes of file object 155
6.2 Closing a File 156
6.3 Writing to a File 156
6.3.1 with Statement 157
6.4 Reading from a File 157
6.5 File Methods 158
6.6 Renaming a File 160
6.7 Deleting a File 160
6.8 Directories in Python 160
(ix)
6.8.1 mkdir() method 161
6.8.2 chdir() method 161
6.8.3 getcwd() method 161
6.8.4 rmdir() method 161
6.9 Solved Lab Exercises 162
6.10 Conclusion 169
6.11 Review Questions 169

Chapter 7 Object Oriented Programming 170 – 195

7.1 Class Definition 171


7.2 Creating Objects 172
7.3 Built-in Attribute Methods 173
7.4 Built-in Class Attributes 174
7.5 Destructors in Python 175
7.6 Encapsulation 176
7.7 Data Hiding 176
7.8 Inheritance 177
7.8.1 Deriving a Child Class 177
7.8.2 Multilevel Inheritance 178
7.8.3 Multiple Inheritance 180
7.8.4 Invoking the Base Class Constructor 182
7.9 Method Overriding 183
7.10 Polymorphism 184
7.10.1 Operator Overloading 184
7.11 Solved Lab Exercises 185
7.12 Conclusion 194
7.13 Review Questions 195

Chapter 8 Exception Handling 196 – 206

8.1 Built-in Exceptions 196


8.2 Handling Exceptions 198
8.2.1 try….except 199
8.2.2 except clause with no Exception 200
8.2.3 except clause with multiple Exceptions 201
8.2.4 try…finally 202
8.3 Exception with Arguments 203
8.4 Raising an Exception 203
(x)

8.5 User-defined Exception 204


8.6 Assertions in Python 205
8.7 Conclusion 206
8.8 Review Questions 206

Chapter 9 Regular Expressions 207 – 222

9.1 The match() function 207


9.2 The search() function 208
9.3 Search and Replace 209
9.4 Regular Expression Modifiers: Option Flags 209
9.5 Regular Expression Patterns 210
9.6 Character Classes 211
9.7 Special Character Classes 212
9.8 Repetition Cases 212
9.9 findall() method 212
9.10 Solved Lab Exercises 213
9.11 Conclusion 222
9.12 Review Questions 222

Chapter 10 Database Programming 223 – 233

10.1 Connecting to a Database 223


10.2 Creating Tables 224
10.3 INSERT Operation 225
10.4 UPDATE Operation 225
10.5 DELETE Operation 226
10.6 READ Operation 226
10.7 Transaction Control 228
10.7.1 COMMIT Operation 228
10.7.2 ROLLBACK Operation 229
10.8 Disconnecting from a Database 229
10.9 Exception Handling in Databases 229
10.10 Solved Lab Exercises 230
10.11 Conclusion 233
10.12 Review Questions 233
(xi)

Chapter 11 Iterators, Generators and Decorators 234 – 249

11.1 Iterators 234


11.1.1 Data Types that Support Iterators 235
11.1.2 Iterators with Classes 236
11.2 Generators 237
11.2.1 Passing Values to a Generator 239
11.3 Generator Expressions 240
11.4 Nested Functions 240
11.4.1 Encapsulation 241
11.4.2 Closures 242
11.5 Decorators 243
11.5.1 Functions inside Functions 244
11.5.2 Functions as Arguments 244
11.5.3 Functions returning Functions 245
11.5.4 A Simple Decorator 245
11.5.5 Checking Arguments with a Decorator 246
11.5.6 Counting Function Calls with Decorators 247
11.5.7 Classes as Decorators 248
11.6 Conclusion 249
11.7 Review Questions 249

Chapter 12 GUI Programming 250 – 293

12.1 Introduction 250


12.2 Tkinter Widgets 251
12.2.1 Label 251
12.2.2 Message Widget 253
12.2.3 Entry Widget 253
12.2.4 Text Widget 256
12.2.5 tk Message Box 259
12.2.6 Button Widget 260
12.2.7 Radio Button 262
12.2.8 Checkbutton 264
12.2.9 Listbox 267
12.2.10 Frames 270
12.2.11 Toplevel Widgets 271
12.2.12 Menu Widget 273
(xii)

12.2.13 Menubutton Widget 276


12.2.14 Scrollbar 278
12.2.15 Scale Widget(Slider Widget) 280
12.2.16 Canvas 283
12.3 Layout Managers 286
12.3.1 Pack 286
12.3.2 Place 291
12.3.3 Grid 291
12.4 Conclusion 293
12.5 Review Questions 293

Chapter 13 Multithreading 294 – 306

13.1 Introduction 294


13.2 Threads in Python 295
13.2.1 The thread Module 295
13.2.2 The threading Module 296
13.3 Thread Objects 297
13.4 Lock Objects 299
13.5 RLock Objects 301
13.6 Condition Objects 301
13.7 Semaphore Objects 303
13.8 Event Objects 305
13.9 Timer Objects 305
13.10 Conclusion 306
13.11 Review Questions 306

Chapter 14 CGI Programming 307 – 320

14.1 Introduction 307


14.2 A Simple CGI Program 308
14.3 HTTP Headers 308
14.4 Environment Variables 309
14.5 Forms 310
14.5.1 GET Method 311
14.5.2 POST Method 311
(xiii)

14.6 Radio Buttons 312


14.7 Drop Down Box 313
14.8 Check Boxes 314
14.9 Text Area 315
14.10 Cookies 317
14.10.1 Cookie Attributes 317
14.10.2 Setting Up Cookies 317
14.10.3 Retrieving Cookies 318
14.11 Uploading File 318
14.12 Conclusion 319
14.13 Review Questions 320

Chapter 15 Socket Programming 321 –327

15.1 Introduction 321


15.2 The socket Module 322
15.3 Socket Methods 322
15.4 Connecting to a Server 323
15.5 Making a Server 324
15.6 Making a Client 325
15.7 Conclusion 327
15.8 Review Questions 327
(xiv)

You might also like