5.data Types (Tuple Vs List Vs Array) in Python
5.data Types (Tuple Vs List Vs Array) in Python
Data Science
Neovarsity
Topics
Explore
New
Skill Test
CoursesFree Masterclass
Search for Articles, Topics
Experience Scaler
Overview
In Python, data types are classifications that specify the kind of values a variable can hold. There are several built-in data types in Python, broadly
categorized into Numeric, Sequence Types, Mappings, Sets, and Boolean.
In Python, every value is associated with a data type because Python is an object-oriented language where everything is treated as an object.
Before proceeding further it's important to know that data types are also classified on the basis of their mutability. Mutable data types can be
modified after creation, whereas Immutable data types can't be modified after creation.
Integers in Python
Integers, as you know in mathematics are numbers without any fractional part. They can be 0, positive or negative. There is no limit to how long an
integer can be in Python. They are represented by int class.
Syntax:
Integer numbers are of int type. It is just written as a number. Here's an example of Integer data type:
Output:
number = 5
Floating point numbers are of float type. It is written as a number with a decimal point. Here's an example of Float data type:
Output:
number = 5.55
Syntax:
Complex numbers are of complex type. They are written in the form a+bj in which a is the real value and b is the imaginary value, where j represents
the imaginary part. Here's an example of a Complex data type:
Output
number = (5+5j)
Note: In Python, the type() function is used to determine the data type of a value or a variable. To know more about type() function Click here.
x = 10
y = 10.0
z = 10 + 10j
Output:
In the above example, we are using the type() function to show the type of data. It can be seen that "10" is an int type, "10.0" is a float type, and
"10+10j" is a complex type.
Strings in Python
A string can be defined as a sequence of characters enclosed in single, double, or triple quotation marks. While in most cases single and double
quotation marks are interchangeable. Triple quotation marks are used for multi-line strings. It is an immutable data type, i.e. its values cannot be
updated. String is represented by string class.
Syntax:
Strings are of string type, and are represented by enclosing in quotation marks.
print()
print()
Output:
x = Scaler
The data type of x: <class 'str'>
y = Scaler
The data type of y: <class 'str'>
In the above example, we are demonstrating Python string, using single, double, and triple quotation marks. While single and double quotation marks
are used for normal strings, triple quotation marks are used for multi-line strings.
Any character of a string can be accessed using indexing. Python also allows us to use negative indexing to access characters even from the back of a
string.
Note: Indexing of a sequence starts from 0.
my_string = "ScalerTopic"
print("String:", my_string)
Output:
String: ScalerTopic
First character: S
Last character: c
In the above example, we are accessing elements of the string using indexing. We are printing the first character of the string using the 0th index and
the last character of the string using the -1th index (as Python supports negative indexing).
Lists in Python
Lists are an ordered sequence of one or more types of values. They are just like arrays in other languages. They are mutable, i.e. their items can be
modified.
Syntax:
Lists are of the type list. They are created by enclosing items (separated by commas) inside square brackets [].
print(my_list)
print("Its data type is", type(my_list))
Output:
In the above example, we are creating a list by enclosing the elements inside square brackets[] and then printing it. In the second line, we are printing
its type using the type() function.
Elements of a list can be accessed by referring to their index numbers, negative indexes to access the list items from its back.
Output:
In the above example, we are accessing the elements of the list using indexing. At first, we are accessing the first element of the list by referring to its
index "0" (Index number = Position of the element - 1) then we are referring to the fourth element by its index "3" and the last element of the list is
accessed using negative indexing.
Tuples in Python
Like lists, Tuples are also an ordered sequence of one or more types of values except that they are immutable, i.e their values can't be modified. They
are represented by the tuple class.
Syntax:
Tuples are of tuple type. They are created by a sequence of values separated by a comma with or without parentheses "()" for grouping of the
sequence.
print(my_tuple)
print("Its data type is", type(my_tuple))
Output:
In the above example, we are creating a tuple by enclosing the elements inside parentheses and then printing it. In the second line, we are printing its
type using the type() function.
Elements of a tuple can be accessed by referring to their index numbers, negative indexes to access the tuple items from its back.
Output:
In the above example, we are accessing the elements of a tuple using indexing. At first, we are printing the first element using the 0th index, then we
are printing its third element using the 2nd index, at last, we are printing its last element using negative indexing.
3. Dictionaries in Python
In Python, Dictionaries are an unordered collection of key-value pairs (key: value). This helps in retrieving the data and makes the retrieval highly
optimized, especially in cases of high volume data. Keys can't be repeated in a dictionary while values can be repeated. Dictionaries are mutable, i.e.
their values can be modified.
Syntax:
Dictionaries are of the type dict. The elements of a dictionary are enclosed in curly brackets {}, where each element is separated by a comma , and key-
value pair by a colon :.
print(my_dict)
print("Its data type:", type(my_dict))
Output:
In the above example, we are creating a dictionary by enclosing the key-value pair in curly brackets {} then we are printing it. In the second line, we are
printing its type using the type() function.
print(my_dict["Name"])
print(my_dict["Age"])
print(my_dict["Movie"])
Output:
Tom
50
Mission Impossible
In the above example, we are accessing the values of a dictionary by referring to their keys, in the first line we are referring to Tom using the key Name,
then we are referring to 50 using the key Age, at last, we are referring to Mission Impossible using the key Movie.
4.Booleans in Python
Boolean is a data type that has one of two possible values, True or False. They are mostly used in creating the control flow of a program using
conditional statements.
Syntax:
The True value in the boolean context is called "truthy" and False value is called "falsy".
a = True
b = False
print()
Output:
a = True
b = False
In the above example, we are printing the boolean variables a (True) and b (False), then we are printing their types using the type() function.
5. Sets in Python
Sets in Python are an unordered collection of elements. They contain only unique elements. They are mutable, i.e. their values can be modified.
Syntax:
Sets are of the type set. They are created by elements separated by commas enclosed in curly brackets {}.
print(my_set)
print("Its data type:", type(my_set))
Output:
In the above example, we are creating a set by enclosing the elements inside curly brackets "{}" then we are printing it. In the second line, we are
printing its type using the type() function. While creating the set, we have put two duplicate values 8 but when we printed it, 8 occurred only once,
that's because sets only have unique elements.
Conclusion
Data types are classes in Python, whereas variables are objects of these classes.
Each and every value or variable has a data type in Python.
Numeric data types in python represent data with numeric values.
Sequence data types in python are an ordered collection of items.
Dictionary (also called maps, in other languages) helps in the optimized retrieval of values.
Boolean has two types of values, True or False, True for truthy values, False for falsy values.
Sets are an unordered collection of unique elements (like set theory in mathematics).
Challenge Time!
Skip to content
dynamic-sizedList: Lists are just like dynamic-sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists
need not be homogeneous always which makes it the most powerful tool in Python. The main characteristics of lists are –
The list is a datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
List are mutable .i.e it can be converted into another data type and can store any data element in it.
List can store any type of element.
Example:
Python3
# List
# Creating a List
List = []
print(List)
print(List)
# using index
print(List[0])
print(List[2])
Output:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
Tuple: Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are
indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define
a tuple by closing the sequence of values in parentheses. The main characteristics of tuples are –
Tuple is an immutable sequence in python.
It cannot be changed or replaced since it is immutable.
It is defined under parenthesis().
Tuples can store any type of element.
Example:
Python3
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
list1 = [1, 2, 4, 5, 6]
print(tuple(list1))
#Creating a Tuple
Tuple1 = tuple('Geeks')
print(Tuple1)
Output:
Initial empty Tuple:
()
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with the use of function:
('G', 'e', 'e', 'k', 's')
Set: In Python, Set is an unordered collection of data type that is iterable, mutable, and has no duplicate elements. The major advantage
of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the
set. The main characteristics of set are –
Sets are an unordered collection of elements or unintended collection of items In python.
Here the order in which the elements are added into the set is not fixed, it can change frequently.
It is defined under curly braces{}
Sets are mutable, however, only immutable objects can be stored in it.
Example:
Python3
# Set in Python
# Creating a Set
set1 = set()
print(set1)
String = 'GeeksForGeeks'
set1 = set(String)
print(set1)
print(set1)
Output:
Initial blank Set:
set()
Set with the use of an Object:
{'G', 's', 'e', 'o', 'r', 'F', 'k'}
Set with the use of List:
{'Geeks', 'For'}
Items in list can be Items in set cannot be changed or Items in tuple cannot be
List Set Tuple