In [ ]:
# setting a variable in python uses dynamic typing
a = 1.23
# in this cell, several python commands can be given. Execute this cell with "SHIFT-ENTER"
# although just writing the variable will show it's value, but this is not the recommended
# way, because per cell only the last one will be printed and stored in the out[]
# list that the notebook maintains, use the print() function for this, which we will see below.
a
a+1
The right way to print is using the official print() function in python and this way you can also print out multiple lines while the cell is executing, but no output value will be added to the Out[] list.
In [ ]:
print(a)
print(type(a))
Now you can see that each call to print() will cause output on a new line,and also note they are not in the Out[] list
overwriting the same variable , now as a string
In [ ]:
a=111
a="1.23"
print(a,type(a))
In [ ]:
a,type(a)
In [ ]:
# checking the value of the variable again
a
btw, did we tell you that if you know latex, you can do some nice math here as well:
$$ { v^2 \over r } = {{ G. M(<r) } \over r^2} $$neat, eh?
Python3 should be the de-facto standard, but Python2 is still being used today. So safeguard printing so this notebook can still be used in python2:
In [ ]:
from __future__ import print_function
Now we can print(pi) in python2. The old style would be print pi, which does not work in python3!
In [ ]:
pi = 3.1415
print("pi=",pi)
print("pi=%15.10f" % pi)
In [ ]:
# for reference, here is the old style of printing in python2 if you want to uncomment this and try it out
# print "pi=",pi
Most programming languages have a few common ways to control the flow of the program. The common ones in python are
While you are looking at this code, note that white-space (indentation) controls the extent of the code, and unlike other languages, there is no special {} symbol or end type statement. This forced indention is probably the single most confusing thing about those new to the python language.
In [ ]:
n = 1
# if/then/else
if n > 0:
print("yes, n>0")
else:
print("not")
# loop over a list
for i in [2,4,n,6]:
print("i=",i)
print("oulala, after this for loop, i=",i)
# while loop
n = 10
while n>0:
# n = n - 2
print("whiling",n)
n = n - 2
print("last n",n)
A list is one of four major data structures (lists, dictionaries, sets, tuples) that python uses. It is the most simple one, and has direct parallels to those in other languages such as Fortran, C/C++, Java etc.
Python uses special symbols to make up these collection, briefly they are:
In [ ]:
a=[1,2,3]
a[1]=22
print(a)
In [ ]:
a1 = [1,2,3,4]
a2 = range(1,5)
print(a1)
print(a2)
a2 = ['a',1,'cccc']
print(a1)
print(a2)
a3 = range(12,20,2)
print(a3)
In [ ]:
tuple(range(3))
In [ ]:
a1=list(range(3))
a2=list(range(3,6))
print(a1,a2)
#a1=[0,1,2]
#a2=[3,4,5]
In [ ]:
a1+a2
Python lists have some interesting slicing operators that allow you to take sub-lists. See if you can figure it out from the following examples:
In [ ]:
a=range(10)
print(a[2])
print(list(a[2:4]))
print(list(a[-2:]))
print(list(a[2:]))
print(list(a[:2]))
In [ ]:
import math
import numpy as np
In [ ]:
math.pi
In [ ]:
np.pi
In [ ]:
a=np.arange(0,1,0.2)
a
In [ ]:
b = a*a
c = np.sqrt(a)
print(a)
print(b)
print(c)
In [ ]:
In [ ]:
# now for some plotting, using matplotlib, neatly integrated with numpy arrays
# we are going to plot the arrays we just created
# these next two lines are optional, if you want a different backend
import matplotlib
matplotlib.use('agg')
# any command starting with % is special to ipython notebooks, it tells to produce plots inline,
# and not externally in a different graph (although that's fun too)
%matplotlib inline
import matplotlib.pyplot as plt
In [ ]:
plt.plot(a,b,'-bo',label='b')
plt.plot(a,c,'-ro',label='c')
plt.legend()
In [ ]:
# purposly adding another plot in another cell... see what that does
plt.plot(a,a+1)
In [ ]:
# plt.show() is only needed to finally plot on screen, in the case an external plot was to be produced
plt.show()
In [ ]:
n=5
x = np.arange(n) * 2.0
print(x,x.mean())
for i in range(n):
x[i] = x[i] + 0.1
print(x,x.mean())
y = x*x
for (i,xi,yi) in zip(range(n),x,y):
print(i,xi,yi)
y[i] = 0.0
print(y)
In [ ]:
print(Out)
In [ ]: