Home » Python

from keyword with example in Python

Python from keyword: Here, we are going to learn about the from keyword with example.
Submitted by IncludeHelp, on April 16, 2019

Python from keyword

from is a keyword (case-sensitive) in python, it is used to import a specific section from a module, like functions, classes, etc.

Syntax of from keyword

    from module_name import specific_section1, specific_section1, ...

Here, specific_section(s) are the names of functions, classes, etc.

Example:

    # importing factorial from math module
    from math import factorial

Python examples of from keyword

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  


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.