Series of characters or data stored as text my_string = “Hello”
my_string.upper()
len(my_string)
my_string.find('l')
my_string.replace('H', 'C')
A whole number
my_integer = 12321
A decimal number
my_decimal = 3.14
Discrete value true or false
a = True
b = False
Changeable collection of key-value pairs
my_dictionary = {'banana': 1, 12: 'laptop', (0,0):'center'}
my_dictionary['banana']
my_dictionary.keys()
my_dictionary.values()
Unchangeable collection of objects
tup = (1, 3.12, False, "Hi")
Changeable collection of objects
my_collection = [1, 1, 3.12, False, "Hi"]
len(my_collection)
my_collection.extend(["More", "Items"])
my_collection.append("Single")
del(my_collection[2])
clone = my_collection[:]
my_collection_2 = ["a", "b", "c"]
my_collection_3 = my_collection + my_collection_2
number_collection = [1,2,3,4.5]
sum(number_collection)
item in my_collection
item not in my_collection
Unordered collection of unique objects
a = {100, 3.12, False, "Bye"}
b = {100, 3.12, "Welcome"}
my_set = set([1,1,2,3])
a.add(4)
a.remove("Bye")
a.difference(b)
a.intersection(b)
a.union(b)
a.issubset(b)
a.issuperset(b)
Accessing data from a string, list, or tuple using an element number
my_string[element_number]
my_collection[element_number]
my_tup[element_number]
Accessing a subset of data from a string, list, or tuple using element numbers from start to stop -1
my_string[start:stop]
my_collection[start:stop]
my_tup[start:stop]
Comparison 0perators compare operands and return a result of true or false
a == b
a < b
a > b
a >= b
a <= b
a != b
+
: Addition-
: Subtraction*
: Multiplication/
: Division//
: Integer Division (Result rounded to the nearest integer)Conditional 0perators evaluate the operands and produce a true of false result
Returns true if both statement a and b are true, otherwise false
a and b
Returns true if either statement a or b are true, otherwise false
a or b
Returns the opposite of the statement
not a
for x in range(x):
Executes loop x number of times
for x in iterable:
Executes loop for each object in an iterable like a string, tuple, list, or set
while statement:
Executes the loop while statement is true
if statement_1:
# Execute of statement_1 is true
if statement_2:
# Execute if statement_1 is false and statement_2 is true
else:
# Execute if all previous statements are false
try:
# Code to try to execute
except a:
# code to execute if there is an error of type a
except b:
# code to execute if there is an error of type b
except:
# code to execute if there is any exception that has not been handled
else:
# code to execute if there is no exception
IndexError - When an index is out of range
NameError - When a variable name is not found
SynntaxError - When there is an error with how the code is written
ZeroDivisionError - When your code tries to divide by zero
range(stop)
range(start, stop, step)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html5lib')
soup.prettify()
soup.find(tag)
soup.find_all(tag)
import requests
response = requests.get(url, parameters)
response.url
response.status_code
response.request.headers
response.request.body
response.headers
response.text
response.json()
requests.post(url, parameters)
def function_name(optional_parameter_1, optional_parameter_2):
# code to execute
return optional_output
output = function_name(parameter_1, parameter_2)
file = open(file_name, "r")
file.name
file.mode
file.read()
file.read(characters)
file.readline()
file.readlines()
file.close()
file = open(file_name, "w")
file.write(content)
file.append(content)
class class_name:
def __init__(self. optional_parameter_1, optional_parameter_2)
self.attribute_1 = optional_parameter_1
self.attribute_2 = optional_parameter_2
def method_name(self, optional_parameter_1):
# Code to execute
return optional_output
object = class_name(parameter_1, parameter_2)
object.method_name(parameter_3)