Python Programming

Using Input in Python

In Programming, Python by Baqir Ali

In order to interact with the application or software, you develop you should be able to take input from users either using GUI or Console, but here we are going to talk about the console input or the input function.

input(msg)

the input function takes values of any type from the keyboard so it should be saved within any variable such as

variable=input(msg)

Remember whenever you use the input function to take input, the input is always in string format you can not use that value directly in any mathematical formula without converting/casting it into integer or float value.

Take you name as input and display it on console in python

name=input("What is you name?")
print("You name is "+name)

Output:

What is you name?Elia
You name is Elia

Take input and convert/cast them to float then using it in a formula.

pi=3.1416
radius=input("What is the radius?")
radius=float(radius)
area=2*pi*radius*radius
print("The area of a circle is %.2f"%(area))

Output:

What is the radius?2.5
The area of a circle is 39.27

Support us by sharing this post