learn code with examples

Python Built-in Data Types

Programming languages use data types, so that programmer and end users - both can interact with computer in well manner. Example, Money - 10 (its numerical), Name - "Adam" (its textual). Humans can identify that name can not be multiplied or divided, but number can be added, subtracted or divided. Here, number (integer, decimal) and text (string) are kind of data types.

Programming languages define rules for each data type and provides different facilities for each one.

Python data types are:
  • Boolean (bool)
  • Numeric (int, float, complex)
  • Sequence (list, tuple, range)
  • Text Sequence (str)
  • Binary Sequence (bytes, bytearray, memoryview)
  • Set (set, frozenset)
  • Mapping (dict)
  • Classes and Instances
  • Exceptions
Below details explains python data types.

Numeric Types

Its a facility to store numbers as int (negative / positive integers, zero), as float (number with decimal points) or as complex (mathematical complex numbers with real and imaginary).

# Example, Numeric data types in python.
my_int = 5 # int
current_temp = 98.3 # float
my_complex_no = 10 + 1j # complex

Boolean Type

Its a facility to store truth value as True or False, useful for logical operations. Internally, it is subtype of int from python 3.x.

# Example, Boolean data type in python.
holiday = True
holiday = False

Sequence Types

Its a facility to store more than one element together. Also word sequence confirms that each element's location matters. (i.e. they are in certain meaningful order / position).

Python Sequence data types are list, tuple, range.
list provides operational flexibility (to add or delete element).
tuple provides performance advantage compare to list, but can not be modified, once created.
range provides facility for looping on numbers.

Sequence type can also be segregated as:
Mutable Sequence - Can be modified once created. Example, List
Immutable Sequence - Can not be modified once created. Example, Tuple, Range.

# Example, Sequence data types in python.
my_list = [ 0, 1.1, True, "Hello", 1, 1, 1]
my_tuple = ( 0, 1.1, True, "Hello", 1, 1, 1)
range(1,100) # range from 1 to 100, including 1, excluding 100

Text Sequence Type

Its a facility to store textual data as string. Python does not have separate char data type. String is sequence of one or more characters stored together, specified within single quotes, double quotes or triple quotes. Strings are immutable (i.e. can not be modified).

# Example, String data type in python.
name = "Eve"

Binary Sequence Types

Its a facility to store sequence of byte. bytes are immutable (i.e. can not be modified), bytearray are mutable (i.e. can be modified) - both stores sequence of byte.
memoryview provides facility to view internal representation of object.

# Example,  Binary data type in python.
var1 = b"\t\\AZaz09+-" # Note prefix b, characters are ASCII, \ is used for escape sequence.

Set Types

Its a facility to store elements without meaningful order / position. But it gives unique advantage for fast searching of element within set. It does not support duplicate elements.

set is mutable (i.e. can be modified), while frozenset is immutable (i.e. can not be modified).

# Example,  Set data type in python.
continents = { "Asia", "Africa", "North America", "South America", "Antarctica", "Europe", "Australia "}

Mapping Type - dictionary

Its a facility to define corelation from key to value, say ID number to name. It is called dictionary. It is mutable.

# Example,  Mapping - Dictionary data type in python.
country_code = { "ZIMBABWE" : "ZW", "JAPAN" : "JP", "GERMANY" : "DE", "FRANCE": "FR" }

When you define class, you are creating user defined / custom data type.

Other ways to segregate

Mutable data types

Mutable data types support facility to change its content (i.e. add, delete, rearrange elements) without returning collection object itself, i.e. in place operations. Python mutable data types are:

  • list
  • bytearray
  • set
  • dict

Immutable data types

Immutable data types does not support facility to change its content. Python immutable data types are:

  • tuple
  • range
  • bytes
  • frozenset

Ordered and Unordered data types

Sequence data types - list, tuple, range, str, bytes, bytearray, memoryview are ordered.
Set and Mapping data types - set, frozenset, dict are unordered.

Primitive data types

Primitive means something basic - at atomic level. Python does not separate primitive data types. In other programming language if primitive indicates without class, challenge with python is - bool, int, float, complex - all are classes. And, there is no char type in python.

Collection data types

list, tuple, set, dict - are general purpose collection data types in python.

Char, Long int, Long long int, Double

Python does not have char data type, rather its considered as string with one character.

Python does not have long int or long long int data type, rather int data type support unlimited value range for integer! Python has unique way to store large number such as 10**10000 (10 power to 10000).

Python does not have double data type, rather decimal data type supports fixed point and floating point arithmetic as humans learn in school (1.1 + 2.2 = 3.3 and not 3.300000000000000003).

Python Variable

Variable is something that can hold value. Its defined with some name excluding reserved keywords of programming language. Example,

my_int = 5
Here my_int is variable, 5 is its value.

In Python, you do not need to declare variable separately. Variable creation happens without declaration and without specifying data type.
PS: Declaring variable and creating / initializing is concept for C-style language (example, C array can be declared and later on created in memory).

Specify Data type while creating Variable

In Python, you do not need to specify data type with variable name. This gives you flexibility to use same variable name to represent another data type (based on need).

In this page...

Copyright © All rights reserved.