How to programmatically assign user roles
Drupal core user.module provides a simple function to add/remove roles to/from a user. See user_multiple_role_edit().

$uid = 12345; // The user ID of the account you want to change.
$role_id = 4; // The numeric role ID you want to add.
$role_id_rm = 3; // The numeric role ID you want to remove.

user_multiple_role_edit(array($uid), 'add_role', $role_id);
user_multiple_role_edit(array($uid), 'remove_role', $role_id_rm);