Thursday, December 28, 2017

Input/Output In Python

Python provides a very nice way to provide input and output .Some of the functions like input() and print() are widely used for standard input and output operations respectively.

print('This is my tutorial')
# Output: This is my tutorial

a = 3

print('The value of a is', a)
# Output: The value of a is 3


The actual syntax of the print() function is:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
 
objects :- Values to be printed.
Sep :- The Separator is used between the Values.Default it is Space.
end:- Every Line ends with a default end line.
The file is the object where the values are printed and its default value is sys.stdout.


print(1,2,3,4)
# Output: 1 2 3 4

print(1,2,3,4,sep='*')
# Output: 1*2*3*4

print(1,2,3,4,sep='#',end='&')
# Output: 1#2#3#4&


 In Python , we can even format the output also. This property can be achieved
by the placeholder {}.

print('I eat {0} and {1}'.format('bread','rice'))
# Output: I love bread and rice

print('I love {1} and {0}'.format('bread','rice'))
# Output: I love rice and bread

print("This is {an} first {cn}".format(an="my",cn="Tutorial"))
# Output: This is my first Tutorial
 
 
 

Python Input : 

In Python, we have the input() function to allow the Input . The syntax for input() is
 
input([prompt]) 
 
>>> num = input('Enter a number: ')
Enter a number: 5
>>> num
'5'
 
 
we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions. 
 
Example:- 
a= int(input())

print(a**3)
 

No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...