Based on Lecture Materials By: Milad Fatenejad, Katy Huff, Joshua R. Smith, Tommy Guy, Will Trimble, and many more
This lecture is on basic programming in python. In order to do the examples, we are going to use an environment called IPython Notebook. I expect this lecture to be interactive, so stop me at any point if you have questions. The correct power dynamic is that people are the masters and the machines are servants. The computer is a hammer; it exists to help us get things done. We can hammer nails with the handle, with the claw of the hammer; some of us even hammer nails with bricks. But when you learn what part of the hammer works best with nails, and have some experience swinging it, you spend less time worrying about the hammering and more time worrying about your furniture.
So now would be a good time to roll out PEP 20, The Zen of Python
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!
Here are some links to tutorials and reference material.
You can run python commands in a handful of ways; you can create executable scripts, you can run the python interpreter, you can run IPython, or you can run IPython notebook. IPython is an alternative to the built-in Python interpreter with some nice features. IPython notebook gives you interactive access to the python interpreter from within a browser window, and it allows you to save your commands as a "notebook".
Let's give the built-in interpreter a spin just this once. Open a Terminal window, which starts your default shell. Type
python
And you should see python start up. Type
print "Fresh out of parrots"
Note the black-and-white wallpaper.
Escape from python with
quit()
IPython has more useful features for interactive use than the standard python interpreter, but it works in the same way--it is interactive, and you get there from the command line. IPython Notebook uses javascript to allow you to enter python commands in your browser and show you the result in the browser. We'll use it from here on out.
print "hello world"
The box above is called the input cell; commands you put here will be fed to the python interpreter one at a time when you press Shift-ENTER.
The output of the command, or the error message, appears below the line you entered it on.
The panel which may appear on the left has some notebook options; you can minimize the panel by double-clicking on the bar separating the windows.
print "Try and tell that to the young people"
print "of today--they won't believe you."
If you hit ENTER only, IPython Notebook gives you another line in the current cell.
This allows you to compose multi-line commands and submit them to python all at once.
Up and down arrows will allow you to move the cursor to different cells in the notebook, including these cells containing text (which you can edit in your browser).
Only the cells for which you press Shift-ENTER or Control-ENTER will be executed by the python interpreter.
You can enter the same line over and over again into the interpreter. It's weird, but it's life.
i = 0
Shift-ENTER executes and moves to the next cell.
Control-ENTER (Cmd-ENTER on Macs) executes the cell and does not move to the next cell.
Try entering this cell a few times:
i = i + 1
print i
If you want to create new empty cells, you can use Insert -> Insert Cell Below or use the Insert Cell Below button at the top of the notebook.
IPython has some nice help features. Let's say we want to know more about the integer data type. There are at least two ways to do this task:
help(int)
which displays a scrolling text help, or
int?
Which displays a shorter help summary in the magic pane at the bottom of the screen. You can minimize the magic pane when it gets in your way.
If you wanted to see all the built-in commands available for something, use the dir
command. Check out all of the methods of the object "Hello world", which are shared by all objects of the str type.
dir("Hello world")
There's a method that looks important -- .swapcase()
. Let's see what it does:
"Hello world".swapcase()
Hrm. Ahem.
If your code is in a file, you can execute it from the IPython shell with the %run command. Execute hello.py
like so
%run hellp.py
Ooops. We misspelled hello.py, and python is giving us an error message. Change the line above to hello.py, hit Shift-ENTER, and see what it does.
To clear everything from IPython, use the %reset command.
mystring = "And three shall be the count."
print mystring
%reset
print mystring
Note that the error message contains a recap of the input that caused the error (with an arrow, no less!) It is objecting that mystring
is not defined, since we just reset it.
All programming languages have variables, and python is no different. To create a variable, just name it and set it with the equals sign. One important caveat: variable names can only contain letters, numbers, and the underscore character. Let's set a variable.
experiment = "current vs. voltage"
print experiment
voltage = 2
current = 0.5
print voltage, current
This last line shows us a piece of the syntax of the print
command: if we give print
comma-separated variables, it prints them with a space between them.
Like most programming languages, things in python are typed. Data types reflect the way that the data is ultimately stored as ones and zeroes and affect the operations that are permitted on the data. We have already defined three different variables: experiment
, voltage
, and current
. They are stored in three different data types: string
, integer
, and float
.
string
data types are for lists of consecutive characters, such as text.int
data types are exact representations of the integers.float
data types, short for floating point numbers, are inexact representations of numbers that are internally stored in scientific notation.There are many other data types, but these are fundamental.
You can inspect the type of a variable by using the type
command.
type(experiment)
type(voltage)
type(current)
Why should you care about data types? In some cases, failing to pay attention to the data types will cause wrong answers.
Python is a dynamically typed language (unlike, say, C++).
Dynamic typing means that you don't have to declare the type of a variable when you define it; Python just figures out the fundamental data type based on how you set the variable. Lets say you set a variable. Sometime later you can just change the type of data assigned to a variable and Python is perfectly happy about that.
Here's an example of dynamic typing. What's the type of data assigned to voltage
?
type(voltage)
Lets assign a value of 2.7 (which is clearly a float) to voltage
. What happens to the type?
voltage = 2.7
type(voltage)
You can even now assign a string to the variable voltage
and python would be happy to comply.
voltage = "2.7 volts"
type(voltage)
I'll let you ruminate on the pros and cons of this construction while I change the value of voltage back to an int
:
voltage = 2
It is possible to coerce (a fancy and slightly menacing way to say "convert") certain types of data to other types. For example, its pretty straightforward to coerce numerical data to strings.
voltage_string = str(voltage)
current_string = str(current)
voltage_string
type(voltage_string)
As you might imagine, you can go the other way in certain cases. Lets say you had numerical data in a string.
resistance_string = "4.0"
resistance = float(resistance_string)
resistance
type(resistance)
What would happen if you tried to coerce resistance_string
to an int? What about coercing resistance to an int? Consider the following:
resistance_string = "4.2 Ohms"
Do you think you can coerce that string to a numerical type?
Assign three variables, called versionnumber
, numberofpythons
, and thenumberten
with the values 2, 6, and "ten" with the types float, int, and string respectively.
This cell prints the values, so you can check if the values are what you expect:
print versionnumber
print numberofpythons
print thenumberten
Test your work. Run the following three expressions. If all three evaluate as True
you have set the data types correctly.
print type(versionnumber) == float
print type(numberofpythons) == int
print type(thenumberten) == str
Let's say you had some voltage data that looks like the following
0
0.5
1
1.5
2
Obviously, if you just assigned this data individually to a variable, you'd end up with the following types
0 -> int
0.5 -> float
1 -> int
1.5 -> float
2 -> int
But what if you wanted all of that data to be floats on its way in? You could assign the variable and then coerce it to type float:
voltage = float(1)
But that's ugly. If you want what is otherwise an integer to be a float, just add a period at the end
voltage = 1.0
type(voltage)
This point becomes important when we start operating on data in the next section.
What's the point of data if we aren't going to do something with it? Let's get computing.
a = 1
b = 2
c = a + b
c
type(a), type(b), type(c)
So we got a value of three for the sum, which also happens to be an integer. Any operation between two integers is another integer. Makes sense.
So what about the case where a is an integer and b is a float?
a = 1
b = 2.0
c = a + b
c
type(a), type(b), type(c)
You can do multiplication on numbers as well.
a = 2
b = 3
c = a * b
c
type(a), type(b), type(c)
Also division.
a = 1
b = 2
c = a / b
c
ZING!
This is why type is important. Dividing two integers returnes an integer: this operation calculates the integer part of the quotient and discards the remainder.
If everything was a float, the division is what you would expect.
a = 1.0
b = 2.0
c = a / b
c
type(a), type(b), type(c)
There are operations that can be done with strings.
firstname = "Johann"
lastname = "Gambolputty"
When concatenating strings, we must explicitly use the concatenation operator +
which takes two strings and creates one longer string.
fullname = firstname + lastname
print fullname
fullname = firstname + " " + lastname
print fullname
There are other operations defined on string data. Use the dir
command to find them. One example I'll show is the .upper()
method. Lets take a look at the documentation.
str.upper?
So we can use it to upper-caseify a string.
fullname.upper()
You have to use the parenthesis at the end because upper is a method of the string class.
For what its worth, you don't need to have a variable to use the .upper()
method, you could use it on the string itself.
"Johann Gambolputty".upper()
What do you think should happen when you take upper of an int? What about a string representation of an int?
That wraps up this lesson. We tried out the IPython shell and got some experience with ints, floats, and strings. Along the way we talked about some philosophy and how programming is like hammering.
You can paste things into the IPython console by copying text from your machine with ctrl+c and typing %paste at the IPython prompt. The %paste is necessary syntax for multi-line clipboard deposits.
%paste # Use ctrl-c in the browser %paste in the terminal