3  Basic Syntax and Expressions

In the realm of programming, understanding the basic syntax and semantics of a language is akin to grasping the fundamentals of a natural language. Syntax refers to the structure or format of the code, dictating how symbols and keywords can be combined to create valid instructions. Semantics, on the other hand, pertains to the meaning behind these instructions — what actions they perform when executed.

3.1 Syntax in Programming Languages

Syntax rules in programming languages define how the code should be written and structured. These rules ensure that the code is readable both by humans and by the computer. Just as syntax in a spoken language dictates how words and phrases should be combined to convey meaning, syntax in programming languages dictates how symbols and keywords can be combined to create valid instructions.

Key elements of syntax include keywords, operators, punctuation, and the overall structure of statements.

Keywords

Keywords are reserved words that have special meanings in a programming language. For example, in English, words like “and,” “or,” “if,” and “then” are used to connect ideas and indicate logical relationships. Similarly, in Python, keywords like if, else, while, for, and def are used to construct control flow and define functions.

Example in English: - “If it rains, then bring an umbrella.”

Example in Python:

if it_rains:
    bring_umbrella()

Operators

Operators are symbols that perform operations on values. In both English and programming languages, operators are used to combine or modify elements.

Example in English: - “Three plus five equals eight.”

Example in Python:

3 + 5

Punctuation

Punctuation marks, such as periods, commas, and parentheses, are used to group and organize sentences in English. Similarly, in programming languages, punctuation marks like parentheses (), brackets [], and braces {} are used to group and organize code.

Example in English: - “He said, ‘Hello!’”

Example in Python:

print("Hello!")

Statements and Expressions

A statement is an instruction that the Python interpreter can execute, while an expression is a combination of values, variables, and operators that can be evaluated to produce another value. Every expression is a statement, but not every statement is an expression.

Example in English: - “The sum of three and five is eight.” (Expression: “Three plus five”)

Example in Python:

3 + 5  # Expression
print(3 + 5)  # Statement
Note

Comments in Python code are denoted by the # symbol and are used to explain and document the code. They are ignored by the Python interpreter and do not affect the execution of the program, making them useful for enhancing code readability and providing context for developers.

Understanding the syntax rules in both natural and programming languages is crucial for effective communication and problem-solving. Syntax ensures that our messages, whether written in English or Python, are clear and interpretable by others, including computers.

3.2 Semantics in Programming Languages

Semantics concerns the meaning of syntactically correct strings of symbols. In other words, while syntax is about the form, semantics is about the function. Semantics ensure that a piece of code not only adheres to the rules of the language but also performs the intended operations.

For instance, the expression 3 + 4 adheres to Python’s syntax rules and its semantics dictate that this expression will evaluate to 7.

To illustrate basic syntax and semantics, let’s look at some simple Python expressions.

Example 1: Arithmetic Operations

Arithmetic operations in Python are straightforward and follow standard mathematical conventions.

# Adding two numbers
3 + 5

# Subtracting two numbers
10 - 4

# Multiplying two numbers
7 * 6

# Dividing two numbers
8 / 2

Each of these expressions follows the syntax rules for arithmetic operators in Python and has clear semantics: they perform addition, subtraction, multiplication, and division, respectively.

Example 2: Comparison Operations

Comparison operations are used to compare values and produce Boolean results (True or False).

# Checking equality
4 == 4

# Checking inequality
5 != 3

# Checking greater than
9 > 7

# Checking less than
2 < 6

These expressions compare two values and return a Boolean value based on the comparison.

Example 3: Logical Operations

Logical operations combine Boolean values and are useful in conditional expressions.

# Logical AND
True and False

# Logical OR
True or False

# Logical NOT
not True

In these examples, logical operators evaluate the Boolean expressions and return a Boolean result.

Understanding the basic syntax and semantics of a programming language is crucial for writing correct and efficient code. Syntax provides the structure, while semantics ensures that the code performs the intended operations.