All the Operators in Python! Precedence too

Updated 08 September 2020

To fully equip ourselves on our Python odyssey we will take a leap into Operators and their Precedence. Python is undoubtedly a Homeric programming language, epic in its possible applications and vast in its potentials. Operators within this language are tools to implement operations on variables and data values. These are the sails, the ropes and the oars of your galley for the Mediterranean trip to Ithaca. 

This guide will provide a complete understanding of what operators within Python are. Furthermore, it will elucidate every single operator type, summarise their purposes and demonstrate to you how they can be useful in your coding ventures. Contents of this guide can be seen below.

There are several related tutorials on our website to help you become a true Pythonista and to save you Sisyphean effort. A great place to start would be Python Rundown and Main Windows. This will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand.

Following these guides, your mastery of this programming language will come substantially quicker than the length of time Penelope waited. As always if you have any questions, queries, or things to add please let us know your thoughts!


Overview of Operators

Operators are used to perform operations on variables and values. Depending on what they are they can be a method of comparison, give tools for calculations or even assign values to identifiers. They can be applied to all types of data such as binary, decimal and hexadecimal values. Order is very important when it comes to calculations and without consistent precedence, predictable reliable results would be impossible. Python has a predilection to perform certain calculations before others when they are written on a single line of code. If a line of code is beneath another line of code the above code will always be run before it.


Comparison Operators

Comparison operators are used when comparing two values. These operators give tools to the user to determine differences and similarities between data types. Seen below is a table with every comparison operator for decimal data within Python. This table also shows the special symbol allocated to each operator, a description of the operator and a code example. The result of the code example is also outlined. Within the table when there is text/symbols that would be written inside a Python IDLE Shell or a Python Program Window (ergo inputted as code) it will be denoted in this tutorial with line symbols, |  |. This notation will stay consistent throughout the whole page.

Python Comparison Operator

 


Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations. Seen below is a table with every arithmetic operator for decimal data within Python. This table also shows the special symbol allocated to each operator, a description of the operator and a code example. The result of the code example is also outlined.
 
Python Arithmetic Operators Table

 


Assignment Operators 

Assignment operators are used for assigning values to variables whilst also performing common mathematical operations. Seen below is a table with every assignment operator for decimal data within Python. This table also shows the special symbol allocated to each operator, a description of the operator and a code example. The result of the code example is also outlined and equivalent code is given. Using these symbols or using the equivalent code will always result in the same outcomes. Worth noting a most of these assignment operators will only work on mutable objects. Mutable objects are objects that can be changed after the creation whereas immutable objects cannot be changed once it is created. The type of the variable is the deciding factor of whether an object is mutable or immutable. Refer to the guide Python Variables and Types if you desire more information on this.
 

Python Assignment Operators

 


Bitwise Operators 

Bitwise operators are used when comparing two binary number values. Bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. A binary number is a number expressed in the base-2 numeral system. These operators give tools to the user to determine differences and similarities between data types. Seen below is a table with every bitwise comparison operator for byte data within Python. This table also shows the special symbol allocated to each operator, a description of the operator and a code example. The result of the code example is also outlined. Worth noting a unary operation is an operation with only one input (ergo operating on a single object). 

To write byte data directly into Python you must use code notation of | 0b | or | 0B | followed directly by the sequence of ones and zeros. For example, inputting a variable with an identifier A and a byte data of 0011 1100, the line of Python code would be | A = 0B00111100 |.
 
Python Bitwise Operators

 


Bitwise Assignment Operators

Bitwise assignment operators are used for assigning values to byte variables whilst also performing common mathematical operations to byte data. Seen below is a table with every bitwise assignment operator for byte data within Python. This table also shows the special symbol allocated to each operator, a description of the operator and a code example. The result of the code example is also outlined and equivalent code is given. Using these symbols or using the equivalent code will always result in the same outcomes. 
 
Python Bitwise Assignment Operators

 


Logical Operators, Identity Operators and Membership Operators

pretty tube boiOperators can be keywords. A keyword is a combination of letters, usually forming a word, that has a special meaning. This special meaning can be commands or parameters which will execute as soon as the computing device encounters the keyword when the code is run. All of these operators under logical, identity and membership categories are in fact keywords. Logical operators are used for conditional statements and return results of either true or false. Identity operators are used as a test to compare objects to see if they are actually the same object with the same memory location. These operators compare class, type and data values to determine this. Membership operators are used as a test to see whether a value or a sequence of values is present in objects.

The three logical operators in Python are the keywords below.
                     - | or |
                     - | 
and |
                     - | 
not |

The two identity operators in Python are the keywords below.
                     - | is |
                     - | 
is not |

The two membership operators in Python are the keywords below.
                     - | in |
                     - | 
not in |
 
 

Within this tutorial when there is text/symbols that would be written inside a Python IDLE Shell or a Python Program Window (ergo inputted as code) it will be denoted inside line symbols, |  |. Check out the guide All the Keywords in Python! to learn more about each of these specific keyword operators. That guide will give you a grasp on what each keyword operator does and will also present code examples demonstrating how to disco with them.


Precedence for Operators

Order is very important when it comes to calculations. Predictable and reliable results would be impossible without consistent precedence. Python has a predilection to perform certain calculations before others when they are written on a single line of code. Worth noting, if a line of code is beneath another line of code the above code will always be run before it. Also, if all the operators on a single line of code have the same precedence value the flow of operations will be from left to right. This section will also introduce the final operator within Python which is the parentheses operator. Normal bracket symbols are used to denote the parentheses operator, notation in code as | ( ) |. This is the ultimate method of ordering operations as anything inside the parentheses will be the first to run in a line of code. If multiple levels of parentheses are used the operations inside the most parentheses will be run first.

Seen below is a table listing all the operators within Python, ignoring the assignment operators. They are ordered with a descending value of precedence. This means operators higher in the table will run before those operators lower down in the table as the higher operators have higher precedence.
 Operator Precedence Table

 


The Next Step

With this doozy over the next natural step in your journey through Python would be functions. And then we're onto learning about loops structures! Exciting examples of code coming soon, let me just equip you with all the tools and means Python comes with as well as present you the best, most comprehensive and truly easy to understand reference pages.

The great next step on your coding adventures would be the guide All the Built-in Functions of Python.


Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.