




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
4) Summing a list ... Python includes several built-in sequences: lists, tuples, strings. We will discuss ... 1) Find sum of a list of numbers.
Typology: Study notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!
Topics
Lists Lists are the basic ordered and mutable data collection type in Python. They can be defined with comma-separated values between square brackets. L = [ 2 , 3 , 5 , 7 ] print(len(L)) # 4, len() also works with strings L.append( 11 ) # append to the end of the list print(L) # [2, 3, 5, 7, 11]
Indexing Indexing is a means the fetching of a single value from the list. This is a 0-based indexing scheme. L = [ 2 , 3 , 5 , 7 , 11 ] print(L[ 0 ]) # 2 print(L[ 1 ]) # 3 print(L[ 5 ]) # index out of bounds error.
Lists can contain different types of objects List can contain different types and even other lists. L = [ 1 , 'two', 3.14, [- 2 , 3 , 5 ]] print(L[ 0 ]) # 1 print(L[ 1 ]) # two print(L[ 3 ]) # [-2, 3, 5] print(L[ 3 ][0]) # - 2 print(L[ 3 ][1]) # 3 print(L[ 3 ][2]) # 5
Modifying a List Indexing can be used to set elements as well as access them. L = [ 2 , 3 , 5 , 7 , 11 ] L[ 0 ] = 100 print(L) # [100, 3, 5, 7, 11] L[ 2 ] = - 4 print(L) # [100, 3, - 4, 7, 11]
Modifying a list Consider the following code that is intended to change all even numbers in a list to 0. nums = [ 24 , 3 , 34 , 6 , - 5 , 4 ] for x in nums: if x % 2 == 0: x = 0 print(nums) Output: [24, 3, 34, 6, - 5, 4]
Modifying a list Here's the correct code to change all even numbers in a list to 0. Compare the following code to the previous slide. nums = [ 24 , 3 , 34 , 6 , - 5 , 4 ] for i in range(len(nums)): if nums[i] % 2 == 0: nums[i] = 0 print(nums) Output: [0, 3, 0, 0, - 5, 0]
Creating a list with list comprehensions List comprehensions is a way to create a list in Python that is concise and elegant. Its main use is to create a new list from a given list. Instead of: squares = [] # create empty list for i in range(5): squares.append(i ** 2) # add each square to list Do this: squares = [i ** 2 for i in range(5)] # one line! List comprehensions allow you to use a conditional. even_squares = [i ** 2 for i in range(5) if i % 2 == 0] print(even_squares) # [0, 4, 16]
Algorithms to know The following algorithms are useful. Know how to implement these algorithms!
Sum Function Write a function that accepts a list of numbers as a parameter and returns its sum. def sum(nums): s = 0 for x in nums: s += x return s lst = [ 2 , - 1 , 3 , 4 , - 3 ] print(sum(lst)) # 5 lst2 = [ 1 , 5, 4, 2] a = sum(lst2) print(a) # 12
Average Function Write a function that accepts a list of numbers as a parameter and returns its average. def average(nums): s = 0 for x in nums: s += x return s/len(nums) lst = [ 2 , 5, 4, 3] a = average(lst) print(a) # 3.
Find Maximum Function Write a function that accepts a nonempty list of numbers as a parameter and returns its maximum value. Does the code below work? def maximum(nums): current_max = 0 for x in nums: if x > current_max: current_max = x return current_max lst = [- 2 , - 5, - 12, - 3 ] a = maximum(lst) print(a) # 0 INCORRECT! No! What if the list contains only negative numbers?This function returns 0 which is not even in the list! 19
Find Maximum Function Here's the correct implementation of maximum. The minimum function is similar. def maximum(nums): current_max = nums[0] # the first value is maximum for x in nums: # until a bigger value shows up if x > current_max: current_max = x return current_max lst = [ 2 , 5, 12, 3, 4, 11] a = maximum(lst) print(a) # 12