Introduction to Python Operators

Contents

keyboard_tab
Python Operators
Introduction to Python Operators
Overview
Difficulty
Beginner
Duration
14m
Students
22
Ratings
5/5
starstarstarstarstar
Description

This course provides an intro to Python Operators, exploring what they are used for and the different types of operators available in Python.

Learning Objectives

  • Describe the purpose of an operator
  • List the different categories of operators
  • Describe the concept of operator precedence

Intended Audience

This course was designed for first-time developers wanting to learn Python. Existing developers may want to skip.

Prerequisites

This is an introductory course and doesn’t require any prior programming knowledge. However, conceptual knowledge of Python objects is recommended.

 

 

Transcript

Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. 

This course is part of a series of content designed to help you learn to program with the Python programming language. 

Should you wish to ask me a specific question, you can do that with the contact details on screen. 

You can also reach support by using the email address: support@cloudacademy.com. And one of our cloud experts will reply.

The purpose of the Python programming language is to create and control objects. And operators are one of the primary mechanisms for creating and controlling objects. 

For example, the addition operator allows us to add two objects. 

And the assignment operator allows us to perform name binding. You may recall that name binding is the process of binding a name to an object for future reference.

The purpose of operators is to perform an operation on an object or objects. In this lesson, we’re going to cover two types of operators. 

These are: 

  • Unary operators
  • Binary operators

Unary operators perform an operation with one object.

Binary operators perform an operation with two objects.

Across these two types exists several different categories of operators built into Python’s syntax. 

Here are the different categories. We have:

  • Arithmetic operators.
    • We’ve seen some of these used in a previous lesson. These interact with numeric objects.
  • Assignment operators.
    • This includes the operator used for name binding.
  • Comparison operators.
    • These are used to compare objects. 
    • For example: 
      • Are two strings identical? 
  • Logical operators.
    • These are used to create more complex logic. 
    • For example:
      • Imagine that you want to determine if two things are true.
      • Something such as: 
        • Is this car available in blue and is it on sale?
  • Identity operators.
    • These are used to determine if two name bindings reference the same object.
  • Membership operators.
    • These are used to determine if a specific sequence exists inside a sequence-based object.
    • For example:
      • We can determine if one string exists inside another
  • Bitwise operators.
    • These are used to perform operations on individual bits of data. I’m not going to cover these. The reason being, these get a bit too low level and we just don’t need to be distracted with very specific operators. Not when there are so many that are more common for most development.

Okay, so the purpose of operators is to perform an operation on an object or objects. Let’s go through each category to take a closer look.

The addition operator is used to add or combine two objects. Different types of objects can be added together in different ways. 

Numeric objects such as integers and floats are added mathematically. 

When string objects are added together, they’re combined together into one string. 

When lists are added together they merge into one list.

Objects only support operators that make sense for the concept being modeled. 

For example, the subtraction operator functions as you’d expect for numeric objects. 

However, unlike the add operator, subtract doesn’t make sense for a string, or a list. So it’s not implemented for those types. 

Python includes a mechanism that enables us to specify how an object behaves for most operators. By using specially named methods we can control the code that is called for a given operator. This is a fascinating rabbit hole to explore. However, not just yet. For now, just know that an operator is only supported if it makes sense for the object. 

The multiplication operator multiplies numbers as you’d expect. It can also be used to repeat sequences such as strings.

The division operator divides two numbers. 

The modulus operator divides two numbers and returns the remainder. 

The exponent operator calculates the exponent of two numeric objects. 

The floor division operator divides two number objects and returns the results without any remainder. 

Next up: Assignment operators.

Assignment operators are related to name binding. 

The assignment operator binds a name to an object. This enables us to reference the object by using the name as stand-in.

The assignment expression operator is used to bind a name in the middle of an expression. This is typically used to make certain expressions more readable. This operator is often referred to as the walrus operator due to it looking like sideways walrus.

The remainder of these operators are shorthand operators for performing an arithmetic operation on the bound object. 

For example the plus-equal operator can be used to add a number to the bound numeric object. 

Imagine we bind the name year to the number 2021. By using the plus-equal operator we could add one. After this operation the name year references an object with the value 2022. 

These operators are inline operations. Which means they perform an operation on the bound object. After the operation completes the object our name is bound to is updated to reflect the change. The way you might describe this in English would be to say: year is equal to itself plus one. Which equals 2022.

The minus equal operator subtracts from a bound object. 

The multiply-equal operator multiples a bound object.

The divide-equal operator divides a bound object.

The modulus-equal operator divides a bound object and returns the remainder. 

The floor-divide-equal operator divides a bound object and returns the result without a remainder.

The exponent-equal operator calculates the exponent of a bound object.

Each of these assignment operators is used in some way to interact with name bindings. The equal operator is used to create a name binding. As is the assignment expression operator. The remaining operators in this category are used to update a bound object. For example, by incrementing a number by one.

Next up: Comparison operators.

Comparison operators are used to compare two objects. Comparing objects forms the foundation for decision making in code. When these operations complete they return a boolean object. Recall that the boolean object type can be either true or false. 

The double equal operator is used to determine if two objects are equal. If they are equal then this operation returns True. 

For example: does the text on the left match what’s on the right?

The not-equal operator returns True if the two objects are not equal to one another. 

The greater than operator determines if the object on the left is greater than the one on the right.

The less than operator determines if the object on the left is less than the one on the right.

The greater than or equal operator determines if the object on the left is greater than or equal to the one on the right.

The less than or equal  operator determines if the object on the left is less than or equal to the one on the right.

Comparison is the basis for decision making in code. These operators will become common to you as you progress with Python.  

Next up: Logical operators.

Comparison operators enable us to make decisions by comparing two objects and returning a boolean value of True or False. However, sometimes you’ll want to perform more complex comparisons. Which include multiple comparisons. 

For example: is this feature enabled and does this user have permission to access it?

The and operator is used to determine if two comparisons are both true. If both comparisons return True, then the and operation returns True. Otherwise it returns False.

The or operator is used to determine if either comparison is True. If either one is true then this operation returns True. Otherwise it returns False. 

The not operator is used to reverse a boolean value. If a value is True then not True will return False. If a value is False then not False will return True. This is to swap a boolean from its value to the opposite. For example: “if subscription not expired: download content.”

Next up: Identity operators.

Identity operations are used to determine if two name bindings point to the exact same object. Not just the value of the object but the exact same object in memory.

There are two identity operators: is and is not. The is operator determines if two names are bound to the same object. This has some very specific use cases. And we’ll cover those in a future lesson. 

The is not operator determines if two names point to different objects. Again, this has some specific use cases that’ll be a future topic.

Next up: Membership operators.

Membership operators are used to determine if an object exists inside of a sequence. In Python object types such strings and lists are sequences. Which means we can search strings and lists for other objects.

The in operator determines if an object exists inside a sequence. In this case we can ask: does the string “python” exist in this list? Or, does this text include an exclamation point?

The not in operator determines if an object does not exist in a sequence. If the object is missing from the sequence this operation returns True. 

As an example: “if this user is not in the list of banned users, allow them to log in.”

Now that we’ve covered the different operators provided by the runtime, let's talk about what happens when we use multiple operators together in a single expression.

Sometimes to solve a specific problem we’ll need to use multiple operators in the same expression. 

For example: imagine that we want to calculate some value such as 1000 + 2 * 3.14. 

The result of this expression is: 1006.28. Math has a particular order for operations and so does Python. 

Operators are evaluated in very specific order. This order is referred to as: operator precedence. 

We’re not going to go through list table: https://docs.python.org/3/reference/expressions.html#operator-precedence

For now, knowing about operator precedence is enough. 

Okay, let’s stop here and recap what we’ve covered.

The Python programming language is used to create and control objects. And operators are one of the primary means of interacting with objects. The purpose of operators is to perform an operation on an object or objects.

Python has many operators built into the language syntax. These operators fall into different categories, depending on how they interact with objects. Those categories include:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

It’s not important to try and memorize all of these. Actually, I recommend against it. You’ll learn these as you find a need for them. You could go through your entire career and not have a need to use absolutely all of these. Just knowing these categories exist, you’ll be able to search for operators as you need them. For example, when you first need to search a list you’ll recall that there was some operator just for that. 

Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:

  • Operators are used to perform an operator on an object or objects. 
  • There are several categories of operators
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Logical operators
    • Identity operators
    • Membership operators
    • Bitwise operators
  • Operators are evaluated in very specific order known as: operator precedence

Okay, that's going to be all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!

About the Author
Students
86941
Labs
11
Courses
38
Learning Paths
36

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics