Methods for importing and reading keyboard input from a module in Python

Source: Internet
Author: User
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

    • Input


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]
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.