Python 3.x's input and Python 2.x's raw_input are similar and raw_input is not available in Python 3.x. I explained why one should be careful when using it, in this answer.īut, raw_input doesn't evaluate the input and returns as it is, as a string. Note: You should be careful when you are using input in Python 2.x. > data = eval(raw_input("Enter a number: ")) You can think of input as the raw_input composed with an eval call. So, the type conversion is done for free, and 22 is returned as the result of the input and stored in the data variable. When it evaluates the expression 5 + 17, it detects that you are adding two numbers and so the result will also be of the same int type. The data 5 + 17 is evaluated and the result is 22. So even if you provide a string and it’s a valid value for the float function, it’ll convert it into a floating number. '2.7.6 (default, Mar 22 2014, 22:59:56) \n' How Does float() Work in Python The float() function type casts any right and acceptable data types into a float number. But, input will evaluate whatever you entered and the result of evaluation will be returned. The difference between them is, raw_input doesn't evaluate the data and returns as it is, in string form. There were two functions to get user input, called input and raw_input.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |