Data Types in Python – Detailed Instructions From [A-Z]

Whether it is an integer variable, string variable, tuple, dictionary, list, etc., the data type determines the type of the variable. You will discover the Python data types and how to use them in this guide.

Data types in Python

The two sorts of data types in Python are changeable data types and immutable data types.

Python’s immutable data types
1. Numeric
2. String
3. Tuple

Python’s mutable data types
1. List
2. Dictionary
3. Set

1. Python’s Numeric Data Type

In Python 3, there is no upper limit on integer numbers, therefore the value can be as large as the system memory will support.

# Integer number
num = 100
print("Data Type of variable num is", type(num))

Python data type integers

Since the integer has no upper limit, there is no need for a data type that enables a bigger upper limit than integers, and the long data type is deprecated in Python 3.

Python does not require the data type to be specified when dealing with float numbers because they are values with decimal points. It is deduced automatically from the value we are giving a variable. Here, for instance, the data type fnum is a float.

# float number
fnum = 34.45
print("Data Type of variable fnum is", type(fnum))

Python data type float

Complex numbers are those that have both real and fictitious components. Python is able to match up these complex numbers with their values, unlike other computer languages like Java. When the type of the variable cnum is printed in the example below, it appears as a complex number.

# complex number
cnum = 3 + 4j
print("Data Type of variable cnum is", type(cnum))

Python data type complex number

Hexadecimal, Octal, and Binary numbers
Using prefixes, Python allows us to display the decimal equivalent of binary, octal, and hexadecimal values.
Binary Numbers 0b (zero plus “b”) and 0B (zero plus “B”)
Octal Numbers 0o(zero + ‘o’) and 0O(zero + ‘O’)
Hexadecimal numbers 0x(zero + ‘x’) and 0X(zero + ‘X’)

# integer equivalent of binary number 101
num = 0b101

# integer equivalent of Octal number 32
num2 = 0o32

# integer equivalent of Hexadecimal number FF
num3 = 0xFF

Python data type binary octal hexadecimal

2. String data type in Python

In Python, a string is a collection of characters. Python’s “str” data type designates the String data type.

Python uses either single or double quotes to enclose strings. In the example below, we have two strings, one with double quotes and the other with single quotes, s1 and s2. Python Strings is a good resource to read more about strings.

# Python program to print strings and type

s = "This is a String"
s2 = 'This is also a String'

# displaying string s and its type

# displaying string s2 and its type

Python string data type

3. Tuple Data Type in Python

Python’s immutable data type, the triple, cannot be altered. It is a list of elements that are in alphabetical order, enclosed in round brackets, and separated by commas. Refer to this tutorial on Python tuples to learn more about tuples.

# tuple of integers
t1 = (1, 2, 3, 4, 5)
# prints entire tuple

# tuple of strings
t2 = ("hi", "hello", "bye")
# loop through tuple elements
for s in t2:
    print (s)

# tuple of mixed type elements
t3 = (2, "Lucy", 45, "Steve")
Print a specific element
indexes start with zero

Python tuple data type

4. List Python Data Type

While list and tuple are both ordered collections of elements, list is changeable, but tuple is an immutable data type, meaning it can be modified.

A list is denoted by square brackets, and commas are used to divide its items. Consult this article for more information about lists: Lists in Python

# list of integers
lis1 = [1, 2, 3, 4, 5]
# prints entire list

# list of strings
lis2 = ["Apple", "Orange", "Banana"]
# loop through list elements
for x in lis2:
    print (x)

# List of mixed type elements
lis3 = [20, "Chaitanya", 15, "BeginnersBook"]
Print a specific element in list
indexes start with zero
print("Element at index 3 is:",lis3[3])

Python data type list

5. Dictionary Data Type in Python

The key and value pairs in a dictionary are arranged in pairs. While duplicate keys are prohibited in dictionaries, duplicate values are permitted. It is a group of elements that is arranged, indexed, and changeable. Python dictionary is a good resource for more information.

As you can see in the example below, we have two keys that are strings and one key that is an integer, proving that keys in dictionaries don’t always have to be of the same data type.

# Dictionary example

dict = {1:"Chaitanya","lastname":"Singh", "age":31}

# prints the value where key value is 1
# prints the value where key value is "lastname"
# prints the value where key value is "age"

Python data type dictionary

6. Set data type in Python

An unindexed, unordered collection of items is referred to as a set. As a result, when we print a set’s elements, they will appear in a random sequence, and since the set is unindexed, we cannot retrieve its elements using indexes.

Set components are encased in curly braces and are separated by commas. Let’s use an illustration to better comprehend Python sets.

# Set Example
myset = {"hi", 2, "bye", "Hello World"}

# loop through set
for a in myset:

# checking whether 2 exists in myset
print(2 in myset)

# adding new element

Python data type set

Related Articles

Leave a Reply

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

Back to top button