Hi guys,
Firstly I would like to thank you all for the giving us the wonderful response for our previous posts! Thank you very much for that. It means a lot to us. :) :D
In my
earlier post, I had blogged about Basic Output in Python programming language.
I hope you have now understood the concept of printing data onto the standard
output device using the print( ) statement.
In
this post, we shall learn how the Python program receives input from the user
or standard input device. Once again, as I had mentioned in the earlier post,
please be careful regarding the version of python you are using.
If
you are using Python version 2.7, the input function to be used is: raw_input( )
Whereas, if you’re using Python
3.5, then the input function to be used is: input( )
Before we begin into
the coding part, let us compare the input operation in Python with the input
operation in C as we had did with the print( ) function in our previous post.
C
|
Python
|
Uses
scanf( ) function.
|
Uses
raw_input( ) or input( ) function.
|
Makes
use of type specifiers like %d, %f etc.
|
Does
not make use of type specifiers.
|
Can
receive multiple inputs using a single scanf( ) function.
|
Can
receive only one input using single raw_input( ) or input( ) function.
|
e.g.
scanf(“%d%f”,&i,&f);
|
e.g.
var = raw_input( )
|
Now let us learn how to
use the raw_input( ) or input( ) funcrions.
The best part about
these functions is that, you can write the required message inside these
functions; unlike any other programming languages.
For example, if you
want to ask the user to enter his/her name, then you can:
name = raw_input(
“Enter your name” ) #Python 2.7
name = input( “Enter your name” ) #Python 3.5
|
Consider a program to
input two numbers and display their sum on the screen:
a = int(input( “Enter
the first number\t” ))
b = int(input( “Enter the second number\t” ))
sum = a + b
print “The sum is: ”,sum
|
As you can see, there
is a slight change in this program compared to the program to Enter your name. The difference is that,
we have encapsulated the input( ) function within the int( ). This is mainly
done for type conversion.
The raw_input( ) or
input( ) functions executes until the user types a value in the console or
shell and Enter or Return is pressed. Later, the input
from the user is scanned and parsed as String which is the default parse
type for this function.
What I am trying to
tell is that, raw_input( ) or input( ) function always considers the input from
the user as string, no matter enter an interger number or a single character.
Hence,
when we want the raw_input( ) or input( ) function to know that we want numbers
as input and not string, we have to make the type conversion by encapsulating
the input( ) function inside the int( ) or float( ).
Here’s a little program
to summarize this tutorial:
name = raw_input(
"Enter your name\n" )
print "Hello ", name print "Let us add two numbers and display their sum" a = int(input( "Enter the first number\t" )) b = int(input( "Enter the second number\t" )) sum = a + b print "The sum is: " ,sum |
Output:
Enter
your name
User
Hello User
Let
us add two numbers and display their sum
Enter
the first number 77
Enter
the second number 108
The
sum is: 185
|
Comments
Post a Comment