15-110: Principles of Computing

Slides:



Advertisements
Similar presentations
Lists Ruth Anderson CSE 140 University of Washington 1.
Advertisements

Course A201: Introduction to Programming 10/28/2010.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Modeling and Simulation Monte carlo simulation 1 Arwa Ibrahim Ahmed Princess Nora University.
Writing Solid Code Introduction to Python. Program 1 python -c "print 'Hello World' “ python -c "import time; print time.asctime()“ Start an interactive.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
13.1 Matrices and Their Sums
Numbers, lists and tuples Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
Notes 7.2 – Matrices I. Matrices A.) Def. – A rectangular array of numbers. An m x n matrix is a matrix consisting of m rows and n columns. The element.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Lists Michael Ernst CSE 140 University of Washington.
Lists Ruth Anderson University of Washington CSE 160 Winter
Do Now: Perform the indicated operation. 1.). Algebra II Elements 11.1: Matrix Operations HW: HW: p.590 (16-36 even, 37, 44, 46)
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
13.4 Product of Two Matrices
12-1 Organizing Data Using Matrices
Multiplying Matrices.
Matrix Operations Free powerpoints at
Matrix Operations.
Containers and Lists CIS 40 – Introduction to Programming in Python
Matrix Operations.
Matrix Operations Free powerpoints at
Ruth Anderson University of Washington CSE 160 Spring 2015
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Matrix Operations Free powerpoints at
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Numbers, lists and tuples
Bryan Burlingame Halloween 2018
Chapter 10 Lists.
8 – Lists and tuples John R. Woodward.
Matrices Elements, Adding and Subtracting
MATRICES MATRIX OPERATIONS.
Chapter 5: Lists and Dictionaries
Section 2.4 Matrices.
2.2 Introduction to Matrices
Topics Sequences Introduction to Lists List Slicing
15-110: Principles of Computing
15-110: Principles of Computing
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
3.5 Perform Basic Matrix Operations
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
2-Dimensional Lists (Matrices) in Python
Chapter 4 Matrices & Determinants
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
COMPUTER SCIENCE PRESENTATION.
Python List.
Chapter 7 Section 7.2 Matrices.
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Presentation transcript:

15-110: Principles of Computing Sequences- Part III (Lists) Lecture 14, October 21, 2018 Mohammad Hammoud Carnegie Mellon University in Qatar

Today… Last Session: Today’s Session: Sequences- Part II (Lists) Sequences- Part III (More on Lists): Deleting elements in lists More List Functions Example: Adding Matrices

Removing Elements From a List Elements can be removed from a list one at a time using the built-in function remove(elem) If elem is not in the list, an error will be issued myList = [100, 200, 300, 400, 200, "Python"] print(myList) myList.remove(200) Output: [100, 200, 300, 400, 200, 'Python'] [100, 300, 400, 200, 'Python'] Only the first occurrence of the element is removed!

Removing Elements From a List To remove an element at a certain position i, pop(i) can be used pop() with no argument is valid, but it will remove the last element in the list myList = [100, 200, 300, 400, 200, "Python"] print(myList) myList.pop(4) myList.pop() Output: [100, 200, 300, 400, 200, 'Python'] [100, 200, 300, 400, 'Python'] [100, 200, 300, 400]

List Functions Here is the set of functions that you can use with lists Function Description L.append(x) Add element x to the end of list L L.extend(L2) Add all elements of list L2 to the end of list L L.insert(i, x) Insert item x at the defined index i of list L L.remove(x) Removes item x from list L (valueError exception will be thrown if x does not exist) L.pop(i) Removes and returns the element at index i of list L. If no parameter is passed, the last item in L will be removed and returned

List Functions Here is the set of functions that you can use with lists Function Description L.clear() Removes all items from list L L.index(x) Returns the index of the first matched item x in list L L.count(x) Returns the count of times item x appears in list L L.sort() Sort items in list L in an ascending order L.reverse() Reverses the order of items in list L L.copy() Returns a copy of list L (making any change to the returned list will not impact the original list L)

Example: Matrix-Matrix Addition A matrix is a rectangular (2D) list of numbers or other mathematical objects for which operations (e.g., addition/subtraction) are defined It is used in various real-world applications (e.g., image processing, graph theory, physics, probability and statistics, etc.) # of columns = n = 5 1 2 3 4 1 2 3 4 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 An m×n Matrix with m = 5 rows and n = 5 columns (this is referred to as square matrix since m = n) # of rows = m = 5

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 17 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 17 16 14 63 21 23 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 17 16 14 63 21 23 18 93 103 31 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 17 16 14 63 21 23 18 93 103 31 27 19 20 86 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C

Example: Matrix-Matrix Addition 1 4 7 9 12 10 55 13 81 90 17 8 11 2 3 66 34 52 97 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 6 10 13 17 16 14 63 21 23 18 93 103 31 27 19 20 86 55 74 120 32 47 + = 5×5 Matrix A 5×5 Matrix B 5×5 Matrix C Matrix-matrix subtraction follows exactly the same flow as matrix-matrix addition, but with the (–) operation being applied between every two elements instead of the (+) operation

Example: Matrix-Matrix Addition import random def buildSquareMatrix(m, n): if m == n: matrix = [] for i in range(m): r = [] for j in range(n): r.append(random.randint(1, 100)) matrix.append(r) return matrix else: return "This function builds only square matrices with random integers\n"

Example: Matrix-Matrix Addition #This function assumes equal square matrices (i.e., each matrix is a square matrix #and both have the same numbers of rows and columns) def addMatrices(a, b): m = n = len(a) c = [] for i in range(m): r = [] for j in range(n): r.append(a[i][j] + b[i][j]) c.append(r) return c

Example: Matrix-Matrix Addition def printMatrix(a): for i in a: for j in i: print(j, end = "\t") print() a = buildSquareMatrix(5, 5) b = buildSquareMatrix(5, 5) printMatrix(a) printMatrix(b) c = addMatrices(a, b) printMatrix(c)

Next Lecture… Tuples and dictionaries