The difference between input and raw_input in Python is pythonraw_input.

Source: Internet
Author: User

The difference between input and raw_input in Python is pythonraw_input.

Both input and raw_input can be used to read the input from the console, but the input and raw_input are different in processing numbers.

When the input is a pure number:

Input returns numeric values, such as int and float.
Raw_inpout returns the string type and the string type.

Input string as expression

Input calculates the number expression in the string, but raw_input does not.

For example, enter "57 + 3 ":

Input returns an integer of 60.

Raw_input will get the string "57 + 3"

Python input implementation

Read the python input document and you can see that the input is actually implemented through raw_input. The principle is very simple. The following code line is as follows:
Copy codeThe Code is as follows:
Def input (prompt ):
Return (eval (raw_input (prompt )))



What is the difference between raw_input and input in python?

You can understand this as follows:

Def input (prompt ):
& Nbspreturn eval (raw_input (prompt ))

That is to say, call the eval function to see what the input is. In fact, you can even enter an expression. Python calculates the value of the expression to return the result.

Why does a python script write raw_input () for output?

If the output is a row or a row, it is okay.

If you want to display it on the console, you must know that the displayed window disappears automatically once it is executed. Your first sentence does output Hello world in the window, but without the next sentence, the window will automatically disappear immediately, and you cannot see whether there is output. However, if raw_input () exists, the window will not disappear, because the code is not executed completely. This raw_input () requires you to enter something in the window and press enter to complete the execution, if the execution is complete, the window disappears.

Other languages have similar problems and solutions.

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.