DEV Community

Cover image for Python Basics 2: Datatypes
Irfan Faisal
Irfan Faisal

Posted on Edited on

Python Basics 2: Datatypes

Datatype:
Every value in Python has a datatype. The datatype is mainly the category of the data. There are basically 5 categories of datatypes; however, these categories have further classifications as well.

1.Numeric Type:
a) Integer (int): positive or negative whole numbers (without a fractional part). Example: 10,-3,10000

b) Floating point (float): Any real numbers with 'decimal' points or floating point representation.
Example: -3.14, 10.23

c) Complex Number: Combionation of real and imaginary number. Example: 2+3i [we don't use this that much]

2.Boolean Type(bool): Data with one of the two built-in values True or False. Often used in the comparison operations of logical statements.
Example:

x=10
y=15
print(y>x)
#output: True
Enter fullscreen mode Exit fullscreen mode

3.Sequence Type: A sequence is an ordered collection of similar or different data types.

a)String(str): It is a sequence of ordered characters (alphabets-lower case, upper case, numeric values, special symbols). String is represented with quotation marks (single('), double(''), triple(' ' ', '' '''')).

For example:
using single quotes ('python'),
double quotes ("python") or
triple quotes ('''python''' or """python""")

b)List: It is an ordered collection of elements where the elements are separated with a comma (,)and enclosed within square brackets [].

The list can have elements with more than one datatypes.

For example:
i) list with only integers; x= [1, 2, 3] and

ii) a list with mixed data types; y= [110, "CSE110",12.4550, [], None]. Here, 110 is an integer, "CSE110" is a string, 12.4550 is a float, [] is a list, None is NoneType.

c)Tuple: It is an ordered collection of elements where the elements are separated with a comma (,) and enclosed within parenthesis().

Example: x= ("apple", "banana", Cherry")

List and Tuple are used to contain multiple values in a single variable.

Basic difference between List and Tuple: You can change the values of a list(mutable) but you can't do the same with tuple(immutable) [more about this in later series]

4) Mapping Type:

Dictionary: It's an unordered collection of data. It's written as key:value pair form.
Example:

 cardict= {'brand' : 'Lamborghini' , 'model': 'Aventader', 'year'=2018}
            #key        #value

 print (cardict['model'])
 #Output:Avantader

Enter fullscreen mode Exit fullscreen mode

5)Nonetype(None):It refers to a null value or no value at all. It's a special data type with a single value, None.

Datatypes

Type Function:
Type() function returns the type of argument(object) passed as a parameter. It's mainly used for debugging(Code correcting) purpose.
To know the type of a variable just write type(variable name)

text="Python is awesome"
print(type)
#output:<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Example:
type("Hello python") #output: str
type(2024) #output: int
type(3.14) #output: float
type(True) #output: bool
type(None) #output: NoneType

isinstance function:
The isinstance() function checks if an object belongs to a specified class or data type.
In the code:

isinstance(myfloat, float)

This checks if the variable myfloat is an instance of the float class. If it is, it returns True, otherwise False.

Similarly, for integers:
isinstance(myint, int)
This checks if the variable myint is an instance of the int class.

Top comments (1)

Collapse
 
coderanger08 profile image
Irfan Faisal •

Hi everyone! I'm back with my second posts of my python basics tutorial series. This time I have talked about the various datatype of python. I hope it may help you guys to learn or brush up the basics. If you have any questions or suggestions, please let me know in the comments. Happy Learning!