When a pin is not connected to anything, it's said to be floating. As shown in the preceding example, it floats around, picking up electrical noise around it from other nearby components, wires connected to it, and charges coming from yourself.
Referring again to the preceding diagram, when the button is pressed, the circuit completes and GPIO 21 gets connected to the ground, and hence we can say for certain that the pin is low. And as we just saw when the button is not pressed, GPIO 21 is floating – it can fluctuate between high and low due to external noise.
This needs to be rectified, and we can do this two ways – with a resistor or in code.