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:
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
-
In this example, above code save into any file name with extension
.py. I save this file with
fun.py name.
After that again create new Python file and add
import fun where fun is python file name.
After that call that function which you want to use.
Example 1: Using the module name you can access the functions:
-
-
- import fun
- fun.fun1(1000)
- val = fun.fun2(100)
- print val
- print fun
- print fun.__name__
- fib = fun.fun1
- fib(500)
- print dir(fun)
-
-
Output:
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)
Output:
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:
-
-
- 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
-
-
-
- hello = 'hello, world\n'
- hellos = repr(hello)
- print hellos
-
-
-
- print repr((x, y, ('spam', 'eggs')))
-
-
Output:
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=' ')
-
- 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))
Output:
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))
Output:
Reading and Writing Files
open() returns a file object, and is most commonly used with two arguments: open(filename, mode).
- f = open('workfile', 'w')
The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. Mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it is omitted.
Methods of File Objects Example: - f = open('file', 'wb+')
- f.write(b'0123456789abcdef')
- f.seek(5)
- print(f.read(1))
- f.seek(-3, 2)
- print(f.read(1))
Output:
Example:
- with open('workfile', 'r') as f:
- read_data = f.read()
- f.closed
- print(read_data)
Output:
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.