Skip to content

Latest commit

 

History

History
66 lines (28 loc) · 1.14 KB

File metadata and controls

66 lines (28 loc) · 1.14 KB

Fun with Python Notes

Dictionaries

Delimited with curl braces

Contain comma separate key value pairs

Keys must be unique

Get values via keys

Key objects must be immutable

Values can be mutable

Order of dictionary is general random

Name construct dict to create a dictionary

dict.copy() to copy a dictionary

Pass a dict to the dict() construction to copy a dictionary

Use the update method to add values to your dictionary

Use the for key in dictionary to get the key of the dictionary

Exceptions

Try is where the code is. except is where the error is handle

Each try block can intercept many types of errors

Except can take a tuple for exceptions types

The pass keyword - check out

Add as e on the except statement to get the error

Exception objects can be converted to strings using the str construtor

Raise keyword re raises the exception that is being currently handled

Create exceptions by ValueError() creation. The function takes the error message.

Classes

Class name uses CamelCase

Can import classes from a module

Instance methods take self as an attribute

Self is analogous to this in C++ or Java