When you need to walk away from your desk, you can log out your account to ensure your data is not accessible. A quicker way to lock your Mac is by putting the display to sleep.
In order to properly lock your Mac when putting the screen to sleep, you’d need to configure your Mac to ask for a password after sleep or when screen saver begins.
Go to System Preferences > Security & Privacy and select the General tab.
Check the box Require password after sleep or screen saver begins and select immediately for the time interval.
If your Mac has an internal optical drive and Eject key, press Control+Shift+Eject together to put the display to sleep.
If your Mac does not have an internal optical drive or an Eject key, press Control+Shift+Power together to put the display to sleep.
When the screen is put to sleep, the Mac continues to run as it is not in sleep mode. Press any button to wake the display and you should be prompted to enter your password to unlock the screen.
You can also setup a Hot Corner to instantly putting the display to sleep. Go to System Preferences > Mission Control and click on the Hot Corners… button at left bottom.
A sheet will drop down allowing you to set the functions of the four active corners of the screen. The picture above shows a setup with the right bottom corner assigned with the Put Display to Sleep function. You can choose any corner. Click the OK button to save the changes.
Once a corner is setup with Put Display to Sleep, OS X will put the screen to sleep when you mouse your cursor over the active corner.