So in the previous video,
we stored some strings in variables but we didn't really explain what was going on.
So we wanna go through and
actually explain what is happening a little bit more now behind the scenes.
So when the Python programming is running,
everything the program needs ends up being stored somewhere in the computer's memory.
You don't actually need to know where that is because the computer
actually tracks that.
For the program code itself, in other words the instructions that you type in,
will be stored in one area of memory, but also the data that it's gonna work on,
the variables that you've typed in etc., will be stored somewhere else in memory.
So the variable,
the name that we assigned is just really a way to give a meaningful name to
a area of memory into which we can place certain values like strings and so forth.
So when we create a variable called greeting like greeting = Bruce.
Python's actually allocating an area of computer memory for us, and
it knows to refer to that area whenever we talk about the variable greeting.
So think of it like a box in memory that's storing the values, Bruce in this case,
and the box has got a name on, a sticker on it that says greeting.
And when we type in greeting, the computer knows to go and
find the value that's in that particular box.
So that all happens automatically behind the scenes.
Just in terms of creating these variable names,
there are a few rules to keep in mind when you actually created them.
Firstly, the actual name has to start with either a letter, it could be upper or
lower case, or an underscore.
So in other words, we can type _myName = Tim, that's valid.
So a variable name can start with either the letter or an underscore but
we can't start with a number.
Actually get an error.
But we can actually also use letters.
We can also use numbers in our variable names, just not as the first letter.
So, we can put something like Tim45 = Good.
That's quite valid and we could put a combination.
So we could put Tim_Was_57 = Hello and
that's quite valid as well.
Now the other important thing to consider here is that I could also
type a variable name called Greeting, with uppercase G.
And that's different and distinct to this variable name.
So that one's all in lowercase, this one's got an uppercase first letter.
Even though the actual word is the same,
they are treated as different variable names as far as Python is concerned.
Now in terms of the allocation of memory,
it actually happens at the point that you've initialized it.
So in other words once you've talked in the variable name and put an equal and
then the value, at that point python will allocate the space for
that in the computers memory.
In terms of using variables we saw that we could
use the prints statement to print some out.
We could do something like TIm was here, or
Tim was 57 + a single-quote space plus greeting.
We could run thatm, Hello Bruce.
Obviously it's taking the value of Hello from Tim_Was_57 and
Bruce from the variable greeting and
actually printing that on the screen with a space between the two there.
We could do something like age equals 24, print age.
We can also do something like greeting plus age, since we're on this program.
Now this is where we actually get an error.
So you can see it should the first Hello Bruce which we saw before,
which was this first line.
We then assigned a value of 24 to the variable age, and we printed the age,
which 24 worked.
We're going to try to do this next line, print greeting plus age.
And we actually got an error in red.
And you can see that Python's actually trying to help you here by actually sort
of saying, look I've got a problem with what you've typed.
It's actually come up and actually said, I can't convert int object to a string.
Str is the abbreviation of string implicitly.
So what we've tried to do is Get it to do something that Python can't work with, and
this is what happens when you get an error.
So Python will hopefully show you some information which hopefully leads to you
figuring out what the problem was, and it actually shows also the actual lines.
So if we move over a little bit you can see it's saying the name of the Python
file which was one that I created for this video varibles.py py.
And it says that line 12, which equates to line 12 over here.
So it's very handy to look at these when you trying to figure out whether there's
a problem, and you can go back to line 12, and know this was where the problem was.
Now the other thing is that IntelliJ actually does a lot of this for
you automatically as well, it tries to help you out with hints as well.
So, over here we can see these little dashes.
If I actually hover over there with my mouse,
we actually get a helpful error message.
Expected type string str got int instead.
So that's IntelliJ, trying to give you some information to say,
you've got this wrong, basically.
And so it can be really useful when you're creating Python programs to keep
checking these hints out to see whether there actually is an issue.
Notice also there's a keyboard shortcut,
if you want to expand this to get some more information.
So on a Mac, I can actually type Cmd + F1, I get more information like so.
And press Ctrl + F1 on Windows machine or a Linux machine and
you can get the full message.
So that's very handy to refer to that all the time, and
later in the course we'll be looking at handling these errors.
In other words, if you're program crashes or goes to crash,
we can handle that more gracefully.
But for now, think of those error messages that come up as your friend because what
it's really doing, it's telling you that your code is not gonna work and
providing as much information as possible to you to help you try and
figure out what the solution is.
So what can be frustrating, you really did learn a lot from errors like these,
because you can figure out something that Python doesn't like, and
how to work around that to come up with actually a solution.
Okay, so moving on,
and later in the course we'll be talking about these conversions.
So, in terms of this particular error message, what's actually happening was
again we determined that Python wasn't happy because we're trying to add the age,
the numeric variable age to a number, and you can't actually do that.
It doesn't actually work like that.
So Python doesn't know what to do in that scenario.
It knows what to do in the situation like this,
we're actually adding strings together.
Because Tim_Was_57 is a string, and
greeting was a string and it knows to concatenate those.
Likewise, if we tried it to do something with two numbers,
it knows to add to some of those two numbers.
But in this scenario where we've got a string and
a number, Python doesn't actually know how to do that.
Not know how to handle that and that's why we actually got the message.
So what we dealing more with actually how to get around that.
And do note that some other programming languages, Java being one of them,
does actually allow you to actually do something like that.
And in that scenario what would happen is it would actually automatically convert
what you're trying to do.
In this case, it would append the value of 24 to the string age.
But Python doesn't do that, it makes you get it right at the start.
And that's actually a good thing in many ways because it means you're not
going to be getting any weird little bugs that crept about
because of automatic type conversion.
That you may get in other programming languages.
So the bottom line is, you need to get that right first, and
really it's just a matter of checking out the error message and fixing it.
And we're talking more about this type of thing as the course progresses anyway.
So moving on, let's talk now a little bit about variable types.
So Python has got several built-in data types.
We've got a heap of them and we'll be dealing with those in detail.
Data types can be classified as numerics, sequences, mappings, files,
classes, instances, and exceptions.
And I [LAUGH] know that sounds like a lot and it probably is but
you'll be introduced to all of these as the course progresses.
But for now we're gonna look at numeric data types and
one of the sequence types which you've already seen.
String being a sequence type, which we've actually looked at before.
Now Python 2, the older version of python which as I mention is no longer
being used and we're not using it in this course.
That actually had various data types such as int, long, float, and complex,
but in Python 3 the change they've made is there's no longer a long type, and
the int now behaves like a long used to.
So they've really consolidated the amount of data types.
Now we're not going to discuss complex numbers
without going into too much detail.
A complex number contains a real and
an imaginary part based on the square root of minus one.
Now, if you understand a complex number and wanna use Pyton to manipulate them,
then by the end of the course you'll understand Python well enough to do so.
But really the complex number theories in advance branch of mathematics slash
engineering, it's not really appropriate to go into too much detail with that.
But let's look at the basic data types, and
a basic data type we're going to start with an integer.
An integer being a whole number so a number that hasn't got any decimal points.
First, you can see on the screen I have typed in an integer.
So an integer is a just whole number.
It's a number having no fractional part where as a float
in the other example is another name for a real number.
That is a number having a fractional part after the decimal point.
There is a very small number of computer languages that make no distinction between
real numbers and integers.
I guess you could say that integers can be considered
just special cases of real numbers.
But when represented in a computer, computations using integers,
these whole numbers, are significantly faster than using floating point numbers.
So that's one of the main reasons that we actually distinguish in programming
an integer as a whole number and a real number,
a floating point number and that's because it's a lot faster to process.
So anything that doesn't need, as a general rule, a floating point number for
calculation purposes, you shouldn't actually be using it.
Because you'll find that your programs will operate a lot faster because of it.
There are pros and cons, because of the way that integers are stored in
the computer's memory, there's actually a limit to the size of an int.
It's about 9 trillion in European terms.
I think it's nine quintillion, in American format.
But effectively the Python 2 long data type the Python 3,
effectively have no real maximum size.
Now floating point numbers on the other hand,
that's floats, they're used to represent numbers having a fractional part.
So the maximum float value on a 64 bit computer is, well that's, it's just huge,
we'll go through it, but it's quite large.
Basically a number that actually moves the decimal point 308 places to the right.
And the smallest float is a negative number which has 307 zeros before
the decimal float.
So, it's obviously a huge variance and
a huge number can be stored in that floating point number.
Floating points have 52 digits of precision, which should be adequate for
most purposes from financial calculations where you're needing decimals.
And because Python doesn't really have variable declarations,
that's where you specify the type of a variable before you can use it, and
lots of other computer languages do that.
It's probably tempting to think that you don't need to understand the difference
between an int, a long, and a float type, but
really it is something you really need to consider when writing your programs.
This makes more sense when we look at some of the operators that can be
performed on numbers.
So let's just go through the first few scenarios.
So I typed a variable called a with an integer value of 12.
Let's do another one, b = 3.
And let's perform some calculations.
We'll do print, we'll do a plus b, we should get the value of 15,
a take b, we should get the value of nine.
Print, a times b, the value of 36, we should get and we'll test this shortly.
a divided by b, we should get the answer four, but
there's a special rule there about that which I will talk about shortly.
And we're going to introduce another operator, //b and
I'll explain that shortly, and last one we're going to use is the remainder,
remainder of a divided by b effectively.
Otherwise dividing 12 by 3, how much remainder is left after that.
So let's just run this, and then we'll just go through it.
So the first one is 15.
The second one is 9.
And the third value is 36.
And there's the fourth one, a divided by b.
The actual answer is returned as a float, with a decimal point, .0.
And that's the thing to keep in mind, with a divide,
a division operation, in Python, by default it returns as a float.
If you want the result to return as an integer,
a whole number, you need to use the two slashes in this case.
So a, slash slash b, returns four as a whole number.
And lastly,
the remainder of a divided by b was zero because b goes into a four times.
Four times three is twelve and there would be no remainder.
So that's how you do some basic calculations in Python.
Now this slash and slash slash can really trip you up, and
we're gonna be talking about loops later.
But I just want to give you an example of typing in a loop where you could get into
difficulty without using that slash or
slash slash because it can be a common source of problems.
So let's look at an example here.
We're gonna type in some code, and again we'll talk more about this for, and
the concept of loops later.
But for now we'll just type in for
i in range, 1, 4, : print i.
So if we run this, You get the values 1, 2, 3.
We've actually changed this here and put a divided by b, cuz instead
of putting a constant number, a literal, we can actually put an expression like so.
If we run that, we actually get an error.
And that's because float object cannot be interpreted as an integer,
and that's because the answer,
if you remember, with division by default is a floating point number.
The range actually needs an integer, so
we'd actually put two slashes in that scenario.
Run the code again, it actually correctly returns the results one, two, and three.
As we progress through the course, you'll see other ways of actually handling this,
but I just wanted to point that out.
At this point in time, that's a common source of problems when you're
looking at Python for the first time.
And again, don't be too worried about the concepts of what this is doing,
we'll be covering loops, which is what this actually is In future videos.
So the code something like a plus b, that's actually an expression and
I mentioned expressions.
In Python an expression is anything that can be calculated to return a value.
So we've really only used simple expressions to date like a plus b or
a take b but we can get a lot more complex.
We could do something, just let me clear some space here.
So a lot more complex if we want to,
we can do something like print (a + b / 3- 4 * 12),
and that's quite a valid expression.
So if we actually run this, you might be surprised to look at that and think,
why is that come up with a value of 35?
You'd expect looking at that, that the answer should actually be 12.
It should be a plus b is 15 divided by 3 is 5, take 4 is 1, times 12 is 12.
So why have we actually got 35 here instead of 12?
[LAUGH] Now don't worry, your computer's not broken.
And you can reliably perform arithmetic in Python.
But, as is the case in all programming languages,
you have to understand some of the basic rules of the arithmetic operators.
Out problem here is operator precedence.
Now operator precedence is a fancy term for
the relative importance given to each of the operators.
If all operators were equal, we'll get the expression how I read it earlier.
By calculating a plus b is 15 divided by three is 5 take 4 is 1 time 12 is 12.
But the thing to remember is multiplication and
division have higher operator precedence than addition and subtraction.
So those operators are performed first.
And most languages you'll find working the same way so
this is not just peculiar to Python.
So what's actually happening is b divided by 3, this is actually happening first.
So, b divided by 3 is happening first, which is 1.
Then 4 times 12 is added, which is 48.
So the expression is actually evaluated as 12 plus one is 13, take 48,
which is negative 35.
That's weird, but that's the way it actually happens by default
because multiplication and division, as I mentioned, have got a higher precedence.
So the case where you've actually got expressions that use just those operators,
they will actually work from left to right.
So we can type a divided by two times three,
and that should give us the answer of four times three is 12.
So we run that, we actually get the answer of twelve and
even something like eight times three divided by two.
24 divided by two is 12, we get the answer correct.
Then the best way to actually overcome this,
if you want your expressions to be clear and unambiguous to use parenthesis freely,
even when Python doesn't specifically require them.
So, in our earlier example, the one that didn't work, this first line here.
So we could do something like, print,
a plus b, divide it by three, take four, times 12.
Now that's a lot of brackets there but what we're doing is we're actually putting
the expressions in parentheses to make it clear what we're trying to perform.
And when we actually run it we're actually correct, you now have the answer 12.
So you can see we've got a plus b in its own set of brackets, that's gonna be 15.
But then outside to that there's this bracket here, which covers that.
So therefore, that calculation is now 15 divided by three, which is three.
Then take four from that is one, then multiply by 12, is 12,
and that's why we're getting the answer of 12.
Okay, so I'm gonna stop the video here.
In the next video we're gonna continue on talking about variables and
operator precedence etc.
So I'll see you in the next video.