Hello, and welcome to the second episode of the Software Carpentry lecture on Python. This episode will introduce you to the basic elements of the language.
Python is a simple interpreted language.
The word "interpreted" means that unlike programs in C, Java, Fortran, and C#, Python programs don't have to be compiled before they can be run: commands are executed immediately.
To run Python interactively from a command shell, just type
python. This will bring up a prompt, which is usually three greater-than signs.
You can have Python do simple arithmetic…
…or manipulate strings of text, just by typing in commands.
More often, though, you will put commands in a file and have Python execute that.
For example, you can use a simple editor like Pico…
…to put the same commands we just ran in a file called
…and then have Python run that file.
A third choice that combines the best features of the two above is to use an integrated development environment, or IDE. We recommend Wing 101, the free student version of Wing, but there are many others.
The IDE gives you an editor that is customized for displaying Python source code…
…and an interactive shell for trying things out. We'll explore some of the other tools IDEs provide later in this course.
Back to Python itself. Like other languages, it allows you to create variables, which are names for values.
Unlike compiled languages, though, Python variables are created by use:
there's no need to declare them explicitly.
For example, assigning the string
'Pluto' to the variable
planet automatically creates that variable.
We can then print out its value to make sure it's right.
Internally, Python stores a table of all the variables created so far, and keeps track of the values they refer to.
If we assign the string
'Charon' to the variable
moon, Python adds an entry to this table for
moon, allocates some memory to store the string
'Charon', and makes the variable point to that string.
We can assign variables to variables, too, with statements like
p = planet.
This creates a new variable as before, but this time, that variable points to the same value that
planet was pointing to.
Sure enough, if we print
p, Python displays the string
A variable in Python is just a name.
Unlike variables in some other languages, Python's variables do not have specific data types.
For example, we can assign a string
…creating the table entry shown here…
…and then immediately assign the integer 9 to the same variable. This would be an error in a language like Java, since strings and integers are different data types, but Python is quite happy to do this.
One other important feature of Python is that values are garbage collected.
This means that if nothing refers to a piece of data any longer, Python recycles its memory.
For example, once we assign 9 to
planet, nothing is pointing at
'Pluto' any longer, so Python is free to reclaim that memory and use it to store other information.
Python is relaxed about the type of data assigned to variables, but it does insist that variables have values before they are used.
For example, let's assign the string
…and then try to print the value of the variable
plant (without an 'e').
Python immediately displays an error message because we haven't defined a value for anything called
It does not fill in some sort of default value for undefined variables, such as 0 or the empty string…
…because doing so can mask errors resulting from simple typing mistakes.
Also, while we're here, notice Python's commenting convention: anything from the '#' character to the end of the line is ignored, which allows programmers to add explanations (or warnings) to their programs.
In contrast with variables, values in Python do have types.
For example, suppose we try to multiply a string by a number. Python is OK with this: it knows what
* means when applied to a string and an integer.
But if we try to add a string and a number, we get an error message, because Python doesn't know what
+ means when applied to those types.
In this case, you'd probably think that
'two3' would be a sensible answer.
But then what should be the result of adding the string
'2' to the string
'3'? Should it be the string
'23', the integer 5, or the string
In programming languages, experience teaches that doing too much is just as bad as doing too little.
If we really do want to "add" the string
'2' to the integer 3, we can use some built-in functions to convert one or the other first.
int takes a string of digits and produces the corresponding number…
…while the function
str turns a number (or almost anything else) into text.
And speaking of numbers, Python provides several types.
The simplest are integers, which on most machines are stored in 32 bits (though 64-bit machines are now becoming common).
Python also provides floating point numbers, which are usually stored in 64 bits…
…and complex numbers, which are written using 'j' for the imaginary part, and are stored in a pair of 64-bit numbers.
x is a complex number, then
x.imag are its real and imaginary parts.
Python provides the usual arithmetic operations on numbers.
Plus for addition…
…which can also be used to concatenate strings.
Minus for subtraction…
Star for multiplication…
…which can be used on strings as well.
Slash for division…
…which produces an integer (throwing away any fractional part) if both its arguments are integers.
Double star for exponentiation…
…and percentage sign for integer remainder.
Like other languages in the C tradition, Python allows you to use in-place forms of binary operators to make your programs more readable.
For example, suppose we've assigned 500 to the variable
If we write
years += 1
, Python adds 1 to the value in
It's exactly the same as writing
years = years + 1, but it's shorter and easier to read, particularly if the expressions involved are long or complicated.
Sure enough, we've incremented
years by 1.
Other binary operators have in-place equivalents as well. Here, for example, we use
%= to replace the value in years with the old value modulo 10.
Again, this is the same as
years = years % 10.
And the answer is what we expect.
Arithmetic operators turn numbers into numbers; comparisons turn numbers or strings into
Simple ones, like less than, are written as you'd expect.
But Python uses
!= for "not equal".
And a double equal sign to test for equality.
Remember: a single
= is assignment, while a double
== is a comparison.
Of course, partial inequalities like
>= can be used too.
And unlike most programming languages, Python allows inequalities to be chained together, just as in mathematics.
You can even change the sense of the comparison midflight, but please don't: it's very hard to read.
And please don't try to do any kind of comparison on complex numbers other than equality and inequality: it doesn't work in Python any more than it works in mathematics.
Now that we've seen the basic "atoms" of Python, the next lecture will explore how to combine them to make useful programs.