<p style='text-indent:20px;'>Linear codes with few weights are widely used in strongly regular graphs, secret sharing schemes, association schemes and authentication codes. In this paper, we construct several two-weight and three-weight linear codes over finite fields by choosing suitable different defining sets. We also give some examples and some of the codes are optimal or almost optimal. Their applications to secret sharing schemes are also investigated.</p>