Skip to content

Learning Python: Day 8

Using range() to make a list of numbers

  • list() function – allows you to convert the results of range() directly into a list
  • when you wrap list() around a call to the range() function the output will be a list of numbers

numbers = list(range(1, 6))

print(numbers)

[1, 2, 3, 4, 5]

  • can use range() function to tell Python to skip numbers in a given range
  • if you pass a third argument to range(), Puthon uses that value as a step size when generating numbers
  • here’s how to list even numbers between 1 and 10

even_numbers.py

even_numbers – list(range(2, 11, 2))

print(even_numbers)

[2, 4, 6, 8, 10]

  • in Python, two asterisks (**) represent exponents

square_numbers.py

squares = []

for value in range(1, 11):

square = value**2

squares.append(square)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

  • more precise version, omit temporarily variable square and append each new value directly to the list

squares = []

for value in range(1, 11):

squares.append(value**2)

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Simple statistics with a list of numbers

  • a few Python functions are helpful when working with lists of numbers
  • can easily find the minimum, maximum, and sum of a list of numbers:

>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

>>> min(digits)

0

>>> max(digits)

9

>>> sum(digits)

45

List comprehensions

  • List comprehension – combines the for loop and the creation of new elements into one line and automatically appends each new element

squares.py

squares = [value**2 for value in range(1, 11)]

print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Working with part of a list

  • slice – a specific group of items in a list

Slicing a list

  • specify the index of the first and last elements to make a splice
  • similar to range() function, Python stops one item before the second index you specify

players.py

players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]

print(players[0:3])

[‘charles’, ‘martina’, ‘michael’]

  • if you omit the first index in your slice, Python automatically starts at the beginning, the reverse is true

players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]

print(players[:4])

print(players[2:])

[‘charles’, ‘martina’, ‘michael’, ‘florence’]

[‘michael’, ‘florence’, ‘eli’]

Looping through a slice

  • can use a slice in a for loop if you want to loop through a subset of elements in a list

players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]

print(“Here are the first three players on my team:”)

for player in players [:3]:

print(player.title())

Here are the first three players on my team:

Charles

Martina

Michael

  • helpful to use slices in many contexts, such as displaying the top three high scores in a game, processing data in chunks, or when building a web app and using slices to display an appropriate amount of data on each page

End of study session.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *