Lists#

Lists are a way Python can store multiple pieces of data. For instance the abbreviated days of the week:

['M','T','W','R','F','S','S']
['M', 'T', 'W', 'R', 'F', 'S', 'S']

We can make lists of numbers and even us numbers from an imported math module to add some interesting numbers.

import math as math
math.pi
3.141592653589793
[math.pi,0,-1,math.e]
[3.141592653589793, 0, -1, 2.718281828459045]

List Comprehension#

Operations or functions can be performed on members of a list using a powerful technique know as list comprehension, study these examples:

list = [0, 1, 2, 3, 4]
list
[0, 1, 2, 3, 4]
[2*x for x in list]
[0, 2, 4, 6, 8]
[2**x for x in list]
[1, 2, 4, 8, 16]

The x in these examples is a placeholder which iterates through the values in the list and communicates these vales to the operation (i.e 2**x) to create the new list. We can use any other label here such as temp.

Example#

An ideal gas is the simplest description of the way gas molecules and atoms interact and the relationship between temperature, volume, pressure, and number of molecules. See:Open Stax

P V = n R T

P = pressure in atm
V = volume in liter
n = number of moles
R = gas constant = 0.082057 L atm / mol K
T = temperature in Kelvin

Challenge: Compute the volume of 1 mole of gas at 1 atm and the follwing temperatures T = 298 K (room remperature), 310 K, 273 K

T = [273, 298, 310]
P = 1.0
n = 1.0
R = 0.082057
  • Use a list to compute volume at the 3 temperatures

V = [n*R*temp/P for temp in T]
V
[22.401561, 24.452986000000003, 25.43767]