datatype casting in python
Python determines the datatype automatically, to illustrate
a= 3
b= "string"
Functions accept a certain datatype. For example, print only accepts the string datatype.
If you want to print numbers you will often need casting.
In this example below we want to print two numbers, one whole number (integer) and one floating point number.
We cast the variable a(integer) and the variable b (float) to a string using the str() function.
What if we have text that we want to store as number? We will have to cast again.
In the example above we cast two variables with the datatype string to the datatype float.
a= 3
b= "string"
It finds a is of type integer and b of type string.
Functions accept a certain datatype. For example, print only accepts the string datatype.
If you want to print numbers you will often need casting.
In this example below we want to print two numbers, one whole number (integer) and one floating point number.
a = 4 b = 3.88773838383 print("We have made two numbers: ") print("a = " + str(a)) print("b = " + str(b))
|
What if we have text that we want to store as number? We will have to cast again.
a = "135.31421" b = "133.1112223" c = float(a) + float(b) print(c) |
Comments
Post a Comment