Import Module
Import statement
To use a Python source file, simply execute the import statement in another source file with the following syntax:
Import module1[, module2[,... Modulen]
When the interpreter encounters an import statement, the module is imported if it is in the current search path.
A search path is a list of all directories that an interpreter will search first. To import the module hello.py, you need to place the command at the top of the script:
#!/usr/bin/python#-*-coding:utf-8-*-# Importing module Import Support # can now call the function contained in the module Support.print_func ("Zara")
The result of the above example output:
Hello:zara
A module will only be imported once, no matter how many times you execute the import. This prevents the import module from being executed over and over again.
From...import statements
The FROM statement of Python lets you import a specified section from the module into the current namespace. The syntax is as follows:
From ModName import name1[, name2[, ... Namen]]
For example, to import the Fibonacci function of a module FIB, use the following statement:
From fib import Fibonacci
This declaration does not import the entire FIB module into the current namespace, it only introduces the Fibonacci individual in the FIB to the global symbol table of the module that executes the declaration.
from...import* statements
It is also possible to import all the contents of a module into the current namespace, just use the following declaration:
From ModName Import *
This provides an easy way to import all the items in a module. However, such statements should not be used too much.
Reading keyboard input
Python provides two built-in functions to read a line of text from standard input, and the default standard input is the keyboard. As follows:
Raw_input function
The Raw_input ([prompt]) function reads a line from the standard input and returns a string (minus the end of the newline character):
#!/usr/bin/python str = raw_input ("Enter Your input:");p rint "Received input is:", str
This will prompt you to enter any string and then display the same string on the screen. When I enter "Hello python! ", its output is as follows:
Enter your Input:hello pythonreceived input Is:hello Python
Input function
The input ([prompt]) function and the raw_input ([prompt]) function are basically interchangeable, but input assumes that your input is a valid Python expression and returns the result of the operation.
#!/usr/bin/python str = input ("Enter your input:");p rint "Received input is:", str
This will produce the following results for the input:
Enter your input: [x*5 for X in range (2,10,2)]recieved input is: [10, 20, 30, 40]