Page 206 - Computer Science V2.0 Class 11
P. 206
In the above example, the print() function accepts the argument 2+14, evaluates it, and prints the result of
the evaluation (16) on the screen. More generally, we may invoke the print() function with several arguments
separated by commas. For instance, the following function call displays the string 'Sum total of 4 and 5 is'
followed by a space followed by the result of expression 4+5:
>>> print('Sum of 4 and 5 is',4+5)
Sum of 4 and 5 is 9
What will be displayed on executing the following statements?
print('4*5', 4*5)
input()
The input() function reads the text entered by a user until a newline is encountered. Suppose, we wish to take
the name of a programming language as the input from the user. For this purpose, the function input may be invoked
as follows:
>>> language = input()
Python
>>> language
'Python'
On execution of the above statement, Python prompts the user for the name and waits for the input. The above use
of the input() functions seems simple and devoid of any problems. If we write a small program that needs only one
or two inputs and execute it soon after we are done with writing the program, then this approach works fine. But real-
life programs may require several inputs and may be executed days, months, or years after they are written. In such
scenarios, it is nearly impossible to remember the inputs required by the program. In fact, most of the software is used
by people without a role in developing it. Therefore, it is a good practice to display a suitable message indicating the
inputs required as illustrated below:
>>> input('Enter name of a Programming language: ')
Enter name of a Programming language: Python
'Python'
>>> language = input('Enter name of a Programming language: ')
Enter name of a Programming language: Python
>>> language
'Python'
input() function reads the text entered by a user until a newline is encountered.
eval()
The eval() function evaluates a string argument passed to the function and returns the result of the evaluation. For
instance, the following expression evaluates 15+10 and yields 25 as a result:
>>> eval('15+10')
25
>>> eval('hello')
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
eval('hello')
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined. Did you mean: 'help'?
Note that to evaluate hello, Python interprets hello as the name of an object and finds that no object is associated
with the name hello, and points out an error.
192 Touchpad Computer Science (Ver. 2.0)-XI

