2

Any idea what colons is supposed to be doing in this case? I just stumbled upon this and saw that it doesn't produce any error, I got curious to see if it did anything. So far I don't see colons doing anything in this case.

Can somebody enlighten me?

import numpy as np
x = np.arange(1, 10)
x
output: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

x:5
x
output: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

enter image description here

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43

1 Answers1

3

As the comments say you might be looking for slice notation but actually this is no longer a SyntaxError. You are giving variable x a type annotation of 5 (which doesn't really make any sense to actually do).

You can look in the __annotations__ of your module to see it has been added there e.g.

print(__annotations__)
x : 5
print(__annotations__)

will print

{}
{'x': 5}

For more details on variable annotations see PEP 526.

tomjn
  • 5,100
  • 1
  • 9
  • 24