Answer these in Python
pi
from the math
module.for
loop:
operator in a string.}
In [1]:
#1.1
import math
print('{:.4}'.format(math.pi))
In [2]:
#1.2 - no for loop
print('{:5.4}'.format(math.sqrt(1)))
print('{:5.4}'.format(math.sqrt(2)))
print('{:5.4}'.format(math.sqrt(3)))
print('{:5.4}'.format(math.sqrt(4)))
print('{:5.4}'.format(math.sqrt(5)))
In [3]:
#1.2 - for loop
for i in range(1, 6):
print('{:5.4}'.format(math.sqrt(i)))
In [4]:
print('{0:} {0:}'.format('test'))
Answers these symbolically
z
is greater than 10.z
less than -25 but greater than -35.%
operator in python, which computes the remainder of the left number after being divided by the right number, to compute the remainder of 12 divided by 5.%
operator in a boolean expression to print 'even' if the variable z
is even.z
is positive or negative, even or odd, and greater in magnitude than 10.
In [5]:
#3.1
z = -27
if z > 10:
print('greater than 10')
In [6]:
#3.2
if z < -25 and z > -35:
print('special')
In [7]:
#3.3
12 % 5
Out[7]:
In [8]:
#3.4
if z % 2 == 0:
print('even')
In [9]:
#3.5
if z % 2 == 0:
print('even')
else:
print('odd')
if z > 0:
print ('positive')
elif z < 0:
print('negative')
if abs(z) > 10:
print('greater in magnitude than 10')
Answers these questions in Python. Print your result
range
function.=
), override the last element to be 100. for
loop print out $2^i$, where $i$ is the elements in your list.
In [10]:
#4.1
x = list(range(2,20,2))
x
Out[10]:
In [11]:
#4.2
x[-3]
Out[11]:
In [12]:
#4.3
x[:3]
Out[12]:
In [13]:
#4.4
x = list(range(1,11))
x[-1] = 100
x
Out[13]:
In [14]:
#4.5
x = list(range(1,5))
for thing in x:
print(2**thing)
Answers these questions in Python
When specifying an interval, parenthesis indicate an exclusive end-point and brackets indicate an inclusive end-point. So $\left(0,1\right)$ means from 0 to 1, not including 0 or 1. $\left[0, 1\right)$ means from 0 to 1 including 0 but not including 1.
arange
function.linspace
function.arange
function.linspace
function.
In [15]:
#5.1
import numpy as np
np.arange(1, 10, 0.25)
Out[15]:
In [16]:
#5.2
np.linspace(1,9.75,36)
Out[16]:
In [17]:
#5.3
np.arange(0,1.05, 0.05)
Out[17]:
In [18]:
#5.4
np.linspace(0,1,21)
Out[18]:
In [19]:
#5.5
x = np.arange(0,8)
print(3**x)
Answer these in questions in Python. Label your axes as x-axis
and y-axis
.
[4 points] Make a plot of the cosine from $-pi$ to $0$.
[4 points] We will see that the equation $e^{-x^2}$ is important in a few weeks. Make a plot of it from -2, to 2.
[8 pints] Plot $x$, $x^2$, and $x^3$ from -1 to 1. Label your lines with a legend, add a title, use the seaborn-darkgrid
style, and set the figure size as $4\times3$.
In [20]:
%matplotlib inline
import matplotlib.pyplot as plt
#6.1
x = np.linspace(-np.pi, 0, 100)
y = np.cos(x)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
In [21]:
#6.2
x = np.linspace(-2, 2, 100)
y = np.exp(-x**2)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
In [22]:
#6.3
plt.style.use('seaborn-darkgrid')
plt.figure(figsize=(4,3))
x = np.linspace(-1, 1, 100)
y1 = x
y2 = x**2
y3 = x**3
plt.plot(x,y1, label='x')
plt.plot(x,y2, label='x^2')
plt.plot(x,y3, label='x^3')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Nice Title')
plt.show()
In [ ]: