The jupyter notebook allows code and markdown. For example:
pwd
linspace()
%pylab inline
We learned about different types of variables
x = 1
type(x)
x = 1.
type(x)
x = "Hello"
type(x)
x = False
type(x)
2 >= 2
True > False
1 > 2/2
x = []
type(x)
len([2,3,4])
x = array([1,2,3])
type(x)
x = array([[1,2,3],[4,5,6]])
print(x)
x.shape
y = np.full((2,3),0)
print(y)
What are the types of the following variables x?
What are the types of x[1] ?
Consider
What is going on when you try to run the following?
Conclusions:
#
try to run the following line:
[1,2,3] + 1
error message implies:
[1,2,3] + []
works. Talk to your neighbor and find out how "+" works with lists.
[1,2,3] + ["Hello"]
You can use "+" on strings
"Hello" + " " + "World" + "!"
You can think of a string as a list of characters. The variable type is string, not list though!
x = [0,1,2,3,4,5,6,7]
x[:]
x[-1]
print(x[0:3])
print(x[3])
Last index is not displayed!
x[2:]
print(x[:-2])
print(x[0::3])
Indexing works on strings too!
x = "HelloWorld"
x[3:6]
x = [10,3,4,2,888,99,1000,3]
y = array(x)
ind = [0,3,4,5,7]
y[ind]
indbool = array([True,False,False,True,True,True,False,True])
y[indbool]
Why would that ever be useful?
y < 100
print(y)
print(y < 100 )
I want to give a challenge that uses smart slicing. Before I just want to make sure everyone understands plotting.
plot([1],[2],"*")
ylim((0,3))
xlim((0,4))
plot([1, 1.5],[2, 0.5],"*")
ylim((0,3))
xlim((0,4))
Discuss with a partner and explain the output of the following three lines of code:
x = linspace(0,4*pi,num=1000)
y = sin(x)
plot(x[y>0],y[y>0],'*')
x = 2
y = 1
if x < y:
print("x is smaller than y")
else:
print("x is equal to or larger than y")
x = 1
y = 2
if x < y:
print("x is smaller than y")
elif x == y:
print("x is equal to y")
else:
print("x is lager than y")
"hello" == "Hello"
[1,3,2] == [1,2,3]
Advanced function challenge:
def step(x):
"This is the heaviside step function. It returns 0 for x values below 0, 1/2 for x = 0, and 1 for x > 0."
if x < 0:
return 0
elif x == 0:
return 0.5
else:
return 1
for i in [1,2,3]:
print(i)
x = [[1,2,3],[4, 5, 6]]
y = array(x)
for i in x:
print(i)
for i in y:
print(i)
for i in ["Hello", "World","!","It's","me","!"]:
print(i)
for apple in ["Hello", "World","!","It's","me","!"]:
print(apple)
wordlist = ["Hello", "World","!","It's","me","!"]
for word in wordlist:
print(word)
Conclusion: The place holder is abritrary!
for i in range(1,36):
if i%3 == 0 and i%5 == 0:
print("FizzBuzz")
elif i%3 == 0:
print("Fizz")
elif i% 5 == 0:
print("Buzz")
else:
print(i)
def unit_circle(npoints):
"This function returns the x and y values of a circle of radius 1 with center (0,0). npoints defines how many points of the circle we sample"
alpha = linspace(0,2*pi,npoints)
x = cos(alpha)
y = sin(alpha)
return x,y
def r_circle(r,npoints):
"This functions returns the x and y values of a circle of radius r with the center (0,0). npoints defines how many points we sample"
alpha = linspace(0,2*pi,npoints)
x = r*cos(alpha)
y = r*sin(alpha)
return x,y
#define parameters for the circle
npoints = 30
r = 4
#calculate data to plot
x = r_circle(r,npoints)[0]
y = r_circle(r,npoints)[1]
# execute the plotting
plot(x,y,"o")
axis('equal')
xlim((-10,10))
npoints = 30
rlist = [0.2,1,2,4,5]
for r in rlist:
#calculate data to plot
x = r_circle(r,npoints)[0]
y = r_circle(r,npoints)[1]
# execute the plotting
plot(x,y,"o")
axis('equal')
xlim((-10,10))