Python Module #3
Python Module #3
s = "Python Programming"
index = s.find("Programming")
print(index)
# Output: 7
Return type: `int`
2. `rfind(substring [, start [, end]])`: Similar to `find()`, but searches for the last
occurrence of the substring within the string.
s = "Python Programming"
index = s.rfind("o")
print(index)
# Output: 9
Return type: `int`
s = "Python Programming"
index = s.index("Programming")
print(index)
# Output: 7
Return type: `int`
4. `rindex(substring [, start [, end]])`: Similar to `index()`, but searches for the last
occurrence of the substring within the string.
s = "Python Programming"
index = s.rindex("o")
print(index)
# Output: 9
Return type: `int`
s = "Python Programming"
count = s.count("P")
print(count)
# Output: 2
Return type: `int`
s = "Python Programming"
new_s = s.replace("Python", "Java")
print(new_s)
s = "Python Programming"
parts = s.split(" ")
print(parts)
Example:
s = "PythonProgramming123"
print(s.isalnum()) # Output: True
print(s.isalpha()) # Output: False
print(s.isdigit()) # Output: False
print(s.islower()) # Output: False
print(s.isupper()) # Output: False
print(s.isspace()) # Output: False
my_list = [1, 2, 3, 4, 1, 2, 1]
count = my_list.count(1)
print(count)
# Output: 3
Return type: `int`
2. `index(value [, start [, end]])`: Returns the index of the first occurrence of a value in a
list.
my_list = [1, 2, 3, 4, 1, 2, 1]
index = my_list.index(2)
print(index)
# Output: 1
Return type: `int`
my_list = [1, 2, 3, 4, 1, 2, 1]
my_list.remove(2)
print(my_list)
# Output: [1, 3, 4, 1, 2, 1]
Return type: `None`
4. `pop([index])`: Removes and returns the element at the specified index. If no index is
specified, removes and returns the last element.
my_list = [1, 2, 3, 4, 5]
element = my_list.pop(2)
print(element)
# Output: 3
print(my_list)
# Output: [1, 2, 4, 5]
Return type: `int`
5. `reverse()`: Reverses the elements of a list in place.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)
# Output: [5, 4, 3, 2, 1]
Return type: `None`
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5]
my_list.sort()
print(my_list)
# Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
Return type: `None`
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
# Output: [1, 2, 3, 4]
Return type: `None`
my_list = [1, 2, 3]
other_list = [4, 5, 6]
my_list.extend(other_list)
print(my_list)
# Output: [1, 2, 3, 4, 5, 6]
Return type: `None`
my_tuple = (1, 2, 3, 4, 1, 2, 1)
count = my_tuple.count(1)
print(count)
# Output: 3
Return type: `int`
2. `index(value [, start [, end]])`: Returns the index of the first occurrence of a value
in a tuple.
my_tuple = (1, 2, 3, 4, 1, 2, 1)
index = my_tuple.index(2)
print(index)
# Output: 1
Return type: `int`
my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print(length)
# Output: 5
Return type: `int`
my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5)
minimum = min(my_tuple)
print(minimum)
# Output: 1
Return type: Same type as elements in the tuple
5. `max()`: Returns the largest element in a tuple.
my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5)
maximum = max(my_tuple)
print(maximum)
# Output: 9
Return type: Same type as elements in the tuple
my_tuple = (1, 2, 3, 4, 5)
del my_tuple
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
# Output: {1, 2, 3, 4}
Return type: `None`
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set)
# Output: {1, 2, 4}
Return type: `None`
3. `discard(element)`: Removes a specified element from a set if it is present.
Does not raise an error if the element is not found.
my_set = {1, 2, 3, 4}
my_set.discard(5)
print(my_set)
# Output: {1, 2, 3, 4}
Return type: `None`
my_set = {1, 2, 3, 4}
element = my_set.pop()
print(element)
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set)
# Output: set()
Return type: `None`
my_set = {1, 2, 3, 4}
new_set = my_set.copy()
print(new_set)
# Output: {1, 2, 3, 4}
Return type: `set`
7. `union(*others)`: Returns a new set containing the union of the original set and
other sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)
# Output: {1, 2, 3, 4, 5}
Return type: `set`
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
# Output: {3}
Return type: `set`
# Output: {}
Return type: `None`
2. `copy()`: Returns a shallow copy of a dictionary.
# Output: 2
Return type: Same type as the values in the dictionary or the
specified default value.
7. `pop(key [, default])`: Removes and returns the value associated with a specified key.
If the key is not found, returns the default value or raises a KeyError if not specified.
# Output: 2
Return type: Same type as the values in the dictionary or the
specified default value.