The Special Variable __name__

The Special Variable __name__

  • When a program is executed in python, there is a special variable internally created by the name '__name__'.
  • A special variable __name__ will be added internally. This variable stores information regarding whether the program is executed as an individual program or as a module. 
  • If the program executed as an individual program then the value of this variable is __main__ If the program executed as a module from some other program then the value of this variable is the name of module where it is defined.
  • The value of the special variable '__name__' indicates whether the program is run as an individual program or as a module. 
  • If the value of tha variable '__name__' is '__main__' then the program is run directly as a program or script; otherwise, it is run as a module from another program.  
Demo program: 

def f1():
    if __name__=='__main__':
        print("The code executed as a program")
    else:
        print("The code executed as a module from some other program")
f1()  

Output:
The code executed as a program 

Working with math module: 

  • Python provides inbuilt module math. This module defines several functions which can be used for mathematical operations. The main important functions are:
  1. sqrt(x) 
  2. ceil(x) 
  3. floor(x) 
  4. fabs(x) 
  5. log(x) 
  6. sin(x) 
  7. tan(x)

Eg:

Note: We can find help for any module by using help() function.

Eg: 

  • import math 
  • help(math) 


 
 





Comments