User Input (input())
The input() function in Python is a built-in function used to accept input from the user through the keyboard.
It makes programs interactive and dynamic.
What is input() Function in Python?
The input() function is used to take data or information from the user while the program is running.
The value entered by the user is stored inside a variable and can be used later in the program.
Why User Input is Important
User input is important because it helps programs:
- interact with users
- accept dynamic values
- perform real-time calculations
- create personalized applications
- improve user experience
Without user input, programs will always work with fixed values only.
Real-Life Analogy
User input is like a teacher asking a question and a student giving an answer.
- Teacher asks → “What is your name?”
- Student answers → “Ravi”
Similarly, a Python program asks and the user responds.
Syntax
variable_name = input("Message")
Usually we add a message:
input("Enter your name: ")
How input() Makes Programs Interactive
The input() function allows different users to enter different values.
For example:
- one user can enter Rahul
- another user can enter Akhil
The same program works for everyone dynamically.
Important:
By default, input() stores data as a string (text).
Example of User Input
name = input("Enter your name: ")
print("Hello", name)
Output
Enter your name: Ravi
Hello Ravi
Example - 1
age = input("Enter your age: ")
print("You are", age, "years old")
Output:
Enter your age: 12
You are 12 years old
Taking Number Input (Using Type Conversion)
Since input() stores text, we use int() or float() for numbers.
Example:
num = int(input("Enter a number: "))
print(num + 5)
Output:
Enter a number: 10
15
int() converts text into a number.
Practical Example
name = input("What is your name? ")
print("Welcome", name)
This is like asking a question and getting an answer.
Example
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("Next year age:", age + 1)
Output
Enter your name: Ravi
Enter your age: 12
Hello Ravi
Next year age: 13
Common Mistake (Important)
Wrong:
age = input("Enter age: ")
print(age + 1)
Error
Because input() stores a string.
Correct Way
age = int(input("Enter age: "))
print(age + 1)
Common Beginner Mistakes
| Mistake | Problem |
|---|---|
| Forgetting int() conversion | Concatenation instead of addition |
| Mixing string and integer | Raises error |
| Incorrect indentation | Syntax problems |
| Missing quotation marks | Invalid syntax |
Why User Input is Important
User input in Python helps:
- Make programs interactive
- Accept information from users
- Perform calculations
- Build real applications
➤ Summary
input()is used to take user input- Input is stored as a string by default
- Use int() or float() for numbers
- Makes programs interactive and dynamic