PY0101 - Python For Data Science, AI, & Development Cheat Sheet
PY0101 - Python For Data Science, AI, & Development Cheat Sheet
collection = [1, 1, 3.12, False, "Hi"] tup = (1, 3.12, False, "Hi")
String
Series of characters or data stored as text
string = "Hello" List Operations
Changeble collection of objects
Comparison Operators
Comparison Operators compare operands and return a result of true or false
# returns the length of a list
number_collection = [1,2,3,4.5]
set([1,1,2,3])
Conditional Operators
Conditional Operators evaluate the operands and produce a true of false result
# Add an item to the set
Dictionary a.add(4)
Changeble collection of key-value pairs And - returns true if both statement a and b are true otherwise false
,
dictionary.values() a.issubset(b)
# Returns True if b is a superset of a, false otherwise
b.issuperset(a)
PY0101 – Python for Data Science, AI, & Development Cheat Sheet Page 2
soup = BeautifulSoup(html, ' html 5 lib ' ) # R eturns the file name
for x in iterable:
file . name
# Executes loop for each object in an iterable like a string, tuple, # R eturns formatted htm l
# R eturns the mode the file was opened i n
soup . prettify()
list, or set file . mode
file . read()
while statement:
# Executes the loop while statement is true # F ind all instances of an html ta g
soup . find_all(tag)
# Reads a certain number of characters of a file
file . read(characters)
Conditional Statements
Requests # Read a single line of a file
file . readline()
if statement_1:
elif statement_2:
import re q uests file . readlines()
# Execute if statement_1 is false and statement_2 is true
else:
Try/Except response . ur l
Writing to a File
# G et the status code of the response
response . status_code
# Opens a file in write mode
except a:
# G et the body of the re q uest s
file . write(content)
except b:
# G et the headers of the response
except:
# G et the content of the response in tex t
# Adds content to the end of a file
# Code to execute if there is any exception that have not been response . tex t
file . append(content)
handeled above
# G et the content of the response in jso n
else:
response . json
# Code to execute if there is nto exception
Range
# Send a post re q uests to the url with optional parameter s
range(y)
IndexError - When an index is out of rang
NameError - When a varaible name is not foun Produce an interable sequence from x to y-1
SyntaxError - When there is an error with how the code is written
range(x, y)
ZeroDivisionError - When your code tries to divide by zero