Basic Console Input and Output: The input() and print() functions
Following our exploration of Understanding None, this article dives into the world of interactive Python scripts. We'll learn how to get input from the user and display output to the console using the input() and print() functions.
📚 Prerequisites
A basic understanding of Python variables and data types.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ How to get input from the user using the
input()function. - ✅ How to display output to the console using the
print()function. - ✅ How to convert user input to different data types.
- ✅ How to create a simple interactive script.
🧠 Section 1: Getting User Input with input()
The input() function is used to get input from the user. It pauses the program and waits for the user to type something and press Enter. The input() function always returns the user's input as a string.
name = input("What is your name? ")
print(f"Hello, {name}!")
In this example, the input() function will display the prompt "What is your name? " and wait for the user to enter their name. The user's input is then stored in the name variable.
💻 Section 2: Displaying Output with print()
We've already used the print() function in previous articles, but let's take a closer look at it. The print() function is used to display output to the console. You can pass it one or more arguments, and it will print them to the screen.
print("Hello, World!")
print("My name is", "Alice")
🛠️ Section 3: Converting User Input
Since the input() function always returns a string, you'll often need to convert the user's input to a different data type, like an integer or a float. You can do this using the int() and float() functions.
age_str = input("What is your age? ")
age_int = int(age_str)
print(f"You will be {age_int + 1} years old on your next birthday.")
🔬 Section 4: Creating a Simple Interactive Script
Now let's put it all together to create a simple interactive script that asks the user for their name and age and then prints a personalized message.
# Get user's name
name = input("What is your name? ")
# Get user's age
age_str = input("What is your age? ")
age_int = int(age_str)
# Print a personalized message
print(f"Hello, {name}! You are {age_int} years old.")
💡 Conclusion & Key Takeaways
You've now learned how to create interactive Python scripts that can get input from the user and display output to the console.
Let's summarize the key takeaways:
input(): Gets input from the user as a string.print(): Displays output to the console.- Type Conversion: You often need to convert user input to a different data type.
- Interactive Scripts: You can combine
input()andprint()to create interactive programs.
Challenge Yourself: Write a script that asks the user for two numbers, and then prints the sum, difference, product, and quotient of the two numbers.
➡️ Next Steps
In the next article, we'll learn about "Comments and Docstrings".
Happy coding!
Glossary (Python Terms)
- Console: A text-based interface for interacting with a computer.
- Input: Data that is entered into a program.
- Output: Data that is produced by a program.