Dictionaries
Dictionaries
Objectives
• Introduction to dictionaries in
Python.
• Initializing dictionaries.
• Manipulating dictionaries.
• Using a list or dictionary for a
program.
Introduction to Dictionaries
| A dictionary
is a set of 'keys' (words)
all pointing to their own 'values'
(meanings).
dict1 = {"first_name" : ”Mike", "last_name" : ”Smith"}
dict1['John'] = 'Leo'
| Create Dictionary
- Create a default.
- Use a list of key and pair values.
- Make a parallel list.
- dictionary3 = zip(names, degrees)
Dictionary Operations
| Dictionary
- Access values.
- Update.
- Delete.
Dictionary Functions and Methods
Objectives:
• Discover the types of errors.
• Use exceptions.
Types of Error
| Errors
- Syntax error.
- Semantics error.
- Run time error.
- Logical error.
Types of Error
| Syntax error
- Syntax errors: refers to formal rules governing the
construction of valid statements in a language.
- Syntax errors occur when rules of a programming
language are misused, i.e., when a grammatical rule of
the language is violated.
Types of Error
| Semantics errors
- Semantics errors occur when statements are not meaningful.
- Semantics refers to the set of rules which give the meaning of the
statement.
- Examples:
• Jon is registered for a class.
• It is semantically and syntactically correct.
• Class registered a class for Jon.
• Incorrect and not Semantically correct.
Types of Error
| Logical errors
- A logical error causes a program to produce an
incorrect or undesired output.
- As an example:
• Infinite loops
• Mathematical operation.
Exceptions
| What is an exception?
- Even when a statement or
expression is syntactically correct,
it may cause an error when an
attempt is made to execute it.
- An error during this process is
called an exception.
Handling Exceptions
| IOError
- Raised when the file cannot be opened.
| ImportError
- Raised when Python cannot find the module.
| ValueError
- Raised when a built-in operation or function received an argument that has the right
type but an inappropriate value.
| KeyboardInterrupt
- Raised when the user hits the interrupt key.
| EOFError
- Raised when one of the built-in functions(input() or raw_input()) hits an end-of-file
without reading data.
Reading File
| Errors
Write File
| Errors
Select Topics in Python
Creating and Using Lists
Objectives
Objectives:
• Introduction to Lists in Python
• Initialize List
• Add to List
• Calls to List
• Sort or Reverse a List
• Operations on a List
• Slice a List
• Display a List
Introduction to Lists
0 1 2 3 4
Indexing a List
Create and Add to a List
| Create
- Define a variable name.
- Insert items into the list.
| Add to a list
- Insert.
- Extend.
- Append.
- Concatenate ‘+’
Calls to List
| Sort a list
Operations on a List
| Operations
- Index.
- Count.
- Remove.
- Slice.
Display a List
| Demo
Selected Topics in Python
Using Matplotlib
Objectives
Objectives
• Discover how to use
Matplotlib?
• Explore the different types of
plots
Matplotlib
| What is Matplotlib?
Line Plot
| Line plots
- Used to show trends.
- Compare multiple values.
- Changes of values over time.
- Numerical data.
- Make predictions.
Line Plot
| Line plots
- linestyles
- linewidth
- color
- legend
Bar Plot
| Bar plots
- Normal and/or ordinal
categories.
- Compare data against different
categories.
- Use when greater than 3.
- Compare large data sets over
time.
Scatter Plot
| Scatter plots
- Visualize the relationship between
multiple numerical values.
- Visualize the correlation in a large
data set.
- Predict behavior based on the
measurement of the independent
variable.
Box Plot
| Box plots
- Show whisker plots.
- Displays statistical graph for sets
of numerical data.
- Shows the range, spread and
center.
- Used to compare data from
different categories.
Histograms
| Histograms
- Used for continuous data.
- Displays the frequency distribution.
- Summarizes large data sets
graphically.
- Compare multiple distributions.
Select Topics in Python
Using NumPy
Objectives
Objectives
• Introduction to NumPy.
• Arrays.
• NumPy operations.
Introduction of NumPy
| Importing NumPy
- https://numpy.org/install/
Create Array using NumPy
| Arrays
- Single-dimensional array.
- Multidimensional array.
NumPy Operations
| Operations
- ndim
- itemsize
- dtype
- reshape
- slicing
- linspace
- max
- min
- sum
NumPy Operations (continued)
| Operations
- Square root.
- Standard deviation.
- Math operators.
- Ravel.
NumPy Operations (continued)
| Special functions
- sin(), cos(), tan()
- Exponential
- Log
Selected Topics in Python
Using Pandas
Objectives
Objectives
• What is Pandas?
• Slicing data frame.
• Concatenation.
• Change index.
• Change headers.
• Merging and joining.
What is Pandas?
| Slice
- Show all.
- Show number of rows from head.
- Show number of rows from tail.
Change Index
| Set index
Change Headers
Objectives
• Introduction file I/O.
• Loading a file in Colab.
• Reading from a file.
• String splitting.
Introduction File I/O
| I/O
- Why do we need to use I/O?
• Storing data.
• Accessing data.
• Modifying data.
Loading a File in CoLab
| Loading file
- Select folder.
- Open sample_data.
- Select three dots.
- Upload.
Reading From a File
| String Splitting
- Split default.
- Split by character.
- Split by characters.
- Split using for loop.
Select Topics in Python
Creating and Using Sets
Objectives
Objectives
• Introduction sets.
• Implementation of sets.
• Using range function.
• Iterate through a set.
• Set operations.
• Set functions.
Introduction Sets
| Sets
- Unordered collection.
• Iterable.
• Mutable.
• No duplications.
• Also know as a hash table.
Implementation of a Set
| Set
- Empty set.
- Defined set.
- Set method.
Using the Range Function
| Range
- Using 1, 2, and/or 3 consists.
Iterate Through a Set
| Iteration
- For loop.
Set Operations
| Function
- len()
- max()
- min()
- sorted()
- sum()
Select Topics in Python
Creating and Using Tuples
Objectives
Objectives
• Introduction to tuples in Python
• Initialize tuples
• Convert to tuples
• Slice to tuples
• Iterate through a list of tuples
Introduction to Tuples
| Tuples.
- Data structure in Python.
| Create a tuple
- Empty set.
- Lists.
- Comparison string and tuples.
- Splice of a string.
Converting to Tuples
| Convert to Tuple
- List to tuple.
• Tuple function.
• For loop.
• Unpack list.
Slicing to Tuples
| Slicing
Iterating Through List of Tuples
| Iterations
- For loop.
- Enumerate.
Selected Topics in Python
Writing to a File
Objectives
Objectives
• Writing to a File
Writing to a File
| Write to a file
- Open.
- Write.
• w vs. a
- Close.