How to take integer input in Python

In this tutorial, we will see how to take integer input in Python

There is difference in how you can take input in python 2.x and 3.x. You need to use raw_input in python 2.x and input in Python 3.x

In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.

As you might know, raw_input always returns a String object and same is the case with input in Python 3.x

To fix the problem, you need to explicitly make those inputs into integers by putting them in int() function.


Python 3.x example

a = int(input(“Enter a number: “))
b = int(input(“Enter a number: “))

Let’s understand with the help of example.

Output:

Enter an Integer: 20
Enter an Integer: 10
Sum of a and b: 30
Multiplication of a and b: 200

Python 2.x example

p = int(raw_input(“Enter a number: “))
q = int(raw_input(“Enter a number: “))

Let’s understand with the help of example.

Output:

Enter an Integer: 20
Enter an Integer: 10
Sum of a and b: 30
Multiplication of a and b: 200

That’s all about How to take integer input in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 September

    Print Number without Decimal in Python

    Table of ContentsHow To Print a Number without Decimal in Python?Using the int() FunctionUsing the math.trunc() functionUsing the round() functionUsing the split() function along with the int() functionConclusion. Python provides a float data type that allows the storage of floating-point numbers. The data at the decimal places is also retained while using the float data […]

  • 26 September

    Print hex without 0x in Python

    Table of ContentsHow to print hex without 0x in Python?Using slicing along with the hex() functionUsing the replace() function along with the hex() functionUsing slicing and the zfill() function along with the hex() functionUsing the format() functionUsing %x conversionUsing f-stringsUsing the to_bytes() function along with the hex() functionConclusion. A hex or a hexadecimal string is […]

  • 26 September

    Check if Date Is Between Two Dates in Python

    Table of ContentsCheck if Date Is Between Two Dates in PythonUsing the datetime.Datetime Object to Check if Date Is Between Two Dates in PythonUsing Tuples to Check if Date Is Between Two Dates in PythonConclusion Python allows us to store and work with date values very efficiently by providing the datetime module. This module has […]

  • 26 September

    Prefix r before String in Python

    Table of ContentsPrefix r Before String in PythonConclusion 💡 Quick Definition Prefix r before String denotes raw Strings in Python. Python raw string considers backslash (\) as a literal character. This is generally preferred when you don’t want to treat backslash character as escape character. Here is an example: [crayon-63330324d4eb4029487688/] Output: \n \t are escape […]

  • 26 September

    Print bytes as hex in Python

    Table of ContentsPrint Bytes as Hex in PythonUsing the hex() function to print Bytes as Hex in PythonUsing the format() function to print Bytes as Hex in PythonUsing the binascii.hexlify() function to print Bytes as Hex in PythonConclusion Print Bytes as Hex in Python Bytes are encoded in ASCII and have a very dynamic role […]

  • 23 September

    Convert 0 to 1 and 1 to 0 in Python

    Table of ContentsWays to convert 0 to 1 and 1 to 0 in PythonUsing SubtractionUsing XORUsing not operatorUsing lambda expressionUsing addition and remainder operatorConvert 1 to 0 and 0 to 1 in listConvert 1 to 0 and 0 to 1 in numpy arrayUsing where with bitwise xorUsing bitwise operators In this post, we will see […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.