Esprii Chapman – Notes on Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming (Book 1 of 5)

Chapter 1: Intro (page 3) 

– Contains info on how to install python 2.7/.8 and a list of libraries that already come native with most IDEs (page 3-17)

Chapter 2: Data Types and Variables (page 19) 

– Python has a built in function for capitalizing all words in a string: .Title(). (page 24) 

Otherwise it can upper or lower entire strings with .upper() or .lower(). (page 24) 

– Python uses + instead of .. to connect strings to variables. (page 25) 

– Python uses something similar to lua to put whitespace or new lines in: (page 26) 

\n – newline; \t – tab. (page 26) 

– .rstrip(), .lstrip(), and .strip() will remove spaces from the left and right ends and whole string accordingly. (page 27) 

– The simple math functions are changed as well: (page 30) 

+ – * / as usual but ** for putting a power on a number instead of math.power(). (page 30) 

– Only data type difference from Lua is that floats are handled differently than integers in Python. (page 31) 

If you want an integer to be a float in a math equation such as 3/2, you need to write it 3.0/2 or 3/2.0 or 3.0/2.0 (so that it doesn’t round down). (page 31) 

– When printing an int or float, you need to first convert it to a string using str(int). (page 32) 

– Comments are written with a # in front of them. (page 33)

Chapter 3: Lists (page 37) 

– Unlike Lua, Python will print everything associated with a list variable (square brackets, commas, and apostrophes). (page 38) 

– You can still print specific things inside a list by using list[int]. (page 38) 

– Lua is a weird “start at 1” language, in Python lists start at 0 like a normal programming language. (page 38) 

– You can always access the last things in a list by doing list[-1] and continue reading it backwards by continuing in subtracting order (e.g list[-2], list[-3], etc). (page 39) 

– One can still manipulate anything in a list like a string. (page 39) 

– list.append(string) will add a string to the end of a list. (page 41) 

– list.insert(int, string) will add a string to that position in a list pushing everything after it back. (page 42) 

– del list[int] will delete the string at that position in the list. (page 43) 

– var = list.pop() will assign the last string in a list to its own variable. (page 44) 

– list.remove(string) will search for and remove the first string in the list that matches the one entered. (page 45) 

– list.sort() will sort a list alphabetically. (page 47) 

list.sort(reverse=True) will sort in reverse. (page 48) 

These both will change a list permanently though. (page 48) 

– sorted(list) will sort it temporarily (useful in printing a sorted list). (page 48) 

– list.reverse() will reverse the current order a list is in. (page 49) 

– len(list) will get the length of a list. (page 49)

Chapter 4: Working with Lists (page 53) 

– for var in list: is the start to any loop that repeats for 1 fewer times than there are items on the list. (page 54) 

– range(int,int) will create a range of whole numbers between the first and last entered. (page 61) 

Using this with a for value in range(int,int) will count up to one less than the last int provided. (page 61) 

The same goes even if it’s for value in range(1,11,2) which will count up by 2 to 10. (page 61) 

– min(list) with a list of numbers will return the lowest value. (page 63) 

– max(list) with a list of numbers will return the highest value. (page 63) 

– sum(list) with a list of numbers will return the sum of all values. (page 63) 

– One can print a range of strings in a list by doing print(list[int:int]) or print(list[:int]) to print from the beginning up to that string ID in the list. (page 65) 

You can also do this with print(list[-int:]) to print the last items on your list. (page 66) 

– Explanation and examples of tuples. (pages 69-71)

Chapter 5: If Statements (page 75) 

– No then on the end of if statements, its ended instead with a :. (page 76) 

– Most boolean operations/checking for equality is the same with the exception of != instead of ~=. (page 78) 

– in can check through lists and return a boolean by doing ‘string’ in list_strings. (page 81) 

– More on boolean expressions info on page 81 

– if and else are the same as Lua, however elseif in Lua is shortened to elif in Python. (pages 84-85) 

Additionally, their respective statements all end in : instead of then. (pages 84-85) 

– Make sure to always use int() and str() when comparing things, especially if a user inputs them. 

– You can check if a list has something in it by using if list: and then else:. (page 91)

Chapter 6: Dictionaries (page 95) 

– Dictionaries are like lists that contain 2 or more pieces of information that are linked to each other, for example alien = {‘color’: ‘green’, ‘points’: 5} (page 96) 

You can reference either or by doing dict[‘key’] (page 96-100) 

You can also change the value assigned to the key by doing dict[‘key’] = ‘new_value’ (page 96-100) 

– Deleting key value pairs by doing del dict[‘key’] (page 100) 

– As in any language you can easily format longer lists by adding two enters between the {} (page 101) 

– You can reference a key and its value in a for loop by doing for key, value in dictionary.items(): (page 104-105) 

Same goes for if you want to specifically reference keys for values: for keys in dictionary.keys(): or for values in dictionary.values(): (page 106-107) 

– print()ing a dictionary will print the entire dictionary; brackets, apostrophes, and all (page 109) 

– You can nest lists into dictionaries just like you were creating a list but inside a dictionary (ex. dict = {‘list’: [‘item_1’, ‘item_2’], ‘other_variable’}) (page 111) 

Same method to put a dictionary in a dictionary (page 113)

Chapter 7: User Input and While Loops (page 117) 

– Assign a user’s input to a variable by using variable = input(“optional string to display before the text input: “) (page 118) 

It will only capture the user’s input, not the string written into the code. 

– Add strings to pre-existing variables using variable = “blank” then variable += “\nNew blank” (page 119) 

– Display strings under input through variables variable = “blank string” then userinput = input(variable) (page 119) 

– Convert an input string to an int using userinput_int = int(userinput) (page 119) 

– The modulo operator divides one number by another and then returns the remainder by doing number_1 % number_2 (page 120) 

– while can be used to repeat an action until something else is completed inside the loop (ex. counting up to x or y) (page 122) 

– Flags are variables assigned to boolean values that can be used in a while loop to check if it should be running or not (page 124) 

A break does the same thing to end a while loop. 

– continue can be used in conjunction with a if statement to continue a while loop (page 126) 

If needed, example on page 126

– Simple infinite loops are made by doing while True: (page 127) 

– while list: will repeat until a list is empty (page 128) 

– while ‘item’ in list: can be used to run through a list and remove ‘item’ from it whenever it appears (page 129)

If needed, example on page 129

Chapter 8: Functions (Page 133) 

– def function_name(): is used to define a function that can be called later (page 134) 

– Putting variables within the parenthesis of a function such as def function_name(variable): will allow a variable that can be called within the function to be applied on use of the function by doing function_name(input_variable) (page 134) 

Any number of variables can be put between the parenthesis, separated by , (page 136) 

– By writing def function(variable=’default_variable’), it sets variable to, by default, return ‘default_variable’ (page 138) 

Defaults can be set as anything a normal variable could be set as (page 138) 

Defaults mean that no input is required when calling the function (page 138) 

– return can be used to return updated values from a function (page 142)

If needed, example on page 142

– Make a function variable optional by setting it by default to equal ” and then checking with an if statement if it equals anything more (page 143) 

– return can also be used to return a dictionary, but like before, don’t print a dictionary as it will print the entire thing raw (page 144) 

– Functions can be used anywhere in a code block as long as they are parented to the file and not to a specific chunk (page 146) 

– Functions can easily modify lists and dictionaries if they are assigned to their own variables and referenced from a function variable (page 148) 

– Functions can be prevented from modifying a list by using a variable that has a [:] after it (page 150)

– *variable can be used in place of a set number of variables to incorporate any number of variables provided. (page 151) 

This can also be combined with regular variables. (page 152) 

– **variable can be used to build a dictionary from an arbitrary number of keys and values. (page 153) 

– If two files are made under the same directory, functions can be written in one and then use in the other by using the import name_of_file and then name_of_file.defined_function() (page 155) 

import can be used with an alias to import a module under a certain alias such as import module as m (page 156) 

from can be used to import specific functions from modules in the same directory. (page 156)

If needed, example on page 156.

Aliases can also be given to functions by using from module import function as alias (page 156) 

Import all functions from a module by using from module import *

Chapter 9: Classes (Page 161) 

– Classes are a way to store data to use in functions defined within the class. (page 161) 

All objects within the class normally share a common way of acting or thinking. (page 161) 

They simulate importing functions from another file, but they are in the same file as the rest of the code. (page 161) 

– Create a class with class class_name(): or class class_name: (page 162) 

– Define basic variables much like variables in a function using the __init__() function. (page 163) 

__init__() is a function that will define itself without being called right at the beginning of when a script is ran. (page 163) 

__init__() will always have self in the parenthesis and is used to define extra variables beyond that, ex. __init__(self, variable_1, variable_2) and then indented on a new line self.variable_1 = variable_1 and again self.variable_2 = variable_2 (page 163) 

Any variables defined in the __init__() function can be called using self.variable in any following functions. (page 163) 

When using a class it needs to be assigned to a variable which will then act as a module to call any functions inside. (page 162) 

– def function() can be defined in the class and will already have access to any pre-existing information defined by the __init__() function. (page 162) 

– Once a class is defined under a variable, the self module name is turned into the variable name assigned. (page 164)

– A class can be used to define multiple different variables. (pages 162-165)

If needed, example on pages 162-165. 

– Just like a function, default variables can be set using the __init__() method. (page 168) 

– When defining other functions/methods, more variables can be assigned to them by adding them after (self, ) (page 169) 

– Functions within classes can be used to update any variables called by the __init__() method. (page 169) 

– A child class can be made by using class child_class_name(parent_class_name): (page 172) 

A specific operation can be used on child classes under the __init__() method called super().__init__(self, var1, var2) to initialize all of the variables the parent class has to start out with. (page 173-174) 

Extra variables can be defined after the super() method (page 174) 

That class’s own functions can be defined but it will inherit its parent’s functions as well. (page 175-176) 

– Just like functions, classes can be imported by section. See previous chapter’s notes for reference. (page 181)

Citation: Matthes, Eric. Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming. San Francisco, No Starch Press, 2015.

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s