Running and Quitting
|
|
Variables and Assignment
|
Use variables to store values.
Use print to display values.
Variables persist between cells.
Variables must be created before they are used.
Variables can be used in calculations.
Use an index to get a single character from a string.
Use a slice to get a substring.
Use the built-in function len to find the length of a string.
Python is case-sensitive.
Use meaningful variable names.
|
Data Types and Type Conversion
|
Every value has a type.
Use the built-in function type to find the type of a value.
Types control what operations can be done on values.
Strings can be added and multiplied.
Strings have a length (but numbers don’t).
Must convert numbers to strings or vice versa when operating on them.
Can mix integers and floats freely in operations.
Variables only change value when something is assigned to them.
|
Lists
|
A list stores many values in a single structure.
Use an item’s index to fetch it from a list.
Lists’ values can be replaced by assigning to them.
Appending items to a list lengthens it.
Use del to remove items from a list entirely.
The empty list contains no values.
Lists may contain values of different types.
Character strings can be indexed like lists.
Character strings are immutable.
Indexing beyond the end of the collection is an error.
|
Tuples & Dictionaries
|
A tuple is a list that can never be changed.
Use an item’s index to fetch it from a tuple.
Dictionaries are lists in which the semantic meaning of each entry is more important than its order.
New entries can be added to an existing dictionary by assignment.
Dictionary keys are unique - only one item can exist per key
Use del to remove items from a list entirely
You can access all the keys and all the values of a dictionary
Collections (lists, tuples, dictionaries) may be nested arbitrarily
Code clarity & maintainability should guide choice of collection type
|
For Loops
|
A for loop executes commands once for each value in a collection.
The first line of the for loop must end with a colon, and the body must be indented.
Indentation is always meaningful in Python.
A for loop is made up of a collection, a loop variable, and a body.
Loop variables can be called anything (but it is strongly advised to have a meaningful name to the looping variable).
The body of a loop can contain many statements.
Use range to iterate over a sequence of numbers.
The Accumulator pattern turns many values into one.
|
Conditionals
|
Use if statements to control whether or not a block of code is executed.
Conditionals are often used inside loops.
Use else to execute a block of code when an if condition is not true.
Use elif to specify additional tests.
Conditions are tested once, in order.
Create a table showing variables’ values to trace a program’s execution.
|
Creating Functions
|
Define a function using def function_name(parameter) .
The body of a function must be indented.
Call a function using function_name(value) .
Numbers are stored as integers or floating-point numbers.
Integer division produces the whole part of the answer (not the fractional part).
Variables defined within a function can only be seen and used within the body of the function.
If a variable is not defined within the function it is used, Python looks for a definition before the function call
Use help(thing) to view help for something.
Put code whose parameters change frequently in a function, then call it with different parameter values to customize its behavior.
|
Errors and Exceptions
|
Tracebacks can look intimidating, but they give us a lot of useful information about what went wrong in our program, including where the error occurred and what type of error it was.
An error having to do with the ‘grammar’ or syntax of the program is called a SyntaxError . If the issue has to do with how the code is indented, then it will be called an IndentationError .
A NameError will occur if you use a variable that has not been defined, either because you meant to use quotes around a string, you forgot to define the variable, or you just made a typo.
Containers like lists and strings will generate errors if you try to access items in them that do not exist. This type of error is called an IndexError .
Trying to read a file that does not exist will give you an FileNotFoundError . Trying to read a file that is open for writing, or writing to a file that is open for reading, will give you an IOError .
|
Live Coding Shielding Problem
|
|