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 testfile.py(.py 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 testfile.py 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
20
>>>

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.