Welcome to the second lesson. We're going over three topics in this video:
expressions, datatypes, and variables. Go ahead and start
IDLE, which is the editor that comes installed with IDLE.
On Windows click on the Start button, type IDLE,
and I have multiple versions of Python installed on my computer,
but I'm just going to use IDLE that comes with Python 3.5. The course notes
describe how to start IDLE if you have Mac or Linux.
IDLE has two parts: the interactive shell
and, if you click on File, New File, the file editor
will appear. The interactive shell runs Python instructions one at a time and shows
you results immediately.
It's great for just experimenting and seeing what instructions do.
The file editor lets you enter Python code for complete programs.
There are other editor apps for typing in Python programs.
I use one called Sublime Text and there's another one called PyCharm.
But this course will use IDLE because it comes with Python and there's no
additional set up to do, and because it's the same across
Mac, Windows, and Linux. The interactive shell and file editor windows look
really similar so it's easy to get them confused. We can always tell the difference
because the interactive shell will have the triple angle bracket prompt.
In this lesson, we'll be entirely focused on the interactive show so close the
file editor for now. Let's begin by typing a simple Python instruction.
Type 2 + 2 and then press Enter.
In Python 2 + 2 is called an "expression".
It's the most basic kind of programming instruction in the Python language.
Expressions consist of values, such as the 2s,
and operators, such as the +.
Expressions always evaluate, that is
they always reduce down to a single value.
So in this example 2 + 2 is evaluated down to a single value
4. You can even think of the single value by itself as an expression:
the expression 2 evaluates to itself,
2. There's several other types of operators we can try out.
Type the following: 5 - 3, which does subtraction
and evaluates 2. 3 * 7,
which does multiplication and evaluates to 21. Note that multiplication is done with
the
asterisk and not the X. If you try to type something like
3 x 7, Python will just give you an error.
For division we use the forward slash.
So 22 / 7 evaluates to
3.14 and there are few other less common math
operators so check the course notes for them. Now, order of operation rules apply here.
Multiplication and division are done first, then addition and subtraction.
but you can use parentheses to override the usual precedence if you need to.
So let's look at two similar expressions: 2 + 3 * 6,
which evaluates to 20.
And then take (2 +3), in parentheses,
* 6, which evaluates to 30.
So I have a visualization tool so
that we can see how Python evaluates expressions step-by-step. In
2 + 3 * 6, the multiplication is done
first. So 3 * 6 evaluates to 18, and
2 + 18 evaluates to 20. While the parentheses
overrides the multiplication, so 2 +3 is done
first. And a value just in parentheses by itself just
evaluates to the value, and then 5 time 6 evaluates to 30.
No matter how big the expression is Python evaluates it
until it becomes a single value. Let's use this expression as an example:
(5 - 1) * ((7 + 1) / (3 - 1))
No matter how complicated the expression is,
all expressions are just values and operators, and it
always evaluates down to a single value. You can see this
step-by-step in the visualization tool: 7 +1
evaluates to 8. 3 -1 then evaluates
and then the operation inside the parnetheses evaluate
and then finally it evaluates down to 16.
All of these expressions evaluate to the same thing you can copy and paste
any of these and they evaluate
to the same value.
If you type something that Python can't understand it will display an error
message so let's trigger one of those in IDLE right now.
Type 5 + and press Enter.
You can always test to see whether instruction works by typing it in the
interactive shell.
These errors don't damage the computer. Don't worry. Professionals software developers get
error message while writing code all the time. So don't think that you
messed anything up.
All values have a data type. A data type is
a category of values, and every value belongs to exactly
one data type. So whole number values like
-2 and 30, for example, are integers.
Numbers with a decimal points such as 3.14 are called
floating point numbers. We also call integer and floating point values
ints and floats for short.
Note that even though 42 is an integer,
42.0 is a float. So there's also data type for text values. These are called strings.
When typing strings begin and end them with a single quote so Python knows
where the text begins and ends.
For example type 'Hello world'.
When the plus operator is used on strings
it's the "string concatenation" operator. Concatenation
means, "joins the strings together". For example let us type
'Alice' + 'Bob'
And this expression evaluates down to be single string
'AliceBob'. Less common is the multiplication
operator for the... for string replication which operates on an integer and a string.
This is handy if you have to make a string repeat itself. You can type in
a string 'Alice' * 3 to create
the string 'AliceAliceAlice'. Or try this expression which
uses string concatenation and replication. A string 'Hello'
+ '!'
* 10. And that will evaluate to a string value that has
'Hello' with 10 exclamation marks after it. Your programs can save values to the
computer's memory with variables.
A variable is kind of like a box that can store a single value.
You can do this with an assignment statement. In the interactive shell
enter spam = 42.
This creates a new variable named spam
and stores the value 42 inside of it.
You can think of it as a box with the label "spam"
and it has the value 42 in it. And normally you would give variables names that describe
the data they contain. Imagine if you were moving and you labeled all of your boxes
"stuff". That would be too helpful. But for these lessons we just need a generic name so
the words spam, eggs, bacon, and ham are often used in Python tutorials
because of the spam comedy sketch from Monty Python.
"Egg, bacon, sausage, and spam. Spam, bacon, sausage, and spam. Spam, egg, spam, spam, bacon, and spam. Spam, spam, spam, spam, egg, & spam. Spam-"
You can use variables and expressions
anywhere you would normally use values. A variable just evaluates
to the value it contains. So when I type spam,
that evaluates to 42. So in the interactive shell
type spam = 'Hello'. That will store the string Hello' in the variable spam.
Now when we have the expression spam + 'world'...
oh, whoops, well Python does exactly what we tell it to
which is why it doesn't have a space in between these words. Let's try that again.
spam + ' world' with space in front.
There we go. So this is just
regular string concatenation because spam evaluates down to the
string inside of it, and then that gets
concatenated with the ' world' string. Additional assignment
statements can change the value that's inside the variable. The old value is forgotten.
This is called "overwriting" the variable. So I could set span to a new vari-
uh, value such as 'Goodbye'.
This same spam + ' world' expression
evaluates to a different value. So you can think of
the old value as just being tossed out and forgotten and the new value
being assigned every time we have an assignment statement.
Expressions are one kind of instruction in Python. The other kind are statements.
Unlike expressions, statements don't evaluate to single value.
Although an assignment statement can have an expression as part of itself.
Let's try typing
spam = 2 + 2. The 2 + 2 expression
evaluates to 4 and so 4 is the value that gets
assigned to spam. We can even use the variable itself
to set the variable's new value. Try typing
spam = 10 and then spam = spam + 1.
This assigns spam a new value.
The new value for spam is equal to the current value of spam,
plus 1. Spam, spam, spam, variable, spam.
So if a Python instruction evaluates to a single value,
it's an expression. Otherwise it's a statement. But mostly we just generically
called them instructions or code.
To recap: IDLE is the editor program that you type code into.
It has two parts: the interactive shell which runs code
one instruction at a time, and the file editor
where you'll take complete programs into. Expressions are made up of values and
operators and they always evaluate down to one value.
And every value belongs to exactly one data type.
The data types we've seen so far are integers, floating point numbers, and strings.
And strings contain text, and they start and end with a single quote character.
And values can be stored in variables, which can then be used in expressions
anywhere values are used.