Today I will take a new part of python from my tutorial series. In this part you will learn the usage of Module and I/O. Both concept are known in every language.
I already told you the following:
- Installation of Python
- Python Programming tutorial Part-1
- Python Programming Tutorial - Part 2
- Python Language Tutorial Part- 3 (Data Structure in List)
Download Python Language Tutorials and give star on GitHub if that files are helpful for you.
In this article you will understand the following:
- Module
- Input and Output (I/O)
Module
If you want to write a longer program, then you can use file as input. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a function that you’ve written in several programs without copying its definition into each program.
Python has a way to put definitions in a file and use them in a script. Such a file is called a module; definitions from a module can be imported into other modules or into the main module. A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
- def fun1(n):
- a, b = 0, 1
- while b < n:
- print(b, end = ' ')
- a, b = b, a + b
- print(a)
- def fun2(n):
- result = []
- a, b = 0, 1
- while b < n:
- result.append(b)
- a, b = b, a + b
- return result
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import fun
- fun.fun1(1000) # it call first method from fun.py file
- val = fun.fun2(100) # it call second method from fun.py file
- print val
- print fun # it show location of modula/file
- print fun.__name__ # it print the file name
- fib = fun.fun1
- fib(500)
- print dir(fun)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 987
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
<module 'fun' from 'C:\\Users\\NeErAj\\Projects\\demo\\fun.py'>
Fun
1 1 2 3 5 8 13 21 34 55 89 144 233 377 377
['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'fun1', 'fun2']
Example 2: import statement that imports names from a module directly into the importing module:
- from fun import fun1,fun2
- fun1(500)
- val=fun2(500)
- print(val)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 377
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
Input & Output
There are several ways to present the output of a program through which data can be printed in a human-readable form.
Output Formatting:
Example 1:
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- s = 'Hello, world.'
- print str(s)
- print repr(s)
- print str(1 / 7)
- x = 10 * 20
- y = 200 * 300
- s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
- print s
- # The repr() of a string adds string quotes and backslashes:
- hello = 'hello, world\n'
- hellos = repr(hello)
- print hellos
- # The argument to repr() may be any Python object:
- print repr((x, y, ('spam', 'eggs')))
Hello, world.
'Hello, world.'
0.14285714285714285
The value of x is 200, and y is 60000...
'hello, world\n'
(200, 60000, ('spam', 'eggs'))
Example 2:
- for x in range(1, 11):
- print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ') #rjust() = Right Justify
- # Note use of 'end' on previous line
- print(repr(x*x*x).rjust(4))
- for x in range(1, 11):
- print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
Example 3:
- table = {'Alwar': '0144' , 'Jaipur': '0141' , 'Ajmer': '0142' }
- for city, pincode in table.items():
- print('{0:5} ==> {1:5}'.format(city, pincode))
Ajmer ==> 0142
Jaipur ==> 0141
Alwar ==> 0144
Reading and Writing Files
open() returns a file object, and is most commonly used with two arguments: open(filename, mode).
- f = open('workfile', 'w')
- f = open('file', 'wb+')
- f.write(b'0123456789abcdef') #It write text into file
- f.seek(5) # Go to the 6th byte in the file
- print(f.read(1))
- f.seek(-3, 2) # Go to the 3rd byte before the end
- print(f.read(1))
b'5'
b'd'
Example:
- with open('workfile', 'r') as f:
- read_data = f.read()
- f.closed
- print(read_data)
0123456789abcdef
File objects have some additional methods such as isatty() and truncate() which are less frequently used.
I hope you will understand this part of python programming language.

Santhakumar MunuswamyPosted Oct 5, 2015, 11:52 PM
Nice Share
Neeraj KumarPosted Oct 5, 2015, 9:01 AM
thank you Ankit Bansal
Neeraj KumarPosted Oct 5, 2015, 9:01 AM
thank you Gowtham Rajamanickam
Neeraj KumarPosted Oct 5, 2015, 9:00 AM
thank you Abhishek Arora
Ankit BansalPosted Oct 5, 2015, 5:56 AM
Nice..thanks for sharing with us..
Gowtham RajamanickamPosted Oct 5, 2015, 4:58 AM
useful....
Abhishek AroraPosted Oct 5, 2015, 1:09 AM
Good one
Neeraj KumarPosted Oct 4, 2015, 11:44 PM
Thank you Nilesh Jadav
Nilesh JadavPosted Oct 4, 2015, 8:48 PM
Great Work Sir !