Welcome to Our Tutorials! Let`s continue.
<What You'll Get Here
We continue with the next lesson.
We study some more python. Ready to dive in? 🚀
Input and output
Python Tutorial 6: Basic Input and Output
Program Description
Programs need to interact with the user. This involves taking input from the user (like text typed on the keyboard) and providing output back to them (like displaying messages on the screen). In Python, the `input()` and `print()` functions are your primary tools for basic input and output.
Illustration
(Imagine an illustration showing a simple command-line interface or a chat bubble, with arrows indicating input going into the program and output coming out.)
Tutorial Sections: Talking to Your Program!
Section 1: The `print()` Function
Goal: Display output to the user.
Explanation: The `print()` function is used to display messages or variable values on the console or terminal. You can print strings, numbers, variables, and more.
Code:
# Printing different types of output
print("Hello, Python!")
print(123)
name = "Charlie"
print("My name is", name) # Printing multiple items separated by a comma
age = 30
print(f"I am {age} years old.") # Using an f-string for easy formatting
What it means: We demonstrate printing a simple string, a number, multiple items separated by a comma (which adds spaces automatically), and using an f-string to embed a variable's value within a string.
Section 2: The `input()` Function
Goal: Get input from the user.
Explanation: The `input()` function pauses your program and waits for the user to type something and press Enter. It reads the user's input as a string and returns it. You can optionally provide a prompt message inside the parentheses.
Code:
# Getting input from the user
user_name = input("Enter your name: ")
print(f"Nice to meet you, {user_name}!")
What it means: The program prompts the user to enter their name. Whatever they type is stored in the `user_name` variable, and then a greeting is printed using that name.
Section 3: Converting Input Data Types
Goal: Convert input strings to numbers or other types.
Explanation: Remember that `input()` always returns a string. If you need the input as a number (integer or float) or another data type, you need to explicitly convert it using functions like `int()`, `float()`, etc.
Code:
# Getting numeric input and converting it
age_str = input("Enter your age: ")
age_int = int(age_str) # Convert the input string to an integer
print(f"You are {age_int} years old.")
print(type(age_int)) # Verify the data type is now int
# Example with float
price_str = input("Enter the price: ")
price_float = float(price_str) # Convert to a float
print(f"The price is: ${price_float}")
print(type(price_float))
What it means: We get input as a string and then use `int()` and `float()` to convert it to numeric types so we can perform calculations or comparisons with numbers.
Excellent! You now know how to make your Python programs interactive by taking input from the user and providing output. Practice creating simple programs that ask for information and then display a response!