Posts

Showing posts from November, 2018

The dir Function

dir()      The built-in dir() lists names from modules. It returns a sorted list of strings.      Let's import regular expression module:- >>> import re >>> dir(re) ['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', ...

Python Modules

Modules      A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Let's understand this by taking example,      We will write module for fibonacci Series. def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result      You can import this like:- import fibo Use like this >>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]      A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. Or you can import specific method o...