EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

US

Python Input & Output Statements

Python Input & Output Statements

Python Input & Output Statements

The date given to computer to process is called as Input and when Computer Process the input date and provide result, that result is called as Output.

When we need to provide input to python programs , Python provides some statements called Input statements and when it want to display some output , it can also be done by Output statements.

For Output statements Python uses Print () functions, which can be used in verity of ways to display programs output. Throughout this Python Course, we will see and learn different methods of Print () functions to display results. So currently we are not going to discuss more on output Statements.

Let’s talk about Input statements

Input Statements

For input statements let’s learn this using some examples

Python uses Input () functions to take input from keyboard. This function takes value from keyboard and returns as a string.

str = Input (“Enter your Company Name :”)                                                                                                              Print (str)                                                                                                                                                                  Output========================                                                                                                                    Enter your Company Name: DCLESSONS                                                                                                              DCLESSONS

As we Know that Value returned from Input () is String, But this Value can also be converted to Int or Float.

Let’s see an Example:

str = Input (“Enter Any Number :”)                                                                                                                            x = int(str)                                                                                                                                                                Print (x)                                                                                                                                                                    Output======================                                                                                                                      Enter Any Number: 1001                                                                                                                                          1001

The above example can also be re-written using Int () before Input () Function. Let’s see an example.

str = int(Input (“Enter your Company Name :”))                                                                                                        Print (str)                                                                                                                                                                  Output=========================                                                                                                                  Enter Any Number: 1001                                                                                                                                          1001

Hands on Python Programs

Program1: Write a Program to accept a character as a String.

str = input("Enter a Character: ")                                                                                                                              print("you have entered:" +str)                                                                                                                                Output: Enter a Character: abcd                                                                                                                              you have entered: abcd

In the above program , Input statement takes the character as a string, but if we need to take only character, then a programmer should use the index as str [0], where 0 is the 0th character of that string.

Lets understand it from below program

str = input("Enter a Character: ")                                                                                                                              print("you have entered:" +str[0])                                                                                                                            Output:===========================                                                                                                          Enter a Character: abcd                                                                                                                                          You have entered: a

Program 2: Write a program to accept two number from keyboard and prints its sum.

x = int (input ("Enter a first number: "))                                                                                                                    y = int (input ("Enter a second number: "))                                                                                                              sum = x+y                                                                                                                                                              print ("sum of two number is :", sum)                                                                                                                      Output:===========================                                                                                                            Enter a first number: 10                                                                                                                                            Enter a second number: 20                                                                                                                                      Sum of two number is: 30

To accept the multiple values in same line, use can use the following for loop with input function as shown below.

x,y = [int(x) for x in input(“Enter two numbers: “).split()]

In above commands, user will provide two values and they will be accepted as string. These string are divided in a space is found by split () method. Here we are getting two strings as an element of list , and these strings are read by for loop and converted in to integers by int() function and then these integers are finally stored in to a and b.

The split () function by default splits the values where a space is found.


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.