0% found this document useful (0 votes)
4 views13 pages

Comprehensive Guide to Python Programming

Uploaded by

regitaraja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Comprehensive Guide to Python Programming

Uploaded by

regitaraja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Table of Contents

SXC LIBRARY

Preface.
KXiii
125146

Chapter 1: Introduction to Python 1


Python. 2
Features of Python.... 3
Execution of a Python Program 7
Viewing the Byte Code ..9
Plavors of Python... ....10
Python Virtual Machine (PVM)..... .... 11
Frozen Binaries .12
Memory Management in Python.... .... 12
Garbage Collection in Python.... 13
Comparisons between Cand Python ... ...... 15
LComparisons between Java and Python.. 16
Points to Remember. 17

Chapter 2: Writing Our irst Python Program .19


Installing Python for Windows. 19
Testing the Installation in Windows 10..... 21
Verifying the Path to Python.. .24
Installing numpy ..27
Installing pandas. .28
Installing xlrd .28
Installing matplotlib.. .29
Verifying Installed Packages. .29
Writing Our First Python Program.. .30
Executing a Python Program. ........31
Using Python's Command Line Window ...32
Using Python's IDLE Graphics Window. .32
Running Directly from System Prompt ..35
Getting Help in Python... 38
Getting Python Documentation Help .41
Points to Remember. 43

J2
-22
X Core Python

Chapter 3: Datatypes in Python 45

Comments in Python 46
Single line comments 46
Multi line comments.. 46

Docstrings. 47
How Python Sees Variables. 49
Datatypes in Python .51
Built-in datatypes ...51
The None Type .51
Numeric Types 52
Representing Binary, Octal and Hexadecimal Numbers 53
Converting the Datatypes Explicitly. . . . 53
bool Datatype .55
Sequences in Python..... 56
str Datatype .56
bytes Datatype .57
bytearray Datatype . .58
list Datatype 59
tuple Datatype 59
range Datatype. 60
Sets. 61
set Datatype........ .61
frozenset Datatype ...62
Mapping Types.. 62
Literals in Python ....7.. .63
Numeric Literals. 64
Boolean Literals 64
String Literals .64
Determining the Datatype of a Variable . .65
What about Characters. .66
User-defined Datatypes.. .67
Constants in Python ,67
Identifiers and Reserved words.. .67
Narning Conventions in Python ....

Points to Remember.

Chapter 4:Operators in Python... .71

Operator. 71
Arithmetic Operators .72
Table of Contents xi

Using Python Interpreter as Calculator. .73


Assignment Operators
.73
Unary Minus Operator.
.75
Relational Operators 75
Logical Operators. 76
Boolean Operators .77
Bitwise Operators 78
Bitwise Complement Operator (- )..... .80
Bitwise AND Operator (&) .80
Bitwise OR Operator (|)........ .....81
Bitwise XOR Operator (^)........... ..81
Bitwise Left Shift Operator (<<) 82
Bitwise Right Shift Operator (>>).... 82
Membership Operators .. .84
The in Operator. 84
The not in Operator. .84
Jdentity Operators. ....85
The is Operator .85
The is not Operator .86
Operator Precedence and Associativity . .87
Mathematical Functions ... .88
Using IDLE Window
.91
Using Command Line Window.... .92
Executing at System Prompt .92
Points to Remember.
.93
Chapter 5: Input and Output .95
Output statements 96
The print() Statement
.96
The print("string") Statement .96
The print(variables list) Statement 97
The print(object) Statement .98
The print("string", variables list) Statement. .98
The print(formatted string) Statement 98
Input Statements. 101
Command Line Arguments. . 108
Parsing Command Line Arguments. l10
Points to Remember.. l15
xii Core Python

Chapter 6: Control Statements. 117


Control Statements. 118
The if Statement 118
A Word on Indentation 119
The if ... else Statement 121
The if ... elif ... else Statement.. 122
The while Loop ... 124
The for Loop 126
Infinite Loops.. 130
Nested Loops 131
The else Suite.... 136
The break Statement ...... 137
The continue Statement 138
The pass Statement 139
The assert Statement. 140
The return Statement 141
Points to Remember.. 149

Chapter 7: Arrays in Python 151


Array 151
Advantages of Arrays 152
Creating an Array ..f. 153
Importing the Array Module . . 154
Andexing and Slicing on Arrays..... 156
Processing the Arrays 160
Types of Arrays............... 167
Working with Arrays using numpy... l68
Creating Arrays using array) 170
Creating Arrays using linspace. 171
Creating Arrays using logspace .172
Creating Arrays using arange) Function 173
Creating Arrays using zeros) and ones() Functions.
.....
174
Mathematical Operations on Arrays ...,. 17+
Cornparing Arrays. 177
Aliasing the Arrays
Viewing and Copying Arrays...
Slicing and Indexing in numpyArrays 1S3
Dimensions of Arrays.. 185
Attributes of an Array 186
The ndim Attribute. 186
Table of Contents xiii

The shape Attribute. 186


The size Attribute 187
The itemsize Attribute 187
The dtype Attribute 188
The nbytes Attribute. 188
The reshape() Method 188
The flatten() Method. 189
Working with Multi-dimensional Arrays . 189
The array() Function 189
The ones() and zeros) Functions.. 190
The eye() Function 191
The reshape() Function.. 191
Indexing in Multi-dimensional Arrays..... 192
Slicing the Multi-dimensional Arrays.. 194
Matrices in numpy...«... 196
Getting Diagonal Elements of a Matrix .197
Finding Maximum and Minimum Elements 198
Finding Sum and Average of Elements 198
Products of Elemnents 198
Sorting the Matrix. 199
Transpose of a Matrix .200
Matrix Addition and Multiplication.... .201
Random Numbers.. .204
Points to Remember. .205
Chapter 8: Strings and Characters 207
Creating Strings 207
Length of a String. 209
Indexing in Strings 209
Slicing the Strings 211
Repeating the Strings.. .....213
Concatenation of Strings.. 213
Checking Membership ... 214
Comparing Strings.. 214
Removing Spaces from a String. 215
Finding Sub Strings. 216
Counting Substrings in a String. .218
Strings are Immutable .219

Replacing a String with another String.. .221


Splitting and Joining Strings.. .222
xiv Core Python

Changing Case ofa String. .223


Checking Starting and Ending of a String. .224
String Testing Methods 224
Formatting the Strings. ...225
Working with Characters .228
Sorting Strings .229
Searching in the Strings .231
Finding Number of Characters and Words... .232
Inserting Sub String into a String. .233
Points to Remember. .235

Chapter 9: Functions ...237


Difference between a Function and a Method .238
Defining a Function .238
Calling a Function 239
Returning Results from a Function .240
Returning Multiple Values from a Function 245
Functions are First Class Objects...... .246
Pass by ObjectReference .248
Formal and Actual Arguments 253
Positional Arguments. .253
Keyword Arguments. 254
Default Arguments .255
Variable Length Arguments. .256
Local and Global Variables.. .258
The Global Keyword. .258
Passing a Group of Elements to a Function 260
Recursive Functions
262
Anonymous Functions or Lambdas 265
Using Lambdas with filter) Function ... 266
Using Lambdas with map) Function.. 268
Using Lambdas with reduce() Function 269
Function Decorators
..270
Generators .274
Structured Programming .276
Creating our Own Modules in Python. 277
The Special Variable name 279
Points to Remember
280
Table of Contents XV

Chapter 10: Lists and Tuples. 283


List. 283
Creating Lists using range() Function 285
Updating the Elements of a List
288
Concatenation of Two Lists
290
Repetition of Lists
..291
Membership in Lists .291
Aliasing and Cloning Lists........ 291
Methods to Process Lists.
.293
Finding Biggest and Smallest Elements in a List.. 295
Sorting the List Elements.
.296
Number of Occurrences of an Element in the List..
298
Finding Common Elements in Two Lists.. 299
Storing Different Types of Data in a List 300
Nested Lists..
301
Nested Lists as Matrices
302
List Comprehensions 305
Tuples
.307
Creating Tuples
307
Accessing the Tuple Elements .308
Basic Operations on Tuples
.310
Functions to Process Tuples.......
311
Nested Tuples .313
Inserting Elements in a Tuple 315
Modifying Elements of a Tuple .......316
Deleting Elements from a Tuple .318
Points to Remember..
.319
Chapter 11: Dictionaries
321
Operations on Dictionaries.
.322
Dictionary Methods. .324
Using for Loop with Dictionaries.
.327
Sorting the Elements of a Dictionary using
Converting Lists into Dictionary Lambdas 331
.332
Converting Strings into Dictionary ...333
Passing Dictionaries to Functions
Ordered Dictionaries. . .
334
335
Points to Remermber 336
XVi Core Python
337
Chapter 12: Introduction to Oops
338
Problems in Procedure Oriented Approach..
...340
Specialty of Python Language.
340
Features of Object Oriented Programming System (00PS) ... ...

340
Classes and Objects
...343
Encapsulation. 344
Abstraction
347
Inheritance
348
Polymorphism 349
Points to Remember.
351
Chapter 13: Classes and Objects
351
Creating a Class 355
The Self Variable.
355
Constructor
.357
Types of Variables.......
359
Namespaces .....361
Types of Methods.
361
Instance Methods
364
Class Methods.
Static Methods 364
367
Passing Members of One Class to AnotherClass.
.369
Inner Classes..
Points to Remember ...371

Chapter 14: Inheritance and Polymorphism 373


Constructors in Inheritance .378
Overriding Super Class Constructors and Methods. .379
The super() Method 380
/Types of Inheritance 382
Single Inheritance .382
Multiple Inheritance 384
Method Resolution Order (MRO).. 388
Polymorphism 390
Duck Typing Philosophy of Python ...
391
VOperator Overloading.. 395
Method Overloading.. 400
Method Overriding. 401
Points to Remember. 402
Table of Contents xvii

Chapter 15: Abstract Classes and Interfaces 405


Abstract Method and Abstract Class 407
Interfaces in Python. 412
Abstract Classes vs. Interfaces. 418
Points to Remember. 419

Chapter 16: Exceptions, 421


Errors in a Python Program 421
Compile-Time Errors .. 421
Runtime Errors 422
Logical Errors. 423
Exceptions.. 425
Exception Handling 426
Types of Exceptions 429
The Except Block. 432
The assert Statement 433
User-Defined Exceptions. 434
Logging the Exceptions 436
Points to Remember. .439

Chapter 17: Files in Python .441


Files 441
Types of Files in Python 442
Opening a File 443
Closing a File. 444
Working with Text Files Containing Strings .446
Knowing Whether a File Exists or Not... ....449
Working with Binary Files. 451
The with Statement.
.432
Pickle in Python. +32
The seek() and tell) Methods ... +56
Random Accessing of Binary Files +57
Random Accessing of Binary Files using mmap
Zipping and Unzipping Files.. +67
Working with Directories. -+69
Running Other Programs from Python Program. .+74
Points to Remember. .475
xviii Core Python

Chapter 18: Regular Expressions in Python 477


Regular Expressions .477
Sequence Charactersin Regular Expressions .483
Quantifiers in Regular Expressions 487
Special Charactersin Regular Expressions 489
Using Regular Expressions on Files.. .492
Retrieving Information from a HTML File ... .495
Points to Remember. 497

Chapter 19: Data Structures in Python 499


Linked Lists. 500
Stacks 503
Queues. 507
Deques. . .

.510
Points to Remember. 514

Chapter 20: Date and Time 515


The epoch.. 515
Date and Time Now. .517
Combining Date and Time..... 519
Formatting Dates and Times 520
Finding Durations using timedelta'. .527
Comparing Two Dates. .528
Sorting Dates. .529
Stopping Execution Temporarily .530
Knowing the Time taken by a Program 531
Working with Calendar Module .532
Points to Remember. ....535

Chapter 21: Threads. 537


Single Tasking. 538
Multitasking. 539
Differences between a Process and a Thread. ..... 541
Concurrent Programming and GIL 542
Uses of Threads
542
Creating Threads in Python. 543
Creating a Thread without using a Class.. 543
Creating a Thread by Creating a Sub Class to Thread Class 544
Creating a Thread without Creating Sub Class to Thread Class 546
Thread Class Methods
547
Table of Contents xix

Single Tasking using a Thread 548


Multitasking using Multiple Threads 549
Thread Synchronization 553
Locks.. 553
Semaphore....... 555
Deadlock of Threads
.555
Avoiding Deadlocks in a Program .557
Communication between Threads 559
Thread Communication using nofity) and wait) Methods. .561
Thread Communication using a Queue 563
Daemon Threads
565
Points to Remember.
.567

Chapter 22: Graphical User Interface 569


GUI in Python. 570
The Root Window
570
Fonts and Colors
..572
Working with Containers .576
Canvas.. 576
Frame 584
Widgets 585
Button Widget 587
Arranging Widgets in the Frame 592
Label Widget. 595
Message Widget..... 597
Text Widget 598
Scrollbar Widget .601
Checkbutton Widget .. .602
Radiobutton Widget .605
EntryWidget 606
Spinbox Widget... .609
Listbox Widget .611
Menu Widget .614
Creating Tables .620
Points to Remember. .621

Chapter 23: Networking in Python 623


Protocol .624
TCP/IP Protocol... 624
User Datagram Protocol (UDP) .626
XX Core Python

Sockets
Knowing IP Address .626
URL.. .628
Reading the Source Code of a Web Page .628
Downloading a Web Page from Internet 630
Downloading an Image from Internet.. ....631
A TCP/IP Server. ....632
.634
A TCP/IP Client
AUDP Server. .635
A UDP Client 637
File Server .638
File Client. 640
641
Two-Way Communication between Server and Client.........
642
N Sending a Simple Mail
Points to Rememnber. .644
.647
Chapter 24: Python's Database Connectivity 649
DBMS
.649
Advantages of a DBMS over Files
.649
Types of Databases Used with Python
650
Installation of MySQL Database Software 651
Verifying MySQL in the Windows Operating System...... 660
Installing MySQLdb Module 661
Verifying the MySQLdb Interface Installation. 663
Working with MySQL Database 663
Using MySQL from Python 665
/Retrieving All Rows from a Table 666
/Inserting Rows into a Table. 669
Deleting Rows from a Table. 672
Updating Rows in a Table 673
,Creating Database Tables through Python. 674
Installation of Oracle 1lg... 678
Verifying Oracle Installation in Windows Operating System 682
Installing Oracle Database Driver... 683
Verifying the Driver Installation 683
Working with Oracle Database 684
Using Oracle Database from Python 686
Stored Procedures 689
Points to Remember.. .691
Table of Contents Xxi

Chapter 25: Data Science Using Python 693


Data Frame
694
Creating Data Frame from an Excel Spreadsheet 695
Creating Data Frame from .csv Files 696
Creating Data Frame from a Python Dictionary 696
Creating Data from Python List of Tuples. .697
Operations on Data Frames. 698
vData Visualization
706
Bar Graph. 706
Histogram 709
Creating a Pie Chart. 711
Creating Line Graph. ...712
Points to Remember..
...713
Program Index .715

You might also like