Python is an interpreted, high-level object-oriented programming language. It supports modules and packages, encouraging program modularity and code reuse. Moreover, Python’s simple and easy-to-learn syntax enhances readability and lowers the cost of program maintenance.
Python data types are a means to classify or categorize data items. Every value in Python has a data type telling what operations we can perform on the data. Since everything in the programming language is an object, Python data types are classes, and the corresponding variables are instances (objects) of the classes.
Python has various standard or built-in data types, and this article explores the top five of them.
- Numeric
- Sequence
- Set
- Boolean
- Dictionary
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
1. Numeric Data Type
Python’s numerical data type represents data having numeric values, such as integers, floating numbers, or complex numbers. These numeric values are defined using Python’s int, float, and complex classes.
- Integers: The int class represents integer values that could be positive or negative whole numbers but not decimals or fractions. Python has no maximum limit on an integer value – it can be as long as the system memory permits.
- Floating-point numbers: Floating-point numbers differ from integers in terms of decimal points. In other words, if an integer value is 1, a floating-point value would be 1.0, accurate up to 15 decimal places.
- Complex numbers: The complex class represents complex numbers specified by “x+yj,” where ‘x’ is the real part, and ‘yj’ is the imaginary part.
The type()function determines which class a value or variable belongs to. Likewise, the isinstance()function tells if an object belongs to a particular class.
Here’s an example to illustrate the numeric data types in Python:
a = 10
print(a, “is of type”, type(a))
b = 7.0
print(a, “is of type”, type(b))
c = 3+6j
print(c, “is complex number?”, isinstance(1+2j,complex))
Output:
10 is of type <class ‘int’>
7.0 is of type <class ‘float’>
(3+6j) is complex number? True
2. Sequence Data Type
In Python, a sequence refers to an ordered collection of different or similar data types. Python has the following sequence data types:
- Python List: In Python, a list is an ordered sequence of items that may or may not be the same type. Lists are flexible, mutable, and one of the most widely used data types in Python.
We can create lists in Python by enclosing the items within square brackets [] separated by commas.
Here’s an example to illustrate lists in Python:
mylist=[‘apples’,’oranges’,’bananas’,50,’grapes’,2]
print(mylist[1:4])
Output:
[‘oranges’, ‘bananas’, 50]
- Python Tuple: Similar to a list, a tuple is an ordered sequence of items. However, unlike lists, tuples are immutable. In other words, tuples cannot be modified once created. Also, items in tuples are defined within parentheses()separated by commas.
Here’s a simple example of a tuple in Python:
t = (2, 5, 4.5, ‘Hi’)
print(“Contents of tuple is:”, t)
Output:
Contents of tuple is:2, 5, 4.5, ‘Hi’
- Python String: A Python string is a sequence of unicode characters. We can represent strings using single quotes (‘’) or double quotes (“”). Multi-line strings are written in triple quotes (‘’’) or (“”””””).
Below is an example showing Python strings:
s = “This is a string”
print(s)
s = ”’This is a multiline
string”’
print(s)
Output:
This is a string
This is a multiline
string
Popular Courses & Articles on Software Engineering
3. Set Data Type
A set in Python is an unordered collection of unique items, declared inside braces {}with comma-separated values. Python sets keep only unique values and eliminate duplicates. Moreover, we can perform operations like intersection and union on two sets. The slicing operator []does not work on a set since a set comprises unordered items with no scope of indexing.
Here are some examples of Python sets:
Example #1
s = {5,2,3,1,4}
# printing set variable
print(“s = “, s)
# data type of variable s
print(type(s))
Output:
s = {1, 2, 3, 4, 5}
<class ‘set’>
Example #2
s = {1,2,2,3,3,3}
print(s)
Output:
{1, 2, 3}
4. Boolean Data Type
Boolean data types in Python have either of the two in-built values: True or False. In the boolean context, objects equal to True are truthy values, and those equal to False are falsy values. We can also evaluate non-boolean objects in the boolean context. Boolean is denoted by the class bool.
Below is a program snippet where we evaluate an expression in Python to get one of the two answers, True or False.
print(11 > 10)
print(11 == 10)
print(11 < 10)
Output:
True
False
False
Here’s another example of a Python program to check the boolean type:
print(type(True))
print(type(False))
print(false)
Output:
<class ‘bool’>
<class ‘bool’>
NameError: name ‘false’ is not defined
The above program throws an error message because only True and False with capital ‘T’ and ‘F’ are valid booleans.
5. Dictionary Data Type
A Python dictionary is an unordered collection of data values where values are in pairs known as key-value pairs. The dictionary data type is useful when we have high data volumes, and its most significant function is data retrieval. However, we can only retrieve a value if we know its corresponding key. Dictionaries are defined within curly braces{}, a colon separates each key-value pair (:), and each key is separated by a comma. The value and the key may be of different data types.
A Python dictionary looks like this:
>>> d = {1:’value’,’key’:2}
While we can use a key to retrieve a specific value, the other way round is not true. Look at the example below:
d = {3:’value’,’key’:4}
print(type(d))
print(“d[1] = “, d[1])
print(“d[‘key’] = “, d[‘key’])
# Generates error
print(“d[4] = “, d[4])
Output:
<class ‘dict’>
d[3] = value
d[‘key’] = 4
Traceback (most recent call last):
File “<string>”, line 9, in <module>
KeyError: 4
Conclusion
Python has several built-in data types, each meant for storing values of a specific type. This article discussed the Python type numeric, sequence, set, Boolean, and dictionary.
If you’re reading this article, you’re probably new to Python and have yet to learn all that this programming language offers. However, if you’re interested to learn Python in-depth and more about such tools and libraries, check upGrad’s Advanced Certificate Programme in DevOps
Apply today to get exclusive upGrad advantages, including 360-degree learning support, peer learning, and industry networking.
What are the 7 data types in Python?
The seven standard Python types are numbers, string, list, tuple, dictionary, boolean, and set. A data type describes the characteristics of a variable.
Which Python data types are mutable?
Mutable Python data types are those whose values can be changed. Lists, dictionaries, and sets are mutable data types in Python.
What is pandas astype in Python?
Pandas is a software library built on top of the Python programming language. The pandas astype()function casts an object to a specified data type.