Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 1 secondThere are many different types of data. And different programming languages have the their own rules governing how different data types are treated. In Python for instance, characters such as A or question mark are called strings. You can query different objects using the type of function. So by typing "type" and then the string "hello," I can find out that this is a string. Similarly, type and "I am learning Python" should return the same result. Other data types you should know about are ints, which is short for integer. These are whole numbers like 1 or 1,000. Another numerical data type is the float. This is an implementation of a fraction, such as 1.0 3.142.

Skip to 0 minutes and 57 secondsThere are many other data types such as lists, dictionaries, and sets. And as you become more experienced with Python, you'll learn more about these data types. Different operations in Python will have different outcomes depending on the data type used. So adding the integer 2 to another integer 2 produces the integer 4. Whereas, adding the string 2 to another string 2 will produce the string 22. And adding a list containing the number 2 to another list containing the number 2 will produce a list containing two values of 2. Luckily in Python, it's fairly easy to switch between data types. This is called typecasting. For instance, here I'm going to set a variable and give it the value of the string 3.

Skip to 1 minute and 50 secondsSo when I query what type this is, it should return class string. Now because my_num is a string, I can't add it to another integer. So for instance, if I type my_num plus 3, the interpreter will tell me that I cannot add a string to an int. I can however, add a string to a string. So my_num plus the string of 3 should give you the string 33. If I want to use my_num in calculations, I must first typecast it to an integer. I can do this by typing int and then the variable name. Then, I can add it to 3 and we'll get the output 6.

Skip to 2 minutes and 34 secondsI could also create a new variable called my_new_num, which is equal to the int of my_num. Now, the type of my_new_num will be an int.

Skip to 2 minutes and 50 secondsBecause my_new_num is an int, it's impossible to concatenate it with a string. So typing "my_new_num is" and then adding on my_new_num will error because you can add strings and ints together.

Skip to 3 minutes and 9 secondsTo concatenate the value of my_new_num to a string, I must first typecast it from an int back into a string. So "my_new_num is" plus and then string of my_new_num.

Skip to 3 minutes and 27 secondsNow, it's time for a challenge. You learned about getting user input in the mad libs exercise last week. For example, you can type num equals input, give me a number, and this will ask the user to input a number. However, this number will always be a string, so you can't perform calculations on it. Your next challenge is to make a programme that asks the user for the real age of a dog and then converts this to an integer. Then, calculate the dog's age in dog years-- so multiply by 7. Turn this result back into a string and then output the results so the user can see how old the dog is.

Skip to 4 minutes and 18 secondsThere are a few ways to do this, but you might find it easier if you jot it down in pseudocode first to help you figure it out. As always, you can share your achievements in the comments below.

Data types and dog years

There are many different types of data, and different programming languages have their own rules governing how different data types are treated.

In Python, for instance, characters such as 'a' or '?' are called strings. You can query an object to see which data type it represents, using the type() function. For instance:

type('Hello')

This tells you that 'Hello' is a string, as is "I am learning Python". (Single quotes ' and double quotes " are functionally equivalent when working with strings in Python.)

Other data types include integers, which are whole numbers such as 1 and 1000, and floating point numbers, which are implementations of fractions, such as 1.0 or 3.142.

There are many other data types, such as lists, dictionaries, and sets, which you will learn more about as you become more experienced in Python.

Python recognises various data types and will only allow certain operations on certain types of data. Let’s look at a practical example:

>>> print(2 + 2)
4
>>> print('2' + '2')
22
>>> print([2] + [2])
[2, 2]

In the first line, the numbers were integers, so the Python interpreter added them together. In the second line, the data was in the form of strings. The Python interpreter concatenates strings when it’s told to add them together. In the last line of code, each 2 was surrounded by square brackets. This meant they were lists, and the interpreter merged the lists into one.

Changing data types is fairly easy in Python. Doing so is called type casting. The most common examples of type casting is changing numbers to strings and back again. For instance, here a variable is being declared with a value of '3':

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

As this is a string, it can’t be used in calculations with an integer.

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

To use my_num in calculations, it must first be type cast as an integer.

>>> int(my_num) + 3
6

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

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

If we want to use my_new_num with a string, it needs to be type cast as a string. For instance:

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

Here we’ve used two of Python’s built-in functions: int() to turn a string to an integer, and str() to turn an integer into a string.

Challenge

You learned about getting user input in the Mad Libs exercise last week. For example:

num = input('Give me a number ')

This always saves the user’s number as a string though.

dog years

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’ (multiply by 7)
  4. Turns this result back into a string
  5. Outputs the result so the user can see how old the dog is

There are a few ways to do this, but you will probably find it easiest if you jot down a little pseudocode first to help you figure it out.

As always, you can share your achievements in the comments below.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation

Contact FutureLearn for Support