So the videos that I've been making
using these Python examples,
I've been using Python 2,
which is kind of the standard version of Python
that most of the implementations are in
at the time of making this video.
But there is also a Python 3,
which is kind of the state of the art version of Python,
or I guess you could say it's the newest version of Python.
But it's not backwards compatible.
When I say backwards compatible,
it means that if you are running Python 3,
it will not necessarily be able to interpret everything
that is written in Python 2.
So that's what we mean by backwards compatibility:
You can't take a program that was written in Python 2—
so let's take a program—
that's the program right there—
and let's say it runs fine in Python 2.
If you take that same program
and try to run it in a Python 3 interpreter,
it's not going to work.
So this means,
so this is not backwards compatible.
If this did work,
then Python 3 would be "backwards compatible".
And the whole reason—
well you know this is actually a nice understanding of
what even backwards compatibility means—
but the whole point of making this video is
the program that I ran in the last video
will work in Python 2,
but it will not work in Python 3.
And the reason why it doesn't work in Python 3 is
Python 3 has a different version...
...of this input function over here.
In Python 2, if you use the input function just like I used it—
I put a message here for the user of what to input
and when the user inputs something,
it'll be returned to the program fully evaluated.
So in this case, if the user enters a number,
I get the number as a number.
So I literally get the evaluated expression.
So like we saw in the last few examples,
if the user puts in a 4,
then the number 4 will be put in the variable "number".
If the user inputs literally 3 + 4,
then the number 7 will be put into the variable "number".
In Python 3
the input function does something slightly different
and it's enough to break this program
if you were to try to run it in Python 3.
In Python 3,
the input function doesn't return the evaluated expression,
it returns a string—
and this is a word you're going to...
...hear a lot of in computer science,
and I'll explain it in multiple videos—
but it returns a string of what the user inputted.
And you're saying "Sal, what is a string?"
And this is actually a good introduction:
A string is a "string of characters".
So, to—maybe—make the difference,
if I—if the user inputs—
right over here, this is coming from the user,
if the user inputs—in the Python 2 version—2 × 6,
then what will be stored in the variable "number"
will be the 2 × 6, so it will be 12,
it will actually be evaluated.
In Python 3, if we just did this,
and the user inputted 2 × 6—
so once again the user inputs 2 × 6—
then what's stored in the variable "number",
what's returned from this function right over here,
is not going to be 2 × 6 evaluated.
It is going to be a string of characters that says "2 × 6".
So this is the string and a string is really—
you can view it as a string of characters.
you can kind of view it as some text
that's not actually interpreted in any way.
So it will actually return that string of characters,
then you'd have that string of characters...
...put here in "number"
and then the function here "range" would break down
because it's like I'm trying to take the range of...
...like a word, not like a number here.
So thats why it would break in Python 3.
So if you want that original function to work in Python 3,
what you have to do is put an—
so instead of just calling "input" like this,
you have to—and i will write it below this—
you'd have to write "eval" and then write "input"—
and then you write "input"
and then it quotes all of the stuff I wrote up here,
colons, close quotes, close parentheses
and then close the eval() parentheses right over there.
What eval() does is that it can take a string of characters,
it can take some text like this.
And it will evaluate it.
So what happens is that
in Python 3 you'll get this string and when you eval it—
so then you would do an eval() on this—
so you're doing an eval() on the input()—
the user puts some input,
so then you're doing an eval() on what the user inputted,
and then this will return 12.
So in Python 2, the input function kind of does it all for you,
in Python 3 it returns a string
and then you have to evaluate the string to get 12.
So if you try to do this in Python 3 and it broke,
that is the reason why,
you have to put an eval() around it.