Let's now take a look at a different form of data types. So far,
we've been working with strings, right? But now
let's take a look at floating points
and integers otherwise simply known as numbers.
All right. So
let's go ahead and remove
this program and
let me first of all, create a variable X equals to four.
OK? And then Y equals,
let's say 6.0.
Now,
the obvious difference between these two is that X doesn't have any decimal points.
Why does Y has the six point? Oh
All right.
In fact, let me make this a bit more reasonable.
Let's make it 6.3 just as an example, right? So
why in this case would be a floating point because it does have a decimal point
while four will be the integer. So
if I went ahead right now and I printed
X
plus
Y
and I run my program, you can see we're gonna get the number 10.3
pretty straightforward, right?
However,
we could do certain things like say, for example, if you wanted to convert
10.3
being a floating
number, if you wanted to convert it to an integer,
we could simply use a new function called the integer function.
So, what I'm gonna do right here is inside of this first bracket.
I'm going to open up another bracket and
add int.
All right. So int, oh,
sorry,
sorry about that. Int
ok.
So int here is a function that stands for integer
and of course, since we've opened the integer function,
we're going to have to close it as well.
So let me add
the closing brackets. And now if I was to run a program,
you can see it has converted 10.3 to 10
because of the
function
int integer.
So this is the first time that we have added a function inside of another function.
First, we have the print function
and then inside of the print function, we have the integer function. So
this is one thing you should be aware of when it comes to Python programming,
you can have
a function inside of another function.
And in fact, you can have 34 functions instead of one another
just as long as the hierarchy is correct.
In this case right now, I cannot have the integer
function coming in first
and then print. In fact, you know what, let's try it. OK? Why not?
I'm gonna go ahead right now. Print
OK?
And then int OK, what's gonna happen? What's gonna happen? I'm gonna click on run
and there you go.
So even though it kind of, well, it didn't really work.
I mean, we did get the X plus Y, we did get the correct answer to be 10.3.
But unfortunately, it didn't convert the results to an actual integer. So
Python basically ignored the very first part which is the
integer function and then just went ahead and said,
OK, print X plus Y.
And then of course, they told us that, hey,
there's some sort of an issue with the syntax.
So
you have to be careful whenever you're trying to
add functions inside of another function,
there needs to be a hierarchy and it needs to be correct.
So
let's reverse that run again
and there you go.
So
likewise, if I wanted to convert the results to a
floating number,
I could do so as well. So
for example, right,
if I change the Y here to let's say five,
right?
But instead of nine, which would be the obvious answer here, I wanted it to be 9.0
I will change the integer function in here
to float.
So this is the opposite, right? So
check this out, I'm going to click on run
and there you go. It is now 9.0.
It's, you know, there's a good chance you may not need to use the integer
or float function in your programs.
It all depends on the kind of programs you will create.
But nevertheless, it's important to understand
these concepts that you can add,
you can convert one data type to another
and then you can also have one function
inside of another. So thank you for watching. I will see you in the next class.