D335 Python Cheat Sheet
D335 Python Cheat Sheet
Id=kA00c000001DYibCAG
C859/D335: Course Tips | Webinar Recordings & Supplemental Videos | Study Guide |
Pacing Guide | How to Pass | Python 'CheatSheet'
statement = "I love Python. Working with Python is amazing because, hello, it's Python!"
countPython = statement.count('Python')
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 2/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
name = 'John Jacob Jingleheimer Schmidt'
name[0:4] # slice a string
username = "Connie"
code = "C859"
userMessage = "Welcome to {}, {}.".format(code, username)
print( userMessage )
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 4/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
# note that sets aren't nearly as important to the OA as strings, lists, and dictionaries
idNums = set() # creates a new set, empty
idNums.add(12345) # adds value to set
idNums.add(12367) # adds second value to set
idNums.add(12367) # adds third value to set, but it is duplicate so it will not be added
idNums.discard(12345) # removes value from set, if found
set(myNumbers) # converts the list from line 91 above to a set
list(idNums) # converts the set from line 121 into a list
sum = 0
for num in range(1,5): # runs loop for numbers 1 through 4, as range is exclusive of the ending
position
sum += num
startValue = 0
while startValue < 20:
startValue += 1
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 5/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
if startValue == 4: # if statement checks for a specific value
break # stops the flow of the while loop prematurely
print(startValue) # it's 4!
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 6/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
f1.write('hello, friend') # writes to file
f1.readlines() # returns a list of strings, with each line of the file as one string entry
f1.close() # closes file
# csv
# reading csv and tsv files without the csv module, using plain old IO/filestream methods, is fine...
# with open("filename.txt", "r") as f:
# # when reading, I grab contents and get out of the open block
# contents = f.read() # whole file as one big string
# contents = f.readlines() # a list of line by line strings, WILL have "\n" at the end of each
# but using csv makes it a little easier...
import csv
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 7/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
# the reader() method is the only one I suggest knowing
contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t") for .tsv files... I like to recast the reader
as a list.
# does one more step for us... makes a list (since we recast it) of lists instead of the list of strings
we get from readlines()
#[
# ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30'],
# ["2", "Bob"...],
# ...
#]
print(contents)
# other Standard Library modules that are great but not really needed for the exam:
# random
# string (yes, there's a string module beyond the standard str data type)
# datetime
# os
# regex
# webbrowser
# similarly, useful 3rd Party libraries that are good to know out there in the real world... but NOT
NEEDED for exam:
# pip install pytz # used to install package pytz
# import pytz
# imports the package
# beautiful soup - https://www.crummy.com/software/BeautifulSoup/ - parsing HTML and XML
# NumPy - http://www.numpy.org/ - scientific computing, multi-dimensional arrays and matrices
# pandas - http://pandas.pydata.org/ - data manipulation and analysis
# pillow - https://python-pillow.org/ - work with and manipulate images
# pyglet - http://www.pyglet.org/ - multimedia library for developing games
# pytz - http://pytz.sourceforge.net/ - works with time zone data
C859/D335: Course Tips | Webinar Recordings & Supplemental Videos | Study Guide | Pacing Guide |
How to Pass | Python 'CheatSheet'
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 8/9
10/2/24, 5:59 PM srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
Article Link:
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG
https://srm--c.vf.force.com/apex/coursearticle?Id=kA00c000001DYibCAG 9/9