Python Data Types

Data Types : 

Data type specifies which type of value a variable has and what mathematical, relational, or logical operations can be performed on it without causing an error.

In python, everything is an object, So data types are classes and the variables are instances of these classes.

Python has the Following built-in data types : 

  • Numeric Type
  • Sequence Type
  • Set Type
  • Dictionary Type

Numeric Type: The numeric Data type represents the data that has a Numeric Value. A numeric value can be integer, float, and complex. 

Integer: It is represented by int class. The positive, Negative Whole Numbers, comes under int class. Integers can be any of any length.

Float: It is represented by float class. It is a real number with floating-point representation. The floating-point number is accurate up to 15 decimal places. 

Complex: Complex Numbers are represented by a complex class. The numbers in the form of X + Yj where X is the real part and Y is the Imaginary Part is called Complex Numbers. 

Note: In Python type() function can be used to determine the type of data type.

Code :

Python Code

x = 3               #integer 
y = 3.2233          #float
z = 3 + 7.899j      #complex
a = 3.222342522525664667 


<class ‘int’>
<class ‘int’>
<class ‘complex’>

Note: The floating value ‘a’ got truncated as floating-point accuracy is up to 15 decimal places only.

Sequence Type : 

A sequence of collections of similar data types or different data types.  Sequences Types in Python are

  • List 
  • Tuple 
  • String

List : 

It is an ordered collection of data. It is similar to an array in other programming languages. Unlike other programming languages array, elements in a list can be of any data type. It is dynamic, not static, size need not be specified while declaring. It is the most widely used data type.

Declaration of List : 

A list can be created by placing the comma-separated values in square brackets [ ].

Code : 

Python Code

# Empty List
x = []
print("\n Empty List")

# List with single data type
y = ["takeuforward", "striver", "tuf"]
print("\n List with Same data type ")

# List with multiple data types
z = ["takeuforward", 8.90, 7, 9+899j, ["tuf", "striver"]]
print("\n List with multiple data types")


Empty List

List with Same data type
[‘takeuforward’, ‘striver’, ‘tuf’]

List with multiple data types
[‘takeuforward’, 8.9, 7, (9+899j), [‘tuf’, ‘striver’]]

Accessing the List Elements: List elements can be accessed by the method of Indexing. Indexing start’s with 0. Index operator [ ] is used to access the elements. Element at 0th index is the first element in the list, and so on. In python, reverse indexing is also possible. -1 corresponds to the last element, -2 to the second last, and so on.

Note: Accessing any sequence type is similar to that of a list.


Python Code

# list
x = ["takeuforward", "striver", "tuf", "python"]

print("\nAccessing the elements using the index ")
# prints the first element in x i.e "takeuforward"

# prints the third element in x i.e "tuf"

print("\nAccessing the elements using the Negative Indexing")
# prints the last element is x i.e "python"

# prints the third element from last i.e "striver"


Accessing the elements using the index

Accessing the elements using the Negative Indexing


Tuple : 

Tuples are like lists, the only difference between list and tuple is, that lists are mutable whereas the tuples are immutable. Mutable means the data can be changed, immutable means the data cannot be changed once it is created. So for the list, new data can be added, and existing data can be changed whereas for tuples data cannot be added or changed. 

Declaration of Tuple: 

A tuple can be created by placing the comma-separated values in parenthesis ().


Python Code

# Empty tuple
x = ()
print("Empty Tuple")

# tuple of similar data type
y = ("striver", "python")
print("\nTuple of Same data type ")

# tuple of multiple data types
z = (4.5, "tuf", 4+5j)
print("\nTuple of different data types")

print("\n Accessing through positive Indexing")
# prints first element in tuple

print("\n Accessing through Negative Indexing")
# prints last element in tuple


Empty Tuple 

Tuple of Same data type
(‘striver’, ‘python’)

Tuple of different data types
(4.5, ‘tuf’, (4+5j))

Accessing through positive Indexing

 Accessing through Negative Indexing

String : 

A string is a collection of characters. We can use single, double, or triple quotes to represent strings. Using triple quotes multi-line strings can be denoted. Like tuple strings are immutable.

Code : 

Python Code

# string with single quotes
y = 'takeuforward'
print("\nString with Single quotes")

# string with double quotes
z = "striver"
print("\nString with double quotes")

# string with triple quotes
p = """This is Multiline 
string in python"""
print("\nMultiline string using triple quotes")

# prints last character of z
print("\nz[-1] : ", z[-1])

# print first character of z
print("\nz[0] : ", z[0])

# raises a Error because strings are immutable
z[2] = 'c' 


String with Single quotes

String with double quotes

Multiline string using triple quotes
This is Multiline
string in python

z[-1] :  r
z[0] :  s

TypeError: ‘str’ object does not support item assignment

Set Type : 

Set is an unordered collection of unique items. Duplicates are not allowed in the set. The order in which the elements inserted are is not preserved, they are stored at random places. Sets can contain multiple data types and are mutable.

Declaration of Set : 

A Set can be created by placing the comma-separated values in braces { }.

Code :

Python Code

# Empty set
x = {}
print("\n Empty Set")

# Set with similar data type values
y = {1, 2, 3, 4}
print("\nSet with similar data type")

# Set with multiple data types
z = {"striver", 1, (1, 3, 4)}
print("\nSet with multiple data types")

#Here we assign the duplicates into set,but set takes only unique items.
#see the output,only unique elements are found in set.
p = {1, 1, 2, 2, "striver", "tuf", "tuf", 3, 3}


Empty Set

Set with similar data type
{1, 2, 3, 4}

Set with multiple data types
{1, ‘striver’, 1, 3, 4)}

{‘tuf’, 1, 2, 3, ‘striver’}

Note: Output can be different because elements are stored randomly in the set. For each run, elements occupy random places.

Accessing the Set elements : 

As the sets are unordered the items have no definite index, so the set items cannot be accessed using the index operator. We can loop through the set using, for loop to know what the items are presented in the set, or we can use in keyword to find particular value is part of the set or not.

Code : 


x = {1, 2, "python", "tuf"}

# Accessing the set elements using for loop
print("\nThe elements in the Set are : ")
for item in x:

# Finding whether key is present in set or not.
# checking is "python" present in x
print("python" in x)

# checking is "striver" present in x
print("striver" in x)


The elements in the Set are :



Dictionary Type : 

Dictionary is an unordered collection of key-value pairs. It is similar to maps in other programming languages. Each key-value pair are separated by a colon and each key is separated by a ‘comma’. The data types of key and value can be different. The keys can also have different data types.

Values can be of any data type and can be duplicated, whereas keys can’t be duplicated, which means for one key, only one key-value pair is possible. Moreover, keys are immutable. If duplicate keys are provided, then values assigned last will be taken into consideration.

Note: Dictionary keys are case sensitive, which means the same name with a different case will be treated distinctly.

Declaration of Dictionary :

In python, a dictionary can be created by placing comma-separated key-value pairs in curly braces { }.


Python Code

# Empty Dictonary
dict = {}
print("Empty Dictonary")

# Dictonary with similar data types of key
dict1 = {1: "striver",
         2: "tuf",
         3: [1, 2, 3]}
print("\nDictonary with the Integer keys")

# Dictonary with Mixed data types of keys
print("\nDictonary with Mixed Data Types")
dict2 = {1: "tuf",
         "striver": "takeuforward",
         "kohli": "India",

# Dictonary with Case Sensitive keys
print("\nDictonary with Case Sensitive keys")
dict3 = {"IPL": ["RCB", "MI", "CSK"],
         "ipl": {"RR", "SRH", "LSG"}}

# Dictonary with duplicate keys.
#last value i.e Raja Vikramaditya is taken as value to key striver.
print("\n Dictonary with Duplicate keys")
dict4 = {"striver": "tuf",
         "striver": "Raja Vikramaditya"}


Empty Dictonary

Dictonary with the Integer keys
{1: ‘striver’, 2: ‘tuf’, 3: [1, 2, 3]}

Dictonary with Mixed Data Types
{1: ‘tuf’, ‘striver’: ‘takeuforward’, ‘kohli’: ‘India’}

Dictonary with Case Sensitive keys
{‘IPL’: [‘RCB’, ‘MI’, ‘CSK’], ‘ipl’: {‘SRH’, ‘RR’, ‘LSG’}}

Dictonary with Duplicate keys
{‘striver’: ‘Raja Vikramaditya’}

Accessing the Elements of Dictonary : 

Items can be accessed by referring to their key name. Key is used in the square bracket to get its corresponding value.


Python Code

dict1 = {1: "striver",
         2: "tuf",
         3: [1, 2, 3]}

# accessing a element using key
print("\nAccessing element using key")
# prints value corresponding to key 1
# prints value corresponding to key 2


Accessing element using key

Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article