Skip to main content

Intro to Python Programming - 3. Basic Input

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

Popular posts from this blog

Intro to Python Programming - 2. Basic Output

Hello everyone!           Considering the lot of requests I received from my dear friends and viewers, I shall now start posting step by step tutorials for mastering Python programming. I proceed with the assumption that you have a basic foundation of the C programming language . If not much of it, at least having some idea about the type specifiers such as ‘%d for integer, %f for floating point variables etc.’ and also, some basic ideas about data types such as int , float , char etc. would be of great help. :)           Before we begin with the coding part of python, I want you to understand a few basic concepts, like: 1.      Python is scripting language. 2.      Python programs do not make use of statement terminators like semicolons ‘;’ as in C, C++ and Java. 3.      A basic Python program does not require any header file. e.g. We use # include < stdio.h > in C to include standard I/O library We import libraries in Python only for some using some special

Printing the text horizontally in Python Programming language

Hi guys,         We know that the "print" function is used to print the data to the standard output device in Python Programming language. But, the print function automatically adds a newline character every time we cal it.        For example, consider a program segment to display numbers from 1 to 5:                                                                            for n in range ( 1 , 6 ):                                     print n           The output of the code will be:                               1                               2                               3                               4                               5   But what if we want the output to be something like:                 1 2 3 4 5    How do we go about it?     The answer to this is indeed very simple. The key is to add a "," after the variable  at the end of the print statement. Consider the following code to print the even a

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Firstly