Data Types
A real number is a number that can have a fractional part, such as a bag of flour with a mass of 1.5 kg or a race finished in 12.75 seconds. In many languages, Python included, reals are called float or floating-point numbers. Real and float are simply two names for the same data type, so either name is accepted for a value like 12.75.
You can work out the data type of a number from the way it is written. A numeric literal is a number typed straight into a program.
184 or -12, is an integer.184.0 or -12.5, is a real.The decimal point is not decoration. Writing 184.0 tells the computer to store a real, even though the fractional part is zero, so 184 and 184.0 are different data types and are stored differently in memory. That is why a whole number written with a point is not an integer.
A bag of flour has a mass of 1.5 kg. Adding 0.25 kg of extra flour gives 1.75 kg, and doubling the bag gives 3.0 kg. The answer 3.0 is still a real, because the value you started with was written with a decimal point. Dividing 6 by 4 with / also gives 1.5, a real, even though both literals you typed were integers.
# masses of flour in kilograms
bag_mass = 1.5
extra_mass = 0.25
total_mass = bag_mass + extra_mass
double_mass = bag_mass * 2
print(total_mass)
print(double_mass)
print(6 / 4)Output:
1.75
3.0
1.5 Edexcel GCSE Computer Science · Data Types