Imagine a conversation where only one person talks—that wouldn’t be very interactive! In Python, the input() function allows the program to "listen" to the user, making it interactive.
With input(), a program can ask for information, like a name or age, and store it in a variable. This makes programs more dynamic and responsive.
💡 Analogy: input() is like a waiter taking an order—it asks a question, waits for an answer, and then processes it.
By the end of this lesson, you will be able to:
Use the input() function to collect user input.
Convert user input into different data types.
Understand why user input is useful in interactive programs.
input() function – A built-in function that allows a program to take input from the user as a string.
Variables – Named storage locations in memory that hold data, such as a user's name or age.
String concatenation – The process of combining two or more strings using the + operator (e.g., "Hello " + name).
Type conversion – Changing one data type to another, such as converting a string "25" into an integer 25 using int().
Interactive programs – Programs that ask users for input and respond dynamically based on the provided information.
Example
name = input("What is your name? ")
print("Hello, " + name + "!")
Forgetting type conversion: age = input("Enter your age: ") stores a string, so age + 5 ❌ will cause an error. Use int(age) + 5 ✅.
Forgetting to store input: Just writing input("Enter name: ") without assigning it to a variable means the input is lost.
🔗 Practice on W3Schools: User Input Tutorial – Experiment with taking user input and handling different data types. An easy way to add two user-provided numbers together is shown here.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
1️⃣ Basic Challenge:
Write a program that asks the user for their favorite movie and prints a response like "Wow! I love [movie] too!".
2️⃣ Extended Challenge:
Modify your program to also ask for the user's favorite actor and include both in the response.
3️⃣ Advanced Challenge:
Ask the user for their birth year, convert it to an integer, and calculate their age in the current year.
Write the example from this lesson on paper. Make sure to include:
✅ The input() function to get user input
✅ Storing the input in a variable
✅ Printing the response using string concatenation
Read the example code before starting your handwritten notes!