- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python from Keyword
The from keyword in Python is used when we need to import a specified section from a module. For example:
from operator import add print(add(10, 20))
The output is:
30
In above program, instead of importing the whole operator module, that contains extensive number of methods, I've imported only the add() one.
And because I've imported only the add() from operator module. Therefore if we access the other methods like sub() of same module, then we will get an error saying that the sub is not defined. For example:
from operator import add print(sub(10, 5))
The snapshot given below shows the output produced by above program:
Therefore be sure to import the particular section of a module. That is, only import particular method(s) of a module, if you're sure, not to use any other method(s) of the same module.
But if you replace the following statement, from above program:
from operator import add
with the statement given below:
import operator
Then the output will be:
5
Important - The output will be 5, of course, but you need to use sub() along with operator, in this way:
print(operator.sub(10, 5))
Python from Keyword Example
Let's create another program to demonstrate the from keyword in Python. But before creating the program, that uses from keyword, let's first create a program, without from.
Without from Keyword
import operator print("Enter the Two Numbers: ", end="") a = int(input()) b = int(input()) print("\nAddition =", operator.add(a, b)) print("Subtraction =", operator.sub(a, b))
With from Keyword
from operator import add, sub print("Enter the Two Numbers: ", end="") a = int(input()) b = int(input()) print("\nAddition =", add(a, b)) print("Subtraction =", sub(a, b))
Output of Both the Program
In this program, since I've imported the particular two methods of operator module, therefore these two methods are directly called with its name. That is, without using the operator.methodName format.
« Previous Tutorial Next Tutorial »