So I am taking raw_input
as an input for some list.
x= raw_input()
Where I input 1 2 3 4
How will I convert it into a list of integers if I am inputting only numbers?
Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function.
The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set.
a = input() will take the user input and put it in the correct type. Eg: if user types 5 then the value in a is integer 5. a = raw_input() will take the user input and put it as a string. Eg: if user types 5 then the value in a is string '5' and not an integer.
Basically, the difference between raw_input and input is that the return type of raw_input is always string, while the return type of input need not be string only. Python will judge as to what data type will it fit the best.
Like this:
string_input = raw_input()
input_list = string_input.split() #splits the input string on spaces
# process string elements in the list and make them integers
input_list = [int(a) for a in input_list]
list = map(int,raw_input().split())
We are using a higher order function map
to get a list of integers
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With