0% found this document useful (0 votes)
4 views29 pages

Isom3400 Cheatsheet

Uploaded by

tommyho012334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views29 pages

Isom3400 Cheatsheet

Uploaded by

tommyho012334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Topic1

Division

True/False: Boolean data type

Memory Location

Topic 2
abs() #return the absolute value Lists[] and Tuples()
len() #return the no. of elements in a list or tuple • The physical content of a list or a tuple consists of object
.count() #returns the total number of occurrences of x in s: references (i.e., the address of the memory location where the
Function object is allocated) rather than actual objects:
pow, dimod, isinstance • Both lists and tuples are nestable:

Slicing(*rmb ans include , in tuples)

Order

• The key option must be set to a function that can be called with a
single input value, regardless of whether it be of a primitive or
compound data type (e.g., len, str, max, et

• insert(index, object) takes an element and insert it at a particular


index. The return value is None (this applies to all following
methods unless explicitly mentioned):
• append(object) takes an element and adds it to the end of a list:
Remarks

Dictionaries
• a container that maps a set of labels called keys to a set of values.
The dictionary is not a sequence type.
• The association of a key and a value is called a key-value pair,
and a dictionary can be defined by using the {key: value} syntax:

• When used before a sequence inside a list or tuple display, *


unpacks its individual values:

List comprehensions

• Many of the operators and built-in functions that can be used with
sequences work with dictionaries as well. But they
operate primarily on keys of dictionaries:

• We can use the | operator to create a new dictionary by merging


two existing dictionaries:
To update a dictionary with entries from another dictionary, we can use
its compound assignment form |=, similar to +=, -=
• .get(<key>[, <default>]) returns the value for key if key is present,
else default(defaulting to None):
Sets
• A set is an unordered, mutable collection of distinct immutable elements.
• Non-empty sets can be created by using the curly brace notation ({}):

While loop
Range

Defining a Function

• The global namespace is the area where global variables


enumerate() function provides a simpler way to generate both indices (variables defined outside of any function) are stored.
and items for a loop. • Every time we call a function, Python creates a local
• takes an optional argument called start that lets us tweak the initial namespace and populates it with parameter names that refer to
value: arguments provided at the point of call.
• All the names assigned when a function runs also live in this local
namespace. They will only be accessible inside the function being
called.
• All the local variables exist only while the function runs.
Return Values

zip() takes one or more sequences as arguments and returns a series


of tuples that pair up parallel items taken from those sequences:

Try code that may raise error and want to gracefully handle exceptions
.index(find, starting index, ending index) **arfgs & **kwargs
• By definition, *args cannot have default values and cannot take
keyword arguments.
• Parameters after *args can take keyword arguments only;
Parameter before *args can take keyword arguments only if there
are no additional positional arguments intended for *args.
(positional arguments cannot come after keyword arguments)

• By definition, **kwargs cannot have default values and cannot take


positional arguments. It can only appear at the end of a parameter
list.
• **kwargs does not impose any restrictions on the type of
arguments that come before it.

We can put * before a parameter to indicate that it can take


a variable sequence of positional arguments and pack them into a
tuple

/ : all parameters before it can only take positional argument but not
keyword argument

** pack a variable sequence of keyword into a dictionary


Lambda Class

• If we want to use sorted() to implement some sophisticated sorting,


we can do so by defining a lambda function and passing it into a
function call as the argument to key (Recall we have used sorted by
making key=int to compare string numbers).

• Names in a class's namespace can be exposed by the built-


in __dict__ attribute:
The hasattr() method returns true if an object has the given named
attribute and false if it does not.

__repr__()
Typing the name directly into the interpreter prints out its string
representation:
__str__()
The __str__ method is called when you use the str() function on an
object or when you use the print() function to print an object.
__iter__()
__iter__() methods for most commonly used operations are not
provided by default. The corresponding operations are then not
supported for the class's instances.
What is a selelctor can select “text 2” through “text 5”?
• div p
Beautiful soup
to_csv saving the output
Data Selection
loc:
• The loc attribute uses label-based indexing:
• The loc attribute can be used for slicing based on labels. It can
handle slices, single labels, and lists of labels. Both the start and
the stop of the slice are inclusive

• Topic 6: Pandas

# Row goes first, followed by columns


~ means not
| means or
.query():

include=[‘0’] including only the columns with data type 'object'

(rmb .index in loc)

value_counts return the frequency of distinct values it contains:


iloc 5.5 Handling Missing Values
• The iloc attribute uses Python-style location-based indexing:
• Last index is exclusive

isnull() generates a Boolean mask indicating missing


value
notnull() produces the opposite
deopna() returns a filtered version

fillna() returns a copy of the data with missing values filled or


imputed (set inplace=True to modify it in place):
.read_csv

nunique() count the no. of unique values in each


column
groupby()

Topic 7 Streamlit

Exercise
st.write()

st.markdown()
pandas dataframe

st.dataframe()

st.html()

st.table()
st.code()

st.data_editor()

st.divider()

Another examples:
st.column_config()

st.video()

Data visualization

st.image()
Streamlit provides 5 built-in charting/plotting functions
– st.line_chart(), st.area_chart(), st.scatter_char
t(), st.bar_chart(), and st.map().

st.line_chart()

st.area_chart()
st.scatter_chart()

st.map() st.bar_chart()
st.selectbox()

st.radio()

st.text_input() st.number_input()

st.text_area()

st.checkbox()

st.toggle
st.multiselect()

st.time_input

st.slider()

st.date_input()
Default value
Default values are configurable for all widgets with a few special exceptions
like st.button and st.file_uploader.
Index argument

st.olumn

The defaults of most of the other widegets can be configured via


their widget functions' value argument.

For widget functions such as st.slider and st.number_input, the


defaults can also affect the functionality of the rendered
widegets.

Sidebar
Each page can feature a sidebar implemented by st.sidebar. Passing an
element to st.sidebar positions it to the left of the main viewport,
allowing users to focus on the content in our app.
st.tabs

st.container

Assignment

Fixed height
Q3
Assignment
PP
In-class ex 1
Exercise 2
Exercise 3
Exercise 4

Exercise 5

You might also like