Python Beginners - Data Types. All programming languages have data types, many of which are shared from one language to another. This tutorial will focus on three broad types; numerical, text and boolean. A complete list of all the Python data types can be found at GeeksForGeeks - Python Data Types. You have seen the string type (text) in previous tutorials in this series. The other data types for this post are:

  • Integer/int - Whole Numbers
  • Float - Decimal numbers
  • Boolean/bool - True / False

Strings - Python Beginners - Data Types

Strings typed directly into the code can be identified by quotation marks at the start and end. E.g.

lineOfText = "This line of text is a string"

The above code creates a variable and assigns a string to the variable. If a number is surrounded by quotation marks, it is still a string and can only be treated as text. E.g.

number = "20"

It is still a string. Therefore, if we try to perform any mathematical operations, it will give an error. For example, try the following code to see the result.

number = "20"
number2 = number + 10

You should see an error message telling you that you can only concatenate strings.

Image showing the error in Idle if you try and perform maths on a string - Python Beginners - Data Types

Next, try the same code but without quotation marks, around 20.

number = 20
number2 = number + 10

The result should be like the image below, where the output is 30.

Python Beginners - Data Types, showing addition

Input is always a string.

Whenever user input is taken from the keyboard, it is taken as a string (text). Therefore, if we wish to perform any mathematical operation on it, that text must be converted into an integer or a float. E.g. In the following code, the user is asked to enter a number which is then stored into a variable. However, if you try and do any maths on it, you will get an error. Try the following code.

number = input("Please give me a number")
number2 = number + 10

You will see the same error you saw earlier. This is because the input is always stored as a string.

image showing the error if you try and do maths on an input in Python even if the user types a number. - Python Beginners - Data Types

Casting a string to an integer

Strings can be cast (converted) to integers (whole numbers) or floats (decimal numbers) if the characters in the string are digits or a period. E.g.

  • 20 can be cast to an integer or float.
  • 20.5 can only be cast to a float.
  • Twenty can not be cast to either an integer or a float.

Try the code example below. It creates a variable called strNumber and assigns the text "20" to it. Then casts (converts) the string into an integer and assigns it to a variable called intNumber. Finally, some maths is done on intNumber before the result is printed.

strNumber = "20"
intNumber = int(strNumber)
number2 = intNumber + 10

image of casting a string to an integer and then adding another integer to it in python

Casting An Input

Inputs can be cast to an integer or float precisely the same way as the code above; try the code below.

strNumber = input("Enter a whole number")
intNumber = int(strNumber)
number2 = intNumber + 10

image of idle showing a user input being taken then cast to an integer before some maths is done and the result is printed.

Simplifying casting code with an input

Similarly to other times, we have seen a complete example and a simplified version. The same is true for casting an input. It is often essential to understand the steps, but making the code shorter and easier to read is good practice.

intNumber = int(input("Enter a whole number"))
number2 = intNumber + 10

The code above performs all the same steps, but we have moved the input command inside the parenthesis of the casting.

Casting to a float

Casting a string to a float is very similar to casting to an integer; however, instead of int, we use float. See the code example below.

strNumber = "1.235"
floatNumber = float(strNumber)

Integers and Floats - Python Beginners - Data Types

Integers are whole numbers and are usually written in the shorter form, int. Floats are decimal numbers. We can perform mathematical operations on both floats and integers. However, it is worth discussing the symbols used for those new to programming. The characters we see in maths are:

  • ÷ (divide)
  • × (multiply)
  • + (add)
  • - (subtract)

In programming, the symbols for add and subtract are the same; however, divide and multiply are different.

  • / (divide)
  • * (multiply)
  • + (add)
  • - (subtract)

Addition, subtraction and multiplication with integers work precisely as you might expect. Try the following code examples to see the answer.

number = 10

number = 10

number = 10

In all three cases, the output is a whole number, meaning it has stayed as an integer. Now try the following code.

number = 10

This time the output will be 5.0

image showing integer division in python using Idle

If a division is done on an integer in Python, it is cast to a float (decimal number). This is one area in which Python differs from other languages. It reduces the number of possible outcomes. Since dividing an integer could result in a float (for example, 3/2), it always casts the result as a float. Float values can use all the essential mathematical operators you would expect. Try the following examples and play with the figures to satisfy yourself.

print (1.5 * 2)
print (1.0 / 2)
print (1.1 + 1.2)
print (1.1 - 0.1)
print (1 - -1)
print (1 * -1)
print (5 / -5)

image showing maths done on float type in python

Boolean / Bool - Python Beginners - Data Types

Boolean types have only two possible values. They are either True or False. An example of the code is below.

lightOn = True
ovenOn = False

The true and false values relate to binary or On and Off. Numerically this is 1 and 0. True/On/1 False/Off/0 The following tutorial covers boolean values in more detail when we make decisions. First, however, how to cast from an input to a boolean value is worth noting. Try the following code. Try to understand what is unexpected about it.

strValue = "False"
boolValue = bool(strValue)
strValue2 = "True"
boolValue2 = bool(strValue2)

Image showing a string to bool casting fail using Python in Idle

This is unusual because there is no error; however, both strings cast to the value True! To convert from a string to a boolean, the string must start as 0 or 1. Next, cast the string to an integer and cast the integer to a boolean. Try the code below.

strValue1 = "0"
strValue2 = "1"
intValue1 = int(strValue1)
intValue2 = int(strValue2)
boolValue1 = bool(intValue1)
boolValue2 = bool(intValue2)

Image of casting from a string to bool by first getting the input as 0 or 1, then casting to an integer before casting to a bool