Input From User
Nerd Cafe
1. What is User Input in Python?
Definition: User input allows the program to take data from a human using the keyboard while the program is running.
In Python, we use:
input("Prompt message")Practical Note:
The input is always stored as a string by default.
You must convert it to numbers (if needed) using
int(),float(), etc.
2. Basic Example
name = input("What is your name? ")
print("Hello,", name)Output:
What is your name? Mr Nerd
Hello, Mr Nerd3. Input with Conversion: Numbers
Integers
Practical Note:
int()will cause an error if user enters non-integer liketwenty.Use
try-exceptfor error handling (covered below).
Float (Decimal)
4. Multiple Inputs in One Line
Example:
Output:
Practical Note:
.split()breaks input into list based on space.You can also split by comma
split(',').
5. List Input for ML or Data Science
Often in ML we need to input feature vectors.
Output:
Machine Learning Insight:
This method is commonly used to input values into a trained model for prediction:
7. Advanced Tip: Loop Until Valid Input
Summary Table
Basic text input
name = input()
Always string
Number input
num = int(input()) or float(input())
Convert types
Multiple inputs
a, b = input().split()
Use .split()
List for ML
[float(x) for x in input().split(',')]
For feature vector
Error handling
try-except
Catch invalid inputs
Keywords
input,output,string,integer,float,split,try,except,loop,while,if,elif,else,function,list,tuple,dictionary,conversion,error,prompt,feature, nerd cafe
Last updated