>> So now you've learned how to work with strings,
let's take a look at how we work with
numbers inside our Python code as well.
Just like strings numbers can be stored inside variables.
We always want to give those variables
nice meaningful names get in the habit of that right away,
and we can pass those variables
into functions like the print statement.
So the print statement can take a variable that contains a string,
a variable that contains a number.
It really doesn't matter either way,
it'll just print what it's received out onto the screen.
Of course when you're working with
numbers in your code at some point you're probably
going to have to do math calculations to figure out
people's salaries or taxes and
things are pretty common inside our code.
So we could do math of the numbers and the good news is,
it's going to look a lot like it did when you were in school.
You've got the plus sign for addition,
the negative sign for subtraction,
you've got the slash for division,
and then the only thing that's a little bit
different is it's an asterisk symbol for
multiplication and exponent or to
the power of is a double asterisk.
So if I have two numbers here, six and two,
6 plus 2 prints out an 8,
6 to the power of 2,
6 squared, will give me back 36.
So we can do lots of
different math operations inside
once we start playing with numbers.
One thing you will run into
very quickly once you start working with numbers,
is that when you start combining different data types.
So a string data type in a number datatype,
Python will get confused.
So in this case, I have,
hey, how many days in February?
28, and I just say, hey, print statement.
Let's display on the screen,
28 days in February.
We've already printed numbers,
we've already printed strings yet when
we run this code it blows up.
But why? Well, if you
actually look at the error message it'll give you a hint.
Unsupported operand type for plus int and string.
What it's telling you is it's actually
the plus symbol that's confused.
Five plus six, it knows
a number plus a number I should be doing addition.
Susan plus I back to
strings it knows to concatenate the strings together.
But when you give me one number,
one string, I'm supposed to add them together like numbers?
Or am I supposed to concatenate them like strings?
I'm confused.
So when you're working with Python,
sometimes you have to tell it, hey,
I know that the days in February 28 in this case is a number,
but I want you to treat it like a string here.
I want you to concatenate
that number to the phrase days in February,
so I see 28 days in February on the screen.
So we have to do what's called type conversion.
So get used to this,
you're going to see it a lot in your code,
it's going to come up all over the place.
So all we have to do for Python
is to convert any data type to a string,
you just call these string function.
So we call the str function,
we pass it days in February which is currently a number,
and now it's going to treat that as a string.
Now we say, you want to concatenate
string plus another string, I know what to do,
put those together and
displays our message on the screen correctly.
So string function is going to save you a lot when you
start working with numbers and strings mixing the two up.
Now, when I do this I'm actually storing a number as a string,
and as soon as you store a number as a string,
that also creates mentioning effects.
So if I take a first number and I stored the number 5 in it,
if I put quotes around that number five it now
treats it as a string that contains the number 5,
and a second number equal six,
a string that contains number 6.
Well, what happens when you concatenate
two strings together with a plus sign?
It puts the two strings together, so I get 56.
Well, here you can see what's going on,
but sometime this is happening
back behind the scenes and it'll catch you.
Let's say you're using that clever input function
to ask the user to enter a value.
Turns out the input function will always return a string.
So if you ask a user to enter a number and they enter five,
and then you enter a second number and you enter six,
and then you say let's print out the result
of first number plus second number,
it's going to print out 56.
So just like earlier I needed a way of saying,
hey, I know you think it's
number but I want you treat it like a string.
Sometimes you have a number stored in
a string and you need to treat it like it's a number,
I want to do math with it.
So there is a different data type conversion,
datatype conversion changing from one data type to another.
So last time I was using string to convert from number to string,
now I need to use something to convert from string to number.
I have two choices of functions to work with here.
The int function will convert it into an integer,
a whole number ;1, 2, 3, 4.
Float will convert it into a number of
floating number or something that might have decimal places;
1.0, 1.5, 2.6 or anything like that would be a float.
So by using the int function and if I say print integer version
of first number of plus integer version
second number or float plus float,
you can see it actually does the math
correctly and adds 5 plus 6 together and gives me 11.
So be very careful when you're working with numbers to
make sure it's using it as
the correct data type at the right time.
If you're wondering what other situations
you might be using numbers in, well,
there's basically two situations; one,
you're doing math operations to calculate salaries,
calculate tax, and so on,
and the other place you're going to run into
numbers is when we start getting into things
like arrays and lists and
you might need to point to an index value.
Say start at this position,
go to this position or go to this row here,
you'll find numbers useful for that as well.
So let's step back from this and go take a look at numbers encode.