How do I assign a region to programmatically created block?
That's expected. You can do it from code if needed. The following are the steps to follow.

Settings definition is done as in block.block.test_block.yml. Place that file, after editing it, in my_module/config/install and you're set. A quick hack is managing the block settings manually in a local environment and export them from /admin/config/development/configuration/single/export.

Import your settings, which can be done using code similar to the following one.
use Drupal\Core\Config\FileStorage;

$config_path = drupal_get_path('module', 'my_module') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$block_config = 'block.block.test_block';
$config_storage-&gt;write($block_config , $source-&gt;read($block_config));

Block settings are theme dependent.


For dynamic allocation of blocks, you can load and alter the block entity like this.
use Drupal\block\Entity\Block;

$block = Block::load('test_block');
$block-&gt;setRegion('my_region');
$block-&gt;save();