Casting Data Types (or changing data types)
Casting is the term used for chaging the data type of a variable. It simply means switching the data from one data type to another.
There are a number of different data types in Python:
For numbers
- Integers (int) - whole numbers, positive or negative but no fractions eg . 1, 245, -8
- Float (float) - any real number with a floating point representation, eg : 1.45, 94.65782
- Complex numbers (complex) - a combination of real and imaginary numbers (don't worry about them at this level)
For text
- String - a collectoin of one or more characters eg - dog, cat
Boolean
- Boolean is for data with one of two built in values True of False
Lists, dictionaries and tuples are also considered data types in Python but are not for variables.
Run and experiment with the code in the trinket to see how casting works and what it does. Notes:
- line 8 creates a new variable age1 with a float type and stores the data from age in it
- line 12 converts age1 into an integer
- line 18 just converts the data in the variable bobAge into float format to print it