- Kevin Siswandi
- Antonio Ruiz
Outline for today:
- Lecture - Kevin
- IDE Demo - Tillmann
- OOP - Antonio
- Photoshoot - Joey & Leslie
For those of you who get > 85%, send me an email email@example.com to claim your digital copy of Competitive Programming, with the statement:
I agree not to share this book anywhere to anyone by any means.
Errors And Exceptions
There are two kinds of errors in Python:
- syntax errors
Syntax errors occur when Python can’t interpret our code, since we didn’t follow the correct syntax for Python. These are errors you’re likely to get when you make a typo, or you’re first starting to learn Python.
Exceptions occur when unexpected things happen during execution of a program, even if the code is syntactically correct. There are different types of built-in exceptions in Python, and you can see which exception is thrown in the error message.
- ValueError occurs when an object of the correct type but inappropriate value is passed in as input to a built-in operation or function
- AssertionError occurs when an assert statement fails.
- IndexError occurs when a sequence subscript is out of range
- KeyError occurs when a key cannot be found in a dictionary
- TypError occurs when an object of unsupported type is passed as an input to an operation or function
We can use try statements to handle exceptions. There are four clauses you can use (one more in addition to those shown in the video).
- try: This is the only mandatory clause in a try statement. The code in this block is the first thing that Python runs in a try statement.
- except: If Python runs into an exception while running the try block, it will jump to the except block that handles that exception.
- else: If Python runs into no exceptions while running the try block, it will run the code in this block after running the try block.
- finally: Before Python leaves this try statement, it will run the code in this finally block under any conditions, even if it's ending the program. E.g., if Python ran into an error while running code in the except or else block, this finally block will still be executed before stopping the program.
Third party libraries can be installed using
pip, which is the standard package manager for Python. An alternative is
conda. See pip vs conda
In terminal, you can't use your mouse for navigation, so you need to use some shortcuts for speed:
- CRTL + U = to delete the entire line
- CTRL + E = to move your cursor to the end of line
- CTRL + A = to move your cursor to the beginning of line
Of course, you can also always use the arrow keys for navigation.