# Mastering Python Operators (Part 2)

In our previous blog posts, we extensively covered the essential [operators](https://www.geeksforgeeks.org/python-programming-language/) in [Python](https://www.python.org/downloads/), focusing on [Arithmetic](https://blog.bytescrum.com/mastering-python-operators-part-1#cllrv6r4o03qigpnv2z0whew1) and Assignment [Operators](https://www.digitalocean.com/community/tutorials/python-operators).

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_blank" rel="noopener noreferrer nofollow" href="https://blog.bytescrum.com/mastering-python-operators-part-1#cllrv6r4o03qigpnv2z0whew1" style="pointer-events: none">https://blog.bytescrum.com/mastering-python-operators-part-1#cllrv6r4o03qigpnv2z0whew1</a></div>
</div>

This blog post explores essential [Python operators](https://www.w3schools.com/python/python_operators.asp), including This post expands our exploration to encompass other crucial [Python operators](https://www.geeksforgeeks.org/python-operators/), such as Relational, Logical, Boolean, Bitwise, Membership, and Identity. These [operators](https://www.codecademy.com/catalog/language/python) are vital for decision-making and sophisticated programming techniques, enabling value comparisons, managing complex conditions, and facilitating data sequence searches. Furthermore, Identity [Operators](https://www.programiz.com/python-programming/operators) play a significant role in memory management.

## **Relational (Comparison)** [**Operator**](https://www.tutorialspoint.com/python/index.htm)**s: Compare values and make decisions.**

**Relational (Comparison)** [**Operators**](https://www.learnpython.org/) play a crucial role in assessing connections between values, allowing you to compare items and make choices depending on their magnitudes. Conditional statements and branching logic are built around these [operators](https://www.javatpoint.com/python-operators).

These comparison [operators](https://mindmajix.com/python/basic-operators-in-python) can be used with values or expressions.

* **Equal to** [**Operator**](https://data-flair.training/blogs/python-operator/)
    
* **Not equal to** [**Operator**](https://www.w3resource.com/python/python-operators.php)
    
* **Less than** [**Operator**](https://runestone.academy/ns/books/published/fopp/Conditionals/PrecedenceofOperators.html)
    
* **Greater than** [**Operator**](https://www.scaler.com/topics/python/operators-in-python/)
    
* **Less than or equal to** [**Operator**](https://www.toppr.com/guides/python-guide/tutorials/python-introduction/operators/python-operators-arithmetic-comparison-logical/)
    
* **Greater than or equal to** [**Operator**](https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/python.html)
    

### Equal to [Operator](https://www.pluralsight.com/cloud-guru/courses/introduction-to-python-development?utm_source=google&utm_medium=paid-search&utm_campaign=upskilling-and-reskilling&utm_term=ssi-apac-in-dynamic&utm_content=free-trial&gclid=Cj0KCQjwi7GnBhDXARIsAFLvH4nH2lqpptvxc3nb3FQjn4LJyPz9PDRJ_dUxGcdOws2Lw2gM0eCKIb0aAsI0EALw_wcB)(**\==**)

*Returns True if the* [*operands*](https://www.toppr.com/guides/computer-science/introduction-to-python/variables-expressions-and-statements/operators-and-operands-in-python/) *are equal.*

The **<mark>== </mark>** [operator](https://pynative.com/python-operators/) determines if the variable values on the two sides are equal. The expression evaluates to **True** if they are, and to **False** if they are not.

```python
#Numbers Comparison 
  x = 3
  y = 3
  result = x == y  # x is equal to y
  print(result) # Output: True
  # Strings Comparison
  a = "ByteScrum"
  b = "IT Solutions"
  result = a == b  #a string is not matching with b string
  print(result)    # Output: False

# User authentication example ByteScrum Technologies Private Limited
  username = "Technologies Private Limited"
  password = "ByteScrum" 

  input_username = input("Enter your username: ")
  input_password = input("Enter your password: ")

  if input_username == susername and input_password == password:
    print("Authentication successful! Welcome,", username)
  else:
    print("Authentication failed. Please check your username and password.")
```

A simple user authentication procedure is simulated by the application. The user must input their username and password. To compare the entered login and password with the previously saved username and password, use the **<mark>== </mark>** [operator](https://www.koenig-solutions.com/python-intro-certification-training-course?keyword=&device=c&keyword=&device=c&utm_source=google&utm_medium=cpc&utm_device=c&utm_campaign=DSA-Category-adgroup-DSA-PPC&gclid=Cj0KCQjwi7GnBhDXARIsAFLvH4nb1MOrHn-Dm1rh0WCQAFluVDrqjUqO1-jOW0rAcJJK_vsy1e_ICWcaAib8EALw_wcB). The user is given access if both the submitted username and password match the values that are currently in the database; otherwise, access is refused.

### **Not equal to** [**Operator**](https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-for-python.html?cid=sem&source=sa360&campid=2023_q3_iags_in_iagsoapie_iagsoapiee_awa_text-link_exact_cd_dpd-oneapi-python_3500123524_google_div_oos_non-pbm_intel&ad_group=hpc-accelerated_computing_exact&intel_term=what+is+python+coding&sa360id=43700077484369758&gclid=Cj0KCQjwi7GnBhDXARIsAFLvH4mnvkSOQL97vGT1vTz-jWLRIOhCIh80NURzGyyfyWJFYGg_tVFGo1YaAnZ1EALw_wcB&gclsrc=aw.ds)**(**!=**)**

*Returns True if the* [*operands*](https://www.geeksforgeeks.org/python-operators/) *are not equal.*

The **!=** [operator](https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/python.html) determines if the values of the variables on the two sides are equal. In the case when they are not equal, the expression evaluates to **True**; in the absence of that, it evaluates to **False**.

```python
x = 10
y = 5
result = x != y  # x is not equal to y
print(result)    # Output: True

a = "ByteScrum"
b = "ByteScrum"
result = a != b  #  'ByteScrum' is equal to 'ByteScrum'
print(result)    # Output: False
```

### **Less than** [**Operator**](https://www.simplilearn.com/tutorials/python-tutorial/operators-in-python) **(&lt;)**

*Returns True if the left operand is less than the right operand.*

Decisions based on the relative magnitudes of variables are frequently made using the less-than [operator](https://python-reference.readthedocs.io/en/latest/docs/operators/)(&lt;). It is a vital part of conditional statements, which aid in your program's ability to respond intelligently to various circumstances.

```python
age = 15
eligible_age = 18

can_vote = age < eligible_age # less than eligible_age (18).

if can_vote:
    print("You can't vote yet.")
else:
    print("You can vote!")
```

The operator is used in this instance to contrast the value of age, which is 15 years old, with the value of **<mark>eligible_age</mark>**, which is 18 years old. Since 15 is less than 18, the **<mark>can_vote </mark>** variable is now True. Because the requirement has been satisfied, the code then writes "**You can't vote yet**."

### Greater than **Operator (&gt;)**

*Returns True if the left operand is greater than the right operand*

```python
student_marks = 85
passing_score = 60

result = student_marks > passing_score  
print(result)    # Output: True

high_score = 95
result = high_score > student_marks  

low_score = 40
result = low_score > student_marks  
print(result)    # Output: False
```

When comparing student grades with passing and high scores, the **\&gt;** [operator](https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/python.html) is utilized. The expression evaluates to **True** if the left operand is greater than the right operand; otherwise, it evaluates to **False**.

### Less than or equal to (&lt;=) **Operator**

*Returns True if the left operand is less than or equal to the right operand.*

```python
current_temperature = 28
max_safe_temperature = 30

result = current_temperature <= max_safe_temperature 
print(result)    # Output: True

critical_temperature = 35
result = critical_temperature <= max_safe_temperature 
print(result)    # Output: False
```

The current temperature is compared to a maximum safe temperature threshold using the **<mark>&lt;= </mark>** [operator](https://pynative.com/python-operators/). The formula evaluates to **True** if the current temperature is less than or equal to the maximum safe temperature; otherwise, it evaluates to **False**.

### **G**reater than or equal to(&gt;=) [**Operator**](http://www.trytoprogram.com/python-programming/python-operators/)

*Returns True if the left operand is greater than or equal to the right operand*.

```python
football_score = 5
passing_score = 6

result = football_score >= passing_score  
print(result)    # Output: False

minimum_score = 3
result = minimum_score >= football_score  
print(result)  # Output: True
```

The **football\_score** is compared to the minimum and passing requirements using the **<mark>&gt;=</mark>** [operator](https://www.prepbytes.com/blog/python/what-are-the-python-operator-and-how-to-use-them/). The expression evaluates to True if the left operand is larger than or equal to the right operand; otherwise, it evaluates to False.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Relational (Comparison) <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.shiksha.com/online-courses/articles/operators-in-python/" style="pointer-events: none">Operators</a> enable you to compare objects and make decisions based on their magnitudes while assisting in the assessment of relationships between values. They are necessary for branching logic and conditional statements.</div>
</div>

## **Logical (Boolean)** [**Operator**](https://net-informations.com/python/basics/operators.htm)**s: Handle complex conditions and create logical pathways.**

*Logical* [*operators*](https://docs.python.org/3/library/operator.html) *are used to combine conditional statements and return Boolean values*

By combining and negating logical expressions, logical [operators](https://docs.python.org/3/reference/datamodel.html) enable you to manipulate Boolean values and create complicated situations. They play a crucial role in developing logical pathways and dictating software behavior.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>True and False:</strong> Represent the two possible&nbsp;Boolean&nbsp;values</div>
</div>

These [operators](https://realpython.com/python-operators-expressions/) are used to perform logical operations.

* **Logical AND** [**Operator**](https://www.freecodecamp.org/news/operators-in-python-how-to-use-logical-operators-in-python/)
    
* **Logical OR** [**Operator**](https://flexiple.com/python/arithmetic-operators-in-python/)
    
* **Logical NOT** [**Operator**](https://flexiple.com/python/comparison-operators-in-python/)
    

### Logical AND [Operator](https://hevodata.com/learn/python-operator-in-airflow/)

*Returns True if both* [*operands*](https://www.tutorialspoint.com/python/python_basic_operators.htm) *are True*

```python
#This operator returns True if both operands being evaluated are True, and False otherwise.
is_raining = True
has_umbrella = True

if is_raining and has_umbrella:
    print("You can go outside with an umbrella.")
else:
    print("You should stay indoors.")
```

To combine the requirements **<mark>is_raining </mark>** and **<mark>has_umbrella</mark>**, the and [operator](https://hackr.io/blog/python-operators) is used. The code included within the if block is run if both criteria are True, suggesting that it is okay to step outdoors with an umbrella. The else block is run, telling the individual to stay inside if at least one of the criteria is False.

### Logical OR Operator

*Returns True if at least one of the* [*operands*](https://runestone.academy/ns/books/published/fopp/SimplePythonData/Operators.html) *is True.*

```python
has_ticket = False
is_weekend = True

if has_ticket or is_weekend:
    print("You can attend the event.")
else:
    print("Access denied. Please check your ticket or event day.")
```

The **<mark>has_ticket</mark>** and **<mark>is_weekend</mark>** criteria are combined using the or [operator](https://www.scholarhat.com/tutorial/python/operators-of-python) to determine if a person has a valid ticket and whether the event is on a weekend. The code included within the if block is run, allowing access to the event if at least one of these criteria is True. The else block, which indicates that access is forbidden, is performed if both criteria are False.

### Logical NOT [Operator](https://www.codingninjas.com/studio/library/python-operators)

*Returns the opposite Boolean value of the* [*operand*](https://runestone.academy/ns/books/published/thinkcspy/SimplePythonData/OperatorsandOperands.html)*.*

The operand's opposite Boolean value is returned by this [operator](https://www.developer.com/guides/using-python-numbers-operators-math/). If the operand is **False**, it returns **True**; if it is **True,** it returns **False**.

```python
is_raining = True

if not is_raining:
    print("It's not raining. You can go for a walk.")
else:
    print("It's raining. Stay indoors.")
```

The not [operator](https://www.upgrad.com/blog/operators-in-python/) is used to negate the value of the **is\_raining** variable. If **is\_raining** is True, the not [operator](https://www.edureka.co/blog/operators-in-python/) makes it **False**, and the code inside the if block is executed, suggesting going for a walk. If **is\_raining** is **False**, the not [operator](https://medium.com/@technicadil_001/operators-in-python-5f62ba5b89d8) makes it **True**, and the else block is executed, advising to stay indoors.

to be continued ...

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Conditional statements are combined with logical <a target="_blank" rel="noopener noreferrer nofollow" href="https://isip.piconepress.com/courses/temple/ece_3822/resources/tutorials/python/python_basic_operators.pdf" style="pointer-events: none">operators </a>(also known as "Boolean <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.sevenmentor.com/operators-in-python" style="pointer-events: none">Operators</a>") to produce Boolean results. They play a key role in creating complicated circumstances and manipulating Boolean values, which is essential for creating logical pathways and influencing program behavior.</div>
</div>

<details data-node-type="hn-details-summary"><summary>Summary</summary><div data-type="detailsContent">In summary, learning <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.python.org/" style="pointer-events: none">Python</a> <a target="_blank" rel="noopener noreferrer nofollow" href="https://intellipaat.com/blog/tutorial/python-tutorial/python-operators/" style="pointer-events: none">operators</a> is essential for productive decision- and programming-making. Comparisons are made possible, complex conditions are handled, and data sequence searches are aided by the use of relational, logical, boolean, bitwise, membership, and identity operators. You can build effective and potent <a target="_blank" rel="noopener noreferrer nofollow" href="https://docs.python.org/3/tutorial/datastructures.html" style="pointer-events: none">Python</a> programs that react shrewdly to numerous scenarios by comprehending and using these <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.educative.io/answers/what-is-operator-precedence-in-python" style="pointer-events: none">operator</a>s.</div></details>

**Note:** Essential [operators](https://techvidvan.com/tutorials/python-operators/) including **Relational, Logical, and Boolean**, are covered in Mastering [Python](https://docs.python.org/3/reference/simple_stmts.html) [Operators](https://en.wikipedia.org/wiki/Python_(programming_language)) (Part 2). These [operators](https://www.w3schools.com/python/) support sophisticated programming approaches, value comparison, managing complicated circumstances, and exploring data sequences. They also aid in decision-making.
