This one-liner returns 1 (installed) or 0 (not installed) for the 'nano' package..
$(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed")
even if the package does not exist / is not available.
The example below installs the 'nano' package if it is not installed..
if [ $(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
apt-get install nano;
fi