Friday, 1 May 2020

Python Input() Function

Input( )

Python provides us the facility to take input from user using Input( ) function. This function prompts the user to input a value.

For example :

Name = input("Enter Your Name");
print(Name);

Output will be the name as entered by the user.

Let us take the example again:

name = input("Enter Your Name:");
print("Welcome Mr.",name);

Here suppose the user enter the name as "xyz Kumar" so the variable name will contain xyz Kumar. In such case the output of the program will be:

Output      Welcome Mr. xyz Kumar


 Please note that the input( ) accepts the input only in string format. so, if any mathematical calculation is to be performed, we need to convert the input to integer format. for converting the input to integer format Python provide us a function called :

Program without using int( ) function :

a = input ("Enter 1st Number:");
b =  input("Enter 2nd Number:");
c = a+b;
print("Addition =",c);

In this case if the value of a & b as given by user is 5 & 6 respectively then the output will be 56. As 5 & 6 will be treated as string and not integer.

Program using int( ) function

a = int(input("Enter 1st Number:"));
b = int(input("Enter 2nd Number:"));
c=a+b;
print("Addition=",c);

 In this case if the value of a & b as given by user is 5 & 6 then output will be 11 as we have converted the input from string to integer format.



0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses