3) Intro to python using ipynb¶
Related text: https://jakevdp.github.io/WhirlwindTourOfPython/index.html
Now to start programming! We’ll be documenting what we’re doing in our markdown cells, and writing python in our code cells.
This intro is based on the related text noted above.
Some general notes about python¶
Python is an interpreted, not compiled, language.
Benefit: you can run your python scripts anywhere you install python, without the need to worry about getting code to compile correctly on different platforms.
Drawback: it is slower to run than a compiled language like C++ or FORTRAN.
Good news: many libraries are available for python that are written in fast, compiled languages (they did that trouble for you). If you use them, you will save oodles of time both writing and running your scripts. Thus, we will be using them!
You will import libraries as simply as this:
In [1]:
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
(c) xkcd, https://xkcd.com/353/
More motivation for learning python:¶
From the IEEE Spectrum, 2018-07-31, ranking by typical IEEE member and Spectrum reader, with a “focus on things like what’s au courant for open source projects”:
On to some simple python¶
Remember how I mentioned that simple math is not simple in bash? It is in python!
In [ ]:
2+2
A few important points: - The numbers are very helpful as you figure out what has been run in what order - By default, the output of the last line of code in a cell will be output.
In [ ]:
1*2
3*4
You see that only the last line is output. You can also control what is printed. And, generally, we’ll be wanting to store output in variables.
In [ ]:
result_1 = 1 * 2
print(result_1)
result_2 = 3 * 4
The numbered lines can are stored and can be accessed!
I’m demonstrate below that you can embed your variable output in text. The “:raw-latex:`\n`” gives me another new line so it is easier to see the three outputs.
In [ ]:
print("In is type {} and In = {}\n".format(type(In), In))
print("The third input was '{}'\n".format(In[3]))
print('The third input was "{}"\n'.format(In[3]))
print("Out is type {} and Out = {}".format(type(Out), Out))
Note that Python has what is referred to as dynamic, or duck, typing. That is, if it quacks like a duck, it calls it a duck. This is in contrast to languages like C or Java that have static typing, in which the type of variable must be declared. This provides speed and flexibility, but also slows down running because variables are stored both with the value and type. More on this later.
In [ ]:
var_1 = 'che_696-001'
var_2 = 4
var_3 = 4.3
print(type(var_1), type(var_2), type(var_3))
Unlike other languages (like C++ and FORTRAN), the variable type of object assigned to the variable can be changed.
In [ ]:
var_1 = 8
var_2 = 4.2
var_3 = 'python is great'
print(type(var_1), type(var_2), type(var_3))
In python, variables are pointers. They can first point to a string, and then point to a integer, and then to something else. We’ll come back to this point at times you may not get the behavior you expect if your mental model is “var_2 is 4” versus “right now, var_2 points to 4”. For example, think about what the output will be from the following statements:
In [ ]:
x = [1, 2, 3]
y = x # now x and y point to the same object, a list
print("x =", x, "and y =", y)
x[0] = 4
print("x =", x, "and y =", y)
Right now, x
and y
point to the same object. That can be
changed!
In [ ]:
x = 4
print("x =", x, "and y =", y)
In [ ]:
a = [1, 2, 3]
b = a
a = [4, 2, 3]
print("a =", a, "and b =", b)
In [ ]:
c = [1, 2, 3]
d = c
c.append(4)
print("c =", c, "and d =", d)
In [ ]:
e = [1, 2, 3]
f = e.copy()
e[-1] = 4
print("e =", e, "and f =", f)
In [ ]:
a_num = 3.14
g = a_num
h = a_num
print("g =", g, "and h =", h)
a_num = 4
print("g =", g, "and h =", h)
FYI: When I call type
I get the type of the object the variable
name points to at that moment.