Back

Data types - Answers

Q1. A floating point number is one with a fractional part e.g. -3.5     0.0       5.232      7.0
Q2. Five examples of an integer could include 3232345    5     0     -4      -34565423   
Q3. A string is one or more Unicode characters.
Q4. A Boolean data type can hold True or False. 
Q5. because Python is case-sensitive, true is different to True and it is therefore not Boolean. It is in fact undefined. If it had quotes around it, it would be a string.
Q6. The fifth data type is complex numbers. An example is -5+4j.
Q7. Yes, you define your own data types in Python, each with their own allowable values.
Q8. The Python keyword type can be used to find out the data type of any piece of data or a variable.
Q9. When you type in this code into the Python shell

fantastic = 10
type(fantastic)

you can see that it belongs to the data type class of int.
Q10. Predict what class of data type each of the pieces of data in the following table are. When you have made your predictions, use type and the Python shell to see if you are correct. Type in to the Python shell exactly what you see in the data column!

Data Data type
4.0 float 
3454  int
true error - undefined 
"friend" str 
False bool 
-5+4j  complex
0.000  float
-3  int
-3.0  float
'recipe'  str
table error - undefined
True  bool
"Pi"  str
 3.5+2j  complex

Extension task
Investigate ASCII and Unicode. Print off an ASCII table and write your name in decimal ASCII codes and again using hex. We need Unicode because many languages do not use the English alphabet and ASCII doesn't have enough codes available for them. There is an excellent description of the different forms of Unicode in the section on strings in a free eBook called 'Dive into Python' available online.

Back