0% found this document useful (0 votes)
96 views

PY0101 - Python For Data Science, AI, & Development Cheat Sheet

This document provides a cheat sheet summarizing key Python data types, operations, and concepts for data science, AI, and development. It covers fundamental data types like strings, integers, floats, Booleans, lists, tuples, dictionaries, and sets. It also describes common operations for these types like indexing, slicing, concatenation, membership testing, and more. The document then discusses loops, comparison operators, logical operators, and working with files and web scraping in Python.

Uploaded by

Manju Kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

PY0101 - Python For Data Science, AI, & Development Cheat Sheet

This document provides a cheat sheet summarizing key Python data types, operations, and concepts for data science, AI, and development. It covers fundamental data types like strings, integers, floats, Booleans, lists, tuples, dictionaries, and sets. It also describes common operations for these types like indexing, slicing, concatenation, membership testing, and more. The document then discusses loops, comparison operators, logical operators, and working with files and web scraping in Python.

Uploaded by

Manju Kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PY0101 – Python for Data Science, AI, & Development Cheat Sheet Page 1

Python Data Types List


Changeble collection of objects
Tuple
Unchangeble collection of objects

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

String Operations len(collection)


Equal
# returns the string with all uppercase letters

string.upper() # Add multiple items to a list


a == b
collection.extend(["More", "Items"])
Less Than
# replaces any instance of the first string with the second in the
string
# Add a single item to a list
a < b
string.replace('H', 'C') collection.append("Single")
Greater Than
# returns the index of the first instance of the string inside the # Delete the object of a list at a specified index
a > b
subject string, otherwise -1
del(collection[2])
string.find('l') Greater Than or Equal
# Clone a list

clone = collection a >= b


# replaces any instance of the first string with the second in the
string
Less Than or Equal
string.replace('H', 'C') # Concatenate two lists

collection_2 = ["a", "b", "c"]


a <= b
collection_3 = collection + collection_2 Not Equal
Integer a != b
A whole number # Calculate the sum of a list of ints or floats

number_collection = [1,2,3,4.5]

integer = 12321 sum(number_collection)


Python Operators
Float Set
A decimal number Unordered collection of unique objects +: Additio
decimal = 3.14 -: Subtractio
a = {100, 3.12, False, "Bye"}
*: Multiplicatio
b = {100, 3.12, "Welcome"}
/: divisio
//: Integer Division (Result rounded to neartest integer)
Boolean
Descrete value true or false Set Operations
a = True b = False # Convert a list to a set

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
,

# Remove an item from a set

dictionary = {'banana': 1, 12: 'apple', (0,0):'center'} a.remove("Bye") a and b


# Returns set a minus b
Or - returns true if either statement a or b are true , otherwise false
Dictionary Operations a.difference(b)
a or b
# Access value using key
# Returns intersection of set a and b

Not - returns the opposite of the statement


a.intersection(b)
dictionary['banana']
not a
# Returns the union of set a and b

# Get all keys in a dictionary as a list


a.union(b)
dictionary.keys()
a = {1, "a"}

b = {1, "a", "b", "c", 3.14, True}

# Get all values in a dictionary as a list


# Returns True if a is a subset of b, false otherwise

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

Loops Webscraping Wor k ing w ith Files

For Loops # Import BeatifulSoup


Reading a File
from bs4 import BeautifulSoup
for x in range(x):
# Opens a file in read mode

# Executes loop x number of times # P arse HTML stored as a strin g


file = open(file_name, "r")

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

# F ind the first instance of an html ta g

While Loops soup . find(tag) # Reads the contents of a file

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:

# Execute of statement_1 is true


# Import the re q uests librar y
# Read all the lines of a file and stores it in a list

elif statement_2:
import re q uests file . readlines()
# Execute if statement_1 is false and statement_2 is true

else:

# Send a get re q uests to the url with optional parameter s


# Closes a file

# Execute if all previous statements are false


response = requests.get(url, parameters) file . close()

# Get the url of the response

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

# G et the headers of the re q ues t


file = open(file_name, "w")
try:

# Code to try to execute


response . re q uest . header s

except a:
# G et the body of the re q uest s

response . re q uest . bod y

# Writes content to a file

# Code to execute if there is an error of type a

file . write(content)
except b:
# G et the headers of the response

# Code to execute if there is an error of type b


response . header s

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

re q uests . post(url, parameters)


Error Types
Produce an iterable sequence from 0 to y-1

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

You might also like