Python Programming - Python Input and Output

Python Programming – Python Input and Output

In any programming language, an interface plays a very important role. It takes data from the user and displays the output, One of the essential operations performed in Python language is to provide input values to the program and output the data produced by the program to a standard output device (monitor). The output generated is always dependent on the input values that have been provided to the program. The input can be provided to the program statically and dynamically. In static input, the raw data does not change in every run of the program. While in dynamic input, the raw data has a tendency to change in every run of the program.

Python language has predefined functions for reading input and displaying output on the screen. Input can also be provided directly in the program by assigning the values to the variables. Python language provides numerous built-in functions that are readily available to us at Python prompt. Some of the functions like input ( ) and print() are widely used for standard input and output operations, respectively. Firstly, we discuss the output section.

Output

The function print ( ) is used to output data to the standard output device, i.e., on the monitor screen. The output can be generated onto a file also, which we will discuss later. The standard syntax of the print ( ) function is as follows:

print(*objects, sep=’ end=’\n\ file=sys.stdout, flush=False)

In the above, objects are the value(s) to be printed, sep is the separator that is used between the values. It defaults into a space character, the end is printed after printing all the values. It defaults into a new line. The file is the object, where the values are printed and its default value is sys. stdout, i.e., screen. For instance, consider Code 2.22, where 3 print statements are given. We see that the first print statement prints the values of 10 20 30 40, the second print statement displays the values of 10-20-30-40, with the separator -. The third print statement displays the values of 10-20-30-40# with A as the separator and # as the end.

Code: 2,22. Illustration of print ( ) function

>>> print ( 10, 20, 30, 40)
10 20 30 40
>>> print ( 10, 20, 30,40, sep=’-’)
10-20-30-40
>>> print(10, 20, 30, 40, sep=’A’, end=’#’)
10A20A30A40#

Another way to use the print function for displaying a message on the prompt is as follows:

>>> print (‘ this is an example of displaying output in Python language’)

For displaying the value of a variable the print function is used as shown in Code 2.23.

Code: 2.23.

>>> a=5
>>> print (‘The value of a is ’, a)
The value of a is 5

In the second statement, we can see that space was added between the string (The value of a is) and the value of a variable a. This is the default value of the separator.

Output Formatting

To make the output more attractive formatting is used. This can be done by using the str. format ( ) method. This method is visible to any string object, see Code 2.24.

Code: 2.24,

>>>a= 10;b = 20
>>> print(‘The value of a is {} and b is {}’.format(a,b))
The value of a is 10 and b is 20

In the above code, the curly braces { } are used as placeholders. We can specify the order in which it is printed by using numbers (tuple index) as shown in Code 2.25.

Code: 2,25.

>>>print(‘I eat {0} and {1} in breakfast’.format(‘egg’,’sandwich’))
I eat egg and sandwich in breakfast
>>>print(‘I eat {1} and {0} in breakfast’.format(‘egg’,’sandwich’))
I eat sandwich and egg in breakfast

Another way to format the string is to use keyword arguments as shown in Code 2.26.

Code: 2.26.

>>> print(‘Hello {name}, {greeting}’.format(greeting=’GoodEvening !’,name-Jack’))
Hello Jack, Good Evening!

Alike old sprintf( ) style used in C programming language, we can format the output in Python language also. The % operator is used to accomplish this as shown in Code 2.27.

Code: 2.27.

>>> x = 12.3456789
>>> print(‘The value of x is %3.2f %x)
The value of x is 12.35
>>> print(The value of x is %3.4f %x)
The value of x is 12.3457

The various format symbols available in Python are given in Table 2.3.

Format Symbol

Purpose

%c Character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase ‘e’)
%E exponential notation (with UPPERcase ‘E’)
%f floating-point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

Input

In the above, only static programs are developed in which the values of variables were defined and used in the source code. However, there must be flexibility for the user to provide his own input values at run time rather than hard code (fixed) values. To accomplish this, Python language provides input ( ) function. The syntax of input ( ) function is

input([prompt]) .

where prompt is an optional string that we wish to display on the screen as a message to the user. It is optional. The input ( ) function can be used as shown in, Code 2.28.

Code: 2.28.

>>>num = input(‘Enter a number:’)
Enter a number: 10
>>>num
’10’

Here, we can see that the entered value 10 is a string, not a number. To convert this into a number, we can use int() or float{) functions as shown in Code 2.29.

Code: 2.29.

>>>int(num)
10
>>> float(num)
10.0

This same operation can be performed using the eval ( ) function. As the name implies, the eval ( ) function also evaluates the expressions, provided the input is a string as shown in Code 2.30.

Code: 2.30.

>>>int(‘2+3’)
Traceback (most recent call last):
File “<string>”, line 301, in runcode
File “interactive input>”, line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘2+3’
>>>eval(‘2+3’)
5

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *