- Learn the difference between the data types “string”, “int” and float
- Trace the value stored in a variable throughout a program.
- Concatenate Strings.
- Create Substrings.
All must use three different types of variable in a program, and understand the differences between these variables (Level 5)
Most should complete the above, and swap values between variables, predicting the values stored at any point of a simple program. (Level 5)
Some could complete the above, and start to manipulate strings effectively, returning substrings and concatenating. (Level 6)
Words to learn: String, substring, integer, float, concatenation
There is a good reason for looking at the game though. Play through the game a couple of times.
In class, discuss the aspects of the game that change during play (the position of the penguin, etc).
Now play again, only this time click “show”, and look at all the aspects of the game that change as you play.
These are all variables. Some count things (numberOfFishCaught for example), while others hold information (penguinName). They are all “Containers” of memory with a name. Values are saved inside them.
So now let’s get back to Python, and start looking at variables.
Open IDLE(Python GUI) as you did last lesson, and open a new window.
You’ve realised now that whatever you throw inside-
is sent to the Python shell as Output.
If you want to display a sentence, you would run
print(“This is a sentence”)
Save your code as “dataTypes.py” and run it.
Wait a minute… Why did “Hello” have speech marks, while 5 didn’t?
There are different types of data which we use all the time, without really considering it. Try this:
- Write down answers to the following:
- First name
- Date of birth
- Age in years
- Have you seen “The Maze Runner”?
- Shoe size
- Date of last visit to the dentist
- Place of birth
- Number of books in bag
- Height (in metres)Look at your answers. There are different TYPES of data here, some of which repeat. Can you sort them into 4 (or 5) categories?
In programming there are several very common data types, which are repeated frequently.
We’re going to look at just 4:
int, float string and boolean.
So each data type has a very different use. We’ll look at them individually.
Integers (int) are whole numbers, so we can perform calculations on them, as we saw last lesson. We can store integers in variables and still perform calculations.
Look at this code:
Write down what you think the output will be. Try the code and see if you’re right.
What would happen if you added more code:
number3 =numberOne * numberTwo
Try the code and find out.
Now add this code:
number3 =numberOne / numberTwo
What will the result be? Run the code and…
Oh dear! That wasn’t quite what we expected. So in a division, we can’t split up a whole number.
A float or floating point number lets us use a decimal point. Change your code so that numberOne is now 5.0 and numberTwo is 3.0
Now run the code, and you should see a more predictable result.
Unfortunately, this turns everything into floating point numbers, which look more confusing. We were happy with integers, until we tried a division.
We need a way to turn the answer into a float, without affecting everything else. This is called casting.
Change your code so that numberOne and numberTwo are both integers again.
Now change the line containing the division to read:
number3 = float(numberOne)/float(numberTwo)
So you can’t just mix and match data types as you wish. If you want one to behave like another, you must cast the variable. To do this (as above) write the name of the data type you want, followed by the variable name in brackets.
Go back to the worksheet, and try to trace the values in the program in question 2.
Now let’s have a look at Strings.
A string is a collection of characters. If you’re not sure what character refers to, think of anything you can produce on a keyboard ( letters, numbers, punctuation ) we always enclose a string in either single quotation marks:
‘ a nice string ‘
or speech marks:
” a nice string “
As a string is a collection, all the characters go in a specific order (think – it wouldn’t be much good if “hello” was output as “lolhe”!). This is really useful because it means that each character has a number, starting at 0.
the string “HELLO” would be counted like this:
H |E |L |L |O
0 |1 |2 |3 | 4
Why is this useful? Well, with these index numbers, we can print single characters. Try this code:
testString=”this is a string”
so, using square brackets [ ] we can refer to a single character in a String. Now add these lines:
What happens? How about:
So, using a colon between two index numbers we can print out a segment of the string. This smaller string is called a substring.
Mixing it up
We have learned that int and string are two different types, and therefore can’t be mixed in one statement without casting. Luckily Python gives us a good way to do this in a print() statement. We can separate strings and other types by commas, and Python will automatically concatenate everything to one big string.
Confused? Try this:
Of course, this only works with a print statement. Anywhere else, you must cast strings.
Write a program which:
- asks the user for her name and stores it in a variable
- asks the user to choose a number and stores this in a variable.
**Hint** if you use input(), the input data from the keyboard is automatically a string. You will need to cast this data
- The program will now print:
“Hello, [username]. I can do the [usernumber] times table”
” 1 x [usernumber] is [answer] “
” 2 x [usernumber] is [answer] “
” 3 x [usernumber] is [answer] “
Print your program out and write down a quick explanation of how the code works before putting it in your folder/book.