GeeksBro

FREE EXCLUSIVE TIPS

Python Tutorials, Tricks & Tips, Projects, etc..,

Python Input, Output and Import Functions

| August 02, 2018 | TAG: python | No comments:

In this tutorial, you are going to learn about python input(), output() built-in functions. And also, you will learn how to import modules use them in programs.

Python Input, Output

Python has a large number of built-in functions. Most of them are mostly used in the programs. Now, we discuss about print(), input() and import functions.

python input output

Let's start with input() function.....

Python Input() function

If you want to take the input from the user in python. You can use input() function. Let's see the syntax.....

input([message])

input tells to take the input from the user and message is to display on the screen to the user.

input() function takes the input from the user as a string. See the example.....

>>> input('Enter a number:-')
Enter a number:-2
'2'

Python input() function takes the input as a string. To convert it into int just use the built-in function int() to convert. See the example.....

>>> a = input('Enter a number:- ')
Enter a number:- 2
>>> int(a)
2
>>> float(a)
2.0

That's how you convert the input string number to int or float.

Python has another function eval() which evaluates a string expression which consists of integers. See the example.....

Expressions like:- '2 + 7', '4 - 3', '7 * 7' are evaluated using eval() function.

>>> eval('2' + '7')
27
>>> eval('2 + 7')
9
>>> eval('4 - 3')
1
>>> eval('7 * 7')
49

Python print() function

Python uses the print() function to output the result to display. See the example.....

>>> print('This is the output')
This is the output
>>> a = 5
>>> print(a)
5
>>> a = 'geeksbro'
>>> print(a)
geeksbro

The actual syntax of 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. Its default value is a space character.

The end printed after the above to parameters. Its default value is a new line character '\n'.

The file is to tell where the values are printed. Its default value is sys.stdout which is the screen.

See the example to demonstrate all the optional parameters.....

>>> a = 7
>>> print('the value of a is', a)
the value of a is 7
>>> print('geeksbro', 7)
geeksbro 7
>>> print('geeksbro', 7, sep='@')
geeksbro@7
>>> print('geeksbro', 7, sep='#')
geeksbro#7
>>> print('geeksbro', 7, sep='/', end='@')
geeksbro/7@

Python Output Formatting

String Formatting is comes out when to want to print the result in an attractive way.

This can be done using the str.format() method. See the example.....

>>> a = 5
>>> b = 7
>>> print('The value of a is {} and the value of b is {}'.format(a, b))
The value of a is 5 and the value of b is 7

Whenever you want to use a value in a string, simply put {} there and provide the variable or value in the format() function.

You can also have a choice to arrange them in an order you like. See the example.....

>>> print('I am {0}. I am teaching {1}'.format('GeeksBro', 'Python'))
I am GeeksBro. I am teaching Python.
>>> print('I am {1}. I am teaching {0}'.format('Python', 'GeeksBro'))
I am GeeksBro. I am teaching Python.

You can also use the keyword arguments in string formatting. See the example.....

>>> print('Hello, I am {name}. Welcome to {url}'.format(name = 'GeeksBro', url = 'https://www.geeksbro.com'))
Hello, I am GeeksBro. Welcome to https://www.geeksbro.com

You can also use the string formatting as C programming with the help of %. See the example.....

>>> print('Use %d for Integers' %5)
Use 5 for Integers
>>> print('Use %f for Floats' % 7.45)
Use 7.450000 for Floats
>>> print('Use %s for Strings' % 'GeeksBro')
Use GeeksBro for Strings

Python import

If you have to write a lot of code in Python. You can use the modules concept in Python.

A module is a Python file with extension .py which consists of statements, definitions and functions.

You can import a modules definitions into another module using the import keyword. See the example.....

Program

import math
print(math.pi)

output

3.141592653589793

In the above example, we imported the math module and prints the pi value using the math module.

You can also have a choice to import a single method. See the example.....

Program

from math import pi
print(pi)

output

3.141592653589793

In the above example, we imported the pi method from the math module.

To see the all methods available in module run the following code.

Program

import math
print(dir(math))

Output

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

Above program, prints all the available methods in math module.

If you want to import all the methods at a time from a module, use the following code.

Program

from math import *
print(pi)
print(sqrt(9))

Output

3.141592653589793
3.0


Post a Comment