python programming - Overview of python datatypes

19 Mar 2019 - Mark Edosa

Python datatypes such as numbers, sequences (e.g. strings, lists, tuples), mappings or dictionaries are the building blocks of a python application. This article gives an overview of python’s major datatypes.

NB: A useful function for determining the type of object is type().

NUMBERS

Basically, python numbers includes

  • integers or whole numbers which can either be positive or negative e.g. 1, 100, -5 etc. They are created literally e.g. 100 or with the function int(). Note: Boolean datatype, a variant of integer datatype also exists.

  • float datatypes or numbers with decimal places e.g. 2.50, -3.142 etc. Floats are also created literally e.g. 1.5 or created with the function float()

  • decimal datatype which is a varaiant of the float datatype that produces “humanly” accurate calculations e.g

        1.1 + 2.2 = 3.3000000000000003  # float datatype

        1.1 + 2.2 equals 3.3   # decimal datatype

        0.1 + 0.1 + 0.1 - 0.3 equals 5.5511151231257827e-017 # float datatype

        0.1 + 0.1 + 0.1 - 0.3 equals 0  # decimal datatype
  • complex numbers e.g. 3 + 5j.

Python numerics or numbers except the complex datatype support the usual arithmetic operations such as addition, subtraction etc. s well as logical operations and hence are useful for applications which are monetary or numeric in nature.

SEQUENCES

Python sequences which are ordered collections of items includes

  • strings - set of characters or numbers within single/double/triple quotes. They can be created literally e.g "Peter Griffin" or with the str() function

  • lists - a python list is a comma-seperated, homogenous/heterogenous set of items within square brackets e.g. [1, 2, 4, "dog", "rabbit"]. They are also created with the list() function.

  • tuples - tuples are like lists but contain items enclosed in parenthesis e.g. (1, 2, 4, "dog", "rabbit") and also immutable. Don’t worry if you don’t understand immutability for now. The tuple() function creates tuples as well.

Python sequences share common traits such as indexing which is zero-based, sorting, slicing and length.

        list_of_names = ["Peter", "Lois", "Meg", "Chris", "Brian", "Stewie", "Brian"]
                           0         1      2       3        4       5          6
        ## Indexing

        list_of_names[0]  ## "Peter"   ### the first item on this list, sits at index 0
        list_of_names[6]  ## "Brian"   ### the last item on this list, sits at index 6

        name = "Michael Jordan"
                0123456789...     # the indices.

        name[5]   ## "e"   ### letter "e" is found at the 5th index

However, the list datatype differ from string and tuple datatype because it is mutable. this means you can add or remove items from the original list unlike strings and tuples which cannot be modified in place. New strings or new tuples can only be created from the old string or tuple respectively. Sequences especially lists and tuples are great for storing/transfering items while the string is also useful for displaying messages to users of an application

MAPPINGS (Dictionaries)

Python dictionaries consist of comma-seperated list of key-value pairs with curly braces. They are just like an English dictionary which contains word-meaning pairs. Like the list datatype, the dictionary is mutable but it cannot be sorted or sliced because it does not respect the position of its items i.e. it is unordered.

        # An example of a python dictionary

        some_character = { "name": "Peter Griffin", "age": 1000, "height": "299m"}

        # Retrieving the name
        some_character["name"]  ## "Peter Griffin"

Dictionaries can also be created with the dict() function.

OTHERS

Other datatypes in python includes

  • Sets - which is an unordered collection of unique items. There are created literally as a set of items in curly braces e.g {1, 3, 5, 6} or with the set() function. Like everyday mathematics, set operations e.g. intersections, unions etc. can be performed on python sets. As an example,
 

        # assuming we have a list of duplicated items
        list_containing_dups = [1,1,2,2,3,3,4,4,5,5]

        # the set function takes the list and returns a set of unique values
        set(list_containing_dups)  ## {1, 2, 3, 4, 5}
  • Enums - According to python’s official documentation, an enumeration or Enum is a set of symbolic names (members) bound to unique, constant values. An example will make it clearer.
        from enum import Enum

        class Color(Enum):

            NORTH = 1
            EAST = 2
            SOUTH = 3
            WEST = 4

        ## Color.WEST equals or represents 4
  • Datetime - The datetime object object in python consists of datetime properties useful for manipulating dates and times as well as performing for performing date and time arithmetics.
    from datetime import date

    today = date.today()

    print(today)  ## datetime.date(2018, 11, 21)

    print(today.year)  ## 2018
    print(today.month)  ## 11
CONCLUSION

This article gave an overview of python datatypes. Next is python statements, expressions and variables.

About author
Generic placeholder image
Mark Edosa

Mark Edosa is an Optometrist by day and a Web developer / Data scientist by night. At his spare time, he loves learning new stuffs as well as listening to music.