Built-in DataTypes In Python with Examples

Built-in DataTypes In Python with Examples

Hello Techies,

This blog is about data types in Python where we are learning about different Built-in DataTypes in Python and their examples.

In most Python interview, “tell the built-in DataTypes in Python?” is the interviewer’s favorite question. So read this blog till the end to know the answer to this question.

What is DataType?

Datatypes represent the type of data stored into a variable or memory.

Types Of Datatype in Python:

  1. Built-in Datatype
  2. User-Defined Datatype

So, this blog is dedicated to the built-in datatypes in Python, so we will check the details about it.

Built-in Datatype in Python

These data types are provided by Python language. Following are the Built-in datatypes:

  1. None Type
  2. Numeric Types
  3. Sequences
  4. Sets
  5. Mapping

Mutable data types in Python

  • An object whose value can change is said to be mutable.
  • Below are some mutable data types in Python:
    1. list
    2. set
    3. dictionary

Immutable data types in Python

  • Objects whose value cannot be changed are said to be immutable.
  • Below are some immutable data types in Python:
    1. Boolean
    2. Integer
    3. Float
    4. Tuple
    5. String
    6. Frozenset

#1. None Data Type in Python

None datatype doesn’t contain any value.

Example: a = None

a= None
print(a)
type(a)

Output

None
<class 'NoneType'>

#2. Numeric Data Type in Python

All numbers related datatypes come in Numeric Type. Following are the Numeric Datatypes:

  1. Int
  2. Float
  3. Complex
  4. Bool

Int Data Type in Python

The int datatype represents an integer number. Integer without any decimal point or fraction part. In Python, it is possible to store very large integer numbers as there is no limit for the size of an int datatype.

Example: 20,10, -80, -1000

y = 10
print(y)
type(y)

Output

10
<class 'int'>

Float Data Type in Python

The float type represents floating-point numbers. This number is a number that contains a decimal point.

Example: 20.56, 87.5, -89.50, -0.6

y = 29.58
print(y)
type(y)

Output

29.58
<class 'float'>

Complex Data Type in Python

A complex number is a number that is written in the form of a + bj or a+bJ where,

  • a is the Real Part of the number
  • b is the Imaginary part of the number
  • j or J is the sequence root value of -1
  • a and b can have integer or float numbers

Example: 5+7j, 0.8+2j

com = 8 + 9j
print(com)
type(com)

Output

(8+9j)
<class 'complex'>

Bool Data Type in Python

The bool data type represents the boolean value True(1) or False(0).

Example: True, False

print(2>3) #This type works in conditional statements.

Output

False

#3. Sequence Data Type in Python

Following are sequence type:-

  1. String
  2. List
  3. Tuple
  4. Range

String Data Type in Python

The string represents a group of characters. The string can be enclosed in double quotes or single quotes.

Example:- “Hello”, ‘TechPlusLifestyle’

a = "TechPlusLifestyle"
print(a)
type(a)

Output

TechPlusLifestyle
<class 'str'>

List Data Type in Python

  • A list represents a group of elements.
  • A list can store different types of elements that can be modified.
  • The List is dynamic which means the size is not fixed.
  • Lists are represented using a square bracket [].

Example:- data = [‘Tech’, ‘Lifestyle’, ‘Blog’, 1, 1.2]

list1 = ['Tech', 'Lifestyle', 'Blog', 1, 1.2]
print(list1)
type(list1)
print(list1[1]) # Here you can access the list value using the index.
list1[3] = 2
print(list1)

Output

['Tech', 'Lifestyle', 'Blog', 1, 1.2]
<class 'list'>
'Lifestyle'
['Tech', 'Lifestyle', 'Blog', 2, 1.2]

Tuple Data Type in Python

  • A tuple contains a group of elements that can be of different types.
  • It is similar to List but Tuples are read-only which means we can not modify its element.
  • Tuples are represented using parenthesis ().

Example:- data = (‘Tech’, ‘Lifestyle’, ‘Blog’, 2, 1.2)

data = ('Tech', 'Lifestyle', 'Blog', 2, 1.2)
print(data)
print(data[0]) # Here you can access the data value using the index.
type(data)

Output

('Tech', 'Lifestyle', 'Blog', 2, 1.2)
Tech
<class 'tuple'>

Range Data Type in Python

The range represents a sequence of numbers. The number in the range is not modifiable.

Example: data = range(5)

data = range(5)
type(data)
print(data[1]) # Here you can access the data range value using the index.
print('---range using for loop----')
for i in range(10,22,2):
    print(i)

Output

<class 'range'>
1
---range using for loop----
10
12
14
16
18
20

#4. Set Data Type in Python

  • A set is an unordered collection of elements like a mathematical set.
  • The order of the elements is not maintained in the set. This means that they cannot appear in the same order as the elements entered in the set.
  • A set does not accept duplicate elements.
  • Sets are unordered so we can not access its element using the index.
  • Sets are represented using curly brackets {}.

Example:- data = {10, 58, “Tech”, “Blog”}

data = {10, 58, "Tech", "Blog"}
print(data)
type(data)
data1 = {10, 58, "Tech", "Blog", 10}
print(data1)

Output

{"Tech", 10, 58, "Blog"} # unordered collection
<class "set">
{"Tech", 10, 58, "Blog"}
 # It does not accept duplicate values 

#5. Mapping type/ dict/ Dictionary Data Type in Python

The map represents a group of elements in the form of key value pairs.

Example: data = {‘str’:’test1′, 103: ‘test2’}

data = {'str':'test1', 103: 'test2'}
print(data)
type(data)
print(data['str']) # Here you can access the data dict value using the key.
data['str'] = 'Test5'
print(data)

Output

{'str':'test1', 103: 'test2'}
<class 'dict'>
test1
{'str':'Test5', 103: 'test2'}

These are the built-in datatypes in Python. I hope you have understood the explanations and examples of the built-in datatypes in Python that I have included in this blog.

See their official site for more information.

Leave a Comment