To write a good Python program, it’s really important to learn about the different data types you can work with. Data types define what type of value a variable can hold like numbers, text, or lists. In the article “Python Data Types with Examples”, we will discuss about different Python data types and how to use them.
Table of contents:
Python Data Types
In Python, data types define what kind of value a variable can have such as numbers, text, or collection. Common data types include integers(int), floating point numbers(float), strings(str), lists(list), and dictionaries(dict). users are often inclined toward Python because of its ease of use and the number of versatile features it provides. One of those features is dynamic typing.
Data Type |
Class |
Value |
Numeric |
Int, float, complex |
Numeric value |
String |
str |
Sequence of characters |
Sequence |
List, tuple, range |
Collection of items |
Mapping |
dict |
Data in key-value pair form |
Set |
Set, frozenset |
Unordered, unique collection |
Boolean |
bool |
Boolean value “True” or “False” |
For example,
num = 10
# Here, 10 is an integer and is assigned to the num variable. So the data type of num is ‘int’.
num = false
# Here, false is a boolean value and is assigned to the num variable. So the data type of num is ‘Boolean’
num = ”Ten”
# Here “Ten” is a string value and is assigned to the num variable. So the data type of num is ‘String’.
num = [“apple”, “orange”,”banana”]
# Here num is a list of fruit items and assigned to the num variable. So the data type of num is ‘list’.
Numeric Data Types in Python
Numeric Data types in Python can hold numeric values like integers values, decimal values(floating numbers), complex numbers, etc.
- Integer: integers are represented by int class. It can contain positive and negative values. also, there is no any limit to the size of the integer.
- Floating point: floating point variables are represented by float class. It can contain decimal values.
- Complex Numbers: Complex numbers are represented by complex classes. It contains the (real numbers) + (imaginary part) j. Example: 5 +6j.
Example:
# integer variable.
a=5192436
print("a = ", a, " is ", type(a))
# float variable.
b=120.86
print("b = ", b, " is ", type(b))
# complex variable.
c=1+5j
print("c = ", c, " is ", type(c))
Output:
a = 5192436 is <class ‘int’>
b = 120.86 is <class ‘float’>
c = (1+5j) is <class ‘complex’>
Sequence Data Types in Python
In Python, Sequence data types represent collections of ordered items. They are iterable, support indexing and slicing, and provide various methods for manipulation. there are the following sequence data types in Python:
Unlock Python’s Power!
Enroll today and begin your journey to becoming a Python pro!
Python String Data Type
Python strings are a combination of letters or characters enclosed in single quotes or double quotes. In Python, there is no character data type, here character is also included in the string class. A string of length 1 can be known as a character string. We can access individual characters with the help of an index. It is represented by str class.
Example:
s = 'Intellipaat is an Edtech Organization'
print(s)
# check data type
print(type(s))
# access string with index
print(s[1])
# Prints characters starting from 3rd to 5th
print(s[2:5])
#Prints characters from last(Reverse indexing)
print(s[-1])
Output:
Intellipaat is an Edtech Organization
<class ‘str’>
n
tel
n
Python List Data Type
List data type in Python is a data type similar to an array which is an ordered list of the same data types. Instead of this, we can include values of any data type in the list. The list is a very flexible data type to use as we can grow and shrink the size of the list data types. It is represented by a list class in Python.
- Creating a list in Python
Lists in Python are created using a square bracket( [ ] ) with different types of values separated by commas.
# Empty list
a = []
# list with only float values
a = [2.4, 5.8, 6.9]
print(a)
# list with both string and integer
b = ["Python", "C++", "Java", 4, 5]
print(b)
Output:
[2.4, 5.8, 6.9]
[‘Python’, ‘C++’, ‘Java’, 4, 5]
We can access the list of items with the help of the index. In Python negative indices are used for reversal traversal, like: a[-1] represents the first element from the end, a[-2] represents the second element from the end.
Example:
list1 = [ 'Python', 'Java', 123, 45.6]
list2 = [ 'Intellipaat', False, 34.6, 78]
# Prints complete list
print (list1)
# Prints first element of the list
print (list1[0])
# Prints elements starting from 2nd till 3rd
print (list1[1:3])
# Prints elements starting from 3rd element
print (list2[2:])
# Prints list two times
print (list2 * 2)
# Prints concatenated lists
print (list1 + list2)
Output:
[‘Python’, ‘Java’, 123, 45.6]
Python
[‘Java’, 123]
[34.6, 78]
[‘Intellipaat’, False, 34.6, 78, ‘Intellipaat’, False, 34.6, 78]
[‘Python’, ‘Java’, 123, 45.6, ‘Intellipaat’, False, 34.6, 78]
Python Tuple Data Type
Tuple data types are also the same as list data types in Python, the only difference between them is the tuple is immutable, which means we can not change the tuple elements after created.
- Creating a Tuple in Python
In Python, tuples are created using parenthesis with different types of values separated by commas.
Example:
tuple1=()
tuple2 = ( 'abcd', 34 , 4.56, )
# Prints the tuple
print (tuple1)
print(tuple2)
Output:
()
(‘abcd’, 34, 4.56)
We can access tuple elements with the help of an index. With the help of the index and subscript operator, we can access the tuple elements easily.
Start Your Python Adventure!
Join now and start coding your future with Python!
Example:
tuple = ( 'abcd', 34 , 4.56, )
tuple2 = (45.5, 'intellipaat', 234)
# Prints the complete tuple
print (tuple)
# Prints first element of the tuple
print (tuple[0])
# Prints elements of the tuple starting from 2nd till 3rd
print (tuple[1:3])
# Prints elements of the tuple starting from 3rd element
print (tuple[2:])
# Prints the contents of the tuple twice
print (tuple2 * 2)
# Prints concatenated tuples
print (tuple + tuple2)
Output:
(‘abcd’, 34, 4.56)
abcd
(34, 4.56)
(4.56,)
(45.5, ‘intellipaat’, 234, 45.5, ‘intellipaat’, 234)
(‘abcd’, 34, 4.56, 45.5, ‘intellipaat’, 234)
Python Set Data Type
A set in Python is an unsorted collection of elements that contains unique elements, duplicate elements are not allowed in a Set Data Structure. The set is an iterable and mutable data structure.
Create a Set in Python
set is created in Python using the set() function with an iterable object or list of objects separated by commas. Set can also contain different types of data like: string, int, float, etc.
Example:
# initializing an empty set
s1 = set()
s1 = set("Intellipaat")
print("Set with the use of String: ", s1)
s2 = set([234, "For", 12.45])
print("Set with the use of List: ", s2)
Output:
Set with the use of String: {‘a’, ‘e’, ‘n’, ‘I’, ‘t’, ‘l’, ‘p’, ‘i’}
Set with the use of List: {234, ‘For’, 12.45}
Access Set Items
Set items cannot be accessed with the help of an index, we have to use a loop to iterate through the set items.
Example:
set1 = set(["Geeks", "For", "Geeks"])
print(set1)
# loop through set
for i in set1:
print(i, end=" ")
print("")
# check if item exist in set
print("Geeks" in set1)
Output:
{‘Geeks’, ‘For’}
Geeks For
True
Python Dictionary Data Type
In Python, Dictionary data types are used to store elements in the form of key-value pairs like maps. It is an unordered data collection of data values in the form of key-value pairs. In the key-value pair, there is a colon that separates the key and its values.
Create a Dictionary in Python
In Python, the dictionary can be built using the built-in function dict(). The values in a dictionary can be of any data type and also duplicate values are allowed. The keys of the dictionary are case-sensitive.
Example:
# initialize an empty dictionary
d = {}
d = {1: 'Python', 2: 'C++', 3: 'Java'}
print(d)
# creating dictionary using dict() constructor
d1 = dict({1: 'Python', 2: 'C++', 3: 'Java'})
print(d1)
Output:
{1: ‘Python’, 2: ‘C++’, 3: ‘Java’}
{1: ‘Python’, 2: ‘C++’, 3: ‘Java’}
Access Dictionary Values Using Keys
We can access the values of the dictionary with the help of their keys. Also, we can use get() function to access the dictionary values.
Example:
d={1:'Python', 2: 'C++', 3: 'Java'}
print(d[2])
print(d.get(3));
Output:
C++
Java
Python Range Data Type
Range() in Python generates a series of numbers within a specified range. with the help of a loop, we can use the range function efficiently.
Example:
for i in range(1, 10):
print(i)
Output:
1
2
3
4
5
6
7
8
9
Boolean Data Type in Python
Python also provides one built-in data type Boolean that gives us two values True and False. It is represented by class bool. For any expression, that is true Python gives True as a response. And False if the expression gives us false as a response.
Example:
a = True
# display the value of a
print(a)
# display the data type of a
print(type(a))
print(5>8)
Output:
True
<class ‘bool’>
False
How to check Data Type in Python?
Now that you know about the built-in data types and the core data types in Python, let’s see how to check data types in Python.
You can get the data type of any object by using the type() function:
Python Data Type Exercise Questions
Here are some of the practice questions on Data Types in Python that you should prefer for revising the concept learned in this article.
1. Implement a Basic dictionary in Python:
a = {'Python':30,'C++':45,'Java':65}
print(a);
print(a.get('C++'))
print(a['Python'])
print(a['Java'])
Output:
{‘Python’: 30, ‘C++’: 45, ‘Java’: 65}
45
30
65
2. Implement basic list operations
languages= [“Python", "Java", "Javascript"]
print(languages)
languages.append("CPP")
print(languages)
languages.remove("Java")
print(languages)
print(len(languages))
print(type(languages))
Output:
[‘Python’, ‘Java’, ‘Javascript’]
[‘Python’, ‘Java’, ‘Javascript’, ‘CPP’]
[‘Python’, ‘Javascript’, ‘CPP’]
3
<class ‘list’>
give the correct output of the following Questions
print(5 > 4)
print(6==-6)
print(45 in(34,45,67))
x = "intellipaat"
print(x[-1])
print(x[2:5])
Output:
True
False
True
t
tel
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
In Conclusion, Python has different types of data types including int, float, string, list, tuples, etc. Every data structure has a different use of its own. One of the most crucial parts of learning any programming language is to understand how data is stored and manipulated in that language. Users are often inclined toward Python because of its ease of use and the number of versatile features it provides.
One of those features is dynamic typing. By using the right data structure, we can optimize our code for better performance and readability. You can learn more and explore the new world of programming with Intellipaat’s Python Programming Course. Enroll Today!
Our Python Courses Duration and Fees
Cohort starts on 11th Jan 2025
₹20,007
Cohort starts on 11th Jan 2025
₹20,007