Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

how do you define a function in python

  1. User

    how do you define a function in python

  2. Assistant

    You can define a function in Python using the def keyword followed by the function name, parentheses, and a colon. The function code is indented under the function definition. Here's an example:

    def greet(name):
        print("Hello, " + name + "!")
    
    greet("Alice")  # Output: Hello, Alice!
    greet("Bob")  # Output: Hello, Bob!
    

    In this example, the function greet takes a parameter name and prints a greeting using that name. When called with argument "Alice" and "Bob", it outputs "Hello, Alice!" and "Hello, Bob!" respectively.