We already know how to print and display anything on the screen. Also, we know about the different data types available in Python and how to use them. So, let’s move forward and learn How can I read inputs as numbers? This is one of the most important Python concepts to understand and learn.
We know how to easily assign values to variables while coding. However, at times, we may want to enter a value to be assigned to a variable. Take an example of a calculator during which the user enters the values to be added or subtracted.
How can I take inputs?
To take inputs from a user, we need to use the input() function. So, let’s see the way to take values entered by the user.
name = input()
# Output : Shubham
What does the above statement do? It takes the name input as “Shubham” and then assigns it to the variable “name”.
Now, let’s take an example wherein we print a message in the input() function.
name = input("What is your name? --- ") # Output: What is your name? --- Shubham
The above example prints the message “What is your name? — ” and the value we input will get assigned to the said “name” variable. The message displayed on the screen while taking input is useful to know what sort of input the user is predicted to enter.
Let’s check out another example :
print(" Enter your name : ") name = input() age = input("Age --- ") print(type(age)) # To check the input type of age print("Your name is : ", name, " and ", " your age is : ", age) # Output : Enter your name : "Shubham" <class 'str'> Age --- "24" Your name is : Shubham and your age is : "24" # Thus we see that the age input has been taken as String, the default input type.
Here’s a post on integrating MongoDB with Python !!!
print(" Enter your wish : ") wish = input() print(" May your wish come true! ") # Output : Enter your wish : "I want to be the King of the World" "May your wish come true!"
So, we basically see that the input statement takes in an input and outputs it as a String. So, if we enter Shubham as input it takes it as “Shubham”. Likewise, if we input 24 then also it will take the input as “24”.
How can I read inputs as numbers?
Taking integer input in Python
Till now we have seen how we take input. By now, we know that the default input type is always String. So, in order to make that input a number, we need to typecast the input datatype from String to say integer or float for numbers.
Let’s take one example :
num = int(input("Enter an integer : ")) print ("You have entered : ", num) # Output : Enter an integer : 10 You have entered : 10
Taking Float Inputs in Python Programming
Similar to taking input of integers, we can take decimal number inputs. Just that we will need to use the float() function to typecast the input from the user into a decimal number.
Let’s take an example :
num = float(input(" Enter a number : ")) print("The number you entered is : ", num) print(type(num)) # Output : Enter a number : 50.8 The number you entered is : 50.8 <class 'float'>
The above program teaches us how to take in float/decimal inputs. It is similar to the int() function that we use for taking integer numbers as input.
### Input taking --- 1 num = input("Enter an integer : ") num2 = input("Enter another integer : ") print ("The sum is : ", num + num2) # Output : Enter an integer : 20 Enter another integer : 79 The sum is : 2079 ### Input taking --- 2 num = int(input("Enter an integer : ")) num2 = int(input("Enter another integer : ")) print ("The sum is : ", num + num2) # Output : Enter an integer : 20 Enter another integer : 79 The sum is : 99
In the first example above, we see that by default if we don’t typecast the input variable then the default input is always in String format. Hence 20 + 79 adds up to 2079 as “+” means concatenation while dealing with Strings.
Similarly, in the second example, we see that since we have typecasted our variable into an integer; the output, in turn, returns 20 + 79 as 99 because “+” means adding when dealing with numbers.
So, by now we all are aware of the methods to take in inputs in Python programming. We have seen that the default input type is String. So, if we have to take in integer inputs or any other datatype input then we need to typecast the variable into that datatype. Say, using int() for integer and float() for float/decimal values. Till next time !! See-ya 🙂