This cheatsheet is based on Python Mega Course: Learn Python in 60 Days, Build 20 Apps by Ardit Sulce
Python Data Types
Integers are used to represent whole numbers:
rank = 10Floats represent decimal numbers:
eggs = 12
people = 3
temperature = 10.2Strings represent text:
rainfall = 5.98
elevation = 1031.88
message = "Welcome to our online shop!"Lists represent arrays of values that may change during the course of the
name = "John"
serial = "R001991981SW"
members = ["Sim Soony", "Marry Roundknee", "Jack Corridor"]Dictionaries represent pairs of keys and values:
pixel_values = [252, 251, 251, 253, 250, 248, 247]
phone_numbers = {"John Smith": "+37682929928", "Marry Simpons": "+423998200919"}
volcano_elevations = {"Glacier Peak": 3213.9, "Rainer": 4392.1}
Keys of a dictionary can be extracted with:
phone_numbers.keys()
Values of a dictionary can be extracted with:phone_numbers.values()
Tuples represent arrays of values that are not to be changed during the coursevowels = ('a', 'e', 'i', 'o', 'u')You can get a list of attributes of a data type has using:
one_digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
dir(str)You can get a list of Python builtin functions using:
dir(list)
dir(dict)
dir(__builtins__)
You can get the documentation of a Python data type using:help(str)
help(str.replace)
help(dict.values)
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
0 1 2 3 4 5 6
And they have a negative index system as well:
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]In a list, the 2nd, 3rd, and 4th items can be accessed with:
-7 -6 -5 -4 -3 -2 -1
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]First three items of a list:
days[1:4]
Output: ['Tue', 'Wed', 'Thu']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]Last three items of a list:
days[:3]
Output:['Mon', 'Tue', 'Wed']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]Everything but the last:
days[-3:]
Output: ['Fri', 'Sat', 'Sun']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]Everything but the last two:
days[:-1]
Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]A dictionary value can be accessed using its corresponding dictionary key:
days[:-2]
Output: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
phone_numbers = {"John":"+37682929928","Marry":"+423998200919"}
phone_numbers["Marry"]
Output: '+423998200919'
Functions and Conditionals
def cube_volume(a):Write if-else conditionals:
return a * a * a
message = "hello there"Write if-elif-else conditionals:
if "hello" in message:
print("hi")
else:
print("I don't understand")
message = "hello there"
if "hello" in message:
print("hi")
elif "hi" in message:
print("hi")
elif "hey" in message:
print("hi")
else:
print("I don't understand")
else:
print("No")
Use the and operator to check if both conditions are True at the same time:
x = 1
y = 1
if x == 1 and y==1:
print("Yes")
x = 1Check if a value is of a particular type with isinstance:
y = 2
if x == 1 or y==2:
print("Yes")
else:
print("No")
isinstance("abc", str)or directly:
isinstance([1, 2, 3], list)
type("abc") == str
type([1, 2, 3]) == list
name = input("Enter your name: ")
The input function converts any input to a string, but you can convert it back toexperience_months = input("Enter your experience in months: ")
experience_years = int(experience_months) / 12
name = "Sim"
experience_years = 1.5
print("Hi {}, you have {} years of experience".format(name, experience_years))
Output: Hi Sim, you have 1.5 years of experience
for letter in 'abc':Output: A B C
print(letter.upper())
phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}
for value in phone_numbers.keys():
print(value)
John Smith Marry Simpsons
You can loop over dictionary values:phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}Output:
for value in phone_numbers.values():
print(value)
phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}Output:
for key, value in phone_numbers.items():
print(key, value)
while datetime.datetime.now() < datetime.datetime(2090, 8, 20, 19, 30, 20):The loop above will print out the string inside print() over and over again until
print("It's not yet 19:30:20 of 2090.8.20")
[i*2 for i in [1, 5, 10]]
Output: [2, 10, 20][i*2 for i in [1, -2, 10] if i>0]
Output: [2, 20][i*2 if i>0 else 0 for i in [1, -2, 10]]
Output: [2, 0, 20]def volume(a, b, c):Functions can have default parameters (e.g. coefficient ):
return a * b * c
def converter(feet, coefficient = 3.2808):Output: 3.0480370641306997
meters = feet / coefficient
return meters
print(converter(10))
def volume(a, b, c):An args parameter allows the function to be called with an arbitrary
return a * b * c
print(volume(1, b=2, c=10))
def find_max(*args):Output: 1001
return max(args)
print(find_max(3, 99, 1001, 2, 8))
with open("file.txt") as file:You can create a new file with Python and write some text on it:
content = file.read()
with open("file.txt", "w") as file:You can append text to an existing file without overwriting it:
content = file.write("Sample text")
with open("file.txt", "a") as file:You can both append and read a file with:
content = file.write("More sample text")
with open("file.txt", "a+") as file:
content = file.write("Even more sample text")
file.seek(0)
content = file.read()
Python Modules
Builtin objects are all objects that are written inside the Python interpreter in C language. Builtin modules contain builtins objects. Some builtin objects are not immediately available in the global namespace. They are parts of a builtin module. To use those objects the module needs to be imported first. E.g.:
import time
time.sleep(5)
A list of all builtin modules can be printed out with:
import sysStandard libraries is a jargon that includes both builtin modules written in C and also modules written in Python. Standard libraries written in Python reside in the Python installation directory as .py files. You can find their directory path with sys.prefix. Packages are a collection of .py modules. Third-party libraries are packages or modules written by third-party persons (not the Python core development team). Third-party libraries can be installed from the terminal/command line:
sys.builtin_module_names
Windows:
pip install pandas
if that does not work, then use:
python -m pip install pandas
Mac and Linux:
pip3 install pandas
if that does not work, then use:
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.