Input Data
Input Data
Introduction
We have seen how we can store values in names or associate names with
values using the assignment operator (=). The drawback or limitation of using the ‘=’
operator as a means to associate names and values is that every time the program is
executed, the same values are used for the calculation and hence the same result is
obtained.
Example:
n1=25
n2=30
sum = n1+n2
print('Sum=',sum)
Every time that we execute the program, we will get the same result i.e. 55. But, what if
we want to calculate the sum of different set of values (such as 112, 352; 77, 35 ;
427,835) each time that the program is executed.
But, this is not required when we use the input statement instead of the
association operator(=). With input statements, the values for the program are not
provided in the program but they are provided during program execution. Hence, we
can use the same program without making any changes, to obtain a different result
each time that the program is executed.
Example
n1=int(input('Enter first number'))
n2=int(input('Enter second number'))
sum=n1+n2
print('Sum=',sum)
1 | Page
2. When the message “ Enter first number” is displayed, type in an integer value
and press the enter key
3. Similarly for the 2nd number.
Please note that the message “Enter first number” is displayed only because we
have included it in our program. If we do not include it or specify it, there will be no
message displayed but the computer will wait for us to type in an integer value for the
1st input() and another integer value for the 2nd input() . A message is essential so that
during program execution we know what response the computer expects from us.
Note: Whenever values are given in the question, the assignment operator (=) must be
used to assign the values; otherwise input() must be used.
2 | Page