The module in Python imports and reads keyboard input.

Source: Internet
Author: User

The module in Python imports and reads keyboard input.

Import Module

Import Statement
To use a Python source file, you only need to execute the import Statement in another source file. The syntax is as follows:

import module1[, module2[,... moduleN]


When the interpreter encounters an import Statement, the module will be imported in the current search path.
The search path is a list of all directories that the interpreter will first search. To import the module hello. py, place the command at the top of the script:

#! /Usr/bin/python #-*-coding: UTF-8-*-# import module import support # Now you can call the functions included in the module support. print_func ("Zara ")

Output result of the above instance:

Hello : Zara


A module is imported only once, no matter how many times you execute the import. This prevents the import module from being executed over and over again.

From... Import Statement
The from Statement of Python allows you to import a specified part from the module to the current namespace. Syntax:

from modname import name1[, name2[, ... nameN]]


For example, to import the function of the module fib, use the following statement:

from fib import fibonacci


This statement does not import the entire fib module into the current namespace. Instead, it only introduces the fiber-ACCI in fib to the global symbol table of the module that executes this declaration.

From... Import * Statement
It is also feasible to import all the content of a module to the current namespace. You only need to use the following statement:

from modname import *

This provides a simple way to import all projects in a module. However, such declarations should not be used too much.


Read keyboard input
Python provides two built-in functions to read a line of text from the standard input. The default standard input is the keyboard. As follows:

  • Raw_input
  • Input


Raw_input Function
Raw_input ([prompt]) function reads a row from the standard input and returns a string (remove the line break at the end ):

#!/usr/bin/python str = raw_input("Enter your input: ");print "Received input is : ", str

This will prompt you to enter any string and 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 raw_input ([prompt]) function are basically interchangeable, but the input assumes that your input is a valid Python expression and returns the calculation result.

#!/usr/bin/python str = input("Enter your input: ");print "Received input is : ", str

This will generate the following results corresponding to the input:

Enter your input: [x*5 for x in range(2,10,2)]Recieved input is : [10, 20, 30, 40]

Related Article

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.