Like human languages, programming languages have rules and components:
-
Lexis: The set of all valid words or symbols in a language. In Python, this includes keywords (like
if
,for
,def
), operators (+
,=
,>
), and identifiers (variable names). -
Syntax: The rules that govern how words and symbols can be combined to form valid statements or expressions. For example, in Python, an
if
statement must end with a colon (:
), and the code block below it must be indented.if x > 5: print("Greater")
(Correct syntax) -
Semantics: The meaning of syntactically correct statements. A program can be syntactically correct but semantically wrong if it doesn't do what the programmer intended. For example, using
+
to add two numbers has a clear semantic meaning. If you try to add a number and a string without proper conversion, it might be a syntax error or a semantic error (a TypeError in Python) depending on the language.