DEV Community

An Vo
An Vo

Posted on • Edited on

Python - Small Notes

  • 1. Python allows you to change the datatype of a variable. int -> string -> float -> int
a = 1 # int  
a = "testing" # string
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

OR

# create a new virtual environment
python3 -m venv .venv

#activate environment
source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay