9. Input Output in Python
9. Input Output in Python
com
S.No. Topics
Page 1 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
input ([Prompt])
Prompt is the string we may like to display on the screen prior to taking
the input, and it is optional.
For example,
Page 2 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
When we execute the above program, the program doesn’t add the two
numbers together; it just puts one right after the other as 1020
Python offers two functions int ( ) and float ( ) to be used with input ( ) to
convert the values received through input ( ) into int and float types.
In Python, we can accept two or three values from the user in one input()
call.
Page 4 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
The print ( ) function allows you to print out the value of a variable and
strings in parenthesis.
The general form of print ( ) function is:
print ( )
print (value/expr)
print (value, ...., sep=' ', end='\n', file=sys.stdout, flush=False)
>>> N2 = 30
>>> Sum = N1 + N2
50
50
>>> print (Prog, Ver) # prints two values separated with comma (,)
Python 3.6
The print() function contains number of options. You can use either
one, two or all at once.
For example:
− Print a message.
The sum is
My Python
− Print a value, message or both message and values and vice versa.
>>> print ("The sum is", Sum) # prints a message with a value
The sum is 50
>>> print (Sum, "is the sum of", N1, "and", N2) # print is alternate way
The sep separator is used between the values. It defaults into a space
character. For example:
>>> print (10,20,30,40,sep='*') # prints a separator *
10*20*30*40
Page 6 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
After all values are printed, end is printed. It defaults into a new line.
>>> print (10, 20, 30, 40, sep = '*', end = '@') # A new end is assigned @
10*20*30*40@
Let us take other examples, where a print() function can output multiple
things at once, each item separated by a comma.
# The following code does not work and product Invalid Syntax because
the comma (,) is inside the quotation marks and not outside.
>>> print ("My name is Ahil", "Class-XI A.") # Print with space separator.
>>> print ("My name is Ashaz" + "Class-XI B.") # Print without separator.
My name is AshazClass-XI A.
Page 7 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
% width.precision type-char
For example:
a=2.45567
print("%.2F" %a)
Page 8 of 8