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 (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (112) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (719) Python Coding Challenge (155) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

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