Python 3
Python 3
Computing: IntroductiontotoPython
Python
Lesson Three :
Lesson three
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.
Success Criteria
• To understand why programming languages use data types.
• To use type casting in Python to enable numerical inputs.
• To understand the importance of annotating code.
Quick Quiz
1) What is the name of the programming language we’ve been learning?
A. Prolog B. Python C. Piranha D. Pascal
9
0
records stored on computers would be
confused. How would computers know
which century it was?
1900? 2000? 3000?
Data Disaster
• So what actually happened on 1st January 2000?
Did the Millennium Bug bring the world to a standstill?
• Thankfully not. However, some
computer systems and displays got the
dates mixed up, displaying the year
1900 instead of 2000.
• For us today, it is a useful reminder of
how computers store information (data).
• The way that a computer processes
information depends on the type of
information. We call this the data type.
Data Types
Think back to last lesson when we learned about variables.
name
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).
Float 3.14
2
String Monty
Boolean True
Integer 4
7
String “1234
”
Did this answer surprise you?
Any data enclosed within
“quotes” is always a string!
Integer -
23
• input works fine with strings, but we have to take care with numbers.
• Try entering this Python code into the script mode window, then save
and run your program:
Type Casting
According to our program, 6 + 7 = 67!
What’s gone wrong?
Python thinks that any information we
enter using input is always a string.
In other words, “6” + “7” = “67”.
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().
Let’s see this in action …
Type Casting
Change your program by including int as shown here.
Be careful to add an extra bracket at the end of the first two lines:
Key Terms
Adding strings together is
called concatenation
(con - cat - en - ay - shun).
Rate Your Progress
Red Light: you have understood some of the objective
and you will think about it some more later on, perhaps
asking a friend or teacher for help.
Green Light: you feel fully confident with this objective and
you understand it well.
Success Criteria:
• To know why programming languages use data types.
• To use type casting in Python to enable numerical inputs.
• To understand the importance of annotating code.
Nailing It Down
We have learned a lot today about data types and annotation in Python.
There are many other programming languages, some are easier to learn
than others.
You can find out about different languages at this interesting website:
http://rosettacode.org/wiki/Rosetta_Code
The purpose of this website is to present solutions to the same problems
for lots of different programming languages.