Data Types in Python
Types of Data in Python - Bytesize Python
Types of Data?
Types of data are labels for what different types of information is stored as. Normally python will work out what what type of data is being used, however sometimes you will need to change one type to another to efficiently use it. Here we cover the main types of data.
Numbers
Python has two main data types for numbers. 'Integers' are whole numbers, (numbers without a decimal point). 'Floats' are numbers with a decimal point. (You can find out more about numbers in the Maths in Python page.
Strings
In Python, a piece of text is called a 'string'. Strings can include letters, numbers, spaces, and symbols. Always remember that strings need quote marks at the start and end.(You can find out more about strings in 'Strings in Python')
Booleans
Booleans always have a value that is either 'True' or 'False'. Both words start with a capital letter and do not need quotation marks around them.
Spotting data Types
There are many data types in Python. To find what data type something is, you can use the 'type' command.
int = integer
float = float
str = string
Converting data types
Variables can contain any types of data. Sometimes, errors can occur if you try mix them together. So, data types will sometimes have to be converted. The 'input' command always gives a string, even if a number is entered.
In this example the variable 'ants' actually contains a string, so when trying to +1, and error message is displayed.
To convert the string into an integer, the 'int()' command is used to turn it into an integer.