Docsity

Calculus Lecture 15 SEECS SE, Lecture notes of Calculus

Partial preview of the text

Download Calculus Lecture 15 SEECS SE and more Lecture notes Calculus in PDF only on Docsity!

Week 08 – Lecture 15

Fall 2021 Dr. Zuhair Zafar [email protected]

Agenda

 Creating a List

 Indexing and Concatenation

 Multi-dimensional Lists

 Adding Elements in a List

 Deleting and Removing Elements from a List

Creating List

 In general, we can create lists via putting a number of expressions in square brackets  List = []  List = [expression, …]  E.g., List = [1+2, 7, “Eleven”]  List = [expression for variable in sequence], where expression is evaluated once for every element in the sequence (this is called ”list comprehension”)  E.g., List1 = [x for x in range(10)]  E.g., List2 = [x + 1 for x in range(10)]  E.g., List3 = [x for x in range(10) if x % 2 == 0]

Creating List

  • We can also use the built-in list type object to create lists: - L = list() #This creates an empty list - L = list([expression, …]) - L = list(expression for variable in sequence) >>> mylist = list() >>> mylist [] >>> type(mylist) <class 'list'> >>> mylist_2 = list(["A", 2.3]) >>> mylist_ ['A', 2.3] >>> type(mylist_2) <class 'list'>

Creating List

  • Python creates a single new list every time we use []

    L1 = [1, 2, 3] L2 = [1, 2, 3] L1 == L True L1 is L False

>>> L3 = L4 = [1, 2,

3]

>>> L3 == L

True

L3 is L True

L1 and L2 have the same values but they are independent lists (i.e., they do not point to the same list) L3 and L4 point to the same list

Indexing and Concatenating Lists

Slicing Lists

 Similar to strings as well, lists can be sliced  To slice elements within a range start_index and end_index (inclusive), we can use [start_index : end_index+1] A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[1:3] ['B', 'C']

Slicing Lists

 Similar to strings as well, lists can be sliced  To slice elements from the beginning to a certain ending index (inclusive), we can use [: end_index+1] A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[:3] ['A', 'B', 'C']

Slicing Lists

 Similar to strings as well, lists can be sliced  To slice all elements, we can use [:] A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[:] ['A', 'B', 'C', 'D', 'F']

Slicing Lists

 Similar to strings as well, lists can be sliced  We can even pass a negative index, after which Python will add the length of the list to the index A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[-1] 'F'

Slicing Lists

 Similar to strings as well, lists can be sliced A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[:0] []

If you want to start from - 1 (inclusive), pass nothing to the ending index

Slicing Lists

 Similar to strings as well, lists can be sliced  To slice in steps, we can use [start_index, end_index+1, step] (idea applies to slicing from forward and backward) A B C D F 0 1 2 3 4

grades = ["A", "B", "C", "D", "F"] grades[0:5:2] grades = ["A", "B", "C", "D", "F"] >>> grades[1:3] ['B', 'C'] >>> ## Slicing Lists  Similar to strings as well, lists can be sliced  To slice elements from the beginning to a certain ending index (inclusive), we can use [: end_index+1] A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:3] ['A', 'B', 'C'] >>> ## Slicing Lists  Similar to strings as well, lists can be sliced  To slice all elements, we can use [:] A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:] ['A', 'B', 'C', 'D', 'F'] >>> ## Slicing Lists  Similar to strings as well, lists can be sliced  We can even pass a negative index, after which Python will add the length of the list to the index A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[-1] 'F' >>> ## Slicing Lists  Similar to strings as well, lists can be sliced A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:0] [] >>> If you want to start from - 1 (inclusive), pass nothing to the ending index ## Slicing Lists  Similar to strings as well, lists can be sliced  To slice in steps, we can use [start_index, end_index+1, step] (idea applies to slicing from forward and backward) A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[0:5:2] ['A', 'C', 'F']

Lists are not Sets

 Lists can have duplicate values  We can also iterate over the items of any list

myList = [5, 2, 2, 3] myList [5, 2, 2, 3]

myList = [1, 2, 3, 4, 5] for i in myList: print(i, end = " ") print()

Multi-Dimensional Lists