Electronic structures and optical properties of doped single-walled carbon nanotubes are investigated by the first-principles calculation. B- and N-doped systems have the most stable configuration with direct bang gaps. The covalence between the doped atom and carbon atom is weakened. There is a "blue shift" phenomenon in the absorption spectrum and a "red shift" phenomenon in the reflection spectrum. The research opens up a window for the design and preparation of microelectronics and optoelectronic devices. In this contribution, electronic structures and optical properties of pure and doped single-walled carbon nanotubes (SWCNTs) are investigated by the first-principles calculation. It is found that the B- and N-doped systems have the lowest binding energies of -31.41eV and -30.97eV, respectively, indicating that the configurations are relatively stable. Interestingly, the indirect bandgap is transforming into a direct bandgap in the two systems, which has a good development prospect in the field of light-emitting devices. The charge difference density diagrams show that the electron deletion around all doped atoms is weakened, indicating that the covalence between the doped atom and carbon atom is weakened, which is consistent with the population analysis result. In the absorption spectrum, there is a "blueshift" phenomenon. In the reflection spectrum, the six doping systems show an obvious "redshift" phenomenon. In addition, the polarization ability of all doping systems has become stronger. Also, due to the different changes in the peak values of the imaginary part of the dielectric function, the doping of Al, Ga and P improved the photoresponse of the low-energy region, while the doping of B, N and As weakens it.