When you are writing statements, you cannot display int, float, and bool stuff in a print statement. In that case, you need to use typecasting. Typecasting is converting one datatype to something else.
Let's look at examples with other datatypes.
Notice the + signs. Those make spaces in Python. In that case, the sentences won't run together.
Now, let's look at the float datatype. As noted before somewhere, it stores decimal versions of numbers.
As we have seen, this typecasting is making things come thru with no errors. It is essential for Python. However, though, making little sentences isn't the only use for it. What else might it be used for? Any ideas?
Code:
his_age = 30
print("His age is" + " " + str(his_age))
# The stuff above won't display without the typecasting because his_age is an int.
Let's look at examples with other datatypes.
Code:
he_can_play_guitar_or_not = True
# The above variable is a bool and can only only be true or false.
print ("It is" + " " + str(he_can_play_guitar_or_not) + " " + "that he can play guitar.")
Notice the + signs. Those make spaces in Python. In that case, the sentences won't run together.
Now, let's look at the float datatype. As noted before somewhere, it stores decimal versions of numbers.
Code:
the_gpa = 4.0
# ha ha, yeah right
print("The kid's gpa is" + " " + str(the_gpa) + " " + "in elementary school.")
As we have seen, this typecasting is making things come thru with no errors. It is essential for Python. However, though, making little sentences isn't the only use for it. What else might it be used for? Any ideas?
Last edited: