AppDividend
Latest Code Tutorials

Python Data Types Example | Data Types In Python Tutorial

0

Python Data Types Example | Data Types In Python Tutorial is today’s topic. Data Types are used to define a type of variable. Data types include storage classifications like integers, floating-point values, strings, characters, etc. Data types define particular characteristics of data used in software programs and inform the compilers about predefined attributes required by specific variables or associated data objects.

Python Data Types

Every value in Python language has the datatype. Since everything is the object in Python programming, data types are classes and variables are an instance (object) of these classes.

There are various types of data. Some of the essential types are listed below.

#Python Numeric Data Type or Python Numbers

Python numbers are used to hold numeric values like Integers, floating-point numbers, and complex numbers fall under Python numbers category.

int

It holds signed integers of non-limited length.

float

It holds floating precision numbers, and it’s accurate up to 15 decimal places.

complex

It holds complex numbers.

See the following code example.

# app.py

MBB = 11
print(MBB)

ST = 11.21
print(ST)

KB = 19 + 21j
print(KB)

See the following output.

➜  pyt python3 app.py
11
11.21
(19+21j)
➜  pyt

Now, we can use the type() function to know which class a variable or a value belongs to and the isinstance() function to check if an object belongs to a particular class.

Now, see the following code.

# app.py

MBB = 11
print(type(MBB))

ST = 11.21
print(type(ST))

KB = 19 + 21j
print(type(KB))

See the following output.

➜  pyt python3 app.py
<class 'int'>
<class 'float'>
<class 'complex'>
➜  pyt

Let’s check if an object belongs to a particular class or not.

KB = 19 + 21j
print('Is this a complex number:' ,isinstance(KB, complex))

See the following output.

➜  pyt python3 app.py
Is this a complex number: True

Integers can be of any length, and it is only limited by the memory available.

A floating-point number is accurate up to 15 decimal places. Decimal points separate integer and float numbers. 11 is an integer, 11.0 is a floating-point number.

Complex numbers are written as, a + bj, where a is the real part and b is the imaginary part. Here are some examples.

#Python String Data Type

Python String is the sequence of characters. Python supports the Unicode characters. We can represent the strings by either single or double-quotes.

See the following example of Strings in Python.

# app.py

MBB = 'Stranger Things 3 Is Awesome'
print(MBB)

Netflix = "Suits Is The Best Legal Drama Series"
print(Netflix)
See the following output.
➜  pyt python3 app.py
Stranger Things 3 Is Awesome
Suits Is The Best Legal Drama Series
➜  pyt

#Python List Data Type

There is a total of four collection data types in the Python programming language, and Python List is one of them.

A list is a collection which is ordered and changeable.

We can write lists with the square brackets.

The list is the versatile data type exclusive in Python. 

Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

See the following code.

# app.py

STC = ['MBB', 'FW', 'GM', 'NS', 'CM', 'SS', 'JK', 'DH', 'WR']
print(STC)

See the following output.

➜  pyt python3 app.py
['MBB', 'FW', 'GM', 'NS', 'CM', 'SS', 'JK', 'DH', 'WR']
➜  pyt

All the elements in the list do not need to be of the same type. The list is the same as an array in C/C++ Programming Language. But the interesting thing about a list in Python is that it can simultaneously hold the different types of data which array can not do that.

# app.py

MBB = ['Eleven', 11, 11.21, True]
print(MBB)

See the following output.

➜  pyt python3 app.py
['Eleven', 11, 11.21, True]
➜  pyt

#Python Tuple Data Type

Python Tuple is the collection of Python objects separated by commas.

Python tuples are written with round brackets. 

Since tuples are immutable, iterating through a tuple is faster than a list. So there is a slight performance boost. 

If you have data that doesn’t change in time, then, implementing it as tuple will guarantee that it remains write-protected.

See the following code example.

# app.py

MBB = ('Eleven', 11, 11.21, True)
print(MBB)
print(type(MBB))

See the following output.

➜  pyt python3 app.py
('Eleven', 11, 11.21, True)
<class 'tuple'>
➜  pyt

#Python Dictionary Data Type

Python Dictionary is the collection which is unordered, changeable, and indexed.

It does not allow duplicate members in the dictionary.

Dictionaries are written with the curly brackets, and they have the key-values format. 

The Python dictionary is the unordered collection of items. Whereas, other compound data types like list or tuple have only value as an element, a dictionary has the key-value pair.

See the following code example.

# app.py

MBB = {'character name': 'Eleven', 'age': 11}
print(MBB)
print(type(MBB))

See the following example.

➜  pyt python3 app.py
{'character name': 'Eleven', 'age': 11}
<class 'dict'>
➜  pyt

#Python Set Data Type

Python Set is the unordered collection of elements or items.

Every item is unique means no duplicates are allowed and must be immutable, which means it cannot be changed in the future.

Sets can be used to perform the mathematical set operations like union, intersection, symmetric difference, etc. See the following code example.

Cast = {'Millie Bobby Brown', 'Finn Wolfhard', 'Noah Schnapp', 'Caleb Mclaughlin'}
print(Cast)
print(type(Cast))

See the following output.

➜  pyt python3 app.py
{'Millie Bobby Brown', 'Finn Wolfhard', 'Noah Schnapp', 'Caleb Mclaughlin'}
<class 'set'>
➜  pyt

Set is defined by values separated by a comma inside braces { }.

Items in the set are not ordered.

#Python Boolean Data Type

Python bool() method is used to return or convert the value to the Boolean value i.e., True or False, using a standard truth testing procedure.

The bool method returns the following values.

  1. It returns True if a parameter or value passed is True.
  2. It returns False if a parameter or value passed is False.

See the following code example.

# app.py

MBB = True
print(bool(MBB)) 

Max = False
print(bool(Max)) 

x = 19
y = 11
print(bool(x==y))

See the following output.

➜  pyt python3 app.py
True
False
False
➜  pyt

Finally, Python Data Types Example | Data Types In Python Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.