大象传媒

Data types

Each has a . The data type determines what type of value the variable will hold.

Data typePurposeExample
IntegerWhole numbers27
RealDecimal numbers27.5
CharacterA single alphanumeric characterA
StringOne or more alphanumeric charactersABC
BooleanTRUE/FALSE TRUE
Data typeInteger
PurposeWhole numbers
Example27
Data typeReal
PurposeDecimal numbers
Example27.5
Data typeCharacter
PurposeA single alphanumeric character
ExampleA
Data typeString
PurposeOne or more alphanumeric characters
ExampleABC
Data typeBoolean
PurposeTRUE/FALSE
ExampleTRUE

Different data types have limitations:

  • integers and real numbers cannot be concatenated, ie joined together.
  • numbers held in strings cannot be subject to mathematical operations.

Casting

Sometimes a programmer needs to change the data type of the contents of a variable. For example, an integer may need to be converted to a string in order to be displayed as part of a message. This process is known as casting. The following examples in Python convert a string to an integer and an integer to a string:

str(68) returns 鈥68鈥
int(鈥54鈥) returns 54