Distance-based models compare numeric values, and penalized linear models compare coefficient sizes. A distance measured in miles and a count measured in single digits can influence those calculations differently because of their units. StandardScaler subtracts the training mean of each input and divides by its training scale.
Fit the scaler on training inputs, then use that fitted object on every later partition. Fitting a fresh scaler to the test set changes the meaning of its coordinates and leaks information about its distribution. You will handle an input that is constant as well: scikit-learn maps it to zero without producing an infinite value. These exercises keep both transformed arrays visible so you can inspect the effect on familiar and new rows before putting scaling inside a model pipeline.