Python Cheat Sheet - The Basics Coursera
Python Cheat Sheet - The Basics Coursera
Chan g eable collection of ob ects j Accessin g data from a strin g , list, or tuple usin g an element number
String
my collection
_ = [1, 1, 3 12, False, . "H i ] "
my_string [ element_number ]
L ist Operations
my_tup [ element_number ]
my_string = "Hello"
Slicing
len(my_collection)
String Operations
Accessin g a subset of data from a strin g , list, or tuple usin g element numbers from start to stop -1
# returns the string with all uppercase letters
my_string.upper()
my_collection.extend( [ M " ore", " I tems" ] )
my_collection [ start : stop ]
len(my_string)
my_collection.append(" S ingle")
Co m parison Operators
# returns the index of the first instance of the string inside the
del(my_collection [ ] 2 )
my_string.find('l') Eq ual
# Clone a lis t
a == b
# replaces any instance of the first string with the second in my_string
a < b
Integer
my_collection_2 = [ "a", "b", "c" ]
A whole number
my_collection_3 = my_collection + my_collection_2 G reater T han
my_integer = 12321
a > b
number_collection = [ 1,2,3, 4 5]
sum(number_collection)
Float
a > = b
A decimal number
my decimal
_ = 3 14.
item in my_collectio n
not in
N Eq
item my_collection
Boolean ot ual
a ! = b
a = True
Set
a = { 00
1 , 3.12, F alse, " B ye" }
Python Operators
Dictionary
b = { 00
1 , 3.12, " W elcome" }
Chan g eable collection of k -
ey value pairs
+: Additio
-:
{ : : 0 0 : } Set Operations Subtractio
my_dictionary = 'banana' 1, 12 'laptop', ( , ) 'center'
*: M ultiplicatio
A t
A v k y
# dd an item to the se
4
d
# ccess alue using e
a.add( )
my_dictionary. k eys()
-
R b
And returns true if both statement a and b are true, otherwise false
# eturns set a minus
a.difference(b)
my_dictionary. v alues()
# R eturns intersection of set a and b
O - r returns true if either statement a or b are true, otherwise false
a.intersection(b)
a or b
U nchan g eable collection of ob ects j a.union(b) N - ot returns the opposite of the statement
a.issubset(b)
a.issuperset(b) Page 1
soup = B eautiful S oup(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 . prettif y ()
list, or set file . mode
file . read()
while statement:
# Executes the loop while statement is true # F ind all instances of an HTML tag
soup . find_all(tag)
# Reads a certain number of characters of a file
file . read(characters)
Conditional Statements
Re qu ests
# Read a single line of a file
if statement_1:
file . readline()
# Execute of statement_1 is true
# Import the re q uests librar y
elif statement_2:
import re q uests
# Read all the lines of a file and stores it in a list
file . readlines()
else:
file . close()
Try/Except # Get the url of the response
response . ur l
except a:
response . re q uest . header s
file = open(file_name, "w")
# Code to execute if there is an error of type a
# Get the body of the requests
except b:
response . header s
# Code to execute if there is any exception that has not been handled
else:
response . tex t
# Adds content to the end of a file
SyntaxError - When there is an error with how the code is written class class_name:
# code to execute
return optional_output
return optional_output
range(stop)
# Calling a function
# Create an instance of a class
Page 2
© Copyright IBM Corporation 2021. All rights reserved.