AdvancedLists_Numpy -> Feb 27
Overview
Advanced Lists & Numpy
Course Overview
Title: Advanced Lists & NumpyDate: February 27, 2025Focus: Introduction to Scientific Computing (MTH 280)
Key Topics
Advanced Lists
Numpy
Comprehension and List Membership
Nested Lists and Grid Creation
Slicing and Negative Indexing
Visualization and Plotting
I/O with Numpy
1. Overview of Lists
Lists are ordered collections of elements that can hold any data type.
Basic operations include creating, modifying, and accessing list elements.
Reference book sections 2.3 and 2.4 for complex use cases.
1.1 Motivation for List Techniques
Traditional method: using a for loop for list creation is cumbersome.
Solution: Use list comprehension for concise syntax.
1.2 List Comprehension
Syntax: General form:
new_list = [i-dependent-expression for i in list].It simplifies code and improves readability; common in Python programming.
1.3 Membership Testing
Validate existence of elements in a list using the
inoperator.Example:
print(1 in x)returns True.
2. Nested Lists
Nested lists include other lists as elements, essential for matrices and high-dimensional data.
Example of creation and accessing elements demonstrated.
Introduction to generating a grid of points through nested lists.
3. Slicing Lists
Access specific segments of a list using slicing syntax
x[start:stop:step].Discusses advanced techniques and negative indexing for efficient data retrieval.
4. Numpy Overview
Introduction to Numpy and its role in scientific computing. Comparison of lists vs arrays.
Discusses array creation, handling multi-dimensional data, and vectorization for non-loop based operations.
4.1 I/O with Numpy
Ease of reading and writing files with Numpy arrays.
Examples on loading datasets and handling structured data.
Summary of Concepts Covered
Importance of list comprehensions, membership checks, nested lists for data structures, and Numpy's efficiency in scientific computing.
Action Items for Next Session
Homework/Assignments covering sections on advanced lists and Numpy usage.
Page 1
Course Title: Advanced Lists & Numpy
Course Date: February 27, 2025
Focus: Introduction to Scientific Computing (MTH 280)
Page 2
Outline Topics:
Advanced Lists
Numpy
Looking ahead
Comprehension
List membership
Nested lists
List slicing
Page 3
Overview of Lists:
Lists are ordered collections of elements that can hold any data type.
Basic operations include creating, modifying, and accessing list elements.
Will explore more complex use cases (referencing book sections 2.3 and 2.4).
Page 4
Motivation for List Techniques:
Problem: Create a new list from an existing list.
Traditional method involves using a for loop, which can be cumbersome.
Solution: Employ Python's list comprehension for concise syntax.
Page 5
Explicit Loop Example:
Code snippet provided for calculating ball height over time:
g = 9.81 times = range(0, 5) ball_height = [] for t in times: y = 0.5 * g * (t ** 2) ball_height.append(y) print(ball_height)Output: [0.0, 4.905, 19.62, 44.145, 78.48]
Page 6
List Comprehension: Same calculation using list comprehension:
ball_height = [0.5 * g * (t ** 2) for t in times] print(ball_height)
Page 7
Listing 3: List Comprehension
Same code as Listing 2, showcasing efficient list creation in one line:
ball_height = [0.5 * g * (t ** 2) for t in range(0, 5)]
Page 8
Output for List Comprehension: Resulting output remains consistent with previous examples.
Page 9
List Comprehension Syntax:
General form:
new_list = [i-dependent-expression for i in list]Describes the role of list variables and expressions.
Page 10
Further Explanation:
Breakdown of list comprehension syntax:
i-dependent-expressiondepends on variableiiterated overlist.
Page 11
Components of List Comprehension:
Explanation of the resulting list and its derived value through comprehension techniques.
Page 12
Resulting List: New list is a compilation of evaluated expressions based on the original list.
Page 13
Comparison with Traditional Loop:
Comprehension syntax concise compared to:
new_list = [] for i in list: y = i-dependent-expression new_list.append(y)
Page 14
Benefits of List Comprehension:
Simplifies code and boosts readability.
Common pattern encountered in Python programming.
Page 15
Membership Testing:
Use the
inoperator to verify if an element exists in a list.
Page 16
Membership Test Example:
Code snippet demonstrating membership tests:
x = [1, 2, "hello"] print(1 in x) # True print("goodbye" in x) # False if 2 in x: print("2 is a member of x")
Page 17
Boolean Expression Basics:
Defines the structure of membership tests as boolean expressions evaluating true/false.
Page 18
Nested Lists Concept:
Nested lists allow inclusion of other lists as elements.
Important for constructing matrices and handling high dimensional data.
Page 19
Nested List Example:
Demonstrates accessing nested list elements:
x = [[1, 7], [20, 99]]Accessing values directly and checking types.
Page 20
Creating Points in a Plane:
Introduction to point generation through nested lists.
Page 21
Grid of Points Example:
Creates a grid in 2D with nested lists.
Code structure outlined for point creation:
xs = range(-6, 7, 1) ys = range(-6, 7, 1) points = [] for x in xs: for y in ys: points.append([x, y])
Page 22
Output Review:
Resulting nested list formatted and organized for better readability.
Page 23
Utilizing pprint:
Importance of using the pprint module for formatted printing of lists.
Page 24
Further Example Using pprint:
Demonstrates practical application of pprint to enhance list visualization.
Page 25
Plotting Points:
Introduction to plotting generated points using Matplotlib with RGBA values.
Page 26
Visualization Output: Displays potential output after plotting.
Page 27
Slicing Lists Motivation:
Discusses accessing specific segments of a list, presenting new syntactical features of Python.
Page 28
Accessing Lists with Colons:
Detailed breakdown of the slicing syntax used to retrieve portions of lists.
Page 29
Overview of Slicing Syntax:
General format:
x[start:stop:step].
Page 30
Slicing Variations:
Describes advanced slicing techniques for nuanced data extraction.
Page 31
Negative Indexing:
Discusses accessing elements from the end of a list for streamlined data retrieval.
Page 32
Negative Indexing Summary:
Highlights benefits and functions of negative indexing.
Page 33
Summary of Concepts Covered:
Nested lists behave predictably; utilizing pprint for readability.
Membership checks via
in, list comprehensions for simplification, and slicing for efficiency.
Page 34
Overview of Numpy:
Introduction to Arrays and their relationship to lists.
Page 35
Motivation for Numpy:
Comparison of list operations leading to the need for optimized array handling.
Page 36
Numpy Fundamentals:
Overview of numerical Python and its foundational role in scientific computing.
Page 37
Defining Arrays:
Arrays defined as homogeneous lists and differences with standard Python lists.
Page 38
Testing Numpy Installation:
Example to demonstrate creating Numpy arrays and corresponding outputs.
Page 39
Creating Arrays:
Various methods to create arrays with examples provided for clarity.
Page 40
Matrix Creation with Numpy:
Constructed 2D arrays demonstrated with access patterns explained.
Page 41
Working with Arrays - Vectorization:
Emphasizes non-loop based mathematical operations on arrays.
Page 42
Comparison of Lists and Arrays:
Lists vs. Arrays in context to data types and operational efficiency.
Page 43
Example Usages:
Demonstrates array operations common in scientific computation.
Page 44
I/O with Numpy:
Convenience of reading and writing files with Numpy arrays.
Page 45
Simple Data File:
Structure of a simple dataset and how to load it using Numpy.
Page 46
Working with Multiple Rows/Columns:
Mechanics of handling multi-dimensional data files with more complex structures shown.
Page 47
Loading Your Own Datasets:
Example focusing on a specific CSV dataset using Numpy.
Page 48
Sample Data Structure Explanation:
Overview of the dataset format with examples explaining headers and values.
Page 49
Further Dataset Example:
Input commands shown for reading structured population data into Numpy arrays.
Page 50
Utilizing Numpy:
Summary emphasizing the utility of Numpy in scientific computing.
Page 51
Course Outline Recap:
Key components for advanced programming in the course.
Page 52
Progress Check:
Review foundational programming concepts leading to user-defined functions.
Page 53
Introduction to Scientific Computing:
Overview of algorithms, generating and analyzing datasets.
Page 54
Action Items for Next Session:
Homework/Assignments covering sections on advanced lists and Numpy usage.