In this Python tutorial, you will learn about different data types you can use in Python.
Normally when people think of variables they think of Algebra. Immediately some people get scared to give up on programming. But the fact of the matter is people shouldn’t be scared of the concept of a variable. In simple terms, a variable holds some unknown quantity.
In computer programming, you have to think of a variable as a box that stores some value of some type that we can store away for a later date. When we are programming we work with different data types. some of these data types are integer, float, string, char, Boolean, tuples, list, and object. Lets look have a deeper look at each data type.
Data type : In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data.
Every value in Python has a datatype. Data types are the classification or categorization of data items. The concept of datatype in programming is very important .
We store some data in memory , the stored data in memory can be of many types. For example, income or age are stored as a numeric value , and address , stored as alphanumeric characters. Python has various standard data types to define the storage method for each of them.
Python has five standard data types −
Python has the following data types built-in by default, in these categories:
Watch this video to learn with Examples & Exercise :
Numbers in Python
Number data types store numeric values. Integers, floating point numbers and complex numbers are under the number category in python, defined as
complex classes in Python.
We use the
type() function to know the class of variable or value . Similarly, the
isinstance() function is used to check if an object belongs to a particular class.
Print the data type of the variable x:
Integers can be of any length, it is only limited by the memory available.
A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is an integer and 1.0 is a floating-point number.
Complex numbers are written in the form,
x + yj, where x is the real part and y is the imaginary part. Here are some examples.
As we know , in python we have different datatypes , so we can also convert from one type to another with the
Convert from one type to another:
Note: We can’t convert complex numbers into another number type.
Random Number in Python
In Python , there is no any function like
random() as, other functions , i.e type() , int() , to make a random number, but Python has a built-in Module called
random that can be used to make random numbers:
Import the random module, and display a random number between 1 and 20 :
We will learn more in Random Module .