Python Crash Course Notes Link to heading
Content Link to heading
Basics Link to heading
Variable types Link to heading
- Strings
str - Integers
int- Long numbers can be written with underscores
_for readability.14_000_000_000
- Long numbers can be written with underscores
- Floats
float- Dividing ints will result in a float number
- Lists
list- Collection of items (ints, strs, floats, lists, etc)
.pop(),del x[x],.remove()remove items from a list, pop returns the value
- Tuples:
tuple- Immutable list of items (strings, ints, floats, tuples, lists, etc)
- Dictionaries
dict- Collection of key-value pairs items (strs, ints, floats, lists, tuples, dicts, etc)
Some more tips Link to heading
- Looking for documentation? No worries you can always run
python -m pydoc -band it will load your current environment packages and all its documentation in http://localhost:58261 - If you want to know more about a particular object you can always check it with help() or dir() functions. For example
help(list)ordir(list).- You can also read a brief description of whatever you have in
ipythontyping?or??after any variable or function. For examplelist?orlist??.
- You can also read a brief description of whatever you have in
- You can also check what are the current built-ins available for you with:
help(__builtins__)ordir(__builtins__).- Common builtin functions:
input,range,type,randommodule,open,
- Common builtin functions:
Control flow structures Link to heading
Those are: if, elif, else, for, while, break, continue, pass, with.
- To break the while loop, you can use a flag variable or the break statement.
Functions and arguments Link to heading
Classes and objects Link to heading
__init__()special method that runs on each obj instantiationselfparameter is a reference of the instance itself, always goes first in the method definition- Inheritance:
class ChildClass(ParentClass):...- The init function has to invoke the parent’s class init function:
super().__init__(foo, bar, baz) - A class parameter can also be another object which has parameters and functions
- The init function has to invoke the parent’s class init function:
Files and Exceptions Link to heading
- We can write to files, also read, append, etc. 🦆
Exceptions
- TODO: What kind of Exceptions we should care?: FilenoError, FileNotFoundError, ZeroDivisionError, etc
try,except,else,finallyblocks- You can check all available exceptions by:
import builtinsthen[exc for exc in dir(builtins) if 'Error' in exc or 'Exception' in exc] - Then check the exception class:
help(FileNotFoundError)
- You can check all available exceptions by:
Testing your code Link to heading
- Usage of
unittestmodule
