Subsections of Automate the Boring Stuff with Python
Section 1: Python Basics
Everyone in their life, spent a lot of time on repetitive tasks, which can be automated through a simple script.
Automate the boring stuff with Python uses Python 3.
How to get help?
Being stuck while coding is a normal happening, but not asking for help isn’t.
When you go online to ask for help, make sure:
Explain what you are trying to do, not just what you did.
If you get an error message, specify the point at which the error happens.
Copy and paste the entire body of the error message and your code to a Pastebin site like Pastebin.com or GitHub Gist.
Explain what you’ve already tried to do to solve your problem.
List the version of Python you’re using.
Say whether you’re able to reproduce the error every time you run the program or whether it happens only after you perform certain actions. If the latter, then explain what those actions are.
Specify what Operating System you’re on, what version of that OS you’re using.
Basic Terminology and using an IDLE
IDLE stands for Integrated Development and Learning Environment.
There are different programming text editors available:
Visual Studio Code
Sublime Text
PyCharm
Expressions = Values + Operators
In python, these expressions always evaluate to a single result. Arithmetic Operators are:
Operator
Operation
Example
Evaluates to . . .
**
Exponent
2 ** 3
8
%
Modulus/remainder
22 % 8
6
//
Integer division/floored quotient
22 // 8
2
/
Division
22 / 8
2.75
*
Multiplication
3 * 5
15
-
Subtraction
5 - 2
3
+
Addition
2 + 2
4
Data Types
Integers — “ints” (1,2,3…)
Floating point — “floats” (1.0, 1.1…)
Strings (“Hello World”)
Strings Concatenation: When two strings are joined together using a + symbol. (“Hello " + “World”)
String Replication: A string can be replicated by using * operator. (3 * “Hello World!”)
Both These operations can be combined like this "Hello World" + "!" * 5
Both concatenation and replication accepts strings values only.
Variables
Variable can store different values, like a box:
spam = 42
A too generic name given to a variable is a bad practice, which can create headache down the line while interacting with your code.
If a python instruction evaluates to a single value, it’s called an expression.
If it doesn’t evaluate to a single value, it’s called a statement.
We can update the variable value by calling it down the line in the program:
Just like the box, we can remove the old item with the new one.
Variable Names
You can name your variable anything, but Python does have some restrictions too:
It can be only one word with no spaces.
It can use only letters, numbers, and the underscores (_) character.
It can’t begin with a number.
Var names are case-sensitive too.
Though Spam is a valid var, but it is a Python convention to start var name with a lowercase letter.
camelCase for variables can be used though Python PEP8 style guide instead recommends the use of underscores like this camel_case.
Though PEP8 guide itself says:
Consistency with the style guide is important. But most importantly: know when to be inconsistent—sometimes the style guide just doesn’t apply. When in doubt, use your best judgment.
Writing Our First Program
Python ignore comments starting with #.
It also skips the blank lines.
Functions — They are like mini-programs in Python.
print("Hello World!")# Ask for their nameyourName=input("Type your name: ")print("It is good to meet you, "+str(yourName))print("Your name length is: "+str(len(yourName)))# Ask for their ageprint("What is your age?")yourAge=input("Type your age: ")print("You will be "+str(int(yourAge)+1)+" in a year.")
len(): It prints out the total number of characters in a string.
input() function always returns a string value, so you may have to convert it according to your need to float(), int() etc.
You can not concatenate str() and int() together, you will need to convert int() to str(int()), to concatenate them.
hello.py Evaluation steps look like this:
Extras (BOOK)
Python round(number, ndigits=None) Function
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating-Point Arithmetic: Issues and Limitations for more information.
Section 2: Flow Control
Flow Charts and Basic Flow Control Concepts
A flowchart starts at the start box, and you follow the arrow at the other boxes until you reach the end box. You take a different path depending on the conditions.
Based on how expression evaluate, a program can decide to skip instructions, repeat them, or choose one of several instructions to run. In fact, you almost never want your programs to start from the first line of ode and simply execute every line, straight to the end.
Flow control statements can decide which Python instructions to execute under which conditions.
These flow control statements directly correspond to the symbols in a flowchart.
In a flowchart, there is usually more than one way to go from the start to the end. The same is true for lines of code in a computer program. Flowcharts represent these branching points with diamonds, while the other steps are represented with rectangles. The starting and ending steps are represented with rounded rectangles.
Boolean Values
Boolean Data Type has only to values True and False.
How to represent YES and NO values:
Boolean Values
Comparison Operators
Boolean Operators
When entered as Python code, the Boolean always starts with a capital T or F, with the rest of the word in lowercase.
(Boolean is capitalized because the data type is named after mathematician George Boole)
➊>>>spam=True>>>spamTrue➋>>>trueTraceback(mostrecentcalllast):File"<pyshell#2>",line1,in<module>trueNameError:name'true'isnotdefined➌>>>True=2+2SyntaxError:can't assign to keyword
Like any other value, Boolean values are used in expressions and can be stored in variables ➊. If you don’t use the proper case ➋ or you try to use True and False for variable names ➌, Python will give you an error message.
Comparison Operators
They also called relational operators, compare two values and evaluate down to a single Boolean value.
Operator
Meaning
==
Equal to
!=
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
These operators evaluate to True or False depending on the values you give them.
The == and != operators can actually work with values of any data type.
An integer or floating point value will always be unequal to a string value. There 42 == '42'➊ evaluates to False because Python considers the integer 42 to be different from the string '42'.
The <, >, <=, and >= operators, on the other hand, work properly only with integer and floating-point values.
Boolean Operators
The three Boolean operators (and, or, and not) are used to compare Boolean values. Like comparison operators, they evaluate these expressions down to a Boolean value.
Binary Boolean Operators
The and and or operators always take two Boolean values (or expressions), so they’re considered binary operators.
and Operator: It evaluates to True only if both Boolean values are True.
Expression
Evaluates to…
True and True
True
True and False
False
False and True
False
False and False
False
or Operator: It evaluates to True if one of the Boolean values is True.
Expression
Evaluates to…
True or True
True
True or False
True
False or True
True
False or False
False
The not Operator
It has only one Boolean value (or expression)
Expression
Evaluates to…
not True
False
not False
True
Mixing Boolean and Comparison Operators
Since the comparison operators evaluate to Boolean values, you can use them in expressions with the Boolean operators.
You can also use multiple Boolean operators in an expression, along with the comparison operators:
>>>2+2==4andnot2+2==5and2*2==2+2True
The Boolean operators have an order of operations just like the math operators do. After any math and comparison operators evaluate, Python evaluates the not operators first, then the and operators, and then the or operators.
Elements of Flow Control
Flow control statements often start with a part called the condition and are always followed by a block of code called the clause.
Conditions
The Boolean expressions you’ve seen so far could all be considered conditions, which are the same thing as expressions; condition is just a more specific name in the context of flow control statements.
Conditions always evaluate down to a Boolean value, True or False. A flow control statement decides what to do based on whether its condition is True or False, and almost every flow control statement uses a condition.
Blocks of Code
Lines of Python code can be grouped together in blocks.
There are 3 rules for block:
Blocks begin when the indentation increases.
Blocks can contain other blocks.
Blocks end when the indentation decreases to zero or to a containing block’s indentation.
You can view the execution of this program at https://autbor.com/blocks/. The first block of code ➊ starts at the line print(‘Hello, Mary’) and contains all the lines after it. Inside this block is another block ➋, which has only a single line in it: print(‘Access Granted.’). The third block ➌ is also one line long: print(‘Wrong password.’).
If, Else, and Elif Statements
The statements represent the diamonds in the flowchart. They are the actual decisions your programs will make.
if Statements
If this condition is true, execute the code in the clause. if statement, consists of the following:
The if keyword
A condition (that is, an expression that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the if clause)
else Statements
An if clause can optionally be followed by an else statement. The else clause is executed only when the if statement’s condition is False.
An else statement doesn’t have a condition. In code, an else statement always consists of the following:
The else keyword
A colon
Starting on the next line, an indented block of code (called the else clause)
elif Statements
While only one of the if or else clauses will execute, you may have a case where you want one of many possible clauses to execute.
The elif statement is an “else if” statement that always follows an if or another elif statement. It provides another condition that is checked only if all the previous conditions were False.
In code, an elif statement always consists of the following:
The elif keyword
A condition (that is, an expression that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the elif clause)
ifname=='Alice':print('Hi, Alice.')elifage<12:print('You are not Alice, kiddo.')
The elif clause executes if age < 12 is True and name == 'Alice' is False. However, if both of the conditions are False, then both of the clauses are skipped. It is not guaranteed that at least one of the clauses will be executed. When there is a chain of elif statements, only one or none of the clauses will be executed. Once one of the statements’ conditions is found to be True, the rest of the elif clauses are automatically skipped.
name='Carol'age=3000ifname=='Alice':print('Hi, Alice.')elifage<12:print('You are not Alice, kiddo.')elifage>2000:print('Unlike you, Alice is not an undead, immortal vampire.')elifage>100:print('You are not Alice, grannie.')
The program vampire.py has 3 elif statements. If any of the three, is found True program execution will stop.
The order of elif statements is also important.
Optionally, you can have an else statement after the last elif statement. In that case, it is guaranteed that at least one (and only one) of the clauses will be executed. If the conditions in every if and elif statement are False, then the else clause is executed.
For example, let’s re-create the Alice program to use if, elif, and else clauses.
age=3000ifname=='Alice':print('Hi, Alice.')elifage<12:print('You are not Alice, kiddo.')else:print('You are neither Alice nor a little kid.')
When you use if, elif, and else statements together, remember these rules about how to order them to avoid bugs like the one in Figure 2.7. First, there is always exactly one if statement. Any elif statements you need should follow the if statement. Second, if you want to be sure that at least one clause is executed, close the structure with an else statement.
name='Carol'age=3000ifname=='Alice':print('Hi, Alice.')elifage<12:print('You are not Alice, kiddo.')elifage>100:print('You are not Alice, grannie.')elifage>2000:print('Unlike you, Alice is not an undead, immortal vampire.')
Figure 2-7: The flowchart for the vampire2.py program. The X path will logically never happen, because if age were greater than 2000, it would have already been greater than 100.
While Loops
The while statement always consists of the following:
The while keyword
A condition (that is, an expression that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the whileclause)
You can see that a while statement looks similar to an if statement. The difference is in how they behave. At the end of an ifclause, the program execution continues after the if statement. But at the end of a whileclause, the program execution jumps back to the start of the while statement. The while clause is often called the while loop or just the loop.
Here is the code, which will keep asking your name until you literally type your name in the prompt:
name=""whilename!='your name':print("Please type your name.")name=input()print("Thank you!")
break Statements
If the execution reaches a break statement, it immediately exits the while loop’s clause.
➊whileTrue:print('Please type your name.')➋name=input()➌ifname=='your name':➍break➎print('Thank you!')
The first line ➊ creates an infinite loop; it is a while loop whose condition is always True. (The expression True, after all, always evaluates down to the value True.) After the program execution enters this loop, it will exit the loop only when a break statement is executed. (An infinite loop that never exits is a common programming bug.)
Just like before, this program asks the user to enter your name ➋. Now, however, while the execution is still inside the while loop, an if statement checks ➌ whether name is equal to ‘your name’. If this condition is True, the break statement is run ➍, and the execution moves out of the loop to print(‘Thank you!’) ➎. Otherwise, the if statement’s clause that contains the break statement is skipped, which puts the execution at the end of the while loop. At this point, the program execution jumps back to the start of the while statement ➊ to recheck the condition.
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 re-evaluates the loop’s condition (This is also what happens when the execution reaches the end of the loop).
whileTrue:print('Who are you?')name=input()➊ifname!='Joe':➋continueprint('Hello, Joe. What is the password? (It is a fish.)')➌password=input()ifpassword=='swordfish':➍break➎print('Access granted.')
If the user enters any name besides Joe ➊, the continue statement ➋ causes the program execution to jump back to the start of the loop. When the program reevaluates the condition, the execution will always enter the loop, since the condition is simply the value True. Once the user makes it past that if statement, they are asked for a password ➌. If the password entered is swordfish, then the break statement ➍ is run, and the execution jumps out of the while loop to print Access granted ➎. Otherwise, the execution continues to the end of the while loop, where it then jumps back to the start of the loop.
Truthy and Fasely Values
Conditions will consider some values in other data types equivalent to True and False. When used in conditions, 0, 0.0, and ’’ (the empty string) are considered False, while all other values are considered True. For example, look at the following program:
name=''# `not` is a Boolean operator which flips the `True` or `False` values➊whilenotname:print('Enter your name:')name=input()print('How many guests will you have?')numOfGuests=int(input())➋ifnumOfGuests:➌print('Be sure to have enough room for all your guests.')print('Done')
If the user enters a blank string for name, then the while statement’s condition will be True ➊, and the program continues to ask for a name. If the value for numOfGuests is not 0 ➋, then the condition is considered to be True, and the program will print a reminder for the user ➌.
You could have entered not name != ’’ instead of not name, and numOfGuests != 0 instead of numOfGuests, but using the truthy and falsey values can make your code easier to read.
For Loops
The while loop keeps looping while its condition is True (which is the reason for its name), but what if you want to execute a block of code only a certain number of times? You can do this with a for loop statement and the range() function.
In code, a for statement looks something like for i in range(5): and includes the following:
The for keyword
A variable name
The in keyword
A call to the range() method with up to three integers passed to it
A colon
Starting on the next line, an indented block of code (called the for clause)
print("My name is")foriinrange(5):print("Alex Five Times ("+str(i)+")")
The code in the for loop’s clause is run five times. The first time it is run, the variable i is set to 0. The print() call in the clause will print Jimmy Five Times (0). After Python finishes an iteration through all the code inside the for loop’s clause, the execution goes back to the top of the loop, and the for statement increments i by one. This is why range(5) results in five iterations through the clause, with i being set to 0, then 1, then 2, then 3, and then 4. The variable i will go up to, but will not include, the integer passed to range().
NOTE
You can use break and continue statements inside for loops as well. The continue statement will continue to the next value of the for loop’s counter, as if the program execution had reached the end of the loop and returned to the start. In fact, you can use continue and break statements only inside while and for loops. If you try to use these statements elsewhere, Python will give you an error.
Counting the sums of all the numbers to 100 using both for and while loops:
# For Loop to Count the sums of numbers upto 100sum=0foriinrange(101):sum=sum+i# print(sum, i)print("The sum of 100 using for loop is: ",sum)# While Loop#sum=0i=0whilei<101:sum=sum+ii=i+1print("The sum of 100 using while loop is: ",sum)
The use of for is more efficient though while can also get the job done.
The Starting, Stopping, and Stepping Arguments to range()
Some functions can be called with multiple arguments separated by a comma, and range() is one of them. This lets you change the integer passed to range() to follow any sequence of integers, including starting at a number other than zero.
foriinrange(12,16):print(i)
The first argument will be where the for loop’s variable starts, and the second argument will be up to, but not including, the number to stop at.
12
13
14
15
The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
foriinrange(0,10,2):print(i)
So calling range(0, 10, 2) will count from zero to eight by intervals of two.
0
2
4
6
8
The range() function is flexible in the sequence of numbers it produces for for loops. You can even use a negative number for the step argument to make the for loop count down instead of up.
foriinrange(5,-1,-1):print(i)
This for loop would have the following output:
5
4
3
2
1
0
Running a for loop to print i with range(5, -1, -1) should print from five down to zero.
Importing Modules
All Python programs can call a basic set of functions called built-in functions, including the print(), input(), and len() functions you’ve seen before.
Python also comes with a set of modules called the standard library.
Each module is a Python program that contains a related group of functions that can be embedded in your programs. For example, the math module has mathematics-related functions, the random module has random number-related functions, and so on.
Before you can use the functions in a module, you must import the module with an import statement. In code, an import statement consists of the following:
The import keyword
The name of the module
Optionally, more module names, as long as they are separated by commas.
When you save your Python scripts, take care not to give them a name that is used by one of Python’s modules, such as random.py, sys.py, os.py, or math.py. If you accidentally name one of your programs, say, random.py, and use an import random statement in another program, your program would import your random.py file instead of Python’s random module. This can lead to errors such as AttributeError: module random has no attribute ‘randint’, since your random.py doesn’t have the functions that the real random module has. Don’t use the names of any built-in Python functions either, such as print() or input().
Problems like these are uncommon, but can be tricky to solve. As you gain more programming experience, you’ll become more aware of the standard names used by Python’s modules and functions, and will run into these issues less frequently.
Since randint() is in the random module, you must first type random. in front of the function name to tell Python to look for this function inside the random module.
from import Statements
An alternative form of the import statement is composed of the from keyword, followed by the module name, the import keyword, and a star; for example, from random import *.
With this form of import statement, calls to functions in random will not need the random. prefix. However, using the full name makes for more readable code, so it is better to use the import random form of the statement.
Ending a Program Early with the sys.exit() function
Programs always terminate if the program execution reaches the bottom of the instructions. However, you can cause the program to terminate, or exit, before the last instruction by calling the sys.exit() function.
Since this function is in the sys module, you have to import sys before you can use it.
importsyswhileTrue:print('Type exit to quit.')response=input()ifresponse=='exit':sys.exit()print('You typed '+"'"+response+"'"+'.')
Run this program in IDLE. This program has an infinite loop with no break statement inside. The only way this program will end is if the execution reaches the sys.exit() call. When response is equal to exit, the line containing the sys.exit() call is executed. Since the response variable is set by the input() function, the user must enter exit in order to stop the program.
A Short Program: Guess the Number
We have a pseudocode like this:
I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess.
15
Your guess is too low.
Take a guess.
17
Your guess is too high.
Take a guess.
16
Good job! You guessed my number in 4 guesses!
I have implemented this code as:
fromrandomimportrandintsecretNumber=randint(1,20)# print(secretNumber) # Debuging purposes onlyprint("I am thinking of a number between 1 and 20.")guess=''numberOfGuesses=0whileguess!=secretNumber:guess=int(input("Take a Guess: "))numberOfGuesses=numberOfGuesses+1ifguess<secretNumber:print("Your Guess is too low.")elifguess>secretNumber:print("Your Guess is too high")print("Good job! You guessed my number in "+str(numberOfGuesses)+" guesses!")
This how Al implemented it…
# This is a guess the number game. importrandomsecretNumber=random.randint(1,20)print('I am thinking of a number between 1 and 20.')# Ask the player to guess 6 times. forguessesTakeninrange(1,7):print('Take a guess.')guess=int(input())ifguess<secretNumber:print('Your guess is too low.')elifguess>secretNumber:print('Your guess is too high.')else:break# This condition is the correct guess! ifguess==secretNumber:print('Good job! You guessed my number in '+str(guessesTaken)+' guesses!') else:print('Nope. The number I was thinking of was '+str(secretNumber))
Version 2.0 of my implementation of guessTheNumber2.py game…
fromrandomimportrandintsecretNumber=randint(1,20)# print(secretNumber) # Debuging purposes onlyprint("I am thinking of a number between 1 and 20.")# guess = ''numberOfGuesses=0whileTrue:guess=int(input("Take a Guess: "))numberOfGuesses=numberOfGuesses+1ifguess<secretNumber:print("Your Guess is too low.")elifguess>secretNumber:print("Your Guess is too high")else:breakprint("Good job! You guessed my number in "+str(numberOfGuesses)+" guesses!")
I’m still going with the unlimited number of guesses method, but improved the logic.
A Short Program: Rock, Paper, Scissors
We have the Pseudocode for the program:
ROCK, PAPER, SCISSORS
0 Wins, 0 Losses, 0 Ties
Enter your move: (r)ock (p)aper (s)cissors or (q)uit
p
PAPER versus...
PAPER
It is a tie!
0 Wins, 1 Losses, 1 Ties
Enter your move: (r)ock (p)aper (s)cissors or (q)uit
s
SCISSORS versus...
PAPER
You win!
1 Wins, 1 Losses, 1 Ties
Enter your move: (r)ock (p)aper (s)cissors or (q)uit
q
That’s how I implemented it:
################################################## RPS GAME VERSION 5.0 ####################################################importrandomimportsys# Print to the Screen Onceprint("ROCK, PAPER, SCISSORS")# Counting Streakswins=0losses=0ties=0whileTrue:# Print to the Screenprint("Enter your move: (r)ock (p)aper (s)cissors or (q)uit")# User InputuserMove=input()ifuserMove=="q":print(f"Thank you for playing our Game!\n{wins} Wins, {losses} losses, {ties} Ties")sys.exit()elifuserMove!="r"anduserMove!="p"anduserMove!="s":print("Illegal Guess, Try again.")continueelifuserMove=="r":userMove="ROCK"elifuserMove=="p":userMove="PAPER"elifuserMove=="s":userMove="SCISSORS"# System inputsystemMove=random.randint(1,3)ifsystemMove==1:systemMove="ROCK"elifsystemMove==2:systemMove="PAPER"elifsystemMove==3:systemMove="SCISSORS"# Showing the Played Movesprint(f"{systemMove} vs. {userMove}")# Game LogicifsystemMove==userMove:print("It is a tie")ties=ties+1elif((systemMove=="ROCK"anduserMove=="PAPER")or(systemMove=="SCISSORS"anduserMove=="ROCK")or(systemMove=="PAPER"anduserMove=="SCISSORS")):print("You win!")wins=wins+1elif((systemMove=="ROCK"anduserMove=="SCISSORS")or(systemMove=="PAPER"anduserMove=="ROCK")or(systemMove=="SCISSORS"anduserMove=="PAPER")):print("Loser!")losses=losses+1
Tip
Go to my GitHub to see other versions of the game, and how I went step by step, implementing the logic and cleaning the code. It still isn’t efficient or clean looking code, as we haven’t gotten to some advanced lessons, which can help us clean it up further.
This how Al implemented it…
importrandom,sysprint('ROCK, PAPER, SCISSORS')# These variables keep track of the number of wins, losses, and ties. wins=0losses=0ties=0whileTrue:# The main game loop. print('%s Wins, %s Losses, %s Ties'%(wins,losses,ties))whileTrue:# The player input loop. print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')playerMove=input()ifplayerMove=='q':sys.exit()# Quit the program. ifplayerMove=='r'orplayerMove=='p'orplayerMove=='s':break# Break out of the player input loop. print('Type one of r, p, s, or q.')# Display what the player chose: ifplayerMove=='r':print('ROCK versus...')elifplayerMove=='p':print('PAPER versus...')elifplayerMove=='s':print('SCISSORS versus...')# Display what the computer chose: randomNumber=random.randint(1,3)ifrandomNumber==1:computerMove='r'print('ROCK')elifrandomNumber==2:computerMove='p'print('PAPER')elifrandomNumber==3:computerMove='s'print('SCISSORS')# Display and record the win/loss/tie: ifplayerMove==computerMove:print('It is a tie!')ties=ties+1elifplayerMove=='r'andcomputerMove=='s':print('You win!')wins=wins+1elifplayerMove=='p'andcomputerMove=='r':print('You win!')wins=wins+1elifplayerMove=='s'andcomputerMove=='p':print('You win!')wins=wins+1elifplayerMove=='r'andcomputerMove=='p':print('You lose!')losses=losses+1elifplayerMove=='p'andcomputerMove=='s':print('You lose!')losses=losses+1elifplayerMove=='s'andcomputerMove=='r':print('You lose!')losses=losses+1
abs() Function (Extras)
The Python abs() function return the absolute value. The absolute value of any number is always positive it removes the negative sign of a number in Python.
>>>abs(-10)10>>>abs(-0.50)0.5>>>abs(-32.40)32.4
Section 3: Functions
Python provides several built-in functions like print(), input() and len(), but you can also write your own functions.
A function is like a mini-program within a program.
The first line is a def statement ➊, which defines a function named hello(). The code in the block that follows the def statement ➋ is the body of the function. This code is executed when the function is called, not when the function is first defined.
The hello() lines after the function ➌ are function calls. In code, a function call is just the function’s name followed by parentheses, possibly with some number of arguments in between the parentheses.
A major purpose of functions is to group code that gets executed multiple times. Without a function defined, you would have to copy and paste this code each time, and the program would look like this:
The definition of the hello() function in this program has a parameter called name ➊. Parameters are variables that contain arguments. When a function is called with arguments, the arguments are stored in the parameters. The first time the hello() function is called, it is passed the argument 'Alice' ➌. The program execution enters the function, and the parameter name is automatically set to 'Alice', which is what gets printed by the print() statement ➋.
The value stored in a parameter is forgotten when the function returns. For example, if you added print(name) after hello('Bob') in the previous program, the program would give a NameError because there is no variable named name.
Define, Call, Pass, Argument, Parameter
The terms define, call, pass, argument, and parameter can be confusing. Let’s look at a code example to review these terms:
To define a function is to create it, just like an assignment statement like spam = 42 creates the spam variable. The def statement defines the sayHello() function ➊.
The sayHello('Al') line ➋ calls the now-created function, sending the execution to the top of the function’s code. This function call is also known as passing the string value 'Al' to the function.
A value being passed to a function in a function call is an argument. The argument 'Al' is assigned to a local variable named name. Variables that have arguments assigned to them are parameters.
It’s easy to mix up these terms, but keeping them straight will ensure that you know precisely what the text in this chapter means.
Return Values and return Statements
Calling a len() function with an argument such as 'hello, will evaluate to the integer value 5, which is the length of the string passed.
The value that a function call evaluates to is called return value of the function.
While writing a function, return value should be used with return statement.
A return statement has:
The return keyword
The value or expression that the function should return.
When an expression is used with a return statement, the return value is what this expression evaluates to.
The None Value
In Python, there is a value called None, which represents the absence of a value(a placeholder). The None value is the only value of the NoneType data type.
Other programming languages might call this value null, nil, or undefined.
Just like the Boolean True and False values, None must be typed with a capital N.
This value-without-a-value can be helpful when you need to store something that won’t be confused for a real value in a variable.
One place where None is used is as the return value of print().
The print() function displays text on the screen, but it doesn’t need to return anything in the same way len() or input() does. But since all function calls need to evaluate to a return value, print() returns None. To see this in action, enter the following into the interactive shell:
>>>spam=print('Hello!')Hello!>>>None==spamTrue
Behind the scenes, Python adds return None to the end of any function definition with no return statement. This is similar to how a while or for loop implicitly ends with a continue statement. Also, if you use a return statement without a value (that is, just the return keyword by itself), then None is returned.
Keyword Arguments and the print() Function
Keyword arguments are often used for optional parameters. For example, the print() function has the optional parameters end and sep to specify what should be printed at the end of its arguments and between its arguments (separating them), respectively.
By default, two successive print statements would print their arguments on a separate line, but we can change this behavior with keyword arguments:
print('Hello',end=' ')print('World')
When different strings are concatenated, we can use:
print('Hello!'+'World',sep=':')
The Call Stack
Imagine that you have a meandering conversation with someone. You talk about your friend Alice, which then reminds you of a story about your coworker Bob, but first you have to explain something about your cousin Carol. You finish you story about Carol and go back to talking about Bob, and when you finish your story about Bob, you go back to talking about Alice. But then you are reminded about your brother David, so you tell a story about him, and then get back to finishing your original story about Alice. Your conversation followed a stack-like structure, like in Figure 3-1. The conversation is stack-like because the current topic is always at the top of the stack.
Similar to our meandering conversation, calling a function doesn’t send the execution on a one-way trip to the top of a function. Python will remember which line of code called the function so that the execution can return there when it encounters a return statement. If that original function called other functions, the execution would return to those function calls first, before returning from the original function call.
The call stack is how Python remembers where to return the execution after each function call.
The call stack isn’t stored in a variable in your program; rather, Python handles it behind the scenes.
When your program calls a function, Python creates a frame object on the top of the call stack. Frame objects store the line number of the original function call so that Python can remember where to return. If another function call is made, Python puts another frame object on the call stack above the other one.
When a function call returns, Python removes a frame object from the top of the stack and moves the execution to the line number stored in it. Note that frame objects are always added and removed from the top of the stack and not from any other place.
The top of the call stack is which function the execution is currently in. When the call stack is empty, the execution is on a line outside of all functions.
Local and Global Scope
Parameters and variables that are assigned in a called function are said to exit in that function’s local scope.
Variables that are assigned outside all functions are said to exist in the global scope.
A variable must be one or the other; it cannot be both local and global.
Think of a scope as a container for variables. When scope is destroyed, all variables stored inside it are forgotten.
There is only one global scope, and it is created when your program begins. When your program terminates, the global scope is destroyed, and all its variables are forgotten.
A local scope is created whenever a function is called. Any variables assigned in the function exist within the function’s local scope. When the function returns, the local scope is destroyed, and these variables are forgotten.
Scope matter because:
Code in the global scope, outside all functions, cannot use any local variables.
However, code in a local scope can access global variables.
defspam():print(eggs)eggs=42spam()print(eggs)
Code in a function’s local scope cannot use variables in any other local scope.
We can use the same name for different variables, if they are in different scopes.
It’s easy to track down a bug caused by a local variable. When there are thousands of lines of code, global variables are hard to work with.
Using global variables in small programs is fine, it’s a bad habit to rely on global variables as your programs get larger and larger.
The Global Statement
To modify a global variable from within a function, we can use a global statement.
If you have a line such as global eggs at the top of a function, it tells Python, “In this function, eggs refers to the global variable, so don’t create a local variable with this name.”
Because eggs is declared global at the top of spam() ➊, when eggs is set to 'spam' ➋, this assignment is done to the globally scoped eggs. No local eggs variable is created.
There are four rules to tell whether a variable is in a local scope or global scope:
If a variable is being used in the global scope (that is, outside all functions), then it is always a global variable.
If there is a global statement for that variable in a function, it is a global variable.
Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
But if the variable is not used in an assignment statement, it is a global variable.
Functions as Black Boxes…
Often, all you need to know about a function are its inputs (the parameters) and output value; you don’t always have to burden yourself with how the function’s code actually works. When you think about functions in this high-level way, it’s common to say that you’re treating a function as a “black box.”
This idea is fundamental to modern programming. Later chapters in this book will show you several modules with functions that were written by other people. While you can take a peek at the source code if you’re curious, you don’t need to know how these functions work in order to use them. And because writing functions without global variables is encouraged, you usually don’t have to worry about the function’s code interacting with the rest of your program.
Section 4: Handling Errors With Try/Except
Exception Handling
Getting an error or exception in Python program, without any exception handling means entire program will crash.
In real world, this is not the desired behavior, and we want our program to detect errors, handle them, and then continue to run.
When the program is run we will get ZeroDivisonError at line 6.
You can put the previous divide-by-zero code in a try clause and have an except clause contain code to handle what happens when this error occurs.
1
2
3
4
5
6
7
8
9
10
11
defspam(divideBy):try:return42/divideByexceptZeroDivisionError:return('Error: I cannot do that.')print(spam(2))print(spam(12))print(spam(0))print(spam(1))
When code in a try clause causes an error, the program execution immediately moves to the code in the except clause. After running that code, the execution continues as normal.
A Short Program: Zigzag
This program will create a back-and-forth, zigzag pattern until the user stops it by pressing the Mu editor’s Stop button or by pressing CTRL-C. When you run this program, the output will look something like this:
# An extra project from book's chapter 3importsysimporttimedefasterisks_pattern(startSpace,pattern):print(' '*startSpace+pattern)time.sleep(0.1)pattern='******'whileTrue:try:forstartSpaceinrange(10):asterisks_pattern(startSpace,pattern)forstartSpaceinrange(10,1,-1):asterisks_pattern(startSpace,pattern)exceptKeyboardInterrupt:print(' Quiting the animation pattern. Goodbye!')sys.exit()
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”)
Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.
Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.
The output of this program could look something like this:
Enternumber:3105168421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Extra Project from book's chapter 3defcollatz(number):ifnumber%2==0:result=int(number/2)else:result=int(3*number+1)print(result)returnresulttry:number=int(input("Enter your number:\n"))whilenumber!=1:number=collatz(number)exceptValueError:print('Please enter a valid integer')
Section 5: Writing a Complete Program, Guess the Number
A Guess Game
The output we need:
Hello, What is your name?
Al
Well, Al, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess
5
Your guess is too high.
Take a guess.
6
Good job, Al! You guessed my number in 5 guesses!
importrandom# Ask for Player name and greet themplayerName=input('Hello, What is your name?\n')print(f"Well, {playerName}, I am thinking of a number between 1 and 20.")secretNumber=random.randint(1,20)# print(f"Debug: Secret Number is {secretNumber}")fornumberOFGuessesinrange(1,7):# Max number of Guesses allowedplayerGuess=int(input('Take a Guess\n'))ifplayerGuess<secretNumber:print('Your Guess is too low.')elifplayerGuess>secretNumber:print('Your Guess is too high')else:breakifplayerGuess==secretNumber:print(f'Good job,{playerName}! You guessed my number in {numberOFGuesses} guesses!')else:print(f"Nope. The number I was thinking of was {secretNumber}.")
F-Strings
In this course we were taught about string concatenation using + operator. But that is cumbersome, and we need to convert non-strings values to strings values for concatenation to work.
In python 3.6, F-strings were introduced, that makes the strings concatenation a lot easier.
print(f"This is an example of {strings} concatenation.")
{} We can put our variable name, which will be automatically converted into string type. As you can see, this approach is much more cleaner.
A Guess Game — Extended Version
Let’s take everything we learned so far, write a guess game which has the following qualities:
An error checking
Asking player to choose the lower and higher end of number for guessing game.
Let player exit the game using sys.exit() module or pressing q(uit) button on their keyboard.
Using built-in function title() method, convert a string into title case, where the first letter of each word is capitalized, and the rest are in lowercase.
An extra feature which I want to implement is telling the player, how many guesses they will get. As taught in Algorithm: Binary Search course, offered by Khan Academy. We can calculate max number of guesses using this formula:
$$
\text{Maximum number of guesses} = \log_{2}(n) \
$$
For guess between (1, 20), the n = 20:
$$
\text{Maximum number of guesses} = \log_{2}(20)
$$
$$
\text{Maximum number of guessess} \approx 5
$$
Here is the extended version, I might have gone a bit over the board.
importrandomimportmathimportsysimporttimedefquitGame():# Message to print when CTRL+C keys are pressedprint('\nThanks for Playing, quiting the game...')sys.exit()# Greeting the Playertry:print('Welcome to Guess the Number Game. \nYou can Quit the game any time by pressing CTRL+C keys on your keyboard')playerName=input('Hello, What is your name?\n').title()print(f"Well, {playerName}, let's choose our start and end values for the game.")exceptKeyboardInterrupt:quitGame()# Asking Player for Guessing Range and Error CheckingwhileTrue:try:lowerEndOfGuess=int(input('Choose your start number: '))higherEndOfGuess=int(input('Choose your end number: '))iflowerEndOfGuess>higherEndOfGuess:# Otherwise our random function will failprint('Starting number should be less than ending number')continuebreakexceptValueError:print('Only Intergers are allowed as a start and end values of a Guessing Game.')exceptKeyboardInterrupt:quitGame()# Haing Fun and choosing the secret numbertry:print('Wait, a moment, I m gearing up for the battle.')time.sleep(2)print("Don't be stupid.I'm not stuck., I'm still thinking of what number to choose!")time.sleep(3)print('Dont dare to Quit on me')secretNumber=random.randint(lowerEndOfGuess,higherEndOfGuess)time.sleep(2.5)print('Shshhhhhhh! I have chosen my MAGIC NUMBER!')time.sleep(1.5)print("It's your turn")time.sleep(1.5)exceptKeyboardInterrupt:quitGame()# print(f"Debug: Secret Number is {secretNumber}")# Calculating maximum number of possible guessestotalGuesses=higherEndOfGuess-lowerEndOfGuessmaxPossibleGuesses=math.ceil(math.log2(totalGuesses))print(f"You have {maxPossibleGuesses} guesses to Win the Game.")time.sleep(1.5)# Game LogicfornumberOFGuessesinrange(1,maxPossibleGuesses+1):try:playerGuess=int(input('Take a Guess!\n'))ifplayerGuess<secretNumber:print('Your Guess is too low!')elifplayerGuess>secretNumber:print('Your Guess is too high!')else:breakexceptValueError:print('Only integers are allowed as valid game guess.')exceptKeyboardInterrupt:quitGame()# Ending the Gametry:ifplayerGuess==secretNumber:print(f'Good job,{playerName}! You guessed my number in {numberOFGuesses} guesses!')else:print(f"You lose! Number of guesses are exhausted. The number I was thinking of was {secretNumber}.")exceptNameError:print('Please, try again, something went wrong!')
Section 6: Lists
A list is a value that contains multiple values.
The values in a list are also called item.
You can access items in a list with its integer index.
The indexes start at 0, not 1.
You can also use negative indexes. -1 refers to the last item, -2 refers to the second to last item, and so on.
You can get multiple items from the list using a slice.
The slice has two indexes. The new list’s items start at the first index and go up to, but doesn’t include, the second index.
The len() function, concatenation, and replication work the same way with lists that they do with strings.
You can convert a value into a list by passing it to the first() function.
The list Data Type
A list is a value that contains multiple values in an ordered sequence. The term list value refers to the list itself (which is a value that can be stored in a variable or passed to a function like any other value), not the values inside the list value.
The spam variable ➊ is still assigned only one value: the list value. But the list value itself contains other values. The value [] is an empty list that contains no values, similar to '', the empty string.
Getting Individual Values in a List with Indexes
Lists can also contain other list values. The values in these lists of lists can be accessed using multiple indexes, like so:
The first index dictates which list value to use, and the second indicates the value within the list value.
Negative Indexes
The integer value -1 refers to the last index in a list, the value -2 refers to the second-to-last index in a list, and so on.
>>>spam=['cat','bat','rat','elephant']>>>spam[-1]'elephant'>>>spam[-3]'bat'>>>'The '+spam[-1]+' is afraid of the '+spam[-3]+'.''The elephant is afraid of the bat.'
Getting a List from Another List 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. A slice goes up to, but will not include, the value at the second index.
As a shortcut, you can leave out one or both of the indexes on either side of the colon in the slice. Leaving out the first index is the same as using 0, or the beginning of the list. Leaving out the second index is the same as using the length of the list, which will slice to the end of the list. Enter the following into the interactive shell:
The len() function will return the number of values that are in a list value passed to it, just like it can count the number of characters in a string value.
The del statement can also be used on a simple variable to delete it, as if it were an “un-assignment” statement. If you try to use the variable after deleting it, you will get a NameError error because the variable no longer exists. In practice, you almost never need to delete simple variables. The del statement is mostly used to delete values from lists.
Working with Lists
It’s tempting to create many individual variables to store a group of similar values.
It’s a bad way to write a program.
Down the line, when you will need to store more values, you won’t be able, if you run out of variables.
Let’s look at the example of bad code using a lot of variables to store a group of similar values:
print('Enter the name of cat 1:')catName1=input()print('Enter the name of cat 2:')catName2=input()print('Enter the name of cat 3:')catName3=input()print('Enter the name of cat 4:')catName4=input()print('Enter the name of cat 5:')catName5=input()print('Enter the name of cat 6:')catName6=input()print('The cat names are:')print(catName1+' '+catName2+' '+catName3+' '+catName4+' '+catName5+' '+catName6)
Improved version:
catName=[]whileTrue:print(f"Enter your cat name: {len(catName)+1} (Or Enter nothing to stop.)")name=input()ifname=='':breakcatName=catName+[name]print("The cat names are: ")fornameincatName:print(f" {name}")
for Loops with Lists, Multiple Assignment, and Augmented Operators
For loops technically iterate over the values in a list.
The range() function returns a list-like value, which can be passed to the list() function if you need an actual list value.
Variables can swap their values using multiple assignment.
Augmented assignment operators like += are used as shortcuts.
Using for Loops with Lists
for Loops execute a block of code a certain number of times. Technically, a for loop repeats the code block once for each item in a list value.
#inputforiinrange(4):print(i)#output0123
This is because the return value from range(4) is a sequence value that Python considers similar to [0,1,2,3] (Sequence Data Types).
The following program has same output as the previous one:
foriin[0,1,2,3]:print(i)
A common Python technique is to use range(len(someList)) with a for loop to iterate over the indexes of a list.
supplies=['pens','staplers','printers','binders']foriinrange(len(supplies)):print(f"Index of {i} in supplies is: {supplies[i]}")Index0insuppliesis:pensIndex1insuppliesis:staplersIndex2insuppliesis:printersIndex3insuppliesis:binders
The in and not in Operators
The in and not in operators are used to determine whether a value is or isn’t in a list.
Program: Write a program that lets the user type in a pet name and then checks to see whether the name is in a list of pets.
The Multiple Assignment Trick
The multiple assignment trick (technically called tuple unpacking) is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
The number of variables and the length of the list must be exactly equal, or Python will give you a ValueError.
Using the enumerate() Function with Lists
Instead of using range(len(someList)) technique, enumerate() returns both list item, and its index, when called upon a list.
>>>supplies=['pens','staplers','flamethrowers','binders']>>>forindex,iteminenumerate(supplies):...print('Index '+str(index)+' in supplies is: '+item)Index0insuppliesis:pensIndex1insuppliesis:staplersIndex2insuppliesis:flamethrowersIndex3insuppliesis:binders
The enumerate() function is useful if you need both the item and the item’s index in the loop’s block.
Using the random.choice() and random.shuffle() Functions with Lists
The random module has a couple of functions that accept lists for arguments. The random.choice() function will return a randomly selected item from the list.
Methods are functions that are “called on” values.
The index() list method returns the index of an item in the list.
The append() list method adds a value to the end of the list.
The insert() list method adds a value anywhere inside a list.
The remove() list method removes an item, specified by the value, from a list.
The sort() list method sorts the items in a list.
The sort() method’s reverse=True keyword argument can sort in reverse order.
Sorting happens in “ASCII-betical” order. To sort normally, pass key=str.lower.
These list methods operate on the list “in place”, rather than returning a new list value.
Methods belong to a single data type. The append() and insert() methods are list methods and can be only called on list values, not on other values such as strings or integers.
Calling list methods on str or inte will give the error AttributeError.
Each data type has its own set of methods. This list data type, for example, has several useful methods for finding, adding, removing, and other manipulating values in a list.
Notice that the code is spam.append('moose') and spam.insert(1, 'chicken'), not spam = spam.append('moose') and spam = spam.insert(1, 'chicken'). Neither append() nor insert() gives the new value of spam as its return value. (In fact, the return value of append() and insert() is None, so you definitely wouldn’t want to store this as the new variable value.) Rather, the list is modified in place. Modifying a list in place is covered in more detail later in Mutable and Immutable Data Types.
The sort() method sorts the list in place, don’t try to capture the return value writing code like spam = spam.sort().
You cannot sort lists that have both number values and string values. Since Python doesn’t know what to do with them.
The sort() uses ASCII-betical order rather than actual alphabetical order for sorting strings. This means uppercase letters come before lowercase letters.
In most cases, the amount of indentation for a line of code tells Python what block it is in. There are some exceptions to this rule, however. For example, lists can actually span several lines in the source code file. The indentation of these lines does not matter; Python knows that the list is not finished until it sees the ending square bracket. For example, you can have code that looks like this:
Of course, practically speaking, most people use Python’s behavior to make their lists look pretty and readable.
Similarities Between Lists and Strings
Strings can do a lot of the same things lists can do, but strings are immutable.
Mutable values like lists can be modified in place.
Variables don’t contain lists, they contain references to lists.
When passing a list argument to a function, you are actually passing a list reference.
Changes made to a list in a function will affect the list outside the function.
The \ line continuation character can be used to stretch Python instructions across multiple lines.
Sequence Data Types
Lists aren’t the only data types that represent ordered sequences of values.
Strings and lists are actually similar if you consider a string to be a “list” of single text characters.
The Python sequence data types include lists, strings, range object returned by range(), and tuples.
Many things you can do with lists can also be done with strings and other values of sequence types: indexing; slicing; and using them with for loops, with len(), and with in and not in operators.
Trying to reassign a single character in a string results in a TypeError error:
>>>name='Zophie a cat'>>>name[7]='the'Traceback(mostrecentcalllast):File"<pyshell#50>",line1,in<module>name[7]='the'TypeError:'str'objectdoesnotsupportitemassignment
The proper way to “mutate” a string is to use slicing and concatenation to build a new string by copying from parts of the old string.
>>>name='Zophie a cat'>>>newName=name[0:7]+'the'+name[8:12]>>>name'Zophie a cat'>>>newName'Zophie the cat'
Although a list value is mutable:
>>>eggs=[1,2,3]>>>eggs=[4,5,6]>>>eggs[4,5,6]
The list value in eggs isn’t being changed here; rather, an entirely new and different list value [4, 5, 6] is overwriting the old list.
If you have only one value in your tuple, you cna indicate this by placing a trailing comma after the value inside the parentheses. Otherwise, Python will think you’ve just typed a value inside regular parentheses.
You can use tuples to convey to anyone reading your code that you don’t intend for that sequence of values to change. If you need an ordered sequence of values that never changes, use a tuple. A second benefit of using tuples instead of lists is that, because they are immutable, and their contents don’t change, Python can implement some optimizations.
Converting Types with the list() and tuple() Functions
Just like how str(42) will return '42', the string representation of the integer 42, the functions list() and tuple() will return list and tuple versions of the values passed to them:
Converting a tuple to a list is handy if you need a mutable version of a tuple value.
Reference Types
As you’ve seen, variables “store” strings and integer values. However, this explanation is a simplification of what Python is actually doing. Technically, variables are storing references to the computer memory locations where the values are stored.
When you assign 42 to the spam variable, you are actually creating the 42 value in the computer’s memory and storing a reference to it in the spam variable. When you copy the value in spam and assign it to the variable cheese, you are actually copying the reference. Both the spam and cheese variables refer to the 42 value in the computer’s memory. When you later change the value in spam to 100, you’re creating a new 100 value and storing a reference to it in spam. This doesn’t affect the value in cheese. Integers are immutable values that don’t change; changing the spam variable is actually making it refer to a completely different value in memory.
But lists don’t work this way, because list values can change; that is, lists are mutable. Here is some code that will make this distinction easier to understand.
➊>>>spam=[0,1,2,3,4,5]➋>>>cheese=spam# The reference is being copied, not the list. ➌>>>cheese[1]='Hello!'# This changes the list value. >>>spam[0,'Hello!',2,3,4,5]>>>cheese# The cheese variable refers to the same list. [0,'Hello!',2,3,4,5]
This might look odd to you. The code touched only the cheese list, but it seems that both the cheese and spam lists have changed.
When you create the list ➊, you assign a reference to it in the spam variable. But the next line ➋ copies only the list reference in spam to cheese, not the list value itself. This means the values stored in spam and cheese now both refer to the same list. There is only one underlying list because the list itself was never actually copied. So when you modify the first element of cheese ➌, you are modifying the same list that spam refers to.
What happens when a list is assigned to the spam variable.
Then, the reference in spam is copied to cheese. Only a new reference was created and stored in cheese, not a new list. Note how both references refer to the same list.
When you alter the list that cheese refers to, the list that spam refers to is also changed, because both cheese and spam refer to the same list.
Identity and the id() Function
Why the weird behavior with mutable lists in the previous section doesn’t happen with immutable values like integers or strings.
We can use Python’s id() function to understand this. All values in Python have a unique identity that can be obtained with the id() function.
>id('Howdy')# The returned number will be different on your machine. 139789342729024
When Python runs id('Howdy'), it creates the 'Howdy' string in the computer’s memory. The numeric memory address where the string is stored is returned by the id() function. Python picks this address based on which memory bytes happen to be free on your computer at the time, so it’ll be different each time you run this code.
Like all strings, 'Howdy' is immutable and cannot be changed. If you “change” the string in a variable, a new string object is being made at a different place in memory, and the variable refers to this new string. For example, enter the following into the interactive shell and see how the identity of the string referred to by bacon changes:
>>>bacon='Hello'>>>id(bacon)139789339474704>>>bacon+=' world!'# A new string is made from 'Hello' and ' world!'. >>>id(bacon)# bacon now refers to a completely different string. 139789337326704
However, lists can be modified because they are mutable objects. The append() method doesn’t create a new list object; it changes the existing list object. We call this modifying the object in-place.
>>>eggs=['cat','dog']# This creates a new list. >>>id(eggs)139789337916608>>>eggs.append('moose')# append() modifies the list "in place". >>>id(eggs)# eggs still refers to the same list as before. 139789337916608>>>eggs=['bat','rat','cow']# This creates a new list, which has a new identity. >>>id(eggs)# eggs now refers to a completely different list. 139789337915136
Passing References
References are particularly important for understanding how arguments get passed to functions. When a function is called, the values of the arguments are copied to the parameter variables. For lists (and dictionaries, which I’ll describe in the next chapter), this means a copy of the reference is used for the parameter.
Notice that when eggs() is called, a return value is not used to assign a new value to spam. Instead, it modifies the list in place, directly. When run, this program produces the following output:
[1, 2, 3, 'Hello']
Even though spam and someParameter contain separate references, they both refer to the same list. This is why the append('Hello') method call inside the function affects the list even after the function call has returned.
Keep this behavior in mind: forgetting that Python handles list and dictionary variables this way can lead to confusing bugs.
The copy Module’s copy() and deepcopy() Functions
Although passing around references is often the handiest way to deal with lists and dictionaries, if the function modifies the list or dictionary that is passed, you may not want these changes in the original list or dictionary value. For this, Python provides a module named copy that provides both the copy() and deepcopy() functions. The first of these, copy.copy(), can be used to make a duplicate copy of a mutable value like a list or dictionary, not just a copy of a reference.
>>>importcopy>>>spam=['A','B','C','D']>>>id(spam)139789337916608>>>cheese=copy.copy(spam)>>>id(cheese)# cheese is a different list with different identity. 139789337915776>>>cheese[1]=42>>>spam['A','B','C','D']>>>cheese['A',42,'C','D']
Now the spam and cheese variables refer to separate lists, which is why only the list in cheese is modified when you assign 42 at index 1.
If the list you need to copy contains lists, then use the copy.deepcopy() function instead of copy.copy() The deepcopy() function will these inner lists as well.
Projects
There are following project given in the book. Check their code at my GitHub.
A Short Program: Conway’s Game of Life
Conway’s Game of Life is an example of cellular automata: a set of rules governing the behavior of a field made up of discrete cells. In practice, it creates a pretty animation to look at. You can draw out each step on graph paper, using the squares as cells. A filled-in square will be “alive” and an empty square will be “dead.” If a living square has two or three living neighbors, it continues to live on the next step. If a dead square has exactly three living neighbors, it comes alive on the next step. Every other square dies or remains dead on the next step.
Four steps in a Conway’s Game of Life Simulation
Even though the rules are simple, there are many surprising behaviors that emerge. Patterns in Conway’s Game of Life can move, self-replicate, or even mimic CPUs. But at the foundation of all of this complex, advanced behavior is a rather simple program.
We can use a list of lists to represent the two-dimensional field. The inner list represents each column of squares and stores a '#' hash string for living squares and a ' ' space string for dead squares.
Comma Code
Say you have a list value like this:
spam=['apples','bananas','tofu','cats']
Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it. Be sure to test the case where an empty list [] is passed to your function.
Coin Flip Streaks
For this exercise, we’ll try doing an experiment. If you flip a coin 100 times and write down an “H” for each heads and “T” for each tails, you’ll create a list that looks like “T T T T H H H H T T.” If you ask a human to make up 100 random coin flips, you’ll probably end up with alternating head-tail results like “H T H T H H T H T T,” which looks random (to humans), but isn’t mathematically random. A human will almost never write down a streak of six heads or six tails in a row, even though it is highly likely to happen in truly random coin flips. Humans are predictably bad at being random.
Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails. Your program breaks up the experiment into two parts: the first part generates a list of randomly selected ‘heads’ and ’tails’ values, and the second part checks if there is a streak in it. Put all of this code in a loop that repeats the experiment 10,000 times so we can find out what percentage of the coin flips contains a streak of six heads or tails in a row. As a hint, the function call random.randint(0, 1) will return a 0 value 50% of the time and a 1 value the other 50% of the time.
You can start with the following template:
importrandomnumberOfStreaks=0forexperimentNumberinrange(10000):# Code that creates a list of 100 'heads' or 'tails' values. # Code that checks if there is a streak of 6 heads or tails in a row. print('Chance of streak: %s%%'%(numberOfStreaks/100))
Of course, this is only an estimate, but 10,000 is a decent sample size. Some knowledge of mathematics could give you the exact answer and save you the trouble of writing a program, but programmers are notoriously bad at math.
Character Picture Grid
Say you have a list of lists where each value in the inner lists is a one-character string, like this:
Think of grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin is in the upper-left corner, the x-coordinates increase going right, and the y-coordinates increase going down.
Copy the previous grid value, and write code that uses it to print the image.
Hint: You will need to use a loop in a loop in order to print grid[0][0], then grid[1][0], then grid[2][0], and so on, up to grid[8][0]. This will finish the first row, so then print a newline. Then your program should print grid[0][1], then grid[1][1], then grid[2][1], and so on. The last thing your program will print is grid[8][5].
Also, remember to pass the end keyword argument to print() if you don’t want a newline printed automatically after each print() call.