Great, now let's learn numbers. Let me open an interactive shell here to
explain this faster and so numbers, you know what numbers are. To create a number
just type the number in python and that's it and you also have operators
like this is the plus operator and you can also store numbers in variables
let's say a equals to 2, b equals to 3 and then if you do a plus b you get 5.
Something to note is that you know if you do that in here, a equals to 2 b
equals to 3, and then if you just do a plus B and then you execute the script,
let me open another terminal, you will not get any output in there. The reason
is that you are not printing out anything in your program, so this is
different from an interactive session. Here if you simply do an operation or if
you simply call a variable like that, you'll get the output promptly, so this is just
for simplicity when you when you try things out, so you don't have to pass
print explicitly, but Python will understand that you met print. But here
things are more strict, so you if you want to print out the output, you want
to say print a plus b and then execute and you get the output and yeah basically
that's the only difference between executing a program from a Python file
and running the code in the interactive shell, so these are numbers and I can
prove that if I do type 2 for instance. You'll see that this is an INT
type which means is an integer, so basically we have integers and we have
floats. These are the two main types of numbers.
A float is, let's say 2.4 it's a float and I can prove that 2.4,
type 2.4. You'll see that this is a float, however if you do type 2 in
brackets you'll see that this is a string, it's not a number and therefore
you cannot make operations using a string and a number so Python will give
you a type error and this is saying cannot convert integer to string
implicitly so when you do a string plus something else Python expects that you
were adding another string after a plus sign, so that's why it says I cannot
convert integer object, so it cannot convert this to a string implicitly
because if you were to do two plus three you'd get 23 because Python treats these
two as text. So to understand it better
let's add some text here b so you get 2a3b, so it's a concatenation operation.
When the plus operator is used with strings it is referred to as
concatenation. Now let's go back to our sample program that we used earlier
which was this one in here, so now what if instead of getting a greeting from
the user you ask their for their age? Enter your age, so this is just a message
you can write anything you want there, but just try to have a message that
makes some sense to the user and so also I'm going to change the variable name to
something more meaningful, age and then what I want to do is let's say new age,
I want to calculate the age plus 50. Why not?
I'll print out the new age. Save the script and let's go and execute this, we are
going to get an error here I warn you. But it's good that you see the errors
that you get because that's how you learn better.
So Python my program, execute. For now it's working fine because what
happens is Python executes a script line by line so it goes to the first
line and if there is no error it executes that line and in this case we have
an input function so the program is on standby mode so it expects from the user
to enter the input, so let's enter the input here. Let's say 28 and here is the
error that we get. So again we saw this error before. This is saying type error
cannot convert int object to string implicitly and you also have the line
here that the error occurred. So these are important messages and line two in here.
So what's happening is that age is a string it's not a number therefore
python cannot add up a string with a number and so why is age a string?
Because that's what the input function produces, so the input function get the
user input and converts it to a string so even if you pass a number there these
will always convert everything to a string.
Therefore Python you know cannot concatenate 28 plus 50 so what can you
do in that case? Well you want to convert that string into an integer and for
that you want to use the int function, so it's a Python function that does the work.
As you can see it outputs now the correct output so 28 plus 50 equals
78 and similarly you can convert numbers to strings
and let's say str 50 and I think we'll get an error here. Yeah, it says type error.
Unsupported operand type four plus. So you're adding up an integer with a
string, so integer with a string. If you like to convert this to a float, let me
remove that, save, execute. 28. You get 78 point zero because what
Python does when you use float is you know float 28, it converts it to 28.0.
So floats can be useful in certain scenarios. And you'll have a lot of scenarios in the
course, so don't worry about that now because we have real world
applications later on where you see the real benefit of everything that you are
explaining here in this early sections that cover the basics. So this is about
numbers. Try to practice them a little bit and I'll talk to you in the next
lecture.