Data Types

I. Data types

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool. There are two Boolean values: True and False.
  • Binary Types: bytes, bytearray, memoryview

II. Check the data type.

In Python, to check the data type of a variable, we can use the type function with the following syntax:

type(data)

Example

type(22)
#int

type(8.9)
#float

III. Type Conversion

In a case where you want to convert the data type of a variable, Python also supports you through the following basic functions:

  • float(data) converts to a real number.
  • int(data, base) converts to a numeric type, where the base is the type of coefficient you want to convert to (this parameter can be left blank).
  • str(data) converts to string.
  • complex(data) converts to complex type.
  • tuple(data) converts to tuple type.
  • dict(data) converts to Dictionary type.
  • hex(data) convert to hexadecimal.
  • chr(data) converts to characters.

For example

  • float(3) converts integer 3 to float 3.0
  • int(3.9) converts float 3.9 to integer 3