Home > Backend Development > Python Tutorial > Analysis and introduction to the difference between input() and raw_input() in python

Analysis and introduction to the difference between input() and raw_input() in python

高洛峰
Release: 2017-03-07 16:13:57
Original
1457 people have browsed it

Let’s look at the source code of input

def input(prompt):
return eval(raw_input(prompt))

In fact, input also calls raw_input, just do Eval processing

And what is the role of eval?

input: Type conversion will be done based on the user's input
raw_input: The user's input will be processed as a string

The following is a detailed supplement :

First of all, we know that input() and raw_input() are used to obtain input from the console. Of course, you can add input prompt information when inputting:

        a = raw_input("Please input a:")
        b = input("Please input b:")
Copy after login

Then What's the difference between the two?

Input() supports users to input numbers or expressions, but does not support input of strings. It returns a numerical value of numeric type. Raw_input() captures the original input, which means it returns a string, so if the input is a number, then we must perform a forced conversion. For example:

   a = int(raw_input("Please input the number a:"))
Copy after login

In fact, input() is essentially implemented using raw_input(). It just calls the eval() function after calling raw_input(). Therefore, unless there is a special need for input(), otherwise Under normal circumstances, we recommend using raw_input() to interact with users.


map receives a function and an iterable object (such as a list) as parameters, processes each element with the function, and then returns a new list.
ACM sometimes needs to input a line format such as a b c. In this case, it uses the map function to process it. It is assumed that a, b, and c are all integers.
a,b,c = map(int, raw_input().split()), the raw_input function input is a string, and the split method of the string is used to split the string into sequences.

For more related articles on the analysis and introduction of the difference between input() and raw_input() in python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template