Lecture notes 3#

Numbers#

Example: suppose you have a recepe which requires “red” milk, and you only have “green” milk, how much milk do you have to replace with cream to get the same fat content. With required volume \(v\) and replaced volume \(x\) we have a fat-balance equation

\[1.5\% (v - x) + 40 \% x = 3\% v\]
\[x = \frac{3 - 1.5}{40 - 1.5}v \]
v = 1

x = (3 - 1.5)/(40-1.5) * v

print(x)
0.03896103896103896

For a litre, in tablespoon units, replace about 40ml (~2.5 table spoons)

With some refactoring, introduce more variable names

required_volume = 1
required_fat_content = 3 # in percent
available_fat_content = 1.5 
cream_fat_content = 40

replaced_volume = (required_fat_content - available_fat_content)/(cream_fat_content-available_fat_content) * required_volume

print(replaced_volume)
0.03896103896103896

one line got a bit too long, introduce another intermediate variable conversion_ratio

required_volume = 1
required_fat_content = 3 # in percent
available_fat_content = 1.5 
cream_fat_content = 40

conversion_ratio = (required_fat_content - available_fat_content)/(cream_fat_content-available_fat_content) 
replaced_volume =  conversion_ratio * required_volume

print(replaced_volume)
0.03896103896103896

Strings#

"It's time"
"It's time"
# 'It's time'  invalid
 Cell In[7], line 1
    'It's time'
              ^
SyntaxError: unterminated string literal (detected at line 1)
'It\'s time'   # \' means use the literal character '
"It's time"
print("""
Hello
world
""")
Hello
world
"""
Hello
world
"""
'\nHello\nworld\n'

\n means newline character

'hello' + 'world'
'helloworld'
'bye' + 'bye'
'byebye'
'bye' + 'bye' + 'bye'
'byebyebye'

with this definition of addition a generalized integer multiplication follows

3 * 'bye'
'byebyebye'

Some string functions (or methods)

'gone with the wind'.capitalize()
'Gone with the wind'
'gone with the wind'.title()
'Gone With The Wind'

Suppose we want to underline a title such that

Gone With The Wind
==================
movie = 'gone with the wind'
movie.title() + "\n" + "="*len(movie)
'Gone With The Wind\n=================='
print(movie.title() + "\n" + "="*len(movie))
Gone With The Wind
==================

Lists#

colours = ['hearts', 'spades', 'diamonds', 'clubs']
colours[0]
'hearts'
colours[3]
'clubs'
colours[4]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[20], line 1
----> 1 colours[4]

IndexError: list index out of range
len(colours)
4
# the last element
colours[len(colours) - 1]
'clubs'
colours[-1]  # counts from the end
'clubs'
colours[-2]
'diamonds'
colours
['hearts', 'spades', 'diamonds', 'clubs']
colours[-1]  = 'CLUBS'
colours
['hearts', 'spades', 'diamonds', 'CLUBS']
colours_tuple = ('hearts', 'spades', 'diamonds', 'clubs')
colours_tuple[0]
'hearts'
colours_tuple[-1]
'clubs'
colours_tuple[-1] = 'CLUBS'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[36], line 1
----> 1 colours_tuple[-1] = 'CLUBS'

TypeError: 'tuple' object does not support item assignment

dictionaries#

newdict = {'a': 1, 'b': 2}
len(newdict)
2
newdict.keys()
dict_keys(['a', 'b'])
newdict.values()
dict_values([1, 2])
newdict['a']
1
newdict['b']
2

evaluate#

https://codechalleng.es

sign up for a trial account