Expressions, Statements, and Operators


Statements

What are statements?

Every statement you write is a separate instruction you’re giving the computer.

In Python, statements do not end with any symbols, but in other languages such as Java, statements usually end with a semicolon (;)

In stored programs, you can write statements and, when the program runs, the computer evaluates each statement individually AND in order!

We explain what we want a program to do using statements. For example, statements are used every time we declare or initialize a variable, or when we assign a new value to a variable. We also use statements in other ways, such as when we print things.

Example

The printed output of this program looks like:

‘I’m not superstitious, but I am a little stitious.’

– Michael Scott

To get a better idea of what’s really happening in a statement, let’s look at the first print statement from our example: print(quote)

In this statement, we tell the computer to print to the screen the string that is stored in the quote variable. So, when we run the program the computer will find what’s stored in the quote variable (“‘I’m not superstitious, but I am a little stitious.’”) and print that out on the screen. The computer is now done with this statement, so it will move on to the next statement, evaluating all statements until the program is complete.

Expressions

What are expressions?

Expressions are combinations of variables, literals, operators, or functions that can be evaluated to produce a result. Expressions simplify to a single value.

This is the key: expressions evaluate to a typed value at runtime. The evaluation of an expression only occurs when the program is running OR when you ask the interactive Python interpreter to evaluate it. You can think of an expression as an intent to do something.

Here is an example:

In this example, the final two lines are expressions because they evaluate to a single value. These lines also use operators, which are covered later on this page.

The types of expressions you can execute with certain objects are dictated by the object’s type. In the above example, x and y are typed int, so you can use mathematical operators __*__ and + to compute a product or sum. Another object type is boolean, which we can use to execute a boolean expression (boolean expressions are expressions that return True or False).

For example:

In this example, cat is assigned to the boolean True, and dog is assigned to the boolean false. The expression “cat and dog” evaluates to False, and the expression “cat or dog” evaluates to True. For a recap on why these evaluate to False and True respectively, follow this link

Another example of a boolean expression is:

Both of these expressions will evaluate to False because the value assigned to x is not greater than the value assigned to y, and the value assigned to y does not equal the value assigned to x. More on these operators below.

Function call expressions

Functions are oftentimes thought of as “subprograms” of a larger program and are usually written to carry out a specific task. For a review of functions, follow this link

A function call, once evaluated, returns a value.

For example: let’s use one of Python’s built-in functions, max(). The max() function returns the maximum value of one or more items. Here is an example of a function call expression using max():

Method call expressions

Like function calls, when a method of an object is called, a value is often returned. Let’s use String’s method upper():

In this example, we first assign my_string to the string “I love pizzaaa”, which is a statement because it is an assignment instruction. Then, we evaluate this string to be completely uppercase with the upper() method. THE EXPRESSION IS my_string.upper() BECAUSE IT RETURNS A NEW VALUE, WHICH IS THE UPPERCASE VERSION OF OUR STRING! This is the new value assigned (assignment = statement) to my_string, so when we print my_string, the output is “I LOVE PIZZAAA”.

Operators

What are operators?

Operators are special symbols used to perform mathematical functions, comparisons, and assignments. They are not expressions nor statements, but they are often used in expressions and statements!

Mathematical operators

Most mathematical operators are exactly the same as they are in math! For example: +, -, / and * for addition, subtraction, division and multiplication.

There are a few operators, though, that are not the same as their math counterparts. For example, to raise a number to a power we use the double asterisk (**).

x evaluates to 9.

Another less familiar operator is the modulus (%). The modulus divides one number by another and tells us the remainder. This is useful for determining if one number is a factor of another, but that is not its only use.

x evalues to 1 because 2 can go into 3 once, leaving a remainder of 1. y evaluates to 0 because 2 goes into 4 twice evenly with no remainder.

Don’t forget!: like in algebra, our trusty order of operations applies… and yes, this means to remember PEMDAS! Just a quick reminder that PEMDAS isn’t just Please excuse my dear Aunt Sally, but it stands for the order in which multi-operator expressions are evaluated. (P = Parentheses, E = Exponents, M = Multiplication, D = Division, A = Addition, and S = Subtraction)

Additionally, any mathematical operator can be combined with an equals symbol = when incrementing a variable. For example:

In this example, x is first assigned the integer value of 3, and then it is incremented by another 3, reassigning the value of 6 to x. You can read “x += 3” as “x is x plus three.”

Assignment and concatenation operators

For assigning variables, use the = operator. Assigning a variable is considered a statement because it is an instruction to the computer. However, you can assign a statement to an expression if what’s being assigned is an expression.

For example: (For string concatenation, use the + operator. )

In this example, full_name is assigned to the expression first_name + " " + last_name.

full_name evaluates to ‘Creed Bratton’. We use the = operator to assign the variables first_name, last_name and full_name values, and we use the + operator to concatenate first_name and last_name

Conditional operators

Booleans are either true or false, but we can also have expressions that are true or false. This means we can have whole expressions that are booleans. Boolean expressions use conditional operators, which are operators that help compare data. The following two types of operators are conditional operators!

Relational operators

Many of these symbols come from math and are pretty recognizable:

>

This is the ‘greater than’ operator. Is the number on the left of the ‘>’ larger than the one on the right? If yes, the epxression is True. If no, the expression is False.

<

This is the ‘less than’ operator. Is the number on the left of the ‘<’ smaller than the one on the right? If yes, the epxression is True. If no, the expression is False.

>=

This is the ‘greater than or equal to’/‘is at least’ operator. (Looks like ≥ in math). Is the number on the left of the ‘>=’ larger or equal to than the one on the right? If yes, the epxression is True. If no, the expression is False.

<=

This is the ‘less than or equal to’/‘is at most’ operator. (Looks like ≤ in math). Is the number on the left of the ‘<>=’ smaller than or equal to the one on the right? If yes, the epxression is True. If no, the expression is False.

Equality Operators

Should I use a single = OR a double == ? Remember, the singe = is an assignment operator, which temporarily links a variable to its assigned value. SO what is the double == for?

The == is the equal to operator. It has TWO equals symbols in a row.

The == checks for deep equality, or if the objects are the same. An expression with == evaluates to a boolean.

1066 == 1066 would be True
21 == 939 would be False

The != is the not equal to operator. The ! symbol means “NOT”. It has an exclamation point followed by a singular equals symbol, and it works oppositely to the == operator.

1066 != 1066 would be False
21 != 939 would be True

For more information about expressions, check out this lesson page!