Python Input, Output and Import

Python Input, Output and Import:

 

In this section, we will use the build in input () and print () functions for standard input and standard output respectively. We will also use Import keyword to import modules to our program.

 

Python Output using print () function

In Python the “print ()” function is used to output data to the screen. Consider the following example in which we are using “print ()” function to print data to output screen:

CODE

>>> print(“Displaying output using print () function on screen”)

>>> x = 4

>>> print (“Printing the value of x to the output screen, x = “, x)

OUTPUT

Displaying output using print () function on screen

Printing the value of x to the output screen, x = 4

output-screen

The following is the syntax of print () function in Python:

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

In the syntax “objects” is that value which is going to be printed. The “sep” is the separator between the values by default “sep” is the space character. The “end” is used to generate a new line or to move the cursor to a new line. In the syntax file is an object which determines where the value should be printed. The default value for this is sys.stdout which prints the value on output screen.

Consider the following example:

CODE

>>> print(4,6,2,6,7)

>>> print(2,5,6,sep=’*’)

>>> print(1,3,4,6,5, sep=’#’, end=’&’)

OUTPUT

4 6 2 6 7

2*5*6

1#3#4#6#5&

sep

It can be seen from the above example that first print simply prints numbers and value of “sep” is default value that prints spaces between numbers. In the second print statement “sep” has value (*) that prints (*) instead of spaces between the numbers. In the third print statement now the space between characters is a hash symbol (#) and the line is ended by (&). The default value for this is a new line that is the statement is ended with a new line.

 

Output Formatting

The output in Python can be formatted as the user wants to make the output look more attractive and readable. To output can be formatted using str.format () function. The str.format () method can be used with strings and numbers.

Consider the following example:

CODE

>>> x = 3; y = 5;

>>> print (“The value of x is {} and y is {}”.format(x, y))

OUTPUT

The value of x is 3 and y is 5

the-value-of-x-is-3-and-y-is-5

In the above example the surly braces are used as place holders. The first place holder prints the value of x and the second place holder prints the value of y.

The index is used in place holders to specify in which order should be the value is printed. For this consider the following example:

CODE

>>> print (“I love {0} and {1}”. format(‘cake’, ‘pizza’))

>>> print(“I love {1} and {0}”. format(‘cake’, ‘pizza’))

OUTPUT

I love cake and pizza

I love pizza and cake

i-love-pizza-and-cake

In the above example, the index of “cake” is 0 and the index of “pizza” is 1. Hence we get the corresponding output as the indexes are written.

To format the output string keywords arguments can also be used. Consider the following example:

CODE

>>> print (“I love {FastFood} and {desert}”. format (FastFood = ‘Burger’, desert = ‘Fruit Trifle’))

OUTPUT

I love Burger and Fruit Trifle

i-love-burger-and-fruit-trifle

In the above example, fast food is defined to be burger and desert is fruit trifle. In the place holder when we write fast food and desert it will print their corresponding values.

In Python we can also format the output by using format specifiers that we used in C programming language. A format specifier starts with a % symbol. Consider the following example in which we used format specifier to format the output:

CODE

>>> y = 23.45458437

>>> print (“The value of y is %2.5f” %y)

>>> print (“The value of y is %2.2f” %y)

OUTPUT

The value of y is 23.45458

The value of y is 23.45

in-the-above-example

In the above example %2.5f will print 5 digits after the decimal point and %2.2f will print 2 digits after the decimal point.

 

Python Input

Till now we have been using the variables that were defined into the source code. By this our programs were static. We may want that the user can also input the values of the variables. This can be done in Python by using the input () function in the program. The syntax for the input () function is given below:

input ([prompt])

In the above syntax prompt is that string which we want to display on the screen. The prompt is optional, it is only used to increase the readability or for the user that what value he has to enter.

Consider the following example, in which we have used the input () function to input the string that will be saved in some variable:

CODE

>>> x = input(“Enter a number “)

>>> x

OUTPUT

Enter a number 5

‘5’

5

In the above example, x is the variable in which the value entered by the user will be stored; the value entered by the user is of string data type. To convert the value to integer or float data type, consider the following example:

CODE

>>> int(‘5’)

>>> float(‘5’)

OUTPUT

5

5.0

called-eval

In Python there is another function called eval () which is used to convert the string data type into integers etc. The eval () function can also evaluate expressions. Consider the following example in which eval () function is used to evaluate two strings:

CODE

>>> int (‘3 * 4’)

>>> eval (‘3 * 4’)

OUTPUT

Traceback (most recent call last):

  File “<pyshell#79>”, line 1, in <module>

    int (‘3 * 4’)

ValueError: invalid literal for int() with base 10: ‘3 * 4’

12

12

It can be seen in the above example that when we tried to evaluate the string expression in int () function an error had been generated but this is not the case with eval () function.

 

Python Import

It is not a good practice that our program has too many lines, this problem can be solved by dividing the lines of code into modules or small parts. In python modules are the files that have some definition and statement. The file name of modules of Python ends with the extension .py.

The definition of one module can be imported to another module using the key word import.

import keyword is used just like the from keyword to import a part of program of module. For example math is the standard module in python we can import math to perform functions like sine, cosine etc. using the import keyword:

import math

The above command will import the standard module math and we can use the functions defined in math such as cosine function as math.cos(). If we want o import only the cosine function from the math module then we will use from keyword. Consider the following example:

from math import cos

using this command we will able to use the cosine function as cos() simple there will be no need of using math.cos().

Now consider the following example in which we will import pi from the math module using the import and from keyword:

CODE

>>> from math import pi

>>> pi

OUTPUT

3.141592653589793

module-is-imported

When a module is imported from, the python looks at different locations that are defined in sys.path. Consider the following example:

CODE

>>> import sys

>>> sys.path

OUTPUT

[”,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ Lib \\ idlelib’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ python35.zip’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ DLLs’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages’ ]

output