Variables

Python 3.6 introduced a syntax for annotating variables in PEP 526and we use it in most examples.

  1. # This is how you declare the type of a variable type in Python 3.6
  2. age: int = 1
  3.  
  4. # In Python 3.5 and earlier you can use a type comment instead
  5. # (equivalent to the previous definition)
  6. age = 1 # type: int
  7.  
  8. # You don't need to initialize a variable to annotate it
  9. a: int # Ok (no value at runtime until assigned)
  10.  
  11. # The latter is useful in conditional branches
  12. child: bool
  13. if age < 18:
  14. child = True
  15. else:
  16. child = False