<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged operators</title>
<link>https://coderlegion.com/tag/operators</link>
<description>Powered by Question2Answer</description>
<item>
<title>Building a Kubernetes Operator in Go with Kube-Shift</title>
<link>https://coderlegion.com/4158/building-a-kubernetes-operator-in-go-with-kube-shift</link>
<description>&lt;p&gt;Kubernetes has become the de-facto standard for container orchestration. As its adoption grows, so does the need for extending its capabilities to manage complex, stateful applications. This is where Kubernetes Operators come in. In this article, we'll explore how to build a custom Kubernetes Operator from scratch using Go and the &lt;code&gt;controller-runtime&lt;/code&gt; library. We'll use kube-shift, a custom operator for database schema migrations, as a real-world example to guide us through the process.&lt;/p&gt;
&lt;h3 id=&quot;the-why-what-problem-do-operators-solve-&quot;&gt;The &quot;Why&quot;: What Problem Do Operators Solve?&lt;/h3&gt;
&lt;p&gt;Before we dive into the code, let's understand the problem operators solve. Managing applications on Kubernetes often involves more than just deploying a set of pods. Many applications require complex lifecycle management, including installation, upgrades, and handling failures. Operators automate these tasks by extending the Kubernetes API with Custom Resource Definitions (CRDs) and a custom controller to manage them.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;kube-shift&lt;/code&gt; project is a perfect example. It introduces a &lt;code&gt;DatabaseMigration&lt;/code&gt; resource to Kubernetes, allowing you to manage database schema changes as part of your deployment process, just like any other Kubernetes resource.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=10478430094407607469&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;setting-the-stage-project-scaffolding-with-kubebuilder&quot;&gt;Setting the Stage: Project Scaffolding with Kubebuilder&lt;/h3&gt;
&lt;p&gt;The kube-shift operator, like many Go-based operators, is built using the Kubebuilder framework. Kubebuilder provides a set of tools to quickly scaffold a new operator project, including the boilerplate for CRDs, controllers, and other necessary components.&lt;/p&gt;
&lt;p&gt;The entry point of our operator is &lt;code&gt;cmd/main.go&lt;/code&gt; This file is responsible for setting up and running the controller manager.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// cmd/main.go
package main
 
import (
// ... imports
&quot;github.com/mohit-nagaraj/kube-shift/api/v1alpha1&quot;
&quot;github.com/mohit-nagaraj/kube-shift/internal/controller&quot;
// ...
)
 
func main() {
// ... flag parsing and setup

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
// ... options
})
if err != nil {
setupLog.Error(err, &quot;unable to start manager&quot;)
os.Exit(1)
}
 
if err = (&amp;amp;controller.DatabaseMigrationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, &quot;unable to create controller&quot;, &quot;controller&quot;, &quot;DatabaseMigration&quot;)
os.Exit(1)
}
 
// ...
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we create a new manager and register our &lt;code&gt;DatabaseMigrationReconciler&lt;/code&gt; with it. The manager is responsible for running the controller and managing its lifecycle.&lt;/p&gt;
&lt;h3 id=&quot;defining-our-custom-resource-the-databasemigration-crd&quot;&gt;Defining Our Custom Resource: The DatabaseMigration CRD&lt;/h3&gt;
&lt;p&gt;The first step in creating our operator is to define the custom resource it will manage. This is done in &lt;code&gt;api/v1alpha1/databasemigration_types.go&lt;/code&gt;. This file defines the Go types that represent our DatabaseMigration CRD.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// api/v1alpha1/databasemigration_types.go
package v1alpha1
 
import (
metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
)
 
// DatabaseMigrationSpec defines the desired state of DatabaseMigration
type DatabaseMigrationSpec struct {
Database  DatabaseConfig  `json:&quot;database&quot;`
Migration MigrationConfig `json:&quot;migration&quot;`
}
 
// DatabaseMigrationStatus defines the observed state of DatabaseMigration
type DatabaseMigrationStatus struct {
Phase   MigrationPhase `json:&quot;phase,omitempty&quot;`
Message string         `json:&quot;message,omitempty&quot;`
}
 
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
 
// DatabaseMigration is the Schema for the databasemigrations API
type DatabaseMigration struct {
metav1.TypeMeta   `json:&quot;,inline&quot;`
metav1.ObjectMeta `json:&quot;metadata,omitempty&quot;`
 
Spec   DatabaseMigrationSpec   `json:&quot;spec,omitempty&quot;`
Status DatabaseMigrationStatus `json:&quot;status,omitempty&quot;`
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;DatabaseMigration&lt;/code&gt; struct embeds &lt;code&gt;metav1.TypeMeta&lt;/code&gt; and &lt;code&gt;metav1.ObjectMeta&lt;/code&gt;, which are common to all Kubernetes objects. The &lt;code&gt;Spec&lt;/code&gt; field defines the desired state of our resource, while the &lt;code&gt;Status&lt;/code&gt; field represents its current state.&lt;/p&gt;
&lt;h3 id=&quot;the-heart-of-the-operator-the-controller-and-reconciliation-loop&quot;&gt;The Heart of the Operator: The Controller and Reconciliation Loop&lt;/h3&gt;
&lt;p&gt;With our CRD defined, we need a controller to act on it. The controller's logic is implemented in &lt;code&gt;internal/controller/databasemigration_controller.go&lt;/code&gt;. The core of the controller is the &lt;code&gt;Reconcile&lt;/code&gt; function. This function is called every time a &lt;code&gt;DatabaseMigration&lt;/code&gt; resource is created, updated, or deleted.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// internal/controller/databasemigration_controller.go
 
// DatabaseMigrationReconciler reconciles a DatabaseMigration object
type DatabaseMigrationReconciler struct {
client.Client
Scheme *runtime.Scheme
Log    logr.Logger
// ... other fields
}
 
// +kubebuilder:rbac:groups=database.mohitnagaraj.in,resources=databasemigrations,verbs=get;list;watch;create;update;patch;delete
// ... other rbac markers
 
func (r *DatabaseMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues(&quot;databasemigration&quot;, req.NamespacedName)
 
// your logic here
 
return ctrl.Result{}, nil
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Reconcile&lt;/code&gt; function receives the name and namespace of the &lt;code&gt;DatabaseMigration&lt;/code&gt; resource that triggered the event. It's the reconciler's job to fetch the resource and take the necessary actions to bring the system to the desired state defined in the resource's &lt;code&gt;Spec&lt;/code&gt;. This could involve creating jobs, pods, or other resources to perform the database migration.&lt;/p&gt;
&lt;h3 id=&quot;advanced-reconciliation-beyond-the-basics&quot;&gt;Advanced Reconciliation: Beyond the Basics&lt;/h3&gt;
&lt;p&gt;A robust operator does more than just create resources. kube-shift demonstrates several advanced concepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Status Updates: The controller updates the Status field of the DatabaseMigration resource to reflect the current state of the migration. This provides valuable feedback to users.&lt;/li&gt;
&lt;li&gt;Finalizers: To ensure graceful cleanup, kube-shift uses a finalizer. The DatabaseMigrationFinalizer is added to the resource on creation. This prevents the resource from being deleted until the controller has performed any necessary cleanup tasks.&lt;/li&gt;
&lt;li&gt;Leader Election: In a high-availability setup, you might run multiple instances of your operator. Leader election ensures that only one instance is active at a time, preventing conflicts. This is configured in cmd/main.go.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;testing-your-operator&quot;&gt;Testing Your Operator&lt;/h3&gt;
&lt;p&gt;Testing is crucial for any production-grade software. kube-shift includes both unit and integration tests.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Unit Tests: These tests focus on individual components of the operator in isolation.&lt;/li&gt;
&lt;li&gt;Integration Tests: The project uses the envtest package to run integration tests against a real Kubernetes API server. You can see this in internal/controller/suite_test.go, which sets up a test environment for the controller tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Building a Kubernetes Operator is a powerful way to extend Kubernetes and automate complex application management tasks. By leveraging tools like Kubebuilder and controller-runtime, you can create robust, production-ready operators. The kube-shift project is an excellent example of how to apply these concepts to solve a real-world problem.&lt;/p&gt;
&lt;p&gt;To learn more, I encourage you to explore the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/mohit-nagaraj/kube-shift&quot;&gt;kube-shift repository&lt;/a&gt; on GitHub. You'll find the complete source code, along with more details on how to build, deploy, and use the operator. Happy coding!&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/4158/building-a-kubernetes-operator-in-go-with-kube-shift</guid>
<pubDate>Sun, 20 Jul 2025 07:50:26 +0000</pubDate>
</item>
<item>
<title>Exploring Python Operators: Beyond the Basics</title>
<link>https://coderlegion.com/371/exploring-python-operators-beyond-the-basics</link>
<description>&lt;h2&gt;A. Introduction&lt;/h2&gt;
&lt;p&gt;This article is part 2 of the &lt;strong&gt;Basic Operators&lt;/strong&gt; which examines &lt;strong&gt;Logical&lt;/strong&gt;, &lt;strong&gt;Bitwise&lt;/strong&gt;, &lt;strong&gt;Membership&lt;/strong&gt; and &lt;strong&gt;Identity operators&lt;/strong&gt;. This is a continuation of &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/294/basic-operators-in-python&quot;&gt;Part 1 which discusses &lt;strong&gt;Arithmetic&lt;/strong&gt;, &lt;strong&gt;Comparison&lt;/strong&gt; and &lt;strong&gt;Assignment operators&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Logical operators are used for evaluating conditions in &lt;strong&gt;control flow statements&lt;/strong&gt;, Bitwise operators &lt;strong&gt;manipulate individual bits&lt;/strong&gt; of integers, Membership operators check for the &lt;strong&gt;existence of a value&lt;/strong&gt; within a sequence, and Identity operators &lt;strong&gt;check object identity&lt;/strong&gt; in memory.&lt;/p&gt;
&lt;h2&gt;B. Logical Operators&lt;/h2&gt;
&lt;p&gt;These &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#boolean&quot;&gt;Logical operators&lt;/a&gt; evaluate the left and right operands which will result to either a true or false values.&lt;/p&gt;
&lt;h3&gt;1. and&lt;/h3&gt;
&lt;p&gt;The &quot;and&quot; operator checks if both operands are true, and if it is, then the result will be true, otherwise false.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a == 100 and b == 20
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First it evaluates if &lt;code&gt;a is equal to 100&lt;/code&gt; the result is True. Second it evaluates if &lt;code&gt;b is equal to 20&lt;/code&gt; the result is also True. Since both operands are True the final result is True. We can say that &lt;code&gt;True and True is True&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here is another example where the second operand is false.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a == 100 and b &amp;lt; 20
False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second operand checks if b is below 20, the answer is False because &lt;code&gt;b&lt;/code&gt; which is 20 is not below 20. So &lt;code&gt;True and False&lt;/code&gt; results to a False result.&lt;/p&gt;
&lt;p&gt;Another example where the first operand is False and the second operand is True.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a &amp;gt; 100 and b &amp;gt;= 20
False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first operand evaluates to False because &lt;code&gt;a&lt;/code&gt; which is 100 is not greater than 100. While in the second operand, it evaluates to True because &lt;code&gt;b&lt;/code&gt; which is 20 is equal or greater than 20. So the final evaluation is &lt;code&gt;False and True&lt;/code&gt; will result in False because both are not True.&lt;/p&gt;
&lt;p&gt;Our last example demonstrates a scenario where both operands are false.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100 and b &amp;gt; 20
False
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100
False
&amp;gt;&amp;gt;&amp;gt; b &amp;gt; 20
False
&amp;gt;&amp;gt;&amp;gt; False and False
False
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So &lt;code&gt;False and False&lt;/code&gt; will result in false.&lt;/p&gt;
&lt;p&gt;In summary this is the truth table for &lt;code&gt;and&lt;/code&gt; operator.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A and B&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;h3&gt;2. or&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;or&lt;/code&gt; operator evaluates to true if one or both of the operands are true, otherwise false.&lt;/p&gt;
&lt;p&gt;Let's see an example where the first operand is true and the second is false.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a == 100 or b &amp;gt; 20
True
&amp;gt;&amp;gt;&amp;gt; a == 100
True
&amp;gt;&amp;gt;&amp;gt; b &amp;gt; 20
False
&amp;gt;&amp;gt;&amp;gt; True or False
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The final result is True because one of the operand is True.&lt;/p&gt;
&lt;p&gt;Here is an example scenario where the first operand is false and the second operand is true.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100 or b == 20
True
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100
False
&amp;gt;&amp;gt;&amp;gt; b == 20
True
&amp;gt;&amp;gt;&amp;gt; False or True
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next example shows that both operands are false.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100 or b &amp;gt; 20
False
&amp;gt;&amp;gt;&amp;gt; a &amp;lt; 100
False
&amp;gt;&amp;gt;&amp;gt; b &amp;gt; 20
False
&amp;gt;&amp;gt;&amp;gt; False or False
False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So when both are False, the result is False.&lt;/p&gt;
&lt;p&gt;Our last example examines what happens if both are true.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 20
&amp;gt;&amp;gt;&amp;gt; a &amp;gt;= 100
True
&amp;gt;&amp;gt;&amp;gt; b &amp;lt;= 20
True
&amp;gt;&amp;gt;&amp;gt; a &amp;gt;= 100
True
&amp;gt;&amp;gt;&amp;gt; b &amp;lt;= 20
True
&amp;gt;&amp;gt;&amp;gt; True or True
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So the final result is True if both are True.&lt;/p&gt;
&lt;p&gt;Here is the truth table with &lt;code&gt;or&lt;/code&gt; operator.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A or B&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;h3&gt;3. not&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;not&lt;/code&gt; operator negates the given value. If the value is True, it converts it to False. And if the value is False, it converts it to True.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = True
&amp;gt;&amp;gt;&amp;gt; not a
False
&amp;gt;&amp;gt;&amp;gt; b = False
&amp;gt;&amp;gt;&amp;gt; not b
True
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;In Logical operators, it always results to either True or False values.&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;C. Bitwise Operators&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types&quot;&gt;Bitwise refers to the operations to be done on the binary&lt;/a&gt; bits (0 and 1). There are 6 bitwise operators that we are going to discuss. These are the &quot;&amp;amp;&quot; (and), &quot;|&quot; (or), &quot;^&quot; (xor), &quot;~&quot; (not), &quot;&amp;lt;&amp;lt;&quot; (left shift) and &quot;&amp;gt;&amp;gt;&quot; (right shift).&lt;/p&gt;
&lt;p&gt;These operators are used to operate on the operands of the binary numbers. A binary number is a representation which uses only two symbols: 0 and 1. Each digit in a binary number is called a &quot;bit,&quot; which stands for binary digit. An example of binary number is &quot;0101&quot;. That is a 4-bit binary number.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15827846555906334194&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Convert a decimal number to binary&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Consider the decimal number 4, what is its binary number equivalent? Here is a manual procedure to convert it to binary.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Divide the number by 2 and record the quotient and remainder.&lt;/li&gt;
&lt;li&gt;If quotient is not zero, divide it again by 2 and record the quotient and remainder.&lt;/li&gt;
&lt;li&gt;If quotient is zero record the remainder and stop the division. However if it is not zero go to step 2.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Get all the remainders starting from the last step to form the binary number with 1's and 0's.&lt;/p&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;1. 4 / 2 = 2 remainder 0
2. 2 / 2 = 1 remainder 0
3. 1 / 2 = 0 remainder 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get all the remainder starting from step 3 to step 1.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;`100`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Convert a binary to decimal&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let's convert a binary &quot;100&quot; to decimal number.&lt;/p&gt;
&lt;p&gt;Starting from leftmost bit we will multiply the digit by its power of two.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;0 x 2^0 = 0
0 x 2^1 = 0
1 x 2^2 = 4
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then sum the results which is 4 or &lt;code&gt;0 + 0 + 4&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;1. &amp;amp;&lt;/h3&gt;
&lt;p&gt;The &quot;and&quot; operator &lt;code&gt;&amp;amp;&lt;/code&gt; operates on the operands bit by bit. If both operands are 1, the result is 1, otherwise the result is 0. Let's have an example.&lt;/p&gt;
&lt;p&gt;Here is a table with decimal integer with equivalent 4-digit binary number.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;0 = `0000`
1 = `0001`
2 = `0010`
3 = `0011`
4 = `0100`
5 = `0101`
6 = `0110`
7 = `0111`
8 = `1000`
9 = `1001`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let's solve this, calculate the result if an &lt;code&gt;&amp;amp;&lt;/code&gt; operator is applied to integers 3 and 5.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;3 = `0011`
5 = `0101`

    0011
&amp;amp;   0101
========
    0001
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;In the first column it is `0 &amp;amp; 0` which is 0.
In the second column it is `0 &amp;amp; 1` which is 0.
In the third column it is `1 &amp;amp; 0` which is 0.
In the fourth column it is `1 &amp;amp; 1` which is 1.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, the final result is 0001. We can change that to decimal with the following steps.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;decimal = 1 x 2^0 + 0 x 2^1 + 0 x 2^2 + 0 x 2^3
decimal = 1 + 0 + 0 + 0
decimal = 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So 3 &amp;amp; 5 is equal to 1.&lt;/p&gt;
&lt;p&gt;The truth table for Bitwise operations.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A &amp;amp; B&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;h3&gt;2. |&lt;/h3&gt;
&lt;p&gt;The &quot;or&quot; operator &lt;code&gt;|&lt;/code&gt; operates bit by bit according to the following truth table.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A | B&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;result = 7 | 4

`0111`
`0100`
=====
`0111`

operate vertically

0 | 0 = 0
1 | 1 = 1
1 | 0 = 1
1 | 0 = 1

decimal value for `0111`
result = 1 x 2^0 + 1 x 2^1 + 1 x 2^2 + 0 x 2^3
result = 1 + 2 + 4 + 0
result = 7
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; 7 | 4
7
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. ^&lt;/h3&gt;
&lt;p&gt;The &quot;xor&quot; operator &lt;code&gt;^&lt;/code&gt; performs an exclusive &quot;or&quot; operation on both operands bit by bit. If two corresponding bits are different, the result is 1. If two corresponding bits are the same, the result is 0.&lt;/p&gt;
&lt;p&gt;This is the truth table for `xor`.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A xor B&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;pre&gt;&lt;code&gt;result = 7 ^ 4

`0111`
`0100`
=====
`0011`

decimal value for `0011`
result = 1 x 2^0 + 1 x 2^1 + 0 x 2^2 + 0 x 2^3
result = 1 + 2 + 0 + 0
result = 3
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; 7 ^ 4
3
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;4. ~&lt;/h3&gt;
&lt;p&gt;The &quot;not&quot; operator &lt;code&gt;~&lt;/code&gt; executes a negation operation on its operand bit by bit. If bit is 0, it sets it to 1. And if it is 1, it sets it to 0. It is like a switch on/off.&lt;/p&gt;
&lt;p&gt;Example with a 4-bit integer 7.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;result = ~7

~ `0111`
=======
   1000

decimal value
result = 0 x 2^0 + 0 x 2^1 + 0 x 2^2 + 1 x 2^3
result = 0 + 0 + 0 + 8
result = 8
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;5. &amp;lt;&amp;lt;&amp;lt;/h3&amp;gt;
The &quot;left shift&quot; operator &lt;code&gt;&amp;lt;&amp;lt;&amp;lt;/code&amp;gt; is used to shift to the left, the bits of a binary number by a given number of positions.
Example.
&lt;code&gt;n = 7
8-bit binary is 00000111.

n &lt;/code&gt;
The right-bit is supplied with two 0 bits and the existing bits are shifted to the left by two positions.
6. &amp;gt;&amp;gt;&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &quot;right shift&quot; operator &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; is used to shift to the right, the bits of a binary number by a given number of positions.&lt;/p&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;n = 7
4-bit binary is 0111

shift the bit 2 position to the right.
n &amp;gt;&amp;gt; 2
result = 0001
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In decimal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;result = 1 x 2^0 + 0 x 2^1 + 0 x 2^2 + 0 x 2^3
result = 1 + 0 + 0 + 0
result = 1
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-blue&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;In Bitwise operations, the computation is performed bit by bit.&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;D. Membership Operators&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/reference/expressions.html#membership-test-operations&quot;&gt;Membership operators &lt;code&gt;in&lt;/code&gt; and &lt;code&gt;not in&lt;/code&gt;&lt;/a&gt; are used to test whether a value is found or not within a sequence. This sequence can be a string, dictionary, list or even a tuple.&lt;/p&gt;
&lt;h3&gt;1. in&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;in&lt;/code&gt; operator checks if an item is in a sequence. If so it will return a boolean True otherwise False.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;String example&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 'Paris'
&amp;gt;&amp;gt;&amp;gt; b = 'The 2024 summer olympics will be held in Paris.'
&amp;gt;&amp;gt;&amp;gt; a in b
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Dictionary example&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; cars = {'Ford': ['blind spot monitor', 'hill assist'], 'BMW': ['climate control', 'mild hybrid']}
&amp;gt;&amp;gt;&amp;gt; 'Ford' in cars
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;List example&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 45
&amp;gt;&amp;gt;&amp;gt; b = [1, 58, 69, 45, 12, 100]
&amp;gt;&amp;gt;&amp;gt; a in b
True
&amp;gt;&amp;gt;&amp;gt; 'Toyota' in cars
False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Tuple example&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; countries = ('USA', 'China', 'France', 'Australia')
&amp;gt;&amp;gt;&amp;gt; 'USA' in countries
True
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. not in&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;not in&lt;/code&gt; operator is the opposite of &lt;code&gt;in&lt;/code&gt;. It checks if item is in a sequence and if so, it returns True if not it returns False.&lt;/p&gt;
&lt;p&gt;List example.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = [2, 8, 4, 9]
&amp;gt;&amp;gt;&amp;gt; 5 not in a
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;String example.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = ['basketball', 'cricket', 'soccer']
&amp;gt;&amp;gt;&amp;gt; 'baseball' not in a
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tuple example.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = (8, 9, 1, 3)
&amp;gt;&amp;gt;&amp;gt; 8 not in a
False
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;E. Identity Operators&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/reference/expressions.html#is&quot;&gt;The Identity  &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;is not&lt;/code&gt; operators&lt;/a&gt; are used to check if two variables refer to the same object in memory. The &lt;code&gt;is&lt;/code&gt; operator returns True if the memory addresses of the two variables are the same, otherwise it returns False. The &lt;code&gt;is not&lt;/code&gt; operator is the negatiion &lt;code&gt;is&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;1. is&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;is&lt;/code&gt; operator checks if memory addresses of two operands are the same. If so it returns True otherwise False.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 50
&amp;gt;&amp;gt;&amp;gt; b = 100
&amp;gt;&amp;gt;&amp;gt; a is b
False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let's check the address of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; id(a)
140717562022216
&amp;gt;&amp;gt;&amp;gt; id(b)
140717562023816
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since the address in memory is different, the result is False.&lt;/p&gt;
&lt;h3&gt;2. is not&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;is not&lt;/code&gt; operator checks if memory addresses of two operands are different. If so it returns True otherwise False.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 'basketball'
&amp;gt;&amp;gt;&amp;gt; b = 'soccer'
&amp;gt;&amp;gt;&amp;gt; a is not b
True
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result is True because the addresses of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are different.&lt;/p&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;span class=&quot;faq-body&quot;&gt;
1. How to get the memory address of an object for `is` and `is not` operators?&lt;br&gt;
Answer: Use the id(object) function.&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;F. Summary&lt;/h2&gt;
&lt;p&gt;
&lt;strong&gt;Logical operators&lt;/strong&gt; (and, or, not) evaluate &lt;strong&gt;operands&lt;/strong&gt; to produce true or false outcomes. The &quot;and&quot; results in true only if both are true, &quot;or&quot; is true if either operand is true.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bitwise operators&lt;/strong&gt; work on binary bits. Examples include &quot;and&quot; (&amp;amp;), &quot;or&quot; (|), &quot;xor&quot; (^), &quot;not&quot; (~), &quot;left shift&quot; (&amp;lt;), and &quot;right shift&quot; (&amp;gt;&amp;gt;). They &lt;strong&gt;manipulate binary representations&lt;/strong&gt; of numbers.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Membership operators&lt;/strong&gt; (in, not in) determine if a value is within a sequence (e.g., string, list, tuple). 'in' returns true if found, 'not in' returns true if not found.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Identity operators&lt;/strong&gt; (is, is not) check if variables refer to the same memory location. 'is' returns true if they do, 'is not' returns true if they don't.&lt;/p&gt;
&lt;p&gt;It would be interesting to study further especially on &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/reference/expressions.html#operator-precedence&quot;&gt;operator precedence&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For further reading have a look on &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/174/python-syntax&quot; target=&quot;_blank&quot;&gt;Python's Syntax&lt;/a&gt; which outlines the fundamental grammar of the language. And &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/201/python-variables-and-data-types&quot; target=&quot;_blank&quot;&gt;Python variables and data types&lt;/a&gt; which introduces how to use variables and what data types are.&lt;/p&gt;
</description>
<category>Python Basics</category>
<guid isPermaLink="true">https://coderlegion.com/371/exploring-python-operators-beyond-the-basics</guid>
<pubDate>Sat, 22 Jun 2024 12:48:45 +0000</pubDate>
</item>
<item>
<title>Basic operators in Python</title>
<link>https://coderlegion.com/294/basic-operators-in-python</link>
<description>&lt;h2&gt;A. Introduction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Operators&lt;/strong&gt;, ranging from a simple addition to a more complex operations like bitwise and beyond, serve as  indispensable tools in the hands of the programmers to &lt;strong&gt;manipulate data&lt;/strong&gt; and achieve desired outcomes dictated by the code's objectives. Understanding the specific applications of each operator is crucial to build software that are not only functional but also efficient. Also &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/256/mastering-scripting-with-python&quot; target=&quot;_blank&quot;&gt;mastering Python scripting&lt;/a&gt; requires a solid understanding of the basics.&lt;/p&gt;
&lt;p&gt;Part 1 covers &lt;strong&gt;Arithmetic, Comparison and Assignment&lt;/strong&gt; operators. &lt;a rel=&quot;nofollow&quot; href=&quot;https://logiclair.org/1879/python-basic-operators-part-2&quot;&gt;Part 2&lt;/a&gt; which is in a different article covers &lt;strong&gt;Logical, Bitwise, Membership and Identity&lt;/strong&gt; operators.&lt;/p&gt;
&lt;h2&gt;B. Arithmetic Operators&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex&quot;&gt;Arithmetic operators&lt;/a&gt; are used to perform basic arithmetic operations on numerical values.&lt;/p&gt;
&lt;h3&gt;1. Addition (+)&lt;/h3&gt;
&lt;p&gt;The operator symbol &lt;code&gt;+&lt;/code&gt; is used to get the total or sum from the given operands.&lt;/p&gt;
&lt;p&gt;I will be using REPL most of the time to show some examples. REPL stands for &lt;strong&gt;Read Evaluate Print Loop&lt;/strong&gt;. Open your terminal or powershell for windows and type Python. You need to install Python first.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10 + 5
&amp;gt;&amp;gt;&amp;gt; a
15
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variable &lt;code&gt;a&lt;/code&gt; is the sum, the 10 and 5 are the &lt;code&gt;operands&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;2. Subtraction (-)&lt;/h3&gt;
&lt;p&gt;In the subtraction operation we use the subtraction operator with symbol &lt;code&gt;-&lt;/code&gt;. The result is the difference, the first operand is called the &lt;code&gt;minuend&lt;/code&gt; and the second operand is called the &lt;code&gt;subtrahend&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 50
&amp;gt;&amp;gt;&amp;gt; c = a - b
&amp;gt;&amp;gt;&amp;gt; c
50
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;c&lt;/code&gt; is the difference, &lt;code&gt;a&lt;/code&gt; is the minuend, and &lt;code&gt;b&lt;/code&gt; is the subtrahend.&lt;/p&gt;
&lt;h3&gt;3. Multiplication (*)&lt;/h3&gt;
&lt;p&gt;The result of the multiplication of operands is called a product.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 4
&amp;gt;&amp;gt;&amp;gt; b = 2
&amp;gt;&amp;gt;&amp;gt; c = a * b
&amp;gt;&amp;gt;&amp;gt; c
8
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;c&lt;/code&gt; is called the product, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the operands also called the factors.&lt;/p&gt;
&lt;h3&gt;4. Division (/)&lt;/h3&gt;
&lt;p&gt;The result of the division operation is called a quotient. The dividend is the numerator or the number to be divided and the divisor is the denominator or the number that divides the dividend.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100  # dividend
&amp;gt;&amp;gt;&amp;gt; b = 2  # divisor
&amp;gt;&amp;gt;&amp;gt; c = a / 2  # quotient
&amp;gt;&amp;gt;&amp;gt; c
50.0
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;5. Floor Division (//)&lt;/h3&gt;
&lt;p&gt;It is a division but rounds the result off to a whole number.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; b = 3
&amp;gt;&amp;gt;&amp;gt; c = a // b
&amp;gt;&amp;gt;&amp;gt; c
33
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;6. Exponentiation (**)&lt;/h3&gt;
&lt;p&gt;Exponentiation is an operation that involves raising a base number to a certain number called an exponent. It just multiplies the base n times where n is the exponent.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 2  # base
&amp;gt;&amp;gt;&amp;gt; b = 2  # exponent
&amp;gt;&amp;gt;&amp;gt; c = a ** b
&amp;gt;&amp;gt;&amp;gt; c
4
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is similar to &lt;code&gt;2*2&lt;/code&gt; or multiplying the two twice which is &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;7. Modulo (%)&lt;/h3&gt;
&lt;p&gt;The modulo operator is used to calculate the balance or remainder after performing a division.&lt;/p&gt;
&lt;p&gt;Integer modulo operation.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 5
&amp;gt;&amp;gt;&amp;gt; b = 2
&amp;gt;&amp;gt;&amp;gt; c = a % b
&amp;gt;&amp;gt;&amp;gt; c
1
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we divide 5 by 2 the answer is 2 with some extra because 2 times 2 is 4. Now 5 - 4 equals 1, that 1 is called the remainder or balance.&lt;/p&gt;
&lt;p&gt;Example with zero remainder.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10
&amp;gt;&amp;gt;&amp;gt; b = 5
&amp;gt;&amp;gt;&amp;gt; c = a % b
&amp;gt;&amp;gt;&amp;gt; c
0
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since 10 is divisible by 5 or no excess number, the remainder is zero.&lt;/p&gt;
&lt;p&gt;Float modulo operation.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 6.5
&amp;gt;&amp;gt;&amp;gt; b = 4.3
&amp;gt;&amp;gt;&amp;gt; c = a % b
&amp;gt;&amp;gt;&amp;gt; c
2.2
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-blue&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;The result of addition is a sum, the result of subtraction is a difference, the result of multiplication is a product and the result of division is a quotient.&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;C. Comparison Operators&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#comparisons&quot;&gt;Comparison operators&lt;/a&gt; are used to compare two operands which results to either True or False value.&lt;/p&gt;
&lt;p&gt;Here is a summary of these operators.&lt;/p&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Operation&lt;/th&gt;
    &lt;th&gt;Meaning&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&amp;lt;&amp;lt;/td&amp;gt;
    &lt;/td&gt;&lt;td&gt;strictly less than&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&amp;lt;=&amp;lt;/td&amp;gt;
    &lt;/td&gt;&lt;td&gt;less than or equal&lt;/td&gt;
  &lt;/tr&gt;
 &lt;tr&gt;
    &lt;td&gt;&amp;gt;&lt;/td&gt;
    &lt;td&gt;strictly greater than&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&amp;gt;=&lt;/td&gt;
    &lt;td&gt;greater than or equal&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;=&lt;/td&gt;
    &lt;td&gt;equal&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;!=&lt;/td&gt;
    &lt;td&gt;not equal&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;h3&gt;1. Equal to (==)&lt;/h3&gt;
&lt;p&gt;The operator symbol &lt;code&gt;==&lt;/code&gt; is used to compare if the two operands are equal.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 75
&amp;gt;&amp;gt;&amp;gt; b = 75
&amp;gt;&amp;gt;&amp;gt; c = a == b
&amp;gt;&amp;gt;&amp;gt; c
True
&amp;gt;&amp;gt;&amp;gt; a = 'tuesday'
&amp;gt;&amp;gt;&amp;gt; b = 'wednesday'
&amp;gt;&amp;gt;&amp;gt; c = a == b
&amp;gt;&amp;gt;&amp;gt; c
False
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variables &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are called operands.&lt;/p&gt;
&lt;p&gt;Here is an example script.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;age.py&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;romeo_age = 36
lando_age = 32

if romeo_age == lando_age:
    print('Romeo and Lando do have the same age.')
else:
    print('Romeo and Lando do not have the same age.')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Run&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python age.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;print('Romeo and Lando do have the same age.')
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Not equal to (!=)&lt;/h3&gt;
&lt;p&gt;The operator &lt;code&gt;!=&lt;/code&gt; means not equal. It compares if two operands are not equal. If it is not equal the result is True otherwise the result is False.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 45
&amp;gt;&amp;gt;&amp;gt; b = 50
&amp;gt;&amp;gt;&amp;gt; c = a != b
&amp;gt;&amp;gt;&amp;gt; c
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since &lt;code&gt;45&lt;/code&gt; is not equal to &lt;code&gt;50&lt;/code&gt; the result of the operation is True.&lt;/p&gt;
&lt;p&gt;Here is another example comparing two strings or str.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 'diamond'
&amp;gt;&amp;gt;&amp;gt; b = 'Diamond'
&amp;gt;&amp;gt;&amp;gt; c = a != b
&amp;gt;&amp;gt;&amp;gt; c
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Although the meaning of variables &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the same (diamond) they are not equal. This is because Python follows the rule of case sensitivity. In other words a lower case is different from upper case. &lt;code&gt;D&lt;/code&gt; is the upper case and &lt;code&gt;d&lt;/code&gt; is the lower case.&lt;/p&gt;
&lt;h3&gt;3. Greater than (&amp;gt;)&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;greater than&lt;/code&gt; operator with symbol &lt;code&gt;&amp;gt;&lt;/code&gt; allows us to know if the value in the left-hand side is greater than the value in the right-hand side. If it is, it would return True otherwise False.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 500
&amp;gt;&amp;gt;&amp;gt; b = 450
&amp;gt;&amp;gt;&amp;gt; c = a &amp;gt; b
&amp;gt;&amp;gt;&amp;gt; c
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;code&gt;a &amp;gt; b&lt;/code&gt;, the left-hand side is &lt;code&gt;a&lt;/code&gt; and the right-hand side is &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since 500 is more than 450, the result is True.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 1.25
&amp;gt;&amp;gt;&amp;gt; b = 1.26
&amp;gt;&amp;gt;&amp;gt; c = a &amp;gt; b
&amp;gt;&amp;gt;&amp;gt; c
False
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The value 1.25 is below 1.26 that is why the result of the operation is False.&lt;/p&gt;
&lt;h3&gt;4. Less than (&amp;lt;)&amp;lt;/h3&amp;gt;
The less than operator with symbol &quot;&amp;lt;&quot; is used to check if the value in the left-hand side is below the value in the right-hand side.
&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 36
&amp;gt;&amp;gt;&amp;gt; b = 45
&amp;gt;&amp;gt;&amp;gt; c = a &amp;lt; b
&amp;gt;&amp;gt;&amp;gt; c
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;
The value of the variable &quot;a&quot; is 36 while the value of the variable &quot;b&quot; is 45. The result is True because 36 is below 45.
5. Greater than or equal to (&amp;gt;=)&lt;/h3&gt;
&lt;p&gt;The greater than or equal to operator with symbol &lt;code&gt;&amp;gt;=&lt;/code&gt; compares if the operand in the left is greater than or equal to the operand in the right. If so the result is a True otherwise it is False.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; 100 &amp;gt;= 50
True
&amp;gt;&amp;gt;&amp;gt; 50 &amp;gt;= 75
False
&amp;gt;&amp;gt;&amp;gt; 25 &amp;gt;= 25
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;6. Less than or equal to (&amp;lt;=)&amp;lt;/h3&amp;gt;
The comparison operator with symbol &lt;code&gt;&amp;lt;=&amp;lt;/code&amp;gt; checks if the operand in the left is less than or equal to the operand in the right.
&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; 50 &amp;gt;= 75
False
&amp;gt;&amp;gt;&amp;gt; 25 &amp;gt;= 25
True
&amp;gt;&amp;gt;&amp;gt; 12 &amp;lt;= 13
True
&amp;gt;&amp;gt;&amp;gt; 14 &amp;lt;= 10
False
&amp;gt;&amp;gt;&amp;gt; 8 &amp;lt;= 8
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;

&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;In comparison operators, the result is always True or False.&lt;/span&gt;

D. Assignment Operators
These operators primarily assign the value in the right side to the variable in the left side based on the operator used such as &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;+=&lt;/code&gt; and more.
1. Simple assignment (=)&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The equal symbol &lt;code&gt;=&lt;/code&gt; assigns the value in the right side to the variable in the left side. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 45
&amp;gt;&amp;gt;&amp;gt; a
45
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Addition assignment (+=)&lt;/h3&gt;
&lt;p&gt;The operator &lt;code&gt;+=&lt;/code&gt; is a combination of arithmetic operator &lt;code&gt;+&lt;/code&gt; and assignment operator &lt;code&gt;=&lt;/code&gt;. It adds the value in the right side to the existing value in the left side.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10
&amp;gt;&amp;gt;&amp;gt; a += 4
&amp;gt;&amp;gt;&amp;gt; a
14
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is the same as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10
&amp;gt;&amp;gt;&amp;gt; a = a + 4
&amp;gt;&amp;gt;&amp;gt; a
14
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. Subtraction assignment (-=)&lt;/h3&gt;
&lt;p&gt;This is similar to item 2 except that the arithmetic operation is subtraction.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10
&amp;gt;&amp;gt;&amp;gt; a -= 4
&amp;gt;&amp;gt;&amp;gt; a
6
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;4. Multiplication assignment (*=)&lt;/h3&gt;
&lt;p&gt;The assignment operator &lt;code&gt;*=&lt;/code&gt; is a combination of multiplication and assignment.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 2
&amp;gt;&amp;gt;&amp;gt; a *= 2
&amp;gt;&amp;gt;&amp;gt; a
4
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is similar to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 2
&amp;gt;&amp;gt;&amp;gt; a = a * 2
&amp;gt;&amp;gt;&amp;gt; a
4
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;5. Division assignment (/=)&lt;/h3&gt;
&lt;p&gt;The assignment operator &lt;code&gt;/=&lt;/code&gt; is a combination of division and assignment.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; a /= 2
&amp;gt;&amp;gt;&amp;gt; a
50.0
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is similar to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; a = a / 2
&amp;gt;&amp;gt;&amp;gt; a
50.0
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;6. Modulus assignment (%=)&lt;/h3&gt;
&lt;p&gt;The modulo operator &lt;code&gt;%&lt;/code&gt; is used to get the remainder. Together with assignment &lt;code&gt;=&lt;/code&gt; operator, they are used to assign the remainder in the left side by taking the remainder between the left side value and right side value.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; a %= 3
&amp;gt;&amp;gt;&amp;gt; a
1
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is similar to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 100
&amp;gt;&amp;gt;&amp;gt; a = 100 % 3
&amp;gt;&amp;gt;&amp;gt; a
1
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;7. Exponentiation assignment (**=)&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;**=&lt;/code&gt; operator uses the value in the left side as a base and raises to the power by the value in the right side then assigns the result to the variable in the left side.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 2
&amp;gt;&amp;gt;&amp;gt; a **= 4
&amp;gt;&amp;gt;&amp;gt; a
16
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the same as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 2
&amp;gt;&amp;gt;&amp;gt; a = 2 ** 4
&amp;gt;&amp;gt;&amp;gt; a
16
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;2**4&lt;/code&gt; is just &lt;code&gt;2*2*2*2&lt;/code&gt; which is equal to 16. Also the &lt;code&gt;2**4&lt;/code&gt; is just &lt;code&gt;2^4&lt;/code&gt; or 2 raised to power 4.&lt;/p&gt;
&lt;h3&gt;8. Floor division assignment (//=)&lt;/h3&gt;
&lt;p&gt;It is a combination of floor division and assignment operations. It takes the number in the left side and applies a floor division with the number in the right side and assigns it back to the left side. Floor division is just a division where the remainder is disregarded.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 10
&amp;gt;&amp;gt;&amp;gt; a //= 2
&amp;gt;&amp;gt;&amp;gt; a
5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is similar to &lt;code&gt;10//2&lt;/code&gt; where the answer is 5 and then assign the &lt;code&gt;5&lt;/code&gt; back to variable &lt;code&gt;a&lt;/code&gt;. Note there is no remainder because 10 divided by 2 is perfectly 5.&lt;/p&gt;
&lt;p&gt;Consider another example with remainder.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a = 3
&amp;gt;&amp;gt;&amp;gt; a //= 2
&amp;gt;&amp;gt;&amp;gt; a
1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first step is to divide 3 by 2 and the answer is 1 with a remainder of 1.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;3 / 2 = 1 remainder 1

value = 2 * 1
remainder = 3 - value
remainder = 3 - 2
remainder = 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In floor division &lt;code&gt;//&lt;/code&gt; we ignore the remainder so the final answer is 1.&lt;/p&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;span class=&quot;faq-body&quot;&gt;
1. What is REPL??&lt;br&gt;
Answer: REPL stands for Read Evaluate Print Loop. It is used to test the code interactively. The user inputs the code and Python will READ, EVALUATE and PRINT the results. Install Python, open terminal and type Python to start the REPL.&lt;br&gt;&lt;br&gt;

2. What happens to the remainder in floor division?&lt;br&gt;
Answer: The remainder is discarded or not used.&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;E. Summary&lt;/h2&gt;
&lt;p&gt;Arithmetic operators such as &lt;strong&gt;`+, -, *, /, //, **, and %`&lt;/strong&gt; are used for basic operations like addition, subtraction, multiplication, division, floor division, exponentiation, and modulo respectively.&lt;/p&gt;
&lt;p&gt;Comparison operators such as &lt;strong&gt;`&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=, ==, !='&lt;/strong&gt; are used to compare operands that may yield True or False results.&lt;/p&gt;
&lt;p&gt;Assignment operators such as &lt;strong&gt;`=, +=, -=, *=, /=, %=, **=, and //=`&lt;/strong&gt; are used to assign values to variables. They allow for direct modification of variables by performing arithmetic or other operations on them.&lt;/p&gt;
&lt;p&gt;It is important for a programmer to learn this basic operators as a foundation to learn advanced topics such as &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/27/read-all-files-in-a-directory-in-python&quot; target=&quot;_blank&quot;&gt;reading files in directory&lt;/a&gt;. You should also learn &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/174/python-syntax&quot; target=&quot;_blank&quot;&gt;Python syntax&lt;/a&gt;, which helps in avoiding errors in your code.&lt;/p&gt;
</description>
<category>Python Basics</category>
<guid isPermaLink="true">https://coderlegion.com/294/basic-operators-in-python</guid>
<pubDate>Sun, 19 May 2024 10:05:23 +0000</pubDate>
</item>
</channel>
</rss>