input, output and import in python
Python provides two built-in functions print() and input() to perform I/O tasks. These functions and there usages are listed below.
print() function
The print() function in python is used to output data, the output can be a string, variable or a number.
Example 1
print('This will be displayed on the output screen.')
Output
This will be displayed on the output screen
Example 2
a=7
print('Value of a=',a)
print('Value of a=',a)
Output
Value of a=7
As the second example, we can output values that are stored in a variable by just inserting the variable name without quotes and seperaated by a comma (,).
Actual syntax of print() function
print('object(s),sep=separator,end=end,file=file,flush=flush')
- objects are the values to be printed
- separator specifies how to separate the objects (optional).
- end specifies what to print at the end (optional).
- file is the system specific parameters and functions (optional), default is sys.stdout.
- flush is a boolean specifying the output is flushed or not by representing it with True or False (optional).
Example
print(1,2,3,4)
print(1,2,3,4,sep='-')
print(1,2,3,4,sep='#', end='$')
print(1,2,3,4,sep='-')
print(1,2,3,4,sep='#', end='$')
Output
1 2 3 4
1-2-3-4
1#2#3#4$
This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python.
1-2-3-4
1#2#3#4$
input() function
This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python.
Syntax:
input([prompt])
Example
num=input('Enter a number :')
print(num)
print(num)
Output
Enter a number : 23
23
23
Import Keyword
Python modules can get access to code from another module by importing the file/function using import.When our program grows bigger, it is a good idea to break it into different modules. A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py. Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this.
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.
Example
import math
print(math.pi)
print(math.pi)
Output
3.141592653589793
Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword.
Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword.
Example
from math import *
print(pi)
print(factorial(5))
print(pi)
print(factorial(5))
Output
3.141592653589793
120
Here we imported all the functions and constants by using the * symbol. We can also specify particular name of function or constant for importing that particular function or constants only.
120
Here we imported all the functions and constants by using the * symbol. We can also specify particular name of function or constant for importing that particular function or constants only.
⇐Prev
Next⇒
Comments
Post a Comment