Professional Documents
Culture Documents
Lesson 9
Lesson 9
Huh.
Little bit of a difference.
And in fact, there's no out here because no value is returned.
Print returns that NoneType and doesn't print anything out.
It just printed something.
At this stage, you may be saying, well why do this?
Why the difference?
What we're going to see is, when we're
in the middle of a computation and we want to print something
out to the shell, we can use a print command to do that.
If we just return the value, that
goes back into the computation to be used for the next step.
And so in the simple first case, the whole value
of the computation was 3 plus 2 equals 5.
It was printing all 5.
In the second case, the side effect is to print something.
The value returned is nothing.
There is no value.
We'll come back to that as we go along.
I've already hinted at this, but now we
can start putting things together.
To combine objects and operators into expressions,
we use a standard form.
And the syntax is simply an object, an operator,
and another object.
Any expression like this that is syntactically valid,
has a value, which is itself a type.
So we talk about the types of scalar objects,
we talk about the types of more complicated expressions.
What would those expressions look like?
Well, here's the first set of them.
There's a lot on this slide, but you're
going to get used to them.
And most of them make sense.
If I want to add together either two ints or two floats,
my expression is to have two numbers
as I just did, 3 plus 2.
The value associated with that is the combination
of adding the two things together.
I can subtract, I can multiply.
And for these examples, if both I and J are ints,
the result is an int.
If either of them or both of them are floats,
the result is a float.
And let's look at an example.
If I do 3 plus 2, I get 5.
If I do 3.0 plus 2, I get the float 5.
So Python is always using, if you like,
the higher level representation when it
does those kinds of operations.
Divisions, a little trickier, because I
have both standard division, which will be a float,
and I have integer division, which
will be the quotient without the remainder.
And let's look at two examples.
If I do 5 divided by 2, I get 2 and 1/2.
If I do 5 with two slashes divided by 2,
I get the integer part, which is 2.
The .5 is thrown away.
And if I wanted to get the .5 I could
do 5 remainder 2, which is the percent sign,
and that gives me the piece left over when I divide those two
pieces together.
Finally, I can raise something to a power.
And that is done right here with I. Two stars, J. So one star
is multiplication.
Two stars, gives me power.
Once I've got that, I'm ready to start putting together pieces.
In particular, I've got different operations.
So I have to think about how do I put together pieces in ways
that control what I want to do?
And here, we're going to use parentheses
to tell Python in what order to do the operations.
There's a precedence to those operations.
And those are shown right down here, going in this order.
Exponentiation, powers are always done first.
Then multiplications, then divisions.
And then finally, additions or subtractions.
And overall it's done in a left to right order, as it
appears inside the expression.
And that means that as I change where the parentheses are,
I'm changing the order in which I'm doing things.
So to do an example, if I take 3 times 5 plus 1.
That's going to give me 16.
And that's because the multiplication is done first,
3 times 5 gives me 15.
And then the addition is done, to give me 16.
On the other hand, if I say 3 times,
and then in parentheses 5 plus 1,
the parentheses say reduce this expression first.
Add 5 and 1.
That gives me 6.
And then do the multiplication, which gives me the 18.
As a consequence.
I can create very complicated expressions.
And I will simply use parens open
and close-- to define what operations to do first.
Always simplifying inside out, before then using the operator
precedence order, in order to decide what
the expression actually means.