How to Read User Input as Numbers in Python


In Python 3 onwards, the input function returns a string type..

int value

You can explicitly convert this into an int type.

val = int(input("Enter a number: "))

float value

If you need to accept fractional components, then simply swap this out with a float type:

val = float(input("Enter a number:"))

map multiple values

If you have multiple integers in a single line, then you can use map to extract them into a list:

arr = map(int, raw_input().split())