Python Data Types
• List : A List is an ordered collection, mutable that stores multiple items in a single
variable.
• Tuple : A Tuple is an ordered collection and immutable used to store multiple items
in a single variable.
• Set : A Set is an unordered collection and mutable that stores only unique
elements.
• Dict (Dictionary) : A Dictionary is a mutable that stores data in key-value pairs.
List
• A List is an ordered collection, mutable that stores multiple items in a single variable.
Ordered collection, Allows duplicate values, Mutable (can be modified), Stores different data types.
Created using square brackets [].
Syntax
list_name = [item1, item2, item3]
Example
Example x = [1,2,1,5.5,"a"]
fruits = ["Apple", "Banana", "Mango"] print(x)
print(fruits)
Output
Output [1, 2, 1, 5.5, 'a']
['Apple', 'Banana', 'Mango']
List Methods
Methods Definition Syntax Example Output
append() Adds one element at the end of [Link](item) x=[1,2,3,4] [1, 2, 3, 4, 5]
the list. [Link](5)
print(x)
extend() Adds multiple elements to the end [Link](items) x=[1,2,3] [1, 2, 3, 4, 5]
of the list. [Link]([4,5])
print(x)
remove() Removes the specified value. [Link](value) x=[1,2,3,4] [1, 2, 4]
[Link](3)
print(x)
pop() Removes an element using its [Link](index) x=[1,2,3,4] [1, 3, 4]
index. [Link](1)
print(x)
clear() Removes all elements from the [Link]() x=[1,2,3,4] []
list. [Link]()
print(x)
Tuple
• A Tuple is an ordered collection and immutable used to store multiple items in a single variable.
Ordered collection. , Allows duplicate values. , Immutable (cannot be modified). , Faster than List.
Created using parentheses ().
Syntax
tuple_name = (item1, item2, item3)
Example
Example
colors = ("Red", "Blue", "Green", "Blue")
x=(1,2,3,4)
print(colors)
print(x)
Output
Output
('Red', 'Blue', 'Green', 'Blue')
(1, 2, 3, 4)
Tuple Methods
Methods Definition Syntax Example Output
index() Returns the index position of the [Link](value) x = (1, 2, 3) 1
specified element. print([Link](2))
count() Returns how many times an [Link](value) x = (1, 2, 3, 2, 2) 3
element appears in the tuple. print([Link](2))
Concatenation Combines two tuples into one. tuple1 + tuple2 x = (1,2) (1, 2, 3, 4)
(+) y = (3,4)
print(x + y)
Repetition (*) Repeats the tuple multiple times. tuple * n x = (1,2) (1, 2, 1, 2, 1, 2)
print(x * 3)
Membership Checks whether an element exists value in tuple x=(1,2,3,4) True
(in) in the tuple. print(3 in x) False
print(5 in x)
Slicing Returns a selected range of tuple[start:stop:skip] x=(1,2,3,4,'a','b') (2, 4)
elements from the tuple. print(x[1:4:2])
Set
• A Set is an unordered collection and mutable that stores only unique elements.
Unordered collection. , Does not allow duplicate values. , Mutable (can be modified). , Stores unique elements.
Created using curly braces {}.
Syntax
set_name = {item1, item2, item3}
Example
numbers = {10, 20, 30, 20, 40}
print(numbers)
Output
{10, 20, 30, 40}
Note: Duplicate value 20 is automatically removed.
Set Methods
Methods Definition Syntax Example Output
add() Adds one element to the set. [Link](item) x = {1, 2, 3} {1, 2, 3, 4}
[Link](4)
print(x)
update() Adds multiple elements to the set. [Link](items) x = {1, 2} {1, 2, 3, 4, 5}
[Link]([3, 4, 5])
print(x)
remove() Removes the specified element. [Link](item) x = {1, 2, 3} {1, 3}
[Link](2)
print(x)
union() Combines two sets. [Link](set2) x = {1, 2, 3} {1, 2, 3, 4, 5}
“intersection() y = {3, 4, 5}
difference()” print([Link](y))
issubset() Checks whether one set is a [Link](set2) x = {1, 2} True
“issuperset()” subset of another. y = {1, 2, 3, 4}
print([Link](y))
Dictionary
• A Dictionary is a mutable that stores data in key-value pairs.
Stores data as key-value pairs. , Ordered. , Keys must be unique. , Mutable (can be modified).
Created using curly braces {}.
Syntax
dictionary_name = {
"key1": value1,
"key2": value2
}
Example
student = { Output
"name": “abcd",
"age": 22, {'name': ‘abcd’, 'age': 22, 'course': 'Python'}
"course": "Python"
}
print(student)
Dictionary Methods
Methods Definition Syntax Example Output
get() Returns the value of the [Link](key) x={ 2
specified key. "a": 1, None
"b": 2
}
print([Link]("b"))
print([Link]("c"))
Add New Item Adds a new key-value pair. x={ {'a': 1, 'b': 2, 'c': 3}
"a": 1,
"b": 2
}
x["c"] = 3
print(x)
update() Updates one or more key- [Link](other_ x={ {'a': 1, 'b': 2, 'c': 3}
value pairs. dictionary) "a": 1
}
[Link]({
"b": 2,
"c": 3
})
print(x)
Dictionary Methods
Methods Definition Syntax Example Output
pop() Removes the specified key. [Link](key) x={ {'a': 1}
"a": 1,
"b": 2
}
[Link]("b")
print(x)
keys() Returns all keys. Print([Link]()) x={ dict_keys(['a', 'b', 'c'])
"a": 1,
"b": 2,
"c": 3
}
print([Link]())
values() Returns all values. Print([Link]()) x={ dict_values([1, 2, 3])
"a": 1,
"b": 2,
"c": 3
}
print([Link]())