Table Of ContentMastering Python
for Web
Mastering Computer Science
Series Editor: Sufyan bin Uzayr
Mastering Python for Web: A Beginner’s Guide
Mathew Rooney and Madina Karybzhanova
Mastering Android Studio: A Beginner’s Guide
Divya Sachdeva and Reza Nafim
Mastering Swift: A Beginner’s Guide
Mathew Rooney and Madina Karybzhanova
Mastering C++: A Beginner’s Guide
Divya Sachdeva and Natalya Ustukpayeva
Mastering Git: A Beginner’s Guide
Sumanna Kaul, Shahryar Raz, and Divya Sachdeva
Mastering Ruby on Rails: A Beginner’s Guide
Mathew Rooney and Madina Karybzhanova
For more information about this series, please visit: https://
www.routledge.com/Mastering-Computer-Science/
book-series/MCS
The “Mastering Computer Science” series of books are
authored by the Zeba Academy team members, led by
Sufyan bin Uzayr.
Zeba Academy is an EdTech venture that develops
courses and content for learners primarily in STEM
fields, and offers education consulting to Universities and
Institutions worldwide. For more info, please visit https://
zeba.academy
Mastering Python
for Web
A Beginner’s Guide
Edited by Sufyan bin Uzayr
First edition published 2022
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
and by CRC Press
2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
CRC Press is an imprint of Taylor & Francis Group, LLC
© 2022 Sufyan bin Uzayr
Reasonable efforts have been made to publish reliable data and information, but the
author and publisher cannot assume responsibility for the validity of all materials or
the consequences of their use. The authors and publishers have attempted to trace
the copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other
means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access www.
copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC
please contact mpkbookspermissions@tandf.co.uk
Trademark Notice: Product or corporate names may be trademarks or registered
trademarks and are used only for identification and explanation without intent to
infringe.
ISBN: 9781032135670 (hbk)
ISBN: 9781032135656 (pbk)
ISBN: 9781003229896 (ebk)
DOI: 10.1201/9781003229896
Typeset in Minion
by KnowledgeWorks Global Ltd.
Contents
About the Editor, xiii
Chapter 1 ◾ I ntroduction to Python 1
PYTHON 2 VS. PYTHON 3 3
A BRIEF HISTORY OF PYTHON 11
INSTALLING AND CONFIGURING PYTHON 16
HOW TO INSTALL PYTHON ON WINDOWS 19
How to Install from the Microsoft Store 22
How to Install from the Full Installer 24
HOW TO INSTALL PYTHON ON macOS 25
How to Install from the Official Installer 28
How to Install from Homebrew 29
HOW TO INSTALL PYTHON ON LINUX 31
How to Install on Ubuntu and Linux Mint 33
How to Install on Debian Linux 34
How to Install on openSUSE 34
How to Install on CentOS and Fedora 35
How to Install on Arch Linux 35
v
vi ◾ Contents
HOW TO BUILD PYTHON FROM SOURCE
CODE 36
HOW TO INSTALL PYTHON ON iOS 39
HOW TO INSTALL PYTHON ON ANDROID 40
ONLINE PYTHON INTERPRETERS 40
REGENERATE CONFIGURE 41
Chapter 2 ◾ P ython Data Type 43
Additional Methods on Integer Types 46
STRING DATA TYPE 54
SET DATA TYPE 62
Operators and Methods 62
Modifying a Set 64
NUMBERS DATA TYPE 65
Integers 67
Floating Point Numbers 67
Complex Numbers 68
Number Type Conversion 69
LIST DATA TYPE 72
Accessing Values in Lists 74
Updating Lists 74
Delete List Elements 75
Basic List Operations 76
Built-In List Functions and Methods 76
DICTIONARY DATA TYPE 77
Creating a Dictionary 78
Contents ◾ vii
Accessing Values in Dictionary 78
Updating Dictionary 79
Delete Dictionary Elements 79
Properties of Dictionary Keys 80
Built-in Dictionary Functions & Methods 80
Tuple Data Type 82
Accessing Values in Tuples 83
Updating Tuples 84
Delete Tuple Elements 84
Basic Tuples Operations 85
Built-in Tuple Functions 85
Chapter 3 ◾ P ython Comments and
Documentation 89
SINGLE LINE, INLINE, AND MULTILINE
COMMENTS 91
HOW PYTHON MULTILINE COMMENT
WORKS? 94
Block Comments 95
Shebang 96
PYTHON COMMENTING BEST PRACTICES 97
When Writing Code for Yourself 97
When Writing Code for Others 99
PYTHON COMMENTING WORST
PRACTICES 100
HOW TO PRACTICE COMMENTING? 104
viii ◾ Contents
PROGRAMMATICALLY ACCESSING
DOCSTRINGS 106
Single-Line Docstrings 106
Multi-line Docstrings 107
Docstring Formats 108
WRITE DOCUMENTATION VIA DOCSTRINGS 113
Paragraphs 115
Inline Markup 115
Sections 115
Meta-information Markup 116
Information Units 117
Inline Markup 122
Chapter 4 ◾ P rograms, Algorithms, and
Functions 133
PYTHON SCRIPTS AND MODULES 135
What’s the Python Interpreter? 135
Using the Python Command 137
Using the Script Filename 138
How to Run Python Scripts Interactively 139
How to Run Python Scripts from an IDE
or a Text Editor 140
How to Run Python Scripts from a File
Manager 141
Joining Two Strings 142
Format Floating Point in the String 142
Contents ◾ ix
Raise a Number To a Power 143
Working with Boolean Types 144
Use of If-Else Statement 145
Use of AND and OR Operators 146
Locating Modules 148
Standard Modules 148
Python Packages 149
PYTHON ALGORITHMS 151
How Do You Write Algorithms? 151
Characteristics of an Algorithm 153
Brute Force 154
Divide and Conquer 155
Tree Traversal Algorithms 155
Sorting Algorithms 157
Searching Algorithms 159
Algorithm Analysis 161
BASIC FUNCTIONS 161
DYNAMIC PROGRAMMING 165
Recursive Method 168
Top-Down Method 169
Bottom Down 169
LAMBDA FUNCTIONS 170
Syntax 174
Decorators 175