In [4]:
3.0 + 5.0
Out[4]:
In [5]:
#multiplication
3.0 * 5.0
Out[5]:
In [6]:
#exponents with **
2.0**3
Out[6]:
Note that the pound symbol #, is used to write a comment
The rules of arithmetic are obeyed in python
In [7]:
(15.0 + 3.5) / 0.5 - 0.5**2
Out[7]:
We can assign values to variables
In [9]:
x = 9.0
x
Out[9]:
In [11]:
# y is x times 5.0, if x is 9 then y = 45
y = x * 5.0
#Then 45 plus 9
y + x
Out[11]:
In [4]:
age = 17
# here we are using < , but you can also use, <=, >, == for equal and != for different
if age < 21 :
print "under 21"
else:
print "21 or older"
In [18]:
i = 1
while i < 10 :
print i
i = i + 1
In [1]:
%matplotlib inline
#importing libraries to use
import numpy as np
import matplotlib.pyplot as plt
In [2]:
#function that gives us the cosine of a number
def f(x):
return np.cos(x)
print f(np.pi)
print f(0)
In [3]:
#create an array of numbers from 0 to 5 with 0.1 increments
t = np.arange(0.0, 5.0, 0.1)
t
Out[3]:
In [4]:
#evaluate function for values of t
f(t)
Out[4]:
In [5]:
#plot cosine
plt.plot(t, f(t))
plt.show()
In [ ]: