Loading video player…

Understanding Operators and Expressions

Resource mentioned in this lesson: The Walrus Operator: Python’s Assignment Expressions

00:00 So what are operators? Well, within your programs, there are many different operations you might need to perform, like addition of numeric types, or assigning a value to a variable.

00:10 The special identifiers you use to execute these operations are called operators. In Python, they can be symbols, combinations of symbols or even keywords.

00:20 And these various operations act on operands. We have special names for operators based on how many operands they work with. You can have unary operators, which involve one operand, for example, in -273.15, the minus symbol is acting as the negation operator taking in one operand.

00:41 There’s also binary operators involving two operands. In 8 - 4, the subtraction operator now involves both 8 and 4.

00:51 There’s also something called the ternary operator, which works on, well, three operands, but more on that in a later lesson.

01:00 The categories of operators found in Python include the assignment operators, which assign values to variables, the arithmetic operators for performing mathematical calculations, the comparison operators that you’ll use to evaluate relationships between different values, Boolean or logical operators that you can use to build logical expressions, the identity operators that determine if two operands refer to the same object, and membership operators, which check for the presence of a value in a container.

01:29 So that’s operators, but what about expressions? Well, to understand expressions, you need to start by looking at statements in Python. You can find simple and compound statements.

01:39 Simple statements fit on a single logical line like the assignment statement. Compound statements, on the other hand, take up multiple logical lines. Examples of this would be the conditional if...else, for loops, or even function definitions.

01:54 And now we’re ready to talk about expressions. Expressions are simple statements that produce and return values. This is often done through the use of operators.

02:03 For example, 7 + 5 is an expression that returns the value 12. 42 / 2 is an expression that returns the value 21.

02:12 But expressions don’t have to use operators. For instance, function calls are also a kind of expression, like calling the built-in abs() function and passing in -10 that will return positive 10.

02:24 And while all expressions are statements, the reverse is not true. Not all statements return a value, like the aforementioned assignment statement.

02:33 Now, let’s look closer at the difference between statements and expressions in the REPL. Open up the Python REPL, and actually, why is it called REPL? REPL stands for Read, Evaluate, Print, Loop.

02:46 When you input a line of code, it’s read, evaluated and its return value is printed. So when you type in an expression like 42 - 29, it’s evaluated and its return value, 13, is printed. Or use the built-in abs() function.

03:04 abs() passing in -13, and its evaluation is the return value of the function call. In this case 13, which again is printed.

03:13 So a statement with no return value, like assignment, will print nothing.

03:18 number = abs(-13) goes straight to the next line. Nothing printed. day = the string "Friday",

03:28 again, straight to the next line with nothing printed. But there is actually a kind of assignment that does return a value and it’s aptly named the assignment expression, and it uses what we call the walrus operator.

03:41 It works like this: open parenthesis (value := "walrus") closed parenthesis. Technically, the parentheses aren’t part of the expression, but in most cases, you’ll get a syntax error.

03:52 If you don’t use them, hit Enter and the entire expression returns the value of the right-hand side of the walrus operator. In this case, the string walrus.

04:02 And the assignment worked as well. Examine value and it holds the value walrus. This is a subtle difference and it may not seem useful on its face, but because the assignment expression returns a value, it can be used in more complex logical flows, which we won’t go into in this course.

04:20 But if you’re really curious, check out The Walrus Operator: Python’s Assignment Expressions.

04:26 Okay, back to the course at hand. The next lesson is all about arithmetic operators, where the math is always mathing.

Become a Member to join the conversation.