What is the difference between the 'print' and 'return' statements in Python?

0301

In Python, the print and return statements serve different purposes:

  1. print Statement:

    • Purpose: Outputs data to the console.
    • Usage: Used for displaying information to the user.
    • Example:
      def greet(name):
          print(f"Hello, {name}!")
      greet("Alice")  # Outputs: Hello, Alice!
  2. return Statement:

    • Purpose: Exits a function and optionally passes a value back to the caller.
    • Usage: Used to send data from a function back to where it was called.
    • Example:
      def add(a, b):
          return a + b
      result = add(3, 5)  # result is now 8

In summary, use print to display information and return to send values from functions. If you have further questions or need examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!