In this python input, output function and import example tutorial, we are going to explain daily useful inbuilt functions in python.
Basically, you are familiar with these Python print function. Because we have already use the print() function in the previous python for displaying data as output in python programs.
But so far we have not used the input () function and import keyword in our previous Python tutorial. In this tutorial, we will explain how to import Python modules using the import keyword and how to use them.
And along with this, we will also show how to input value from the user using the input () function.
Python (I/O) input, output function and import keyword
Let’s explain these input, output functions and import keyword:
Output (Print()) function python
In the python programming, print function is used to display data as output.
Syntax of print function
Example of output (Print()) function
print('This is output example')
Input (Input()) function in python
In the python programming, input() function is used to take some input by user.
Till now we were not getting any value input from the user. In the upper example, we had to insert hard code values in the print function. Now we will tell you how to do the input function. How to take value from end-user
Syntax of input() function
Here, prompt is the string we wish to display on the screen. It is optional.
Example of input function
num = input('Enter a number: ') print(num)
When you run this program you will see a prompt, waiting for you to give input. It will take as many characters as you type until Enter is pressed.
The python import keyword, which is used to import the prebuild module or another module in your python program.
Example of import keyword
For example, we can import the math module by typing in import math.
import math print(math.pi)
Example 2 – import keyword with input
Here we will create a program to calculate squart root of any number in python:
from math import sqrt var = int(input("Enter a Number: ")) sqt = sqrt(var) print(sqt)
In this example, we have calculated the square root of the user input number by math function sqrt(). So We have imported sqrt() function from the math module in our python program.