python


Python I/O and Import

While working in a programming language, you may require to input some data and then use instruction to output a value. Also, you might work on files that may need to be opened, closed, read, or write. Thus, you should have knowledge of input and output in Python. In this part of the Python tutorial, you learn how to input and output data.

The input Function

The input() function is used to get data from the user in Python Command Line programs.

Syntax

<return> = input("<prompt>")

Where prompt is a string that will be display for the user to view which should provide some kind of indication of the usage and type of data you are expecting and return is the returned value from the function. e.g.

<<<i = "Hello coders!! Welcome to CODEMISTIC"
<<<x = input("enter a value:")
enter a value: i
<<<print i
Output
Hello coders!! Welcome to CODEMISTIC

Python Import

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.
import module_name
When import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function are then reflected in the output of the initial code.

For example, we can import the math module by typing the following line:

import math

We can use the module in the following ways:

import math
print(math.pi)

Output

3.141592653589793

We can also import some specific attributes and functions only, using the from keyword. For example:

>>> from math import pi
>>> pi
3.141592653589793