Python Data Types
Python Data Types
These 3 are defined as a class in Python. In order to find to which class the variable belongs to
you can use type () function.
Example:
a = 5
print(a, "is of type", type(a))
Output: 5 is of type <class ‘int’>
b = 2.5
print(b, "is of type", type(b))
Output: 2.5 is of type <class ‘float’>
c = 6+2j
print(c, "is a type", type(c))
Output: (6+2j) is a type <class ‘complex’>
#2) String
A string is an ordered sequence of characters.
We can use single quotes or double quotes to represent strings. Multi-line strings can be
represented using triple quotes, ”’ or “””.
Strings are immutable which means once we declare a string we can’t update the already declared
string.
Example:
Single = 'Welcome'
or
Multi = "Welcome"
Multiline: ”Python is an interpreted high-level programming language for general-purpose
programming. Created by Guido van Rossum and first released in 1991”
or
We can perform several operations in strings like Concatenation, Repetition, and Slicing.
Repetition:
It means repeating a sequence of instructions a certain number of times.
Example:
Print(String1*4)
Output: WelcomeWelcomeWelcomeWelcome
print(String1[-3:])
Output: ome
As Strings are immutable in Python, if we try to update the string, then it will generate an error.
Example:
String[1]= "D"
Output: TypeError: ‘str’ object does not support item assignment
#3) List
A list can contain a series of values.
List variables are declared by using brackets [ ]. A list is mutable, which means we can modify
the list.
Example:
List = [2,4,5.5,"Hi"]
print("List[2] = ", List[2])
Output: List[2] = 5.5
Tuples are immutable, which means tuples once created cannot be modified. Tuples are defined
using parentheses ().
Example:
Tuple = (50,15,25.6,"Python")
print("Tuple[1] = ", Tuple[1])
Output: Tuple[1] = 15
As Tuples are immutable in Python, if we try to update the tuple, then it will generate an error.
Example:
Tuple[2]= "D"
Output: TypeError: ‘tuple’ object does not support item assignment
#5) Set
A set is an unordered collection of items. Set is defined by values separated by a comma inside
braces { }.
Example:
Set = {5,1,2.6,"python"}
print(Set)
Output: {‘python’, 1, 5, 2.6}
In the set, we can perform operations like union and intersection on two sets.
Example:
A = {'a', 'c', 'd'}
B = {'c', 'd', 2 }
print('A U B =', A| B)
Output: A U B = {‘c’, ‘a’, 2, ‘d’}
We can perform Intersection operation by Using & Operator.
A = {100, 7, 8}
B = {200, 4, 7}
print(A & B)
Output: {7}
As the set is an unordered collection, indexing has no meaning. Hence the slicing operator [] does
not work.
Set[1] = 49.3
Output: TypeError: ‘set’ object does not support item assignment
#6) Dictionary
Dictionaries are the most flexible built-in data type in python.
Dictionaries items are stored and fetched by using the key. Dictionaries are used to store a huge
amount of data. To retrieve the value we must know the key. In Python, dictionaries are defined
within braces {}.
We use the key to retrieve the respective value. But not the other way around.
Syntax:
Key:value
Example:
Dict = {1:'Hi',2:7.5, 3:'Class'}
print(Dict)
Output: {1: ‘Hi’, 2: 7.5, 3: ‘Class’}
We can retrieve the value by using the following method:
Example:
print(Dict[2])
Output: 7.5
If we try to retrieve the value by using the value instead of the key, then it will generate an error.
Example:
print("Dict[7.5] = ", Dict[7.5])
Output:
Traceback (most recent call last):
print(“Dict[7.5] = “, Dict[7.5])
KeyError: 7.5
We can update the dictionary by using the following methods as well:
Example:
Dict[3] = 'python'
print(Dict)
Output:
{1: ‘Hi’, 2: 7.5, 3: ‘python’}