Learn Visual Studio Code and implement its features in Python coding, debugging, linting, and overall project management. This book addresses custom scenarios for writing programs in Python frameworks, such as Django and Flask.<div><br></div><div>The book starts with an introduction to Visual Studio
Python for Developers: Learn to Develop Efficient Programs using Python
โ Scribed by Mohit Raj
- Publisher
- BPB Publications
- Year
- 2023
- Tongue
- English
- Leaves
- 421
- Category
- Library
No coin nor oath required. For personal study only.
โฆ Synopsis
Master python programming language in easy steps
Key Features
โ Start from basics of Python
Control statement, loop structure, break, continue, and pass statement
โ Detailed description of Python data types: string, tuple, list, and dictionary with the help of example
โ Organizing code using function, modules, and packages
โ Saving text and complex data in text, pickle, and JSON files
โ Learn the use of time and time zones
โ Parallel execution with the help of threading, multiprocessing, and subprocessing
โ Helpful modules for industry
Description
It is said that learning Python is easy, but if a learner did not get the right path, then things can get complicated. This book is designed in such a way that you start from basics, followed by advance levels and then move on to some industry-related modules.
The initial chapters are written in a simple manner; some chapters are of advance level. Start from the data structure of Python, such as string, list, tuple, and dictionary. The function and module chapter will let you know how to organize a large code. The built-in functions and modules like collections will give you greater flexibility to write efficient codes. The "time" chapter is very important when we deal with time-related things.
The mid-chapter contains the advance chapters such as regular expressions, interaction with OS, and multithreading. These chapters are helpful when we want to search the pattern, run the OS commands, and execute the program in parallel. The last chapters are specially designed from an industry point of view. In order to ensure a high quality of code, we use config-parser to avoid hard-coding and logger to log the events. In the multiprocessing and subprocess chapter, you will learn creation, execution, and communication between the processes.
โฆ Table of Contents
- Introduction to Python ....................................................................................... 1
Structure .......................................................................................................... 1
Objective .......................................................................................................... 1
What is Python? ............................................................................................. 2
Reasons to choose Python .......................................................................... 2
Multi-purpose ................................................................................................. 2
Vast library and module support .................................................................... 3
Readability ...................................................................................................... 3
Object-Oriented .............................................................................................. 3
Platform independent ...................................................................................... 3
Python is dynamically typed and strongly typed ........................................... 3
Python installation ......................................................................................... 4
Installation of Python 3.x in Windows 10 ...................................................... 4
Installation of Python in Linux ................................................................. 6
Basic Python syntax ....................................................................................... 9
Print statement .......................................................................................... 9
Saving the program .................................................................................. 10
Triple quotes ............................................................................................. 10
Python Back Slash \ .................................................................................11
Escape sequence of string ..........................................................................11
Python formatted output.......................................................................... 12
Conclusion .................................................................................................... 12
Questions ....................................................................................................... 13 - Python Operators ............................................................................................... 15
Structure ........................................................................................................ 15
Objective ........................................................................................................ 15
Variables ........................................................................................................ 15
Assignment statement ................................................................................. 16
Multiple assignment .................................................................................... 16
Numeric data types ..................................................................................... 17
๏ฎ xi
Integer numbers ........................................................................................... 18
Floating-point numbers .............................................................................. 18
Python character sets ................................................................................... 18
Conversion functions .................................................................................. 19
Operators ....................................................................................................... 20
Arithmetic operators ................................................................................... 20
Type conversions ...................................................................................... 21
Comparison operators ................................................................................. 22
Assignment operators ................................................................................. 23
Bitwise operators .......................................................................................... 24
Logical operators .......................................................................................... 25
Membership operators ................................................................................ 26
Identity Operators ........................................................................................ 27
Operator precedence ................................................................................... 28
Advance part ................................................................................................ 29
Conclusion .................................................................................................... 30
Questions ....................................................................................................... 30 - Control Statement and Loop ........................................................................... 31
Structure ........................................................................................................ 31
Objective ........................................................................................................ 32
Conditional statement ................................................................................. 32
If and if-else statements ............................................................................ 32
if statement .................................................................................................... 33
Tabs or Spaces? ........................................................................................ 34
if-else statement ........................................................................................ 34
if-elif-else structure .................................................................................. 35
Range() ........................................................................................................... 37
Loops.............................................................................................................. 39
For loop..................................................................................................... 39
Printing number 0 to 9 ................................................................................. 40
Printing the numbers in horizontal fashion ................................................. 40
For loop with string ...................................................................................... 41
Exercise 1 ...................................................................................................... 42
xii ๏ฎ
Exercise 2 ...................................................................................................... 43
while loop ...................................................................................................... 43
Break statement ............................................................................................ 45
Break statement with the while loop ........................................................ 46
Continue statement ...................................................................................... 47
else statement ............................................................................................... 48
pass statement .............................................................................................. 49
Conclusion .................................................................................................... 50
Questions ....................................................................................................... 50 - Strings .................................................................................................................. 51
Structure ........................................................................................................ 51
Objective ........................................................................................................ 51
Indexing using subscript operator ............................................................ 53
Slicing for substrings ................................................................................... 54
String methods ............................................................................................. 56
count() ...................................................................................................... 56
find() ......................................................................................................... 57
Justify methods ......................................................................................... 57
ljust() ............................................................................................................. 57
rjust() ............................................................................................................ 58
center() .......................................................................................................... 59
zfill() .............................................................................................................. 59
Case methods ............................................................................................ 60
lower() ........................................................................................................... 60
upper() ........................................................................................................... 60
capitalize() ..................................................................................................... 61
swapcase() ..................................................................................................... 61
Strip methods ........................................................................................... 61
replace() .................................................................................................... 62
Split methods ............................................................................................ 63
Partition methods ..................................................................................... 64
Join method............................................................................................... 65
String Boolean methods ........................................................................... 65
startswith().................................................................................................... 65
๏ฎ xiii
endswith() ..................................................................................................... 66
isdigit() .......................................................................................................... 67
isalpha() ......................................................................................................... 67
isalnum() ....................................................................................................... 68
isspace() ......................................................................................................... 68
format() ......................................................................................................... 68
String functions ............................................................................................ 69
Max()........................................................................................................ 69
min() ......................................................................................................... 70
Conclusion .................................................................................................... 70
Question ........................................................................................................ 70 - Tuple and List ..................................................................................................... 73
Structure ........................................................................................................ 73
Objective ........................................................................................................ 74
Tuple .............................................................................................................. 74
Creating tuple .......................................................................................... 74
Empty tuple................................................................................................... 74
Creating tuple with the items .................................................................. 75
Indexing tuple .............................................................................................. 75
Slicing of tuple ......................................................................................... 76
Tuple methods .............................................................................................. 78
index() ...................................................................................................... 79
Tuple functions ............................................................................................. 79
len() .......................................................................................................... 79
max() ........................................................................................................ 79
Min() ........................................................................................................ 80
Operations of Tuples.................................................................................... 80
Addition of tuples ..................................................................................... 80
Multiplication of tuple ............................................................................. 80
In operator ................................................................................................ 81
for loop with tuple ....................................................................................... 81
Unpacking of tuple ...................................................................................... 82
List .................................................................................................................. 83
Creating a list ........................................................................................... 83
xiv ๏ฎ
Empty list ...................................................................................................... 83
List with the elements ................................................................................... 83
List operations .............................................................................................. 84
Accessing item of a list ............................................................................. 84
Updating list ............................................................................................ 84
Deleting list item...................................................................................... 85
Addition of the lists .................................................................................. 85
Multiplication of List ............................................................................... 86
in operator ................................................................................................ 86
List with for loop ...................................................................................... 86
List functions ................................................................................................ 87
len() .......................................................................................................... 87
max() ........................................................................................................ 87
List methods ................................................................................................. 89
Insert methods .............................................................................................. 89
append().................................................................................................... 89
insert() ...................................................................................................... 91
Deletion ......................................................................................................... 91
remove() .................................................................................................... 91
pop() ......................................................................................................... 92
count() ...................................................................................................... 93
index() ...................................................................................................... 93
Copy() ....................................................................................................... 94
Sort() ........................................................................................................ 95
reverse() .................................................................................................... 98
List comprehensions .................................................................................... 99
Exercise ........................................................................................................ 100
Conclusion .................................................................................................. 100
Questions ..................................................................................................... 101 - Dictionary and Sets ......................................................................................... 103
Structure ...................................................................................................... 103
Objective ...................................................................................................... 103
Dictionary .................................................................................................... 104
๏ฎ xv
Creating a dictionary ............................................................................. 104
Features of dictionary ............................................................................. 104
Operations on dictionary ....................................................................... 104
Accessing the values of dictionary .............................................................. 105
Deleting item from dictionary..................................................................... 105
Updating and adding in the dictionary ...................................................... 106
Adding item to dictionary ........................................................................... 106
Dictionary functions .............................................................................. 107
len(dict) ....................................................................................................... 107
Max(dict) .................................................................................................... 107
Dictionary methods ................................................................................ 107
copy() ........................................................................................................... 108
get() ............................................................................................................. 108
setdefault() .................................................................................................. 109
items() ..........................................................................................................110
keys() ............................................................................................................111
values() .........................................................................................................111
update() ........................................................................................................113
Exercise ...................................................................................................114
Set ..................................................................................................................115
With item ................................................................................................115
Without items ..........................................................................................115
add() ........................................................................................................116
remove() ...................................................................................................116
Conclusion ...................................................................................................117
Questions ......................................................................................................117 - Function ..............................................................................................................119
Structure .......................................................................................................119
Objective ...................................................................................................... 120
What is function? ....................................................................................... 120
Defining a Python function ...................................................................... 120
Function with positional arguments ....................................................... 121
Function with the arguments and return value ..................................... 122
Function with default argument .............................................................. 123
xvi ๏ฎ
Function with variable-length arguments .............................................. 124
Function with keyworded arguments .................................................... 125
Argument pass by reference or value ..................................................... 126
Scope ............................................................................................................ 127
Types of scope ......................................................................................... 127
Local scope ................................................................................................... 128
Enclosing scope ........................................................................................... 128
Global scope ................................................................................................. 128
Built-in scope .............................................................................................. 128
Memory management ............................................................................... 129
Scope of variables ....................................................................................... 129
Conclusion .................................................................................................. 133
Questions ..................................................................................................... 133 - Module ............................................................................................................... 135
Structure ...................................................................................................... 135
Objective ...................................................................................................... 136
Module ......................................................................................................... 136
The import statement ................................................................................ 136
The from statement ................................................................................ 137
Locating Python modules ......................................................................... 140
Compiled Python files ............................................................................... 142
dir() ........................................................................................................ 142
The name statement ....................................................................... 143
Python package .......................................................................................... 145
Importing the modules from different path ............................................ 147
Conclusion .................................................................................................. 148
Questions ..................................................................................................... 148 - Exception Handling ......................................................................................... 149
Structure ...................................................................................................... 149
Objective ...................................................................................................... 149
Exception ..................................................................................................... 150
Try statement with an except clause ....................................................... 150
๏ฎ xvii
Multiple exception block........................................................................... 151
else in exception ......................................................................................... 152
finally statement ......................................................................................... 153
Program find its exception type ............................................................... 154
Raising an exception .................................................................................. 155
Advance section ......................................................................................... 157
User-defined exceptions ......................................................................... 157
Exercise .................................................................................................. 161
Conclusion .................................................................................................. 162
Questions ..................................................................................................... 162 - File Handling .................................................................................................... 163
Structure ...................................................................................................... 163
Objective ...................................................................................................... 164
Text files ....................................................................................................... 164
Reading text from a file ............................................................................. 164
Writing text to a file ................................................................................... 167
The with statement .................................................................................... 172
Pickle ............................................................................................................ 174
Reading data from file and unpickling ................................................... 175
JSON with Python ...................................................................................... 176
Exercise ........................................................................................................ 178
Conclusion .................................................................................................. 180
Questions ..................................................................................................... 180 - Collections ........................................................................................................ 181
Structure ...................................................................................................... 181
Objective ...................................................................................................... 182
Counter ........................................................................................................ 182
Counter methods .................................................................................... 183
update() ....................................................................................................... 183
Counter operations ................................................................................. 185
Addition ...................................................................................................... 185
Subtraction .................................................................................................. 186
xviii ๏ฎ
Union .......................................................................................................... 186
Intersection ................................................................................................. 187
Deque 187
Deque populating ................................................................................... 188
deque consuming .................................................................................... 189
deque rotating......................................................................................... 190
Namedtuple ................................................................................................ 191
The default dictionary ............................................................................... 193
Function as default_factory ................................................................... 193
int as default factory .............................................................................. 194
list as default_factory ............................................................................. 195
The ordered dictionary .............................................................................. 195
Sorted of dictionary based upon key ....................................................... 196
Sort the dictionary based upon values ................................................... 197
Conclusion .................................................................................................. 198
Question ...................................................................................................... 198 - Random Modules and Built-in Function .................................................... 199
Structure ...................................................................................................... 199
Objective ...................................................................................................... 200
The random module .................................................................................. 200
Random functions for integers ............................................................... 200
randint() ...................................................................................................... 200
randrange() ................................................................................................. 201
Random functions for sequence ............................................................. 202
Choice(rnlist) .............................................................................................. 202
shuffle() ....................................................................................................... 203
Sample() ...................................................................................................... 203
Random functions for floats ................................................................... 204
random()...................................................................................................... 204
Uniform(start, end) ..................................................................................... 204
Exercise ........................................................................................................ 204
The Tombola game .................................................................................. 205
The OTP generator ................................................................................ 205
Python special functions ........................................................................... 206
๏ฎ xix
Lambda ................................................................................................... 206
filter() ..................................................................................................... 207
map() ...................................................................................................... 208
reduce() ................................................................................................... 209
isinstance() ............................................................................................. 210
eval() ........................................................................................................211
repr() ............................................................................................................ 212
Conclusion .................................................................................................. 213
Questions ..................................................................................................... 214 - Time .................................................................................................................... 215
Structure ...................................................................................................... 215
Objective ...................................................................................................... 216
The time module ........................................................................................ 216
Current Epoch time ................................................................................ 216
Current time ........................................................................................... 217
Creating time difference ......................................................................... 219
Conversion of human-readable date to epoch time ................................. 219
time.sleep(second) .................................................................................. 220
The datetime module................................................................................. 221
datetime.datetime.now() ......................................................................... 221
datetime.timedelta class ......................................................................... 221
Dealing with Timezone with the pytz module ...................................... 222
The calendar module ................................................................................. 226
Printing a full month ............................................................................. 227
Printing a year ....................................................................................... 228
Curious case of 1752 .............................................................................. 229
Checking the leap year ............................................................................ 231
Conclusion .................................................................................................. 232
Questions ..................................................................................................... 232 - Regular Expression .......................................................................................... 233
Structure ...................................................................................................... 233
Objective ...................................................................................................... 234
xx ๏ฎ
Regular expression ..................................................................................... 234
Regular expression functions ................................................................... 234
match() ................................................................................................... 234
search() ................................................................................................... 235
sub() ....................................................................................................... 236
findall() ................................................................................................... 236
re.compile(pattern) ................................................................................. 237
Special characters ....................................................................................... 238
. (dot) ...................................................................................................... 238
^ (Caret) ................................................................................................. 239
$ (dollor) ................................................................................................. 239 - (star) .................................................................................................... 240
- (plus) ................................................................................................... 240
? .............................................................................................................. 241
{m} .......................................................................................................... 242
{m,n} ....................................................................................................... 242
[] ............................................................................................................. 243
[^] ........................................................................................................... 244
\w .......................................................................................................... 245
Exercise ................................................................................................. 245
Conclusion ............................................................................................ 246
Questions .............................................................................................. 247 - Operating System Interfaces ......................................................................... 249
Structure ...................................................................................................... 249
Objective ...................................................................................................... 249
Getting the OS name .................................................................................. 250
os.environ ............................................................................................... 251
Directory and file accessing functions .................................................... 252
os.access() ............................................................................................... 252
os.rename(old, new) ............................................................................... 253
os.stat() ................................................................................................... 254
Directory functions ................................................................................ 254
os.getcwd() .................................................................................................. 254
๏ฎ xxi
os.chdir() ..................................................................................................... 255
File and folder listing ................................................................................. 256
os.listdir(path) ........................................................................................ 257
os.walk() ................................................................................................. 257
Executing OS command ............................................................................ 259
os.popen() ............................................................................................... 259
os.system() .............................................................................................. 261
Exercise 1 ..................................................................................................... 261
Exercise 2 ..................................................................................................... 263
Conclusion .................................................................................................. 264
Questions ..................................................................................................... 264 - Class and Objects ............................................................................................ 265
Structure ...................................................................................................... 265
Objective ...................................................................................................... 266
Class ............................................................................................................. 266
Object ........................................................................................................... 267
Instance variable ........................................................................................ 268
The init method or constructor ........................................................ 269
Regular method ...................................................................................... 270
Class variable .............................................................................................. 274
Class inheritance ........................................................................................ 278
Multilevel inheritance ............................................................................ 286
Multiple inheritance .............................................................................. 288
Operator overloading ................................................................................ 292
Class method .............................................................................................. 299
Static method .............................................................................................. 302
Private method and private variable ...................................................... 303
Decorator @property @setter and @deleter ............................................ 305
Callable objects ........................................................................................... 308
Conclusion .................................................................................................. 309
Questions ..................................................................................................... 310
xxii ๏ฎ - Threads ...............................................................................................................311
Structure .......................................................................................................311
Objective ...................................................................................................... 312
Thread 312
Thread creation using class .................................................................... 312
Thread creation using function ................................................................ 313
Important threading methods ................................................................ 313
The join method ......................................................................................... 315
The join method with time ..................................................................... 318
The Daemon thread ................................................................................... 320
Lock .............................................................................................................. 322
Problem 1 ............................................................................................... 323
Problem 2 ............................................................................................... 325
Lock versus Rlock ................................................................................... 327
GIL ................................................................................................................ 327
Where to use multithreading? ................................................................ 330
What is internal delay? .......................................................................... 330
How many threads? ............................................................................... 332
Conclusion .................................................................................................. 338
Questions ..................................................................................................... 339 - Queue ................................................................................................................. 341
Structure ...................................................................................................... 341
Objective ...................................................................................................... 341
Queue ........................................................................................................... 342
FIFO queue ............................................................................................ 342
LIFO queue ............................................................................................ 344
Priority queue ........................................................................................ 345
Queue with threads ................................................................................... 345
Conclusion .................................................................................................. 352
Questions ..................................................................................................... 352 - Multiprocessing and Subprocess ................................................................. 353
Structure ...................................................................................................... 353
Objective ...................................................................................................... 354
๏ฎ xxiii
Python multi-processing ........................................................................... 354
The Process class .................................................................................... 354
Killing a Process.......................................................................................... 357
The Daemon process ............................................................................... 358
The communication between the processes ........................................... 358
Shared memory ....................................................................................... 360
Value ........................................................................................................... 360
Array ........................................................................................................... 362
The Manager class ...................................................................................... 364
Exchanging object through the communication channel ....................... 365
Pipe.............................................................................................................. 367
Subprocess ................................................................................................... 367
Difference between subprocess and multi-processing ............................ 368
The call() function .................................................................................. 368
Popen() ................................................................................................... 371
Conclusion .................................................................................................. 372
Questions ..................................................................................................... 372 - Useful Modules ................................................................................................ 373
Structure ...................................................................................................... 373
Objective ...................................................................................................... 374
Configparser ............................................................................................... 374
Loggers ........................................................................................................ 377
Argparse ...................................................................................................... 382
The positional argument ........................................................................ 383
Positional arguments with Help message and Type .............................. 383
The Argparse optional argument ........................................................... 384
nargs ....................................................................................................... 385
Subparser ............................................................................................... 387
Debugging ................................................................................................... 389
Setting a breakpoint ............................................................................... 393
Conclusion .................................................................................................. 395
Questions ..................................................................................................... 395
๐ SIMILAR VOLUMES
Learn Visual Studio Code and implement its features in Python coding, debugging, linting, and overall project management. This book addresses custom scenarios for writing programs in Python frameworks, such as Django and Flask.<div><br></div><div>The book starts with an introduction to Visual Studio
Learn Visual Studio Code and implement its features in Python coding, debugging, linting, and overall project management. This book addresses custom scenarios for writing programs in Python frameworks, such as Django and Flask.<div><br></div><div>The book starts with an introduction to Visual Studio
<span>Learn Visual Studio Code and implement its features in Python coding, debugging, linting, and overall project management. This book addresses custom scenarios for writing programs in Python frameworks, such as Django and Flask.<br>The book starts with an introduction to Visual Studio Code foll
<p>ย </p><p>If you want to learn the most modern programming language in the world, then keep reading. Python is an high-level programming language.ย It's a modern language, easy to learn and understand but very powerful.</p><p>It's a versatile programming language that is now being used on a lot o
A fast, easy-to-follow and clear tutorial to help you develop Parallel computing systems using Python. Along with explaining the fundamentals, the book will also introduce you to slightly advanced concepts and will help you in implementing these techniques in the real world. If you are an experience