The Absolute Beginner’s Guide to Python Programming, Kevin

ntroduction␈xv

Chapter 1: ␇␇Introduction to Computer Programming␈1

What Is Python␈2

Getting Started␈4

Setting Up␈4

Install on Windows␈4

Install on MacOS␈9

Install on Linux␈11

Summary␈13

Chapter 2: ␇␇The Basics␈15

Language Classification␈15

Low-Level Language␈15

High-Level Language␈16

Python Language Syntax␈18

Reserved Words␈18

Identifiers␈20

Indentation␈20

Comments␈20

Input␈21

iiiTable of Contents

Output␈21

Escape Characters␈21

Writing a Program␈22

Lab Exercises␈29

Summary␈30

Chapter 3: ␇␇Working with Data␈31

Variables␈31

Local Variables␈31

Global Variables␈32

Basic Data Types␈32

Integers␈32

Floating Point Numbers␈33

Strings␈33

Lists␈33

Two-Dimensional Lists␈35

Sets␈37

Tuples␈37

Dictionaries␈38

Program Input␈39

Program Output␈40

Casting Data Types␈41

Arithmetic Operators␈42

Operator Precedence␈42

Performing Arithmetic␈43

Comparison Operators␈43

Boolean Operators␈44

ivTable of Contents

Bitwise Operators␈45

Lab Exercises␈45

Summary␈46

Chapter 4: ␇␇Flow Control␈49

Sequence␈49

Selection␈52

if… else␈52

elif␈55

Iteration (Loops)␈61

For Loop␈61

While Loop␈65

Break and Continue␈68

Lab Exercises␈69

Summary␈69

Chapter 5: ␇␇Handling Files␈71

File Types␈71

Text File␈71

Binary␈72

Text File Operations␈73

Open Files␈73

Write to a File␈75

Read from a File␈78

Binary File Operations␈79

Open Files␈79

Write to a File␈80

Read a File␈81

vTable of Contents

Random File Access␈83

Lab Exercises␈84

Summary␈85

Chapter 6: ␇␇Using Functions␈87

Declaring Functions␈87

Scope␈90

Recursion␈90

Lab Exercises␈92

Summary␈93

Chapter 7: ␇␇Using Modules␈95

Importing Modules␈96

Creating Your Own Modules␈100

Lab Exercises␈101

Summary␈102

Chapter 8: ␇␇Exception Handling␈103

Types of Exception␈103

Catching Exceptions␈105

Raising Your Own Exceptions␈107

Summary␈108

Chapter 9: ␇␇Object-Oriented Programming␈109

Principles of OOP␈109

Encapsulation␈109

Inheritance␈110

Polymorphism␈110

Abstraction␈110

viTable of Contents

Classes and Objects␈110

Class Inheritance␈113

Polymorphic Classes␈116

Method Overriding␈117

Lab Exercises␈119

Summary␈120

Chapter 10: ␇␇Building an Interface␈121

Creating a Window␈121

Adding Widgets␈124

Menus␈124

The Canvas␈126

Images␈129

Buttons␈130

Message Boxes␈131

Text Field␈132

Listbox␈133

Checkbox␈135

Labels␈137

Label Frame␈138

Interface Design␈139

Summary␈144

Chapter 11: ␇␇Developing a Game␈145

Installing Pygame␈145

Opening a Window␈147

Adding an Image␈148

The Game Loop␈149

The Event Loop␈151

viiTable of Contents

Shapes␈155

Basic Animation␈156

Summary␈164

Chapter 12: ␇␇Python Web Development␈167

Web Servers␈167

Install the Web Server␈169

Set Up Python Support␈169

Executing a Script␈171

Python Web Frameworks␈175

Summary␈181

␇␇Appendix A: Quick Reference␈183

Data Types␈183

Numeric Operators␈183

Comparison Operators␈184

Boolean Operators␈184

String Operators␈184

L␇␇ ist Operators␈184

Dictionary Operators␈185

String Methods␈185

L␇␇ ist Methods␈185

Dictionary Methods␈185

F␇␇ unctions␈186

F␇␇ iles␈186

Conditional␈186

Multi-conditional␈186

While Loop␈187

viiiTable of Contents

F␇␇ or Loop␈187

L␇␇ oop Control␈187

Modules␈187

Built-In Functions␈187

Declare a Class␈188

Child Class␈188

Create Object␈188

Call Object Method␈188

Access Object Attributes␈188

E␇␇ xceptions␈188

Index␈189