Python

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(num)
print("Data Type of variable num is", type(num))

Output:
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(fnum)
print("Data Type of variable fnum is", type(fnum))

Output:
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(cnum)
print("Data Type of variable cnum is", type(cnum))

Output:
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
print(num)

# integer equivalent of Octal number 32
num2 = 0o32
print(num2)

# integer equivalent of Hexadecimal number FF
num3 = 0xFF
print(num3)

Output:
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
print(s)
print(type(s))

# displaying string s2 and its type
print(s2)
print(type(s2))

Output:
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
print(t1)

# 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
'''
print(t3[2])

Output:
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
print(lis1)

# 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])

Output:
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
print(dict[1])
# prints the value where key value is "lastname"
print(dict["lastname"])
# prints the value where key value is "age"
print(dict["age"])

Output:
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:
    print(a)

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

# adding new element
myset.add(99)
print(myset)

Output:
Python data type set

Related Articles

Leave a Reply

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

Back to top button