- 1. Python allows you to change the datatype of a variable. int -> string -> float -> int
a = 1 # int
a = "testing" # string
- 2. To prevent the datatype change in the future, apply "Type Hint" when defining a variable:
a: int
a = 1 # accepted
a = "testing" # Python will reject this assigning
- 3. Apply function Arrow to force the return datatype
def hello(name: str) -> str:
return f"Hello {name}" # this only accept return a string.
def greeting(name: str) -> str:
return True # this boolean will be rejected by Python
It's different from other languages such as C#, Java, and Swift.
- Create a Python environment.
pip install pipenv
pipenv shell #create a new environment
pip i
OR
# create a new virtual environment
python3 -m venv .venv
#activate environment
source .venv/bin/activate
Top comments (0)