Expressions

Expressions are a fundamental concept that's quite overlooked when teaching the basics of programming. I've tried to tutor many students and it seems that nobody really mentions the term when teaching, so here's a short explanation of what expressions are just in case y'all need one :)

Here's a definition of what the term expression means:

An expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a single value.

In Python, an expression can take a variety of forms, including:

# A variable
x = 5

# A constant
y = 10

# A function call
z = max(x, y)

# An arithmetic expression
result = x + y * z / 2

# A comparison expression
is_greater = x > y

# A logical expression
is_true = x > y and y > 0

Most programming languages have similar styles of expressions, even if the syntax is a bit different. Here's some more examples:

Rust:

// A variable
let x = 5;

// A constant
const Y: i32 = 10;

// A function call
let z = x.max(Y);

// An arithmetic expression
let result = x + Y * z / 2;

// A comparison expression
let is_greater = x > Y;

// A logical expression
let is_true = x > Y && Y > 0;

Golang:

// A variable
x := 5

// A constant
const Y = 10

// A function call
z := math.Max(float64(x), float64(Y))

// An arithmetic expression
result := x + Y * int(z) / 2

// A comparison expression
is_greater := x > Y

// A logical expression
is_true := x > Y && Y > 0

Perl:

# A variable
my $x = 5;

# A constant
use constant Y => 10;

# A function call
my $z = max($x, Y);

# An arithmetic expression
my $result = $x + Y * $z / 2;

# A comparison expression
my $is_greater = $x > Y;

# A logical expression
my $is_true = $x > Y && Y > 0;

Expressions can use a variety of operators to combine values and perform calculations. Some common operators include:

  • Arithmetic operators (+, -, *, /, %)

  • Comparison operators (==, !=, >, <, >=, <=)

  • Logical operators (and (&&), or (||), not (!))

  • Bitwise operators (&, |, ^, ~, <<, >>)

  • Assignment operators (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)

  • Unary operators (+, -, not (!), ~)

The syntax and precedence of operators can vary between programming languages. Operator precedence determines the order in which operations are performed when an expression has multiple operators. In programming, different operators have different levels of precedence, which determines their order of evaluation. You'll have to check the programming language's documentation for a better reference :P

Last updated