Python provides some built-in functions that are readily available at the Python prompt
Some of the functions like input()
and print()
are widely used for standard input and output operations
The print()
function is used to output data to the standard output device (screen)
>>>print("A string")
A string
var = 15
>>>print("A variable:", var)
A variable: 15
In the second print() statement, a space was added between the string and the value of variable var
This is by default, but can be configured
Data can also be written to a file
The syntax of the print()
function is
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects
is the value(s) to be printed
The sep
separator is used between the values, which defaults into a space character
After all values are printed, end
is printed, which defaults into a new line
The file
is the object where the values are printed and its default value is sys.stdout
(screen)
Output can also be formatted by using str.format()
method of a string str
This method is accessible by string objects
>>> x = 15; y = 20
>>>print('Value of y is {} and x is {}'.format(y, x))
Value of y is 20 and x is 15
Here the curly braces {}
are used as placeholders
Values are assigned to the placeholders with the arguments of format
function, and in the order provided
Keyword arguments can also be used to format the string
>>>print('{name} can be {age} years old'.format(age=16, name='Johnson'))
Johnson can be 16 years old
Strings can also be formatted like the old sprintf()
style used in C programming language
The %
operator is used here with variable type
>>> y = 15.3156789
>>>print('The value of y is %3.2f'%y)
The value of y is 15.32
>>>print('The value of y is %3.4f'%y)
The value of y is 15.3157
A program might require input to be taken from the user
Python provides the input()
function to facilitate this
The syntax for input()
is :
input([prompt])
where prompt
is the string to be displayed on the screen, which is optional
>>> num = input('Enter a number: ')
Enter a number: 20
>>> num
'20'
Here, the entered value 20
is taken as a string, not a number
To convert this into a number, int()
or float()
functions can be used
>>>int('20')
20
>>>float('20')
20.0
This same operation can be performed using the eval()
function
It can evaluate even expressions, provided the input is a string
>>>int('4+5')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '4+5'
>>>eval('4+5')
9
When a program grows bigger, it is often broken 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
The import
keyword is used to do this
For example, math
module can be imported by import math
The definitions inside math
module are then available in current file or scope
Some specific attributes and functions can also be imported from a module, by using the from
keyword
>>>from math import pi
>>> pi
3.141592653589793
While importing a module, Python looks at several places defined in sys.path
, which is a list of directory locations
>>>import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
More locations can also be added to this list
Python File Handling
to know about reading and writing Files