An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Python: Syntax and Semantics
SYNTAX
The syntax of a programming language refers to the order to which different elements are combined to from valid expressions. These elements may be words, operators, or phrases. The syntax of a programming language doesn't have any relationship with the meaning.
An example of a syntax rule for programming is the assignment statement:
print(expression)
This is a valid syntax for using the print statement in python. If we try this without the closed bracket --> print(expression, it'll return a SyntaxError since we didn't follow the correct syntax.
SEMANTICS
Semantics emphasizes the meaning of a program, so it'll be understandable and easy to predict the outcome of execution.Semantics provides significant information needed to understand a program
For example
while <Boolean expression> : <statement>
This is the general form of a WHILE statement in python. For the semantics, when the value of the boolean expression is met, the embedded statement would run.
<statement>while <Boolean expression>:
The code above has no valid meaning because, we placed the statement before the starting the While loop. In this case the syntax is correct, but the semantics is wrong.
Another example is when we try to divide an integer by a string.
For instance
x = 3y = "Richie"print(x/y)
The syntax for the code above is correct, but the semantics is wrong, because it doesn't send a valid meaning. An integer can not be divided by a string.
Original Link: https://dev.to/realrichi3/syntax-and-semantics-5ac9
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To