Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.

Skip to 0 minutes and 3 seconds There are many different types of data, and different programming languages have their own rules for how different data types are treated. Scratch does not have specific data types for you to use. So this may be the first time your learners have learnt about this concept. In this step, you’re going to use the R-E-P-L, often called the REPL or console, to explore data types. R-E-P-L stands for read, evaluate, print, and loop because this is what the REPL does when code is entered. OK so in the Python REPL, let’s enter this code. First thing I’m going to do is I’m going to create a new variable called message and I’m going to put in a string, hello world.

Skip to 0 minutes and 50 seconds Now this variable is a string data type. And we can see its type by using the type function.

Skip to 1 minute and 0 seconds We use type message, it returns str. And we can print it by entering it. And we can use it as a string. So for example, we could get the letter at the fourth index which returns the letter o. Remembering, of course, that Python indexes start at 0. Now, Python recognises various data types and will only allow certain operations on certain types of data. For example, if we were to give Python two numbers, two integers, it would add them together. If however, we were to give Python two strings and perform the same operation, it would concatenate them together.

Skip to 1 minute and 48 seconds We could also give Python two lists and rather than adding them together or concatenating them – could give Python two lists– it merges them into one list. You can change the data type of a variable in Python using typecasting. And the most common examples of typecasting are changing numbers to strings and back again. So if a variable is created as a string, so for example, my number, and I’m going to create a string but I’m going to put the number 3 in it. Now my number is a string and we can use type to test that. It’s a string. But we can’t use our string variable to do calculations.

Skip to 2 minutes and 34 seconds So for example, if we said my_num plus 4 we would get a type error– must be str, not int. And what Python is telling me is that I’ve got two variables, one’s a string and one’s an integer, and I don’t know how to deal with them. Now to use my_num in calculations I’d have to typecast it to an integer first, and I can do that using the int function. So I can say int my number. And now if I try and use it as part of a calculation, I get the result I expected. You could also create a new variable that is an integer of my number. So I could say my_int_num equals int of my_num.

Skip to 3 minutes and 20 seconds Now I’ve got two variables. And if I was to do a type on my_int_num we would see that, that variable is indeed an integer.

Skip to 3 minutes and 33 seconds Now if I wanted to use my_int_num number within a string, I’d have to typecast it to a string. So for example, if I was to say print my int num is and then I add on my_int_num, again, I receive a type error because I’ve tried to add a string to an integer. In order to run this code, I would have to turn my_int_num into a string, I’d have to typecast it to a string. And I can do that using the str function. This time, I get the result I expected. My_int_num is 3 because I’ve turned my integer back into a string. Now last week you learnt about using input in the silly story game, a little bit like this.

Skip to 4 minutes and 30 seconds We say, give me a number.

Skip to 4 minutes and 36 seconds When I run that, it asks me for a number I’m going to put in number 13 in and 13 will now be stored in the variable num and I can see that. However, input always returns string so if I do a type on num we can see that that’s a string. Now your challenge is to write a program that uses input to ask the user for the real age of a dog. It then converts this to an integer. It then calculates the dog’s age in dog years by multiplying its age by 7. It then turns this result back into a string before outputting the results so the user can see how old the dog is in dog years.

Skip to 5 minutes and 20 seconds Now there are different ways to do this, but you will probably find it easier if you jot down a little pseudo-code first to help you. As always, you can share your code, challenges, and successes, in the comments below.

Data types

Every programming language has its own rules about how to handle different types of data. Scratch does not have specific data types for you to use, so this may be the first time your learners have learnt about this concept.

You have learnt about the variable data types of ‘string’ and ‘integer’, which store text and whole numbers. As you become more experienced in Python you will encounter other data types, such as:

Category type Python data type Example
Text/string str "hello", 'world'
Numeric (integer, decimal) int, float 1, 1.3455
Sequence/array (a store of multiple values in an organised way) list, tuple, [1,2,3,4], (1,2,3,4)
Map (a store of multiple values in an unordered way) dict {"greeting": "hello", "place": "world"}
Boolean bool True, False

In this step, you are going to use the REPL (sometimes called a console and pronounced ‘repel’) in your IDE. A REPL is an interactive way to get instant feedback from entering code. The term REPL is an acronym for: read, evaluate, print, and loop, which is what the REPL does when code is entered. Programmers often use the REPL to try out code before entering it into a program. There are different ways to access the REPL depending on your chosen IDE. You may have to refer to your IDE’s documentation, or perform an internet search to work out how to open it.

In your REPL, enter each line of code below, pressing Enter after each:

>>>message = "Hello World"
>>>message[4]

The REPL will print 'o' because that character is in position 4 in the string message; the first character in a string is always position number 0.

You can use the type() function to find out what data type something is. Enter the code below in your REPL, and press Enter:

>>>type(message)

The output tells you that the variable message is a string. 'Hello' and "I am learning Python" are also strings. (Single quotation marks ' and double quotation marks " are the same when working with strings in Python.)

Python recognises various data types and will only allow certain operations on certain types of data.

  • Enter the example below in your REPL by typing the print lines, followed by Enter:
>>> 1 + 2
3
>>> '1' + '2'
12
>>> [1,2] + [3,4]
[1, 2, 3, 4]

In 1 + 2, the numbers were integers, so Python added them together and output 3. In '1' + '2', the data was two strings, so Python concatenated these strings. In [1,2] + [3,4], the data was two lists, so Python combined the lists.

You can change data types in Python using type casting. The most common examples of type casting are changing numbers to strings and strings to numbers.

Here a variable is being declared with a value of '3' instead of 3:

>>> my_num = '3'
>>> type(my_num)
<class 'str'>

This string can’t be used in calculations with integers and will return a type error:

>>> my_num = '3'
>>> my_num + 4
Traceback (most recent call last):
  File "<pyshell#14", line 1, in <module>
    my_num + 4
TypeError: must be str, not int

To use my_num in calculations, it must first be type cast to an integer using the int() function:

>>> my_num = '3'
>>> int(my_num) + 4
7

You could also create a new variable that is the int of my_num:

>>> my_int_num = int(my_num)
>>> type(my_int_num)
<class 'int'>

If you want to use my_int_num with a string, it needs to be type cast to a string using the str() function:

>>> print("My new number is " + str(my_new_num))
My new number is 3

Challenge

You learnt about getting user input in the silly story exercise from last week. For example:

num = input('Give me a number ')

This always saves the number that the user inputs as a string.

Your challenge is to write a program that:

  1. Asks the user for the real age of a dog
  2. Converts this to an integer
  3. Calculates the dog’s age in ‘dog years’ by multiplying its age by seven
  4. Turns this result back into a string
  5. Outputs the result so the user can see how old the dog is in dog years

Two dogs, an older one and a younger one.

There are a few ways to do this, but you might like to write a little pseudocode first to help you figure it out.

As always, you can share your challenges and successes in the comments below. Use a Pastebin link to share your code.

Share this video:

This video is from the free online course:

Scratch to Python: Moving from Block- to Text-based Programming

Raspberry Pi Foundation