Variables and constants in Python

Data types

Data types

A data type is the characteristic of a variable that determines what type of data it can hold. The following table explains different data types and gives an example of each.

Data Type Description Example
Text (string) Contains alphanumeric. "Hello, World!"
Text (character) Contains a single alphanumeric. "s"
Numeric (integer) Whole positive and negative numbers. 5478
Numeric (floating point) Positive or negative numbers with decimal places. 34.563
Numeric (date/time) Represents a date and time and stored as a numeric.  20241118 153455
 Boolean  A logical value true or false.  True

Python supports the following simple data types for variables and constants:

  • Boolean
  • integer
  • float (also known as floating point)
  • string.

Boolean

A Boolean can either be True or False.

When evaluating whether something is True or False, True can be represented by any value other than 0 or None, and False is represented by 0, None, the string '0', an empty string, or an array with no elements.

Integer

An integer is a positive or negative whole number, e.g. 42, -1, 0, 999999, -504.

Floating-point (or float)

A floating-point, or a float, is a number with a decimal point or an exponential in it, e.g. 1.234, 1.2e3, 7E-10.

String

Strings are text or words – a 'string' of characters. A string is made up of zero or more characters that have been declared with either single or double quotes around them, e.g. "Mary had a little lamb", "cat", "42", "0", '1.23', 'Mouse', '@#$', or even an empty string "".

A note on data types for numeric values

To decide whether a numeric value should be an int or float, or a string, think about how the number will be used. If it represents a measurement or a count of something, then it should be an int or a float. If it is a number that won't be used in calculations – e.g. an ID number, a phone number, a registration number or street number – then it should be stored as a string.

This is particularly important for numbers that might have a leading zero that needs to be preserved (e.g. bank account numbers: 0123456, phone numbers: 0400999000, and post codes: 0123) or that might contain other characters, including spaces (e.g. phone numbers: +61 9999 0000, (03)9999-0000 etc.). These numbers should be stored as strings.

Char

Many other programming languages have a data type called char, which is a single character, e.g. "a", "Y", "3". In some languages, a string is an array of chars. In Python, a variable or constant whose value is a single character in length is still a string.