Lists Python Lecture Notes PDF
Document Details
Uploaded by AffectionateCoconutTree1215
Newgiza University
2024
Tags
Summary
These lecture notes provide a comprehensive introduction to lists in Python. They cover various operations such as creating lists, accessing elements using indices, slicing, and modifying lists. The notes also offer examples to illustrate these concepts.
Full Transcript
Lists Week 4: Lecture 2 DATE : 19/11/2024 1 Learning Outcomes 2 Learning Outcomes By the end of this section, you will be able to: Create lists in python and identify elements using indices. Sort lists of numbers and text....
Lists Week 4: Lecture 2 DATE : 19/11/2024 1 Learning Outcomes 2 Learning Outcomes By the end of this section, you will be able to: Create lists in python and identify elements using indices. Sort lists of numbers and text. 3 Introduction 4 Outline Lists Insertion, deletion, etc Sorting 5 What are lists? A list is a sequence of data values called items or elements. An item can be of any type. Can you think of some real-world examples of lists? A shopping list A to-do list A Class list An attendance list …. 6 Lists in Python Lists in Python are like arrays in other languages but much more powerful. To define a list, we use the the square brackets []: # This code assigns a three-element list to x. This is a list of integers x = [1, 2, 3] fruits = ["apples", "oranges", "cherries"] # A list of strings y = [] # an empty list What do you think the following code will output: print(type(x)) >>> list 7 Other examples Unlike other languages, in Python a list can contain elements of different types: # First element is a number, second is a string, third is another list. x = [2, "two", [1, 2, 3]] 8 The len function The most basic built-in list function is the len function, which returns a number of elements in a list: x = [2, "two", [1, 2, 3]] print(len(x)) >>> 3 9 List indices Each item in a list has a unique index that specifies its position. The index of the first item is 0, and the index of the last item is the length of the list minus 1. x = ["first", "second", "third", "fourth"] x >>> 'first' x >>> 'third’ x[len(x)-1] >>> 'fourth’ x[len(x)] IndexError: list index out of range 10 Negative indexes Python indices can be negative. A negative index means start counting from the end, so a -1 = first element from the end, -2 = 2nd element from the end, etc. x = ["first", "second", "third", "fourth"] x[-1] >>> x[len(x)-1] >>> 'fourth' x[-4] >>> x >>> 'first' x[-5] IndexError: list index out of range 11 List Operations: Slicing Sometimes we need to access a portion of a list, this portion is often referred to as a slice. Slicing is the process by which we obtain part of a list. Slicing takes the form Including start and excluding end list[slice_start : slice_end : step] Examples: >>> grades = [60, 70, 50, 80, 100] >>> print(grades [1:3]) [ 70, 50] >>> print(grades[2: ]) -5 -4 -3 -2 -1 [50, 80, 100] >>> print(grades[ :4]) 60 70 50 80 100 [60, 70, 50, 80] 0 1 2 3 4 >>> print(grades[-10:10]) [60, 70, 50, 80,100] >>> print(grades[ :4:2]) #we included a step factor [60,50] #we start at the beginning, and then add 2, move to right direction 12 Slicing: more examples >>> print(grades[1:-1]) [70,50,80] we start at 1, always moving to the right.. -1 marks the end of the last item, so we stop at the element just before it. It makes no difference whether we start at 1 or -4, both will yield the same result because both refer to the same element >>> print(grades[-4:-1]) [70,50,80] -5 -4 -3 -2 -1 >>> print(grades[-5:3]) 60 70 50 80 100 [60,70,50] >>> print(grades[-1:-5:-1]) 0 1 2 3 4 the –ve step at end, means that we reverse the direction from moving from left to right to moving from right to left. [100, 80, 50, 70] >>> print(grades[-2:-5:-2]) #same as print(grades[3:0:-2]) [80, 70] >>> print(grades[2::-1]) #same as print(grades[-3::-1]) [50, 70, 60] >>> print(grades[::-2]) #bonus question! [100,50,60] 13 Indices for Slices x = ["first", "second", "third", "fourth"] print(x[0:3]) >>> ['first', 'second', 'third’] print(x[1:2]) >>> ['second’] print( x[-2:-1]) >>> ['third’] print( x[1:-1]) >>> ['second', 'third'] 14 Modifying lists: Replacing an element x = [1, 2, 3, 4] x = "two" print(x) >>> [1, 'two', 3, 4] 15 Excersise Write a Python function modify_lists(list1, list2) that takes two lists of numbers as arguments. The function should check if both lists are equal in length: If the lists are equal in length, ask the user for a new value to change the first element of list1. If the lists are not equal in length, ask the user for a new value to change the last element of list2. 16 Answer def modify_lists(list1, list2): if len(list1) == len(list2): new_value = int(input("Enter a new value to change the first element of list1: ")) list1 = new_value else: new_value = int(input("Enter a new value to change the last element of list2: ")) list2[-1] = new_value print(f"Modified list1: {list1}") An f-string allows you to embed expressions print(f"Modified list2: {list2}") inside string literals, using curly braces {} # Example usage list1 = [1, 2, 3] list2 = [4, 5, 6] modify_lists(list1, list2) 17