Tutorial

Data Types in Python

4 min read

In this tutorial, we will learn about data types in Python.

Previously we learned about variables used in Python. The variables declared in Python are put under various categories known as Data types. These data types help us to determine the type of data that can be stored in these variables.

Python Data Types

The different types of standard or built-in data types in Python are:

  • Numeric – int, float, complex.
  • String – str
  • Sequence – list, tuple, range.
  • Binary – bytes, bytearray, memoryview.
  • Mapping – dict
  • Boolean – bool
  • Set – set, frozenset
  • None – NoneType

Numeric Data Types in Python

Python has four different numeric data types −

  • int – used to represent integers.
  • long – used to represent long integers as well as octal & hexadecimal numbers.
  • float – used to represent floating point numbers.
  • complex – used to represent complex numbers.

Example of numeric data types in Python

a = 6
b = 123456
c = 6.107
d = 3.14j
print(a)
print(b)
print(c)
print(d)

The output of this will be:

6
123456
6.10
3.14j

Here, we used four variables to store and print the different numeric data types discussed above.

String Data types in Python

Python has string type of data represented with the help of quotation marks. These can be either single quotes(‘ ‘) or double quotes(” “).

The strings are used with zero-based indexing where the first character starts from zero and it is represented using brackets([ ]). Strings can further be broken down into subsets using the slice operators([:]).

The plus(+) symbol helps us to concatenate or join two strings together.

Example String Data types in Python

a = "Hello World!"
print(a)
print(a[0])
print(a[2:5])
print(a[2:])
print(a + "2023")

The output of this will be:

Hello World!
H
llo
llo World!
Hello World!2023

Here, we used a string variable and printed the different types of operations that can be performed on string data types discussed above.

List Data types in Python

Lists in Python are very similar to arrays.

But unlike arrays, lists can contain items of different data types.

Python lists are enclosed by square brackets([ ]) and the items inside the list are separated by commas( , ).

Values of different items in a list can be used by a slice operator( [:] ). The plus(+) symbol helps us to concatenate or join two strings together.

Example of list data types:

a = [6, "hello", 3.14, "Python", 10]
print(a)
print(a[0])
print(a[1:3])
print(a[2:])

The output of this will be:

[6, "hello", 3.14, "Python", 10]
6
["hello", 3.14]
[3.14, "Python", 10]

Here, we can see the various outputs that we got from the list declared above.

Tuple Data types in Python

Tuples in Python are very similar to lists.

They are separated by commas( , ) and can also contain any type of data. But the main point of difference between tuples and lists is that tuples are surrounded by parenthesis( ).

Tuples are better described as read-only lists.

Example of tuples in python:

a = (6, "hello", 3.14, "Python", 10)
print(a)
print(a[0])
print(a[1:3])
print(a[2:])

The output of this will be:

(6, "hello", 3.14, "Python", 10)
6
("hello", 3.14)
(3.14, "Python", 10)

Here, we can see the various outputs that we got from the tuple declared above.

Dict Data Types in Python

Python dictionaries are unordered data sets that store data just like maps or hash tables. Unlike other data types, a dictionary stores a key and a value together in a pair. They are represented by curly brackets( { } ).

Example of dic data types in Python:

dict = {'name': 'Ravi', 'city': 'Kolkata', 'category': 'Student'}
print(dict)
print(dict[city])
print(dict.keys())
print(dict.values())

The output of this will be:
{'name': 'Ravi', 'city': 'Kolkata', 'category': 'Student'}
Kolkata
['name', 'city', 'category']
['Ravi', 'Kolkata', 'Student']

Here, we can see the various outputs that we got from the dictionary declared above by the name of dict.

Boolean Data types in Python

Boolean data type in Python is built-in with two values named True and False. The Python bool() function is used to determine whether a given argument is True or False.

Example of boolean data type in Python:

print(type(True))  
print(type(False))  
print(true)

The output of this will be:
<class 'bool'>
<class 'bool'>
NameError: name 'true' is not defined

Here, we can see that when we use True it returns a bool class but when we use True (in lowercase) it returns an error during output.

You may want to learn Python with an online Python tutor service.