Method vs Function
While studying Python, I try to organize the method of using . or. () through this post because I was confused about how to write .
In Python, there are cases where you use a dot (.) when calling a function and cases where you don't. Here are the general rules:
Method:
When calling an object's method, you use a dot (.). For instance, when invoking the split() method of a string, you write string.split(). Here, 'string' represents a string object, and split() is a method of the string object.
Function:
When calling a global function that is not a method of an object, you don't use a dot (.). Global functions are defined at the top level of a module, and you can call them by just using the function name without a dot. For example, the len() function, which returns the length, is a built-in function, so you use it like len(sequence).
Here is the example of top 5 Method and Function commonely used
**Method**
- split(): A string method that splits a string based on a specified separator and returns a list.
- strip(): A string method that removes leading and trailing whitespaces or specified characters from a string.
- join(): A string method that concatenates strings together.
- append(): A list method that adds an element to the end of a list.
- upper()/lower(): String methods that convert a string to uppercase or lowercase.
**Function**
- print(): A function that prints to the screen.
- len(): A function that returns the length of a sequence.
- range(): A function that generates a sequence of numbers within a specified range.
- type(): A function that returns the type of an object.
- input(): A function that accepts input from the user.
Differences between dictionary and list:
1. Data Structure:
- Dictionary: A dictionary is a data structure that stores key-value pairs. Each key is associated with a value, representing an associated relationship. Dictionaries are defined using curly braces `{}`, with each key-value pair separated by a colon `:`.
- List: A list is a data structure that stores a sequence of elements with a defined order. Elements can be accessed by their index position. Lists are defined using square brackets `[]`, with elements separated by commas.
2. Storage Method:
- Dictionary: Dictionaries store key-value pairs, where each key must be unique. Therefore, duplicate keys are not allowed within a dictionary.
- List: Lists store elements sequentially, allowing duplicate elements.
3. Access Method:
- Dictionary: Values in a dictionary are accessed using keys. Accessing a key that does not exist in the dictionary may raise a KeyError.
- List: Elements in a list are accessed using their index position.
4. Usage:
- Dictionary: Dictionaries are useful for storing and retrieving associated data efficiently. They provide fast and efficient lookup based on keys.
- List: Lists are suitable for storing and processing sequential data. They are commonly used when maintaining the order of elements is important.
Due to differences in data structure, storage method, access method, and usage, dictionaries and lists are used in different contexts and serve different purposes.
'Study Note > Python' 카테고리의 다른 글
Lambda function (0) | 2024.04.05 |
---|---|
10. Purpose of 'while' and 'for in' (0) | 2024.04.02 |
8. Function simplification (0) | 2024.03.27 |
7. F-strings & Try Except (0) | 2024.03.27 |
6. Set & tuples and list (0) | 2024.03.27 |