Home > Article > Backend Development > Analysis and introduction to the difference between input() and raw_input() in python
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:")
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:"))
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!