0% found this document useful (0 votes)
3 views

Python_Methods_Descriptions

The document provides a comprehensive list of common Python methods categorized into List, String, Dictionary, and General Utility methods. Each method is accompanied by a description and an example demonstrating its usage. This serves as a quick reference guide for Python programming.

Uploaded by

Vedansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_Methods_Descriptions

The document provides a comprehensive list of common Python methods categorized into List, String, Dictionary, and General Utility methods. Each method is accompanied by a description and an example demonstrating its usage. This serves as a quick reference guide for Python programming.

Uploaded by

Vedansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Common Python Methods and Their Descriptions

### List Methods:


1. **`append`**
**Description**: Adds an item to the end of a list.
**Example**:
my_list = [1, 2, 3]
my_list.append(4)
# Result: my_list = [1, 2, 3, 4]

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]

### String Methods:


5. **`upper`**
**Description**: Converts a string to uppercase.
**Example**:
my_string = "hello"
uppercase_string = my_string.upper()
# Result: uppercase_string = "HELLO"

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"

### Dictionary Methods:


10. **`keys`**
**Description**: Returns a view object of dictionary keys.
**Example**:
my_dict = {"a": 1, "b": 2}
keys = my_dict.keys()
# Result: keys = dict_keys(["a", "b"])

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}

### General Utility Methods:


16. **`len`**
**Description**: Returns the number of items in a list, dictionary, or
string.
**Example**:
my_list = [1, 2, 3]
length = len(my_list)
# Result: length = 3

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"

You might also like