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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.