Data Types
Data Types
Python string split() function is used to split a string into the list of strings based on a
split()
delimiter.
Python String count() function returns the number of occurrences of a substring in the
count()
given string.
Python string replace() function is used to create a new string by replacing some parts
replace()
of another string.
Python String find() method is used to find the index of a substring in a string.
find()
3. Tuple
• Tuples are ordered collections of heterogeneous data that are unchangeable and
Contains Duplicates.
• Using parenthesis (): A tuple is created by enclosing comma-separated items inside
rounded brackets.
tuple = (10, 20, 25.75)
print(tuple)
• We can find the length of the tuple using the len() function. This will return the number
of items in the tuple.
print(len(tuple))
• We can access an item of a tuple by using its index number inside the index
operator [] and this process is called “Indexing”.
Adding and changing items in a Tuple
• A list is a mutable type, which means we can add or modify values in it, but tuples are
immutable, so they cannot be changed.
• Also, because a tuple is immutable there are no built-in methods to add items to the
tuple.
• We can convert the tuple to a list, add items, and then convert it back to a tuple. As
tuples are ordered collection-like lists the items always get added in the end.
t2.count(60) Returns the number of times a particular item (60) appears in a tuple. Answer is 2
remove(item) To remove the first occurrence of the item from the list.
pop(index) Removes and returns the item at the given index from the list.
clear() To remove all items from the list. The output will be an empty list.
There are some built-in methods that you can use on lists.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
5. Dictionary
• Dictionary items are unordered, changeable, and do not allow duplicates key.
• Dictionary items are presented in the pair of key: value and can be referred to by
using the key name.
Creating a dictionary
The dictionaries are created by enclosing the comma-separated Key: Value pairs inside the {}
curly brackets. The colon ‘:‘ is used to separate the key and value in a pair.
student1 = {"name": “abc", "class": “BCADS", “Roll no":2022}
• A dictionary value can be of any type, and duplicates are allowed in that.
• Keys in the dictionary must be unique and of immutable types like string, numbers, or
tuples.
Accessing elements of a dictionary
There are two different ways to access the elements of a dictionary.
• Retrieve value using the key name inside the [] square brackets
print(student['name'])
• Retrieve value by passing key name as a parameter to the get() method of a dictionary.
print(student.get(‘name’))
Return and removes the last inserted item from the dictionary. If the dictionary is empty,
popitem() it raises KeyError.
del key The del keyword will delete the item with the key that is passed
clear() Removes all items from the dictionary. Empty the dictionary
Sort dictionary
dict1 = {'c': 45, 'b': 95, 'a': 35}
print(sorted(dict1.items()))
print(sorted(dict1))
print(sorted(dict1.values()))
d1 = {'a': 10, 'b': 20, 'c': 30} and d2 = {'d': 40, 'e': 50, 'f': 60}
Operations Description
dict({'a': 10, 'b': 20}) Create a dictionary using a dict() constructor.
d2 = {} Create an empty dictionary.
d1.get('a') Retrieve value using the key name a.
d1.keys() Returns a list of keys present in the dictionary.
d1.values() Returns a list with all the values in the dictionary.
d1.items() Returns a list of all the items in the dictionary with each key-value pair inside a tuple.
clear() To remove all items from the Set. The output will be an empty set
remove() vs discard()
The remove() method throws a keyerror if the item you want to delete is not present in a set.
The discard() method will not throw any error if the item you want to delete is not present in a set.
Type conversion
1. int(a base): This function converts any data type to an integer. ‘Base’ specifies the base in
which the string is if the data type is a string.
2. float(): This function is used to convert any data type to a floating-point number.
3. tuple() : This function is used to convert to a tuple.
4. set() : This function returns the type after converting to set.
5. list() : This function is used to convert any data type to a list type.
6. dict() : This function is used to convert a tuple of order (key, value) into a dictionary.
7. str() : Used to convert an integer into a string.