Last active
October 26, 2020 14:37
-
-
Save barthelemypousset/7a7854a2b5cefbcc8a39a4c021820021 to your computer and use it in GitHub Desktop.
Python cheatsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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