Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Built-in Data Types
2.1.
Numeric Data Type
2.2.
String Data Type
2.3.
List Data Type
2.4.
Tuple Data Type
2.5.
Boolean Data Type
2.6.
Set Data Type
2.7.
Dictionary Data Type
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

Data Types in Python

Author APURV RATHORE
1 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Python is undoubtedly one of the most widely used programming languages in today's programming world. That is because it is simple to code any program, which helps programmers concentrate all of their work on the implementation parts rather than complex programs.

Python is the programming language with which one can quickly begin their programming journey because of its readability and simplicity. However, to master any programming language, not just Python, but any programming language, we must first have a solid and deep comprehension of its core concepts. Variables and Data Types are two essential concepts in Python that we discussed here.

Different types of data types supported by Python are discussed below. 

Source

You can also read about the Multilevel Inheritance in Python, Swapcase in Python

Built-in Data Types

Numeric Data Type

Numeric data types in Python represent data with a numeric value. Python supports three types of numeric data types:- integers, floats, and complex. 

  • Integers: Integers are represented as int in Python. Integers can be any length; their length is only limited by the amount of memory available in the system. This means that integers contain all numbers, positive and negative, but they don’t contain decimal points.
  • Float: They are represented as float in Python. Floats are real numbers with a decimal point to distinguish between fractional and integer portions. It is accurate up to 15 decimal places. 
  • Complex Numbers: They are represented as complex in Python. Complex numbers can be represented as (x+yj), in which x represents the real part and y represents the imaginary part. 

Additionally, we can use the type() function to determine the data type of a variable. 

# integer
var_int = 50
print(type(var_int))
# float
var_float = 50.51
print(type(var_float))
#complex
var_complex = 1 + 2j
print(type(var_complex))

 

Output:

<class 'int'>
<class 'float'>
<class 'complex'>

String Data Type

Strings are arrays of bytes in Python that represent Unicode characters. A string is made up of one or more characters enclosed in a single, double, or triple quotation. Multiline strings can be created using triple quotation marks. Python has no character data type; a character is a one-length string. The str class is used to represent it. 

Strings can be of any length. The only limitation is the memory of the system. 

Strings are immutable in Python, which means that string does not support modification or deletion. If you try to modify the string, Python will throw an exception. 

Individual characters of the string can be accessed through indexing in Python. 

s = "This is a string"
print("This is a normal string:",s)

s = '''This is
another  string'''
print("This is a multiline string:",s)

 

Output:

This is a normal string: This is a string
This is a multiline string: This is
another  string

List Data Type

The list is a collection of items in an ordered sequence. It's one of Python's most popular data types, and it's pretty versatile. All items in a list don’t have to be of the same type. One of the best properties of the List is that it can hold the number of multiple elements simultaneously while still maintaining their order.

The difference between list and array in Python is that lists can store elements of different data types, while the data types of elements in an array should be the same. 

List can be created by placing the sequence inside square brackets. 

l1 = [1,2,3]
print("List containing integers:\n",l1)

l2 = ["string1", "string2", "string3"]
print("List containing strings:\n",l2)

l3 = ["string1",2,"string3"]
print("List containing both integers and string:\n",l3)

List containing integers:
 [1, 2, 3]
List containing strings:
 ['string1', 'string2', 'string3']
List containing both integers and string:
 ['string1', 2, 'string3']

 

The index number can be used to access the list items. To go to a specific item in a list, use the index operator []. Negative sequence indexes in Python represent positions from the array's end. It is sufficient to write List[-2] instead of needing to compute the offset as in List[len(list)-2]. Negative indexing indicates starting at the end, with -1 being the last item, -2 denoting the second-to-last item, and so on.

Also see, Python Operator Precedence

Tuple Data Type

The tuple is an ordered collection of Python objects, similar to a list. The sole difference between a tuple and a list is that tuples are immutable, meaning that once generated, they cannot be changed. The tuple class is used to represent it.

They can be created by placing the sequence of elements separated by a comma inside a parenthesis. They can contain any data type or combination of data types. 

The element inside a tuple can be accessed with an indexing method, similar to the list. 

t1 = (1,2,3)
print("tuple of integers:",t1)
t1 = (1,"string2",3)
print("tuple of integers and string:",t1)
print("type of t1:",type(t1))

 

Output:

tuple of integers: (1, 2, 3)
tuple of integers and string: (1, 'string2', 3)
type of t1: <class 'tuple'>

Boolean Data Type

Boolean data types in Python can have two values: True or False. Non boolean objects can also be evaluated and determined to be True or False in a boolean context. 

a = True
print(type(a))
a = False
print(type(a))

 

Output:

<class 'bool'>
<class 'bool'>

Set Data Type

Set is an unordered data type collection that is iterable, mutable, and has no duplicate elements in Python. The sequence in which elements of a set appear is undefined.

Set can be created by placing the sequence of elements inside curly braces {}, or by passing an iterable inside the built-in set() function. 

The set elements cannot be indexed by indexing since sets are an unordered collection of items. 

s = set([1,2,3])
print("set:",s)
s = {1,2,3,4}
print("set:",s)
print(type(s))

 

Output:

set: {1, 2, 3}
set: {1, 2, 3, 4}
<class 'set'>

Dictionary Data Type

In Python, a dictionary is an unordered collection of data values, similar to a map, used to store data values. Unlike other Data Types, which only contain a single value as an element, Dictionary holds a key-value pair. A colon separates each key-value pair in a Dictionary, whereas a comma separates each key.

Dictionary can be created by placing a sequence of key-value pairs separated by colon within curly braces. It can also be made using the built-in function dict().

The value of the key in the dictionary can be updated or indexed using square brackets. Get function can also be used to get the value of a key. 

d = {1:2,2:3,3:4}
print(type(d))
d = dict()
d[1]=2
print(d.get(1))

>>>
<class 'dict'>
2

 

You can try it on online python compiler.

Check out Escape Sequence in Python

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

  1. Which is faster in Python, list or array?
    Python Lists are slower than NumPy Arrays. A collection of homogeneous data types stored in contiguous memory spaces is referred to as an array. On the other hand, a list is a collection of heterogeneous data types stored in non-contiguous memory regions in Python.
     
  2. What are sequence data types in Python?
    Sequence data types allow you to store multiple values in an organized, ordered, and efficient fashion. The sequence data types in Python are strings, Unicode strings, lists, tuples, byte arrays, and range objects.
     
  3. What is the range of int in Python?
    Unlike C/C++, there is no bound on the range of value of integers in Python. However, the practical limit can be considered to be the amount of address space available. 

Key Takeaways

Congratulations on making it this far.

In this blog, we understood what data types are in Python. We also learned about various data types present in Python. 

To study more about data types, refer to Abstract Data Types in C++.

Recommended Reading:

If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms. 

Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass