python 3 cheat sheet code example
Example 1: python cheat sheet
Best Python Cheat Sheet PDF:
https://websitesetup.org/wp-content/uploads/2020/04/Python-Cheat-Sheet.pdf
Example 2: python cheat sheet
print('Hello World')
string_name = "This is a string"
string_name[5:]
integer_name = 4
float_name = 3.0
list_name = ['a','b','cats','dogs', 1, 2.0]
list_name.append(3)
list_comp = [x for x in list_name if type(x) is int]
dic = {"Bob":9766692343,"Alice":6123336678}
dic["Alice"]
dic_phone_numbers = dic.values()
dic_names = dic.keys()
class Cat:
def __init__(self,name):
self.name = name
self.lives = 9
print(f"{name} is a cat!")
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
Example 3: cheat sheet python
>>> _spam = 'Hello'
Example 4: cheat sheet python
def foo():
"""
This is a function docstring
You can also use:
''' Function Docstring '''
"""
Example 5: cheat sheet python
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Example 6: cheat sheet python
>>> spam = 'Hello'
>>> spam
'Hello'