Home »
Python
Python from Keyword
By IncludeHelp Last updated : December 07, 2024
Description and Usage
from is a keyword (case-sensitive) in python, it is used to import a specific section from a module, like functions, classes, etc.
Syntax
Syntax of from keyword:
from module_name import specific_section1, specific_section1, ...
Here, specific_section(s) are the names of functions, classes, etc.
Example 1
Find factorial of a number by importing factorial from math module.
# python code to demonstrate example of
# from keyword
# Find factorial of a number
# by importing factorial from math module.
# importing factorial from math module
from math import factorial
num = 4
# printing factorial
print("factorial of ", num, " is = ", factorial(num))
Output
factorial of 4 is = 24
Example 2
Find factorial and square root of a number by importing two functions factorial and sqrt from math module.
# python code to demonstrate example of
# from keyword
# Find factorial and square root of a number
# by importing two functions factorial and sqrt
# from math module.
# importing factorial and sqrt from math module
from math import factorial, sqrt
num = 4
# printing factorial & square root
print("factorial of ", num, " is = ", factorial(num))
print("square root of ", num, " is = ", sqrt(num))
Output
factorial of 4 is = 24
square root of 4 is = 2.0