Skip to content

Instantly share code, notes, and snippets.

@barthelemypousset
Last active October 26, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barthelemypousset/7a7854a2b5cefbcc8a39a4c021820021 to your computer and use it in GitHub Desktop.
Save barthelemypousset/7a7854a2b5cefbcc8a39a4c021820021 to your computer and use it in GitHub Desktop.
Python cheatsheet
## Python datatypes:
get a datatype: `type(object)`
get id of object: `id(object)`
### immutable:
int: 42
float: 4,2
str: "hello world"
bool: True/False
tuple: ("hello","world")
### mutable:
list: ["hello","world"]
dict: {1:hello,2:world}
set: {1,2,3}
## test for exceptions
Try:
action to test
except:
executed if error happen. Real errors, not if condition is false
---
Try:
action to test
except exception_type as returned_exeption: # used to catch only this error and print it
print("Error is :", returned_exeption)
else:
do something
---
try:
assert test = value #test a condition and create an "AssertionError" if False
except AssertionError
pass
finally:
This will always be executed at end of try
---
try:
if condition:
raise Type_of_error("exception_description")
except ValueError:
do something
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment