|
| 1 | +--- |
| 2 | +Title: 'from' |
| 3 | +Description: 'Used to import specific attributes, classes, or functions from a Python module.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Attributes' |
| 9 | + - 'Classes' |
| 10 | + - 'Modules' |
| 11 | + - 'Python' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`from`** keyword in Python is used to import specific items (such as [functions](https://www.codecademy.com/resources/docs/python/functions), [classes](https://www.codecademy.com/resources/docs/python/classes), or [variables](https://www.codecademy.com/resources/docs/python/variables)) from a module instead of importing the entire module. It helps keep the code cleaner and avoids unnecessary namespace clutter. |
| 18 | + |
| 19 | +For example, instead of importing the entire `math` module, only the required functions can be imported. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +from module_name import name |
| 25 | +``` |
| 26 | + |
| 27 | +To import multiple items: |
| 28 | + |
| 29 | +```pseudo |
| 30 | +from module_name import name1, name2, ... |
| 31 | +``` |
| 32 | + |
| 33 | +To import all public names from a module (not recommended): |
| 34 | + |
| 35 | +```pseudo |
| 36 | +from module_name import * |
| 37 | +``` |
| 38 | + |
| 39 | +In the syntax: |
| 40 | + |
| 41 | +- `module_name`: The name of the module to import from. |
| 42 | +- `name`: The specific object(s) (function, class, or variable) to import. |
| 43 | +- The asterisk (`*`) imports all public objects from the module, which can lead to namespace conflicts and reduced clarity. |
| 44 | + |
| 45 | +## Example 1: Importing a Specific Function |
| 46 | + |
| 47 | +This example demonstrates importing a single function (`sqrt`) from the `math` module: |
| 48 | + |
| 49 | +```py |
| 50 | +from math import sqrt |
| 51 | + |
| 52 | +# Using the imported function directly |
| 53 | +result = sqrt(16) |
| 54 | +print(result) |
| 55 | +``` |
| 56 | + |
| 57 | +The output of this code is: |
| 58 | + |
| 59 | +```shell |
| 60 | +4.0 |
| 61 | +``` |
| 62 | + |
| 63 | +## Example 2: Importing Multiple Functions |
| 64 | + |
| 65 | +This example shows how to import more than one function from the same module: |
| 66 | + |
| 67 | +```py |
| 68 | +from math import ceil, floor |
| 69 | + |
| 70 | +print(ceil(4.2)) |
| 71 | +print(floor(4.8)) |
| 72 | +``` |
| 73 | + |
| 74 | +The output of this code is: |
| 75 | + |
| 76 | +```shell |
| 77 | +5 |
| 78 | +4 |
| 79 | +``` |
| 80 | + |
| 81 | +## Example 3: Using `from` for Relative Imports |
| 82 | + |
| 83 | +The `from` keyword can also be used with relative imports inside packages, such as: |
| 84 | + |
| 85 | +```py |
| 86 | +from . import utils |
| 87 | +from ..helpers import format_data |
| 88 | +``` |
| 89 | + |
| 90 | +- Relative imports use dots (`.`) to indicate the current and parent packages. |
| 91 | +- Explicit imports using `from` enhance code readability and prevent unnecessary components from being loaded. |
| 92 | + |
| 93 | +## Codebyte Example |
| 94 | + |
| 95 | +The following example illustrates importing multiple functions (`sqrt`, `pow`) and using them directly in a program: |
| 96 | + |
| 97 | +```codebyte/python |
| 98 | +# Import specific functions from the math module |
| 99 | +from math import sqrt, pow |
| 100 | +
|
| 101 | +# Use imported functions directly |
| 102 | +num = 9 |
| 103 | +root = sqrt(num) |
| 104 | +power = pow(2, 3) |
| 105 | +
|
| 106 | +print("Square root of 9 is:", root) |
| 107 | +print("2 raised to the power 3 is:", power) |
| 108 | +``` |
0 commit comments