Python_Methods_Descriptions
Python_Methods_Descriptions
2. **`pop`**
**Description**: Removes and returns the last item from a list.
**Example**:
my_list = [1, 2, 3]
last_item = my_list.pop()
# Result: last_item = 3, my_list = [1, 2]
3. **`sort`**
**Description**: Sorts a list in ascending order.
**Example**:
my_list = [3, 1, 2]
my_list.sort()
# Result: my_list = [1, 2, 3]
4. **`reverse`**
**Description**: Reverses the elements of a list in place.
**Example**:
my_list = [1, 2, 3]
my_list.reverse()
# Result: my_list = [3, 2, 1]
6. **`lower`**
**Description**: Converts a string to lowercase.
**Example**:
my_string = "HELLO"
lowercase_string = my_string.lower()
# Result: lowercase_string = "hello"
7. **`strip`**
**Description**: Removes any leading and trailing whitespace from
a string.
**Example**:
my_string = " hello "
stripped_string = my_string.strip()
# Result: stripped_string = "hello"
8. **`split`**
**Description**: Splits a string into a list of substrings based on a
delimiter.
**Example**:
my_string = "a,b,c"
split_list = my_string.split(",")
# Result: split_list = ["a", "b", "c"]
9. **`join`**
**Description**: Joins elements of a list into a single string, using a
specified delimiter.
**Example**:
my_list = ["a", "b", "c"]
joined_string = ",".join(my_list)
# Result: joined_string = "a,b,c"
11. **`values`**
**Description**: Returns a view object of dictionary values.
**Example**:
my_dict = {"a": 1, "b": 2}
values = my_dict.values()
# Result: values = dict_values([1, 2])
12. **`items`**
**Description**: Returns a view object of dictionary key-value pairs.
**Example**:
my_dict = {"a": 1, "b": 2}
items = my_dict.items()
# Result: items = dict_items([("a", 1), ("b", 2)])
13. **`get`**
**Description**: Returns the value for a key in a dictionary, or a
default value if the key is not found.
**Example**:
my_dict = {"a": 1}
value = my_dict.get("a", 0)
# Result: value = 1
14. **`setdefault`**
**Description**: Inserts a key with a specified value into a
dictionary if the key is not already present.
**Example**:
my_dict = {"a": 1}
my_dict.setdefault("b", 2)
# Result: my_dict = {"a": 1, "b": 2}
15. **`update`**
**Description**: Updates a dictionary with key-value pairs from
another dictionary or iterable.
**Example**:
my_dict = {"a": 1}
my_dict.update({"b": 2})
# Result: my_dict = {"a": 1, "b": 2}
17. **`type`**
**Description**: Returns the type of an object.
**Example**:
my_var = 42
var_type = type(my_var)
# Result: var_type = <class 'int'>
18. **`isalpha`**
**Description**: Checks if all characters in a string are alphabetic.
**Example**:
my_string = "hello"
is_alpha = my_string.isalpha()
# Result: is_alpha = True
19. **`isdigit`**
**Description**: Checks if all characters in a string are digits.
**Example**:
my_string = "12345"
is_digit = my_string.isdigit()
# Result: is_digit = True
20. **`replace`**
**Description**: Replaces occurrences of a substring with another
substring in a string.
**Example**:
my_string = "hello world"
new_string = my_string.replace("world", "Python")
# Result: new_string = "hello Python"