This is a notebook that was created in class on February 11, 2020. The goal was to review some basic programming in Python to be sure your background was strong enough to complete Homework 2.
(1, 2, 3)
[1, 2, 3]
for x in ["cat", "dog", 53]:
print(2*x)
y = "x is {}".format(x)
print(y)
Ranges represent a list of consecutive values.
range(5)
You can convert a range to a list.
list(range(5))
Some information about range can be discovered by evaluating range?
.
range?
list(range(1, 11, 2))
list(range(11, 0, -2))
for i in range(11, 0, -2):
print(i)
sum(range(11, 0, -2))
Here we compute the sums $$\sum_{i=1}^N i$$ for increasing values of $N$ until the first time the sum exceeds 1000.
i = 1
total = 0
while True:
total = total + i
print("total = {} when i = {}".format(total, i))
if total > 1000:
break
i = i + 1
import random
random.randint?
Print a random sequence of twenty zeros or ones.
for i in range(20):
print(random.randint(0, 1))
Print a random sequence of twenty coin flips:
for i in range(20):
x = random.randint(0, 1)
if x == 0:
coin = "Heads"
if x == 1:
coin = "Tails"
print(coin)
def flip(n):
"""Flip a coin n times, and print the results."""
for i in range(n):
x = random.randint(0, 1)
if x == 0:
coin = "Heads"
if x == 1:
coin = "Tails"
print(coin)
flip(5)
flip?
def ternary(n):
# Below we peform sanity checks on the input.
assert type(n) == int, 'n must be an integer'
assert n>0, 'n must be a positive integer.'
rep = '' # This will store the ternary representation.
while n != 0:
trit = n % 3
rep = str(trit) + rep # add the new trit on the left.
n = n // 3 # Update the value of n.
return rep
ternary(10)
s = "My dog's name is Sugar."
Iterating through the characters in a string:
for c in s:
print(c, end="-")
The length of a string:
len(s)
The first character:
s[0]
The second:
s[1]
The 23rd character:
s[22]
There is no 24th character in s
.
s[23]
Another way to iterate through the characters in a string:
for i in range(len(s)):
c = s[i]
print(c, end="-")
Iterating through backwards:
for i in range(len(s)-1, -1, -1):
c = s[i]
print(c, end="-")
int("0")
int("23")
Recall what s is:
s
The capital S
appears at index 17
, and the r
appears at index 21
:
print(s[17])
print(s[21])
To get the substring Sugar
you can type:
s[17:22]
This is called a "slice." The meaning of numbers is similar to the range
function. We are extracting the substring beginning in index 17
up index 22
(excluded).
You can also use a step size of 2
if you wanted:
s[17:22:2]
Load the interval arithmetic package.
from mpmath import iv
Use 3 bits of precision:
iv.prec = 3
This will find an interval with endpoints using 3 bits of precision that contains 1.3.
iv.mpf("1.3")
The value None
is returned if the truth of a comparison can not be decided from the intervals.
print(iv.mpf("1.3") < iv.mpf("1.4"))
Normally you would get true
or false
:
print(iv.mpf("1.3") < iv.mpf("2.1"))
Assume $x \neq y$. If you want to decide if $x < y$, you can repeat a comparisson at higher and higher precision until you get a definitive answer.
x = "1.3"
y = "1.4"
prec = 1
while True:
iv.prec = prec
value = (iv.mpf(x) < iv.mpf(y))
print("At {} of bits of precision, the comparisson returned {}.".format(prec, value))
if value == True or value == False:
# The comparison worked
break
prec = prec + 1
print("The comparison is {}.".format(value))
x = iv.pi
x
x.delta
gives the width of the interval (as an interval).
x.delta
Print out the interval to two significant digits:
iv.nstr(x,2)
To three, and store the value in x
.
x = iv.nstr(x,3)
x
We can use a substring to pull out the first number.
x[1:5]
Here is a fancier way of extracting the first number from the string. If s
is a string, you can type s.split?
to find out what split does. Play around with it!
x.split("[")[1].split(",")[0]