How do i use modules in python?



  • Python has a ton of module built-in, save a lot of code. Here we use the math module to work out the square root of a number.

    The import math or the from math import sqrt can be used in this case, this is because the latter is more specific and the former contains many more functions.

    import math
    from math import sqrt
    
    class MyModuleClass():
        def demo_math_mod(self):
            print(sqrt(100))
    
    m1 = MyModuleClass()
    m1.demo_math_mod()
    

    The output is:
    10.0


Log in to reply
 

© Lightnetics 2024