Since the discovery of a two-dimensional (2D) electron gas at the LaAlO/SrTiO interface, 2D carrier gases at such oxide interfaces and surfaces have attracted great attention because they can host many important phenomena and may produce novel functional devices. Here, we show through first-principles investigations that the surface 2D electron and hole gases in a KTaO (KTO) thin film can be tuned by applying biaxial stress. When increasing compressive in-plane strain, the 2D carrier concentrations decrease down to zero and then a new pair of surface 2D electron and hole gases appears in which the carrier types are switched to the opposite ones. Our analysis indicates that this carrier-type switching occurs because the increasing compressive strain reverses the slope of monolayer-resolved electrostatic potential along the [001] direction. We also present strain-dependent carrier concentrations and effective masses, and explore their thickness dependence. It is further shown that the 2D carrier gases and their strain-driven carrier-type switching across the KTO layer still remain true in the presence of overlayers and epitaxial substrates. These phenomena should be useful to design novel functional devices.