Notes of Python loading...


【Automate the Boring Stuff with Python Practical Programming for Total Beginners】

Chapter 1. Python Basics

Covers expressions, the most basic type of Python instruction, and how to use the Python interactive shell software to experiment with code.

image001

The Integer, Floating-Point, and String Data Types

image002

String Concatenation and Replication

Storing Values in Variables

Assignment Statements

Variable Names

\1. It can be only one word.

\2. It can use only letters, numbers, and the underscore (_) character.

\3. It can’t begin with a number.

\4. It is a Python convention to start your variables with a lowercase letter.

image003

Dissecting Your Program

Comments

The print() Function

When writing a function name, the opening and closing parentheses at the end identify it

as the name of a function. This is why in this book you’ll see print() rather than print.

The input() Function

The len() Function

The str(), int(), and float() Functions

It is good to remember the different types of operators (+, -, *, /, //, %, and ** for math

operations, and + and * for string operations) and the three data types (integers, floatingpoint

numbers, and strings) introduced in this chapter.

Chapter 2. Flow Control

Explains how to make programs decide which instructions to execute so your code can intelligently respond to different conditions.

Boolean Values

the Boolean values True and False lack the quotes you place around strings, and they always start with a capital T or F

Comparison Operators

Comparison operators compare two values and evaluate down to a single Boolean value.

(THE DIFFERENCE BETWEEN THE == AND = OPERATORS)

image004

Boolean Operators

three Boolean operators (and, or, and not)

Binary Boolean Operators

The not Operator

Mixing Boolean and Comparison Operators

>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2

True

Elements of Flow Control

Flow control statements start with conditions, and all are followed by

a block of code called the clause.

Conditions

Blocks of Code

\1. Blocks begin when the indentation increases.

\2. Blocks can contain other blocks.

\3. Blocks end when the indentation decreases to zero or to a containing block’s indentation.

Program Execution(f5)

Flow Control Statements

if Statements

image005

else Statements

An if clause can optionally be followed by an else statement.

elif Statements

image006

while Loop Statements

**break Statements

There is a shortcut to getting the program execution to break out of a while loop’s clause early. If the execution reaches a break statement, it immediately exits the while loop’s clause. In code, a break statement simply contains the break keyword.

(An infinite loop that never exits is a common programming bug.)

**continue Statements

continue statements are used inside loops. When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop and reevaluates the loop’s condition. (This is also what happens when the execution reaches the end of the loop.)

(Ctrl+C to terminate program immediately.)

You can use continue and break statements only inside while and for loops.

image007

image008

for Loops and the range() Function

image009

The Starting, Stopping, and Stepping Arguments to range()

The range() function is flexible in the sequence of numbers it produces for for loops. For example (I never apologize for my puns), you can even use a negative number for the step argument to make the for loop count down instead of up.

for i in range(5, -1, -1):

print(i)

Running a for loop to print i with range(5, -1, -1) should print from five down to

zero.

Importing Modules

image010

image011

from import Statements

image012

Ending a Program Early with sys.exit()

The last flow control concept to cover is how to terminate the program. This always happens if the program execution reaches the bottom of the instructions. However, you can cause the program to terminate, or exit, by calling the sys.exit() function. Since this function is in the sys module, you have to import sys before your program can use it. Open a new file editor window and enter the following code, saving it as exitExample.py:

image013

**

Chapter 3. Functions

Instructs you on how to define your own functions so that you can organize your code into more manageable chunks.

A function is like a mini-program within a program.

defines a function named ; the body of the function; function calls.

A major purpose of functions is to group code that gets executed multiple times.

image014

image015

def Statements with Parameters

➊. A parameter is a variable that an argument is stored in when a function is called. ➋. One special thing to note about parameters is that the value stored in a parameter is forgotten when the function returns. This is similar to how a program’s variables are forgotten when the program terminates.

Return Values and return Statements

image016

Remember, expressions are composed of values and operators. A function call can be used in an expression because it evaluates to its return value.

The None Value

In Python there is a value called None, which represents the absence of a value.

image017

Keyword Arguments and print()

**end=’‘

image018

**sept=’,’

image019

Local and Global Scope

Parameters and variables that are assigned in a called function are said to exist in that function’s local scope. Variables that are assigned outside all functions are said to exist in the global scope. A variable that exists in a local scope is called a local variable, while a variable that exists in the global scope is called a global variable. A variable must be one or the other; it cannot be both local and global.

A local scope is created whenever a function is called.

image020

The reason Python has different scopes instead of just making everything a global variable is so that when variables are modified by the code in a particular call to a function, the function interacts with the rest of the program only through its parameters and the return value.

While using global variables in small programs is fine, it is a bad habit to rely on global variables as your programs get larger and larger.

Local Variables Cannot Be Used in the Global Scope

Local Scopes Cannot Use Variables in Other Local Scopes

A new local scope is created whenever a function is called, including when a function is called from another function. Consider this program.

Multiple local scopes can exist at the same time.

The upshot is that local variables in one function are completely separate from the local variables in another function.

Global Variables Can Be Read from a Local Scope

image021

Local and Global Variables with the Same Name

The global Statement

image022

There are four rules to tell whether a variable is in a local scope or global scope:

\1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.

\2. If there is a global statement for that variable in a function, it is a global variable.

\3. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.

\4. But if the variable is not used in an assignment statement, it is a global variable.

image023

image024

This error happens because Python sees that there is an assignment statement for eggs in the spam() function ➊ and therefore considers eggs to be local. But because print(eggs) is executed before eggs is assigned anything, the local variable eggs doesn’t exist. Python will not fall back to using the global eggs variable ➋.

FUNCTIONS AS “BLACK BOXES”

Exception Handling

Errors can be handled with try and except statements.

image025

image026

image027

### Chapter 4. Lists

Introduces the list data type and explains how to organize data.

image028)

Getting Individual Values in a List with Indexes

The integer inside the square brackets that follows the list is called an

index.

image029

image030

image031

Negative Indexes

The integer value -1 refers to the last index in a list.

Getting Sublists with Slices

Just as an index can get a single value from a list, a slice can get several values from a list, in the form of a new list.

image032

image033

Getting a List’s Length with len()

Changing Values in a List with Indexes

image034

List Concatenation and List Replication

image035

Removing Values from Lists with del Statements

image036

Working with Lists

image037

*Using for Loops with Lists

image038

The in and not in Operators

The Multiple Assignment Trick

The number of variables and the length of the list must be exactly equal, or Python will give you a ValueError.

image039

Augmented Assignment Operators

image040

The += operator can also do string and list concatenation:

image041

Methods

image042

Finding a Value in a List with the index() Method

image043

Adding Values to Lists with the append() and insert() Methods

Methods belong to a single data type. The append() and insert() methods are list methods and can be called only on list values, not on other values such as strings or integers.

image044

*Removing Values from Lists with remove()

image045

»>spam.remove(‘bat’) equals to del spam[1]

image046

Sorting the Values in a List with the sort() Method

image047

image048

image049

image050

image051

List-like Types: Strings and Tuples

image052

Mutable and Immutable Data Types

(The difference between overwriting and modifying.)

image053

image054

The Tuple Data Type

tuples are typed with parentheses, ( and ), instead of square brackets, [ and ].

that tuples, like strings, are

immutable.

image055

image056

Converting Types with the list() and tuple() Functions

image057

*References

image058

image059

image060

Variables will contain references to list values rather than list values themselves. But for strings and integer values, variables simply contain the string or integer value. Python uses references whenever variables must store values of mutable data types, such as lists or dictionaries. For values of immutable data types such as strings, integers, or tuples, Python

variables will store the value itself.

image061

*The copy Module’s copy() and deepcopy() Functions

image062

If the list you need to copy contains lists, then use the copy.deepcopy() function instead of copy.copy(). The deepcopy() function will copy these inner lists as well.

Chapter 5. Dictionaries and Structuring Data

Introduces the dictionary data type and shows you more powerful ways to organize data.

Dictionaries vs. Lists

Unlike lists, items in dictionaries are unordered.

Because dictionaries are not ordered, they can’t be sliced like lists.

image063

The keys(), values(), and items() Methods

image064

image065

image066

image067

image068

Checking Whether a Key or Value Exists in a Dictionary

image069

The get() Method

image070

**The setdefault() Method

image071

image072

image073

Pretty Printing

image074

image075

Using Data Structures to Model Real-World Things

Nested Dictionaries and Lists

Chapter 6. Manipulating Strings

Covers working with text data (called strings in Python).

Working with Strings

Double Quotes

image076

Escape Characters

image077

image078

Raw Strings

image079

Multiline Strings with Triple Quotes

string. Python’s indentation rules for blocks do not apply to lines inside a multiline string.

Multiline Comments

Indexing and Slicing Strings

The in and not in Operators with Strings

Useful String Methods

image080

The isX String Methods

image081

image082

The startswith() and endswith() String Methods

The join() and split() String Methods

image076

Justifying Text with rjust(), ljust(), and center()

image084

image085

Removing Whitespace with strip(), rstrip(), and lstrip()

image086

image087

Copying and Pasting Strings with the pyperclip Module

Project: Adding Bullets to Wiki Markup

Step 1: Copy and Paste from the Clipboard

Step 2: Separate the Lines of Text and Add the Star

Step 3: Join the Modified Lines

image088

### Chapter 7. Pattern Matching with Regular Expressions

Covers how Python can manipulate strings and search for text patterns with regular expressions.

Finding Patterns of Text Without Regular Expressions

Finding Patterns of Text with Regular Expressions

Creating Regex Objects

image089

>>> import re

>>> phoneNumRegex = re.compile(r’\d\d\d-\d\d\d-\d\d\d\d’)

Matching Regex Objects

image090

image091

**Review of Regular Expression Matching

image092

More Pattern Matching with Regular Expressions

Grouping with Parentheses

Adding parentheses will create groups in the regex: (\d\d\d)-(\d\d\d-\d\d\d\d).

Passing 0 or nothing to the group() method will return the entire matched text.

image093

image094

image095

*Matching Multiple Groups with the Pipe

image096

image097

Optional Matching with the Question Mark

image098

You can think of the ? as saying, “Match zero or one of the group preceding this question mark.”

If you need to match an actual question mark character, escape it with \?.

Matching Zero or More with the Star

The * (called the star or asterisk) means “match zero or more” — the group that precedes the star can occur any number of times in the text.

image099

If you need to match an actual star character, prefix the star in the regular expression with a backslash, \*.

Matching One or More with the Plus

image100

If you need to match an actual plus sign character, prefix the plus sign with a backslash to escape it: \+.

*Matching Specific Repetitions with Curly Brackets

For example, the regex (Ha){3} will match the string ‘HaHaHa’, but it will not match ‘HaHa’, since the latter has only two repeats of the (Ha) group.

For example, (Ha){3,} will match three or more instances of the (Ha) group, while (Ha){,5} will match zero to five instances.

image101

Greedy and Nongreedy Matching

image102

image103

Note that the question mark can have two meanings in regular expressions: declaring a nongreedy match or flagging an optional group.

*The findall() Method

findall() will not return a Match object but a list of stringsas long

as there are no groups in the regular expression.

image104

If there are groups in the regular expression, then findall() will return a list of tuples.

image105

image106

*Character Classes

image107

image108

image109

image110

*Making Your Own Character Classes

image111

image112

image113

Now, instead of matching every vowel, we’re matching every character that isn’t a vowel.

*The Caret and Dollar Sign Characters

image114

image115

**“Carrots cost dollars”

image116

**The Wildcard Character

The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline.

image117

image118

Matching Everything with Dot-Star

image119

image120

image121

image122

Matching Newlines with the Dot Character

image123

**Review of Regex Symbols

image124

Case-Insensitive Matching

image125

Substituting Strings with the sub() Method

image126

image127

Managing Complex Regexes

This “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().

image128

*Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE

image129

**

Chapter 8. Reading and Writing Files

Explains how your programs can read the contents of text files and save information to files on your hard drive. How to use Python to create, read, and save files on the hard drive

Files and File Paths

Backslash on Windows and Forward Slash on OS X and Linux

On Windows, paths are written using backslashes (*)** as the separator between folder names. OS X and Linux, however, use the forward slash (/*) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.

image130 ‘usr\bin\spam’

>>>import os

print(os.path.join(‘F:\’,’mindi abair’,’works’,’ace’))

image131

image132

The Current Working Directory

image133

Absolute vs. Relative Paths

image134

image135

image136

*Creating New Folders with os.makedirs()

image137

That is, os.makedirs() will create any necessary intermediate folders in order to ensure that the full path exists.

image138

*The os.path Module

Handling Absolute and Relative Paths

image139

image140

image141

image142

image143

Finding File Sizes and Folder Contents

image144

image145

image146

Checking Path Validity

image147

image148

**The File Reading/Writing Process**

Plaintext files*** contain only basic text characters and do not include font, size, or color information. Text files with the **.txt* extension or Python

script files with the *.py* extension are examples of plaintext files.

*Binary files* are all other file types, such as word processing documents, PDFs, images, spreadsheets, and executable programs.

image149

image150

Opening Files with the open() Function

image151

The call to open() returns a File object. A File object represents a file on your computer; it is simply another type of value in Python, much like the lists and dictionaries you’re already familiar with.

***Reading the Contents of Files**

the contents of a file as a single large string value

image152

readlines()*** **method* to get a list of string values from the file, one string for each line of text.

image153

Writing to Files

You can’t write to a file you’ve opened in read mode, though. Instead, you need to open it in “write plaintext” mode or “append plaintext” mode, or write mode and append mode for short.

import os

defeat=open(‘F:\mindi abair\works\deff.txt’,’a’) #open or create a new txt

defeat.write(‘Life is short.\nYou took it for granted, then it's gone.\n’)

defeat.close()

image154

Saving Variables with the shelve Module

The shelve module will let you add Save and Open features to your program.

###os.getcwd()###

image155

Your programs can use the shelve module to later reopen and retrieve the data from these shelf files. Shelf values don’t have to be opened in read or write mode — they can do both once opened.

image156

Just like dictionaries, shelf values have keys() and values() methods that will return listlike values of the keys and values in the shelf.

image157

Saving Variables with the pprint.pformat() Function

pprint.pprint() function will “pretty print” the contents of a list or dictionary to the screen

pprint.pformat() function will return this same text as a string instead of printing it

image158

And since Python scripts are themselves just text files with the *.py file extension, your Python programs can even generate other Python programs.

image159

*Project: Generating Random Quiz Files

Each time through the loop, the %s placeholder in ‘capitalsquiz%s.txt’ and

‘capitalsquiz_answers%s.txt’ will be replaced by (quizNum + 1)

random.sample() function

image160

image161

image162

image163

Project: Multiclipboard

The *.pyw* extension means that Python won’t show a Terminal window when it runs this program. (See Appendix B for more details.)

image164

image165

image166

Chapter 9.

Organizing Files Shows how Python can copy, move, rename, and delete large numbers of files much faster than a human user can. It also explains compressing and decompressing files.

Chapter 10.

Shows how to use Python’s various bug-finding and bug-fixing tools.

Chapter 11.

Shows how to write programs that can automatically download web pages and parse them for information. This is called web scraping.

Chapter 12.

Covers programmatically manipulating Excel spreadsheets so that you don’t have to read them. This is helpful when the number of documents you have to analyze is in the hundreds or thousands.

Chapter 13.

Covers programmatically reading Word and PDF documents.

Chapter 14.

Continues to explain how to programmatically manipulate documents with CSV and JSON files.

Chapter 15.

Explains how time and dates are handled by Python programs and how to schedule your computer to perform tasks at certain times. This chapter also shows how your Python programs can launch non-Python programs.

Chapter 16.

Explains how to write programs that can send emails and text messages on your behalf.

Chapter 17.

Explains how to programmatically manipulate images such as JPEG or PNG files.

Chapter 18.

Explains how to programmatically control the mouse and keyboard to automate clicks and keypresses.