Tags
python
Asked 2 years ago
19 Sep 2021
Views 461
Milton

Milton posted

What does 3 dots mean in Python?

What does 3 dots mean in Python?
jassy

jassy
answered Feb 27 '23 00:00

In Python, the three dots (...) can be used as a placeholder to indicate an incomplete or unfinished code block . This is often referred to as the "ellipsis" syntax.

For example, in an interactive Python shell, entering three dots on a new line and pressing enter will start a new block and indicate that more input is expected:

def my_function():
...     # More code expected here
...

In this example, the three dots indicate that the my_function definition is not yet complete, and that more code is expected on the next line.

In addition to its use as a placeholder, the ellipsis syntax can also be used as a literal value, representing a placeholder or unknown value in a data structure or algorithm. For example, in a numpy array, the ellipsis can be used to represent multiple dimensions:



>>> import numpy as np
>>> a = np.zeros((3, 4, 5))
>>> a[..., 1].shape
(3, 4)


In this example, the ellipsis (...) represents all the dimensions of the numpy array except for the second dimension, allowing us to extract a slice of the array along that dimension.

It is worth noting that the ellipsis syntax is not used in Python to represent variable-length arguments , as it is in some other programming languages (such as JavaScript). In Python, variable-length arguments are represented using the *args and **kwargs syntax.
Post Answer