搜尋此網誌

2026年3月3日星期二

functions in python

parenthesis: IPA[pəˈrenθəsɪs]

parentheses /pəˈrenθəsiːz/

Common Uses of Shift + Enter

- Jupyter Notebook / JupyterLab

- Runs the current cell and moves to the next one.

- Equivalent to executing code in that cell.

- VS Code (with Python extension)

- By default, it can send selected code to the terminal or Python Interactive window.

- Sometimes it runs code in the terminal instead of the interactive window, depending on your settings.

- You can customize this in Keyboard Shortcuts by mapping Shift + Enter to Python: Run Selection/Line in Python Interactive Window.

In Python, the keyword def is used to define a function. Functions are reusable blocks of code that perform a specific task when called

In Python, the return statement is used inside a function to send a value back to the caller. It’s one of the most important ways to control the flow of data in your program.

Examples
Returning a value:
def square(n):
    return n * n

result = square(4)
print(result)  # Output: 16

append something (to something) to add something to the end of a piece of writing

def greet(name):
    print("Hello,", name)

x = greet("Samson")
print(x)  # Output: None

- Use return when you want your function to produce a value that can be used later.
- Skip return when your function’s job is just to perform an action (like printing or updating a file)

- With return → function is like a vending machine (you get something back).
- Without return → function is like a loudspeaker (it just outputs sound, but you don’t get a reusable object).

Mainly from Microsoft Copilot

沒有留言:

發佈留言