Input, output, and comments are fundamental concepts in any programming language. They allow your program to interact with the user and help you, the developer, to write and understand your code.
Output: The print() Function
The primary way to produce output in Python is by using the built-in print() function. Its main job is to display values to the console or terminal.
- Displaying Values: You can pass almost any Python object to print()—strings, numbers, lists, etc.—and it will display a string representation of that object.
- Multiple Items: You can pass multiple items to a single print() call, separated by commas. By default, it will separate them with a single space.
- Controlling the Separator and End: print() has optional arguments like sep (to change the separator) and end (to change what's printed at the very end, which is a newline character \n by default).
# --- 1. Output with the print() Function ---
# This is a single-line comment. It's ignored by Python.
print("--- 1. Output Examples ---")
# The print() function displays text or variables to the console.
print("Hello, World!")
# You can print variables of any data type.
name = "Neha"
age = 29
print("User's Name:", name) # print() automatically adds a space between items.
# Using an f-string is a modern and powerful way to format output.
print(f"User's Age: {age}")
# Using the 'sep' and 'end' arguments.
print("apple", "banana", "cherry", sep=" | ") # Use ' | ' as a separator.
print("This is the first line.", end=" ") # Prevent the default newline.
print("This is the second line.")
Input: The input() Function
To make a program interactive, you need to get input from the user. The built-in input() function is used for this.
- How it Works: When input() is called, the program execution pauses and waits for the user to type something into the console and press Enter.
- The Prompt: You can provide a string to the input() function, which it will display as a prompt to the user.
- Crucial Point: It Always Returns a String: No matter what the user types—a number, a sentence, etc.—the input() function will always return that value as a string (str). If you need to perform mathematical calculations with the input, you must explicitly convert it to a numeric type (like int or float).
# --- 2. Input with the input() Function ---
print("\n--- 2. Input Examples ---")
# The input() function pauses the program and waits for user input.
# It always returns the input as a string.
user_name = input("Please enter your name: ")
print(f"Hello, {user_name}!")
# You must convert the input if you need a number.
user_age_str = input("Please enter your age: ")
# Use int() to convert the string to an integer.
try:
user_age = int(user_age_str)
print(f"You will be {user_age + 1} on your next birthday.")
except ValueError:
print("That's not a valid age! Please enter a number.")
Comments
Comments are notes in your code that are ignored by the Python interpreter. Their purpose is to explain the code to human readers (including your future self). Good comments make your code easier to understand and maintain.
There are two main types of comments in Python:
1. Single-Line Comments: These start with a hash symbol (#). Everything from the # to the end of that line is considered a comment.
2. Multi-Line Comments (Docstrings): While not technically comments, triple-quoted strings ("""...""" or '''...''') are often used for multi-line comments. When a triple-quoted string is the very first thing in a function or class definition, it becomes a docstring, which is a special attribute that explains the purpose of that function or class.
# --- 3. Comments ---
# Comments are essential for explaining your code.
# This is a single-line comment.
"""
This is a multi-line comment, also known as a docstring.
It's created using triple quotes and can span multiple lines.
It's often used at the beginning of a function to explain what it does.
"""
def calculate_area(length, width):
"""
Calculates the area of a rectangle.
Args:
length (int/float): The length of the rectangle.
width (int/float): The width of the rectangle.
Returns:
int/float: The calculated area of the rectangle.
"""
return length * width
# You can see the docstring of a function using help().
# help(calculate_area)