Modules(saving python code in files)

Python code typed on console for practice, other than this we can also save our code to various files.

Step 1:

In the following example we have created is python file extension) in python home directory which is the directory in which python is installed. See the following Image :

Step 2:

Now type the following code into your which we made earlier :

def Hello():
  print "hello function"
  return 20

Step 3:

Now open your console and type the following commands:

>>> import testfile
>>> a = testfile.Hello()
hello function
>>> a

First of all we need to import the file we have created, import it without extension. Next I have called this function into a variable say a which will store the returned result. and at last the function displays hello function and value of 20 is displayed on screen.