Data Types in Python

Data Types in Python

Data types in Python are the categories of data items on which certain operations can be performed. We know that everything in Python is an object, therefore data types are actually classes and variables are instances(objects) of these classes.

 

 

Built-in Data Types in Python

Built-in data types are also called pre-defined data types. They define their own storage method and the types of operations performed on their values. Following are the built-in data types available in Python:

  • Numeric
  • Sequence Type
  • Boolean
  • Set
  • Dictionary

 

 

Numeric Data Type in Python

Numeric data type represents data that has numbers. It can be an integer, a floating-point or a complex number and are stores as int, float and complex class in Python.

Integers

It contains positive and negative whole numbers which do not have a decimal in them. Python does not pose any restriction on how long an integer number can be.

Floats

It contains real numbers that have decimals. They can be represented in a scientific notation which includes a character e or E followed by a positive or negative integer appended to the floating point representation. Python does not pose any restriction on how long a float can be.

Complex Numbers

It specifies numbers that have a real and an imaginary part and are represented like this:

(real_part)+(imaginary_part)j

Note: To know what type of data a variable has, type() function can be used.

#Python code to show the functionality of type()

var_int=3
print(var_int," is of ",type(var_int))

var_float=2.3
print(var_float," is of ",type(var_float))

var_complex=2+6j
print(var_complex," is of ",type(var_complex))

Output:

3  is of  <class 'int'>
2.3  is of  <class 'float'>
(2+6j)  is of  <class 'complex'>

 

 

Sequence Data Type in Python

In Python, an ordered collection of similar or different data types is called a sequence. It allows you to store data as a unit and in an organized manner. Sequence types in Python are:

  • String 
  • List
  • Tuple

String

A string is a collection of one or more characters enclosed within single, double or triple quotes. Python does not have a type to represent a single character the way other languages like C++ and Java have. To use a single character, a string with one character is created. It is of type str class.

Creating String

#Python program showing string creation

line1='Welcome to StudyExperts!'
print(line1," is of type ",type(line1))

line2="Welcome to StudyExperts!"
print(line2," is of type ",type(line2))

line3='''Welcome to StudyExperts!'''
print(line3," is of type ",type(line3))

Output:

Welcome to StudyExperts!  is of type  <class 'str'>
Welcome to StudyExperts!  is of type  <class 'str'>
Welcome to StudyExperts!  is of type  <class 'str'>

 

List

List is an ordered collection of elements of different data types. A list can have all elements of one type and of different types. It is mutable which makes it possible to change it.

Creating List

List is created by placing the elements inside a pair of square brackets.

#Python program showing list creation

list1=[1,2,3]
print(list1)

list2=[1,'StudyExperts',3.4]
print(list2)

list3=[list2,'Best']
print(list3)

Output:

[1, 2, 3]
[1, 'StudyExperts', 3.4]
[[1, 'StudyExperts', 3.4], 'Best']

 

Tuple

A tuple is an ordered collection of elements of same or different data types. It is like list except it is immutable.

Creating Tuple

It is created by placing elements separated by commas and can be places inside parantheses but is not mandatory. To create a tuple with one element, a trailing comma is required.

#Python program showing tuple creation

tuple1=(1,2,4)
print(tuple1)

tuple2=[1,'StudyExperts',3.4]
print(tuple2)



Output:

(1, 2, 4)
[1, 'StudyExperts', 3.4]

 

 

Boolean Data Type in Python

Boolean data type has two values: True and false. It has a class bool.

#Python program showing boolean data type

var_bool1=True
print(var_bool1," is of type ",type(var_bool1))

var_bool2=False
print(var_bool2," is of type ",type(var_bool2))



Output:

True  is of type  <class 'bool'>
False  is of type  <class 'bool'>

 

 

Set Data Type in Python

A set is an unordered collection of same or different type of elements. It is mutable and does not allow duplicate elements. If you try to store two same elements twice, it won’t be stored but will not throw any error, it will just accept only one element.

Creating Set

To create a set, place elements inside a set of curly braces separated by commas.

#Python program showing set creation

set1={12,"Hi","Hi"}
print(set1)

set2={12,"Hi",8.9}
print(set2)

Output:

{'Hi', 12}
{'Hi', 8.9, 12}

 

 

Dictionary Data Type in Python

Dictionary is an unordered collection of key-value pairs where each pair is separated by a comma and key and value of every pair is separated by a colon.

Creating Dictionary

It is created by placing the key-value pairs inside a set of curly braces.

#Python program showing dictionary creation

dict1={1:"You",2:"are",3:"at",4:"StudyExperts!"}
print(dict1)

Output:

{1: 'You', 2: 'are', 3: 'at', 4: 'StudyExperts!'}

 

Share this post

Leave a Reply

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