DATA TYPES IN PYTHON PROGRAMMING A computer is an electronic device that is capable of storing and…
By: Onyinye Okpoko
Tags: She Code Africa, Data Science, Python Programming, Women In Tech

DATA TYPES IN PYTHON PROGRAMMING

A computer is an electronic device that is capable of storing and processing data, usually in binary format. The computer does this by means of stored programs within it. Since the basic raw material that the computer works with is data, it is essential that the data is categorized or classified in a way that tells the processor what data to expect and the amount of memory space to reserve for it’s storage. This is where a data type comes in.

A data type defines the values that an expression (such as a variable or a function) might take. It also defines the operations that can be done on the data, and the way the values of that data type can be stored. It informs the program translator how the programmer intends to use the data. This is important as it makes it easier for the programmer or any other computer professional to focus on the main concern of solving larger problems rather than getting involved in the details of data description and access.

To this end, data types are of two categories. They are primitive data types and derived data types. The primitive data types are the fundamental or built-in data types supported by almost every programming language. On the other hand, the derived data types are usually user-defined. They are formed by combining one or more basic data types. They can be used when there is need to customize a data type or extend the functionality of built-in data types already available.

However, our focus will be on the primitive data types available in Python. They are of four types: integers, floating point, strings and booleans.

1. Integers: An integer data type represents numeric data. It can hold positive and negative whole numbers. Prior to Python 3, the integer data type had two sub-types; integer and long. However, from Python 3, integers are of unlimited size. Integer operations include arithmetic operations (such as addition, subtraction, multiplication, division and modular arithmetic) and logical or comparison operations.

2. Floating points: These are used to hold fractional numbers; that is numbers that contain a decimal point. Floating point numbers can be expressed in two ways: they can have decimal points in them (such as 2.1 and 7.234) or they could be written as exponentials- that is, numbers in standard form. For instance a floating point number written as 4E2 is the same as four times ten raised to power 2.

3. Strings: This data type holds a collection of alphabets, words or other characters. They are created by enclosing a set of characters within single or double quotes. Examples of data of string type include "Python" or "5/7 Kujama Street". Some of the operations that can be carried out on strings in Python include concatenation of two or more strings using the "+" sign, repetition of strings (by "multiplying" the string by a certain number), slicing and finding substrings.

4. Booleans: This data type can take up two values only. These are "True" (corresponding to 1) and "False" (corresponding to 0). They are useful in comparison and conditional operations.

Being that both Boolean values represent numbers, it is noteworthy that the boolean data type is a sub-class of the integer data type. Therefore, arithmetic operations carried out on booleans are evaluated based on what integer value each of them represent.

Determining the data type of a variable in Python is done by using the type() function. The variable is enclosed within the brackets of the type function and the data type of the variable is outputted by the translator program.