Python is a high-level, interpreted programming language that is known for its simplicity and ease of use. One of the most commonly used built-in functions in Python is the "input()" function. This function is used to allow user input in a program.
The "input()" function allows the user to enter a value or string from the keyboard, which is then stored in a variable. This variable can then be used in the program to perform various operations.
Syntax: The syntax for the input() function is simple. It only requires the function name followed by a set of parentheses.
Example 1:
|
name = input("Please enter your name: ")
print("Hello, " + name + "!")
|
In this example, the user is prompted to enter their name, which is then stored in the "name" variable. The "print()" function is then used to output a greeting that includes the user's name.
Output:
|
Please enter your name: John
Hello, John!
|
|
age = input("How old are you? ")
age = int(age)
print("In ten years, you will be " + str(age + 10) + " years old.")
|
In this example, the user is prompted to enter their age, which is stored in the "age" variable. Since the input function always returns a string, we have to use the int() function to convert the string to an integer. The program then adds 10 to the user's age and outputs the result.
Output:
|
How old are you? 30
In ten years, you will be 40 years old.
|
One important thing to keep in mind when using the "input()" function is that it always returns a string. If you need to perform any numerical operations with the input, you will need to convert it to an integer or a float using the int() or float() function.
In conclusion, the "input()" function is a powerful tool in Python that allows users to provide input to a program. With this function, developers can create interactive applications that can respond to user input in real-time.