Basic Data Types

Data Science with Python: Basic Data Types

We have set up the foundation of Data Science in previous articles while learning Linear Algebra, Coordinate Geometry, Planes, and Matrices.

Table of Contents

  1. What is Data Type?
  2. Check the Data Type
  3. Differences between Data Types and Data Structures
  4. Data Types
  5. Conclusion

What is Data Type?

Data Types in elementary language are defined as “classifying a variable concerning the value to which type it belongs and what types of operations can be performed on the variable without error.” There are two properties to classify the data type:

1. Operations performed on Data

Various operations can be performed on variables like arithmetic, logical, boolean, etc. Their type can be defined based on the operations performed on data. In addition, subtraction can be performed on Integers and floats, whereas boolean operations can be performed on boolean data type variables.

2. Data Storage Space

Variables of different data types required different storage space. Accordingly, compilers create and manage the data storage space of variables. The size of the float variable is 24 bytes in a 64-bit system.

The size of variables of different data types can be checked using the getsizeof() function.

Check the Data Type

To get the data type of any object, we use the type() function and to get the size of the variable, the getsizeof() function is used as shown below:

x=5

print(type(x))

print(“The size of the variable is:”,sys.getsizeof(x), “bytes.”)

Output:

>>> <class ‘int’> 

>>> The size of the variable is: 24 bytes.

Set the Data Type

In Python, the data type of a variable is assigned as soon as the value is assigned to a variable. Python Code example shown below:

x = 5.2

print(x)

print(type(x)) 

Output:

>>> 5.2

>>> <class ‘float’’> 

Data Types

Basic data types are divided into Numeric and Boolean data types. Then, further, there are three types of numeric data: Integer, Float, and Complex Numbers. Let’s learn all of these in detail.

Integers

Integers are abbreviated as ‘int’ in programming language. Integers have the following properties:

  • Integers are positive or negative numbers with no decimal point.
  • In Python, there is no limit defined for integers, for how long their value can be. Of course, it is limited to the amount of memory our system has, but beyond that, an integer can be as long as we need it to be.
  • Various arithmetic operations can be performed with Integers.

Here, is an example of an integer with Python code.

x = 1111

print(x)

print(type(x)) 

Output:

>>> 1111

>>> <class ‘int’’> 

Float

The float type in Python represents a floating-point number or a decimal number. Float values can easily be identified with a decimal point. Various arithmetic operations can be performed with Floating Numbers.

Here is an example of a floating number with Python code.

x = 5.24

print(x)

print(type(x)) 

Output:

>>> 5.24

>>> <class ‘float’’> 

Complex Numbers

Complex numbers containing real and imaginary parts specified as <real part> + <imaginary part>. A numeric value with ‘j’ represents an imaginary part like ‘2+5j’. Various arithmetic operations can be performed with complex numbers.

Here, is an example of a complex number with Python code.

x = 2+5j

print(x)

print(type(x)) 

Output:

>>> (2+5j)

>>> <class ‘complex’> 

Boolean

Python provides a Boolean data type represented as a ‘bool’ class. As computers can only understand 0’s and 1’s, Boolean is a helpful data type. Objects of the Boolean type may have one of two values: True or False. Here, True means ‘1’, and False means ‘0’

When two variables or values are compared, python returns the Boolean answer. Let’s learn the Data type with the help of a Python code example.

x = True

print(x)

print(type(x)) 

a = 21

b = 20

 

#Let’s compare variables ‘a’ and ‘b’

print(a > b)

print(a == b)

Output:

>>> True

>>> <class ‘bool’> 

>>> True

>>> False

In the above code, we can see that a is greater than b, so True is the output for that comparison, and when we compare for equality, the output is False.

Conclusion

In this article, we have explained various basic data types in Python in brief with real examples. They are building blocks of learning Python and are very important to understand for advanced topics.

Stay Tuned!!

Data Science with Python: Introduction 

Keep learning and keep implementing!!

Leave a Comment

Your email address will not be published. Required fields are marked *