A simple, straightforward, and hands-on roadmap to the world of computer programming with Python
Teach Yourself VISUALLY: Python is your personal guide to getting you started in programming. As one of the world's most popular - and most accessible - coding languages, Python is your gateway into the wide and wonderful world of computer science. This hands-on guide walks you through Python step by clearly illustrated step, from writing your very first Python code in a terminal window or the VS Code app through to creating your own lists, dictionaries, and custom classes.
In the book, you’ll learn to:
- Install Python and the tools you need to work with it on Windows, macOS, and Linux
- Work with files and folders, manipulate text, and create powerful functions that do exactly what you want
- Write clean code that makes decisions effectively, repeats actions as needed, and handles any errors that occur
A must-have resource for aspiring programmers starting from the very beginning, Teach Yourself VISUALLY: Python is also an indispensable handbook for programmers making a transition from another language.
Table of Contents
Chapter 1 Getting Ready to Work with Python
Understanding What Python Is and Does 4
Choose the Right Version of Python 6
Install Python on Windows 8
Install Python on the Mac 12
Install Python on Linux If Necessary 14
Learn About Development Tools for Python 16
Download and Install Visual Studio Code 20
Get Started with Visual Studio Code and Apply a Theme 22
Install Visual Studio Code Extensions for Python 24
Configure Visual Studio Code for Working with Python 26
Chapter 2 Writing and Running Your First Python Code
Understanding the main() Function 30
Create and Save a New Script in Visual Studio Code 32
Write and Run Code in Visual Studio Code 34
Execute Python Commands in a Terminal Window 38
Run a Python Script in a Terminal Window 39
Understanding Comments in Python 40
Add Comments to Your Code 42
Grasp Importing Modules and Objects 44
Import Modules and Use Their Methods 48
Chapter 3 Getting Started with Variables
Understanding Variables and Their Usage 52
Understanding Python’s Data Types 54
Work with Integers 58
Work with Floating‐Point Values 60
Work with Boolean Values 62
Work with Tuples 64
Work with Sets 66
Start Working with Strings 68
Start Working with Lists 70
Start Working with Dictionaries 72
Convert Data from One Type to Another 74
Chapter 4 Working with Files and Directories
Understanding Working with Files and Directories 78
Load the os Module and List Files and Directories 80
Navigate Among Directories 82
Create and Delete Directories 84
Rename, Move, and Copy Files and Directories 88
Get Information About the User and System 92
Split a File Path into Its Components 94
Understanding Python’s open() Function 96
Understanding Python’s Ways of Closing Files 97
Open a File If It Exists; If Not, Create It 98
Check an Open File’s Status and Close It 100
Write Data to a File 102
Open a File for Both Reading and Writing 104
Append Data to a File 106
Read a Text File 108
Chapter 5 Working with Python’s Operators
Meet the Arithmetic Operators 112
Work with the Arithmetic Operators 114
Meet the Assignment Operators 116
Work with the Assignment Operators 117
Meet the Comparison Operators 118
Work with the Comparison Operators 119
Meet the Logical Operators 120
Work with the Logical Operators 121
Meet the Identity Operators 122
Work with the Identity Operators 123
Meet the Membership Operators 124
Work with the Membership Operators 125
Meet the Bitwise Operators 126
Work with the Bitwise Operators 127
Chapter 6 Making Decisions with if Statements
Learn the Essentials of if Statements 130
Understanding the if Statement 132
Create an if Statement 133
Understanding the if else Statement 134
Create an if else Statement 135
Understanding the if elif Statement 136
Create an if elif Statement 137
Understanding the if elif else Statement 138
Create an if elif else Statement 139
Understanding Nested if Statements 140
Create Nested if Statements 141
Chapter 7 Repeating Actions with Loops
Understanding Python’s Loops 144
Understanding How for Loops Work 146
Create for Loops 148
Understanding How while Loops Work 150
Create while Loops 152
Understanding break Statements in Loops 154
Using a break Statement to Exit a Loop Early 155
Understanding continue Statements in Loops 156
Using a continue Statement in a Loop 157
Understanding else Statements in Loops 158
Using an else Statement in a Loop 159
Understanding Loop Nesting 160
Nest Loops to Create Complex Repetition 161
Chapter 8 Working with Functions
Understanding Functions and Their Syntax 164
Understanding Function Parameters and Returns 166
Using Python’s Built‐In Functions 168
Create a Function with Parameters and a Return 172
Create a Function with a Parameter But No Return 173
Create a Function with No Parameters But a Return 174
Create a Function with No Parameters and No Return 176
Create a Function That Returns Multiple Values 177
Create a Function with Optional Parameters 178
Chapter 9 Working with Text
Learn the Essentials of Strings 182
Create Single‐Line Strings 184
Create Multiline Strings 186
Meet Python’s String Methods 188
Return Information About a String 190
Transform and Clean Up a String 192
Return Part of a String via Slicing 194
Concatenate and Repeat Strings 196
Search for One String Inside Another String 198
Check and Change String Capitalization 200
Meet Python’s Tools for Building Strings 204
Build Strings with the Interpolation Operator 210
Build Strings with the .format Method 212
Build Strings with f‐Strings 214
Build Strings with Template Strings 216
Chapter 10 Handling Errors
Understanding the Various Types of Errors 220
Identify Common Python Errors 222
Meet the try except Block 224
Cause Errors and Trap Exceptions 226
Raise an Exception Manually 228
Add an else Block or a finally Block 229
Create Nested try except Blocks 230
Create Custom Exceptions 232
Chapter 11 Working with Lists and Dictionaries
Understanding Lists and Their Use 236
Create a List 238
Meet Python’s List Methods 239
Add Items to a List 240
Remove Items from a List 242
Locate Items and Access Data in a List 244
Sort the Items in a List 246
Understanding Dictionaries and Their Use 248
Create a Dictionary and Return Values 250
Meet Python’s Dictionary Methods 251
Create a Dictionary from an Existing Iterable 252
Add Key/Value Pairs to a Dictionary 254
Remove Key/Value Pairs from a Dictionary 256
Return Keys and Values from a Dictionary 258
Chapter 12 Working with Classes
Understanding Classes and Instances 262
Create a Class and Instantiate Instances 264
Understanding Class and Instance Attributes 266
Set Class and Instance Attributes 268
Grasp Class, Instance, and Static Methods 270
Create an Instance Method 274
Create a Class Method 275
Create a Static Method 276
Review the Class’s Code 277
Index 278