How do you input integer data in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this post, We will see how to take integer input in Python. As we know that Python’s built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function.

    Let us see the examples:

    Example 1:

    Python3

    input_a = input()

    print(type(input_a))

    input_a = int(input_a)

    print(type(input_a))

    Output:

    100
    
    

    Example 2:

    Python3

    input_a = input()

    print(type(input_a))

    input_b = int(input())

    print(type(input_b))

    Output:

    10
    
    20
    

    Example 3:

    Python3

    input_str_array = input().split()

    print("array:", input_str_array)

    input_int_array = [int(x) for x in input().split()]

    print("array:", input_int_array)

    Output:

    10 20 30 40 50 60 70
    array: ['10', '20', '30', '40', '50', '60', '70']
    10 20 30 40 50 60 70
    array: [10, 20, 30, 40, 50, 60, 70]

    Example 4:

    Python3

    n = int(input("Enter the size of list : "))

    lst = list(map(int, input(

        "Enter the integer elements of list(Space-Separated): ").strip().split()))[:n]

    print('The list is:', lst)  

    Output:

    Enter the size of list : 4
    Enter the integer elements of list(Space-Separated): 6 3 9 10
    The list is: [6, 3, 9, 10]

    Now, you know how to print something on the screen and not just that, you also know about the different data types available in Python and how to use them. Let’s move forward with this chapter and learn a new Python concept.

    Till now, we have assigned values to variables in the code. However, there can be times when we want the user to enter the value which has to be assigned to a variable. Take an example of a calculator in which the user enters the values to be added or subtracted.

    input() function is used to take input from the user. So, let’s see how to take values entered by the user.

    The statement in the above code will read the value entered by the user (xyz) and will assign it to the variable name.

    We can also print a message on the screen while asking the user for input by passing that message to the input() function.

    name = input("What is your name >>>")
    

    Output

    In this example, the message “What is your name >>>” will get printed on the screen and the value entered by the user after that will be assigned to the variable name. The message displayed on the screen while taking input is helpful to understand what type of input the user is expected to enter. Therefore, it is a good practice to display such messages while taking input.

    Let’s look at some other examples.

    print("Enter your name")
    x = input()
    y = input("age >>>") #age>>> will be printed before input
    print("Your name is",x,"and","and your age is",y)
    

    Output

    Enter your name
    xyz
    age >>>20
    Your name is xyz and and your age is 20

    print("Enter your wish")
    wish = input()
    print("May your wish come true!")
    

    Output

    Enter your wish
    I want to be greatest coder ever
    May your wish come true!

    What does input() do?


    x = input(">>>")
    print(x)
    

    Output

    If the user enters 10, then the first statement is equivalent to x = '10'.

    We gave 10 to input(). You can think that input() becomes 10. So, x = input() will be equivalent to x = 10 after the input is given.

    How do you input integer data in python?

    Taking String Input in Python


    By default, any input entered by the user is of type string.

    num = input()
    print(type(num))
    

    Output

    As you can see, the user entered a number 10 as input but its type is str. Therefore, any input entered by the user is always read as string.

    Taking Integer Input in Python


    We just saw that input is always read as a string. So what if we want to read an integer?

    We can do this by converting the string input to int using the int() function.

    x = int(input("Enter an integer >>>"))
    print ("You have entered", x)
    

    Output

    Enter an integer <<<12
    You have entered 12

    int() changes a string to an integer. For example, int('12') will give us an integer 12.

    In the above example, we are taking the input from the user as a string with the input() function and then we are using int() to change it to an integer. So, if a user enters 12, then input() will become '12' (a string) and the above statement int(input()) will become int('12') and we will get 12 as an integer.

    We can also break the above example in one more step to understand better.

    x = input("Enter an integer >>>")
    y = int(x)
    print("You have entered", y)
    

    Output

    Enter an integer <<<12
    You have entered 12

    Here, we passed 12. Thus, x became '12' and then int(x) turned it into an integer.

    Taking Float (Decimals) Input in Python


    Similar to taking input of integers, we can use float() to take an input from the user and change it to a float.

    x = float(input())
    print(x)
    print(type(x))
    

    Output

    12.32
    12.32

    It is similar to taking input of integers. We entered 12.32 to input() which returned '12.32' (a string). Thus, the expression float(input()) became float('12.32'), and that gave us a float 12.32.

    Let your computer do some maths for you


    import math
    print(math.sin(30))
    print(math.cos(10))
    print(math.pow(2,3))
    

    Output

    -0.9880316240928618
    -0.8390715290764524
    8.0

    import math → This will include Python's inbuilt directory 'math'. It contains many mathematical functions for our use. import is a keyword used to import any available directory.

    math.sin() computes sine of a given angle.

    math.cos() computes cosine of a given angle.

    math.pow(a,b) computes a raised to the power of b (a^b).

    We have imported 'math' and these functions are provided by it, so 'math.sin()' can be understood as the sin() function from the 'math' directory.

    Checkout python's official documentation for more functions available in math.

    To learn from simple videos, you can always look at our Python video course on CodesDope Pro. It has over 500 practice questions and over 20 projects.

    In theory, there is no big difference between theory and practice. But in practice, there is.

    - Yogi Berra


    How do you input an integer in Python?

    As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function.

    How do I input data in Python?

    There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.

    How do you define an integer in Python?

    Int. Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

    How do you input a string and integer in Python?

    “how to take integer and string as input in list python” Code Answer's.
    # number of elements..
    n = int(input("Enter number of elements : ")).
    # Below line read inputs from user using map() function..
    a = list(map(int,input("\nEnter the numbers : "). strip(). split()))[:n].
    print("\nList is - ", a).