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

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 Fir...

Introduction to Web

Hello everybody,I would like to begin this topic by giving you some idea about "what is web development?" First of all,Let me tell you in simple words that "Web" and "Internet" are not the same.   Internet is the collection of computer's connected together in a network. It connects millions of computers together globally, forming a network in which any computer can communicate with any other computer as long as they are both connected to the  Internet . The World Wide Web (abbreviated  WWW  or the Web)  is  an information space where documents and other web resources are identified by Uniform Resource Locators (URLs), interlinked by hypertext links, and can be accessed via the Internet. Its very easy to learn website development and its very simple. Lets talk about the technologies used to develop a website and also lets get started with a few. There are a lot of languages to develop websites.In order to master all these languages what yo...

An Alcoholic Python Program! My friends' first step in Python programming XD

Hi folks,              Today my dear friends Akshay and Chetan have taken their first step towards learning the Python programming language. This is the program they have written after learning the language for the whole day.              Let us appreciate their interest towards learning Python.                           They sure have a fun way of learning things. I hope you like the program! Thank you!         :) #Basic input operation day = raw_input ( "What is the day today?? \n " ) #list declarations weekday = [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' ] weekend = [ 'saturday' , 'sunday' ] #Implementation of if statements if day in weekday:     ...