ENGG1810 Recap
ENGG1810 Recap
ENGG1810 Recap
*Use the type() function to get the type or class name of an object
Since tuples are also indexed, tuples can have items with the same value. (Allow Duplicate)
We can do the similar thing for accessing tuples like what we did with lists:
Length using len()
Access a tuple item using tuple_name[specific_index]
Slicing using a range of indexes tuple_name[start_index(included),end_index(NOT
included)]
Technically you cannot update it, but there are some tricks!
1) Convert the tuple to a listà2) Do append() or remove()à3) Convert it back into a tuple
A Set is a collection which is unordered, unindexed, mutable, and does NOT allow duplicate values.
Sets are written with curly brackets. {}
A dictionary is a collection which is ordered*, changeable and does not allow duplicates.
break
• With the break statement we can stop the loop even if the while condition is true.
continue
• With the continue statement we can stop the current iteration, and continue with the next
Lists or Tuples
items = ['Weekly', 'LabTest1', 'LabTest2', 'FinalExam'] btsv = [18, 8, 16, 45]
emma = [20, 8, 16, 42]
sponge = [10, 4, 14, 35]
Dictionaries
bts_v = {
“Weekly”: 18,
“LabTest1”: 8,
“LabTest2”: 16,
“FinalExam”: 45
}
emma = {
“Weekly”: 20,
“LabTest1”: 8,
“LabTest2”: 16,
“FinalExam”: 42
}
sponge = {
“Weekly”: 10,
“LabTest1”: 4,
“LabTest2”: 14,
“FinalExam”: 35
open(file, mode)
In order to open a file - file filename
- mode mode
There are mainly four mode:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
open(file, mode)
In order to open a file
- file filename - mode mode
read(size)
for reading the content of the file
- size specified size of characters
Can we remove those punctuations? I want to clean it!
Yes, we can use Python RegEx!
A RegEx (Regular Expression) is a sequence of characters that forms a search pattern.
RegEx can be used to check if a string contains the specified search pattern.
Start with Import re
The sub() function replaces the The split() function returns a list where matches with the
text of your choice: the string has been split at each match:
The upper() method returns a string where all characters are in upper case. Symbols and Numbers
are ignored. (You can also try lower() method )
Print(uppercase.split(‘\n’))
The split() function returns a list where the string has been split at each match.
Hence, it returns a list where the object uppercase has been split at each match of ‘\n’
The join()method returns a string by joining all the elements of an iterable (list, string, tuple),
separated by a string separator.
Syntax check in detail!
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that are passed into
a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
Variables that are created outside of a function (so far we learned in the course) are known as global
variables. Global variables can be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be used within the function, it is not
accessible outside the function
func (animate): The function to call at each frame.
init_func: A function used to draw a clear frame
frames: iterable, int, generator function
Interval: Delay between frames in milliseconds.
fps: Movie frame rate (per second).
init_func: A function used to draw a clear frame
extra_args: Extra command-line arguments passed to the underlying movie encoder
try:
<statements we intend to do> except <ExceptionName>:
<statements for handling errors>
try:
<statements we intend to do> except <ExceptionName>:
<statements for handling errors> finally:
<statements executed regardless if
the try block raises an error or not>
raise Exception(‘’)
- SyntaxError
- ZeroDivisionError:
- NameError:
- TypeError:
- ValueError:
- RuntimeError
-
min/max
Find the lowest or highest in an iterable:
abs
Returns the absolute (positive) value of the specified number
round
Round number to ndigits beyond the decimal point.
pow
sum
adds the items of an iterable and returns the sum.
int/float
returns a integer or floating point number from a number or a string.
import math
Series
A Pandas Series is like a column in a table.
It is a one-dimensional array holding data of any type.
DataFrames
A Pandas DataFrame is a 2 dimensional data structure,
like a 2 dimensional array, or a table with rows and columns.
loc attribute to return the specified row(s).
Calculating a moving average involves creating a new series where the values are comprised of the
average of raw observations in the original time series.
A moving average requires that you specify a window size called the window width. This defines the
number of raw observations used to calculate the moving average value.
The “moving” part in the moving average refers to the fact that the window defined by the window
width is slid along the time series to calculate the average values in the new series.
The most commonly used moving average is ‘Trailing Moving Average’ The value at time(t) is
calculated as the average of the raw observations at and before the time (t)
The most commonly used moving average is ‘Trailing Moving Average’ The moving average value at
time(t) is calculated as the average of the raw observations at and before the time (t)