Python Lesson 4 - Data Types
Python Lesson 4 - Data Types
Lesson 4
1) What is the name of the programming language we’ve been learning?
A. Prolog B. Python C. Piranha D. Pascal
99
98
97
00
• Many people feared that dates and records stored on
computers would be confused. How would
computers know which century it was?
1900? 2000? 3000?
• So what actually happened on 1st January 2000?
Did the Millennium Bug bring the world to a standstill?
We can think of variables as boxes. Each box has a label, and we can
store information (data) in each box.
This information can vary during the running of a program. The boxes
are emptied of data when the program ends.
Float 3.142
Which data type is this value?
String “Monty”
Which data type is this value?
Boolean True
Which data type is this value?
Integer 47
Which data type is this value?
String “1234”
Did this answer surprise you?
Any data enclosed within
“quotes” is always a string!
Which data type is this value?
Integer -23
• Python has a useful command called type.
• type(76.54)
• type(True)
• type(“Monty”)
• input works fine with strings, but we have to take care with numbers.
• Try entering this Python code into the script mode window, then run
your program:
According to our program, 6 + 7 = 67!
What’s gone wrong?
Python thinks that any information we enter using input is always a string.
Instead of adding the numbers together, the “6” and “7” are just joined together.
(The technical term for adding strings is concatenation.)
Try your program again with different numbers just to make sure.
To fix this problem, and enable us to add our numbers together, we simply make
use of the type casting method int().
Data Types
Python has four data types:
• String (text or alphanumeric information);
• Integer (a whole number, a number which is not a fraction);
• Float (a number which can be a fraction);
• Boolean (can only be one of two values, True or False).
Key Terms
Adding strings together is called concatenation
(con - cat - en - ay - shun).
Type Casting
In programming we sometimes need to change the data type of information.
This is known as type casting.
input()
input() works fine with strings, but we have to take care with numbers.
Python thinks that any information we enter using input is always a string.
Make use of type casting when using numbers int() and float()
Annotation
Programmers use annotation to explain their code.
Annotation is useful for when programs start to get longer,
or if we come back to a program that we’ve not looked at for a while.