Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Python Lists: A Comprehensive Guide with Examples, Study notes of Advanced Computer Programming

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

2021/2022

Uploaded on 09/27/2022

amlay
amlay 🇺🇸

4.1

(19)

384 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Python
Lists
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Introduction to Python Lists: A Comprehensive Guide with Examples and more Study notes Advanced Computer Programming in PDF only on Docsity!

Introduction to Python

Lists

Topics

  1. Lists
  2. List indexing
  3. Traversing and modifying a list
  4. Summing a list
  5. Maximum/Minimum of a list
  6. List Methods

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]

Note: The list is unchanged? Why? How can we fix it? 10

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!

  1. Find sum of a list of numbers.
  2. Find the average of a list of numbers.
  3. Find the maximum/minimum of a list of numbers.

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