Python Operands and Operators - Chapter: 3

Hey there! I'm a passionate tech enthusiast with a diverse skill set in software development, DevOps, and cybersecurity. I love building robust APIs with Python Django and creating interactive user interfaces with React.js. My journey has taken me through the fascinating world of IoT, where I've tinkered with Arduino and Rasberry Pi devices, and I've also delved into the exciting realm of ethical hacking and penetration testing.
Technologies I Know:
Python Django Framework for API: I have hands-on experience in building APIs using the powerful Python Django framework, enabling me to create scalable and efficient web applications.
React.js for Frontend Development: With proficiency in React.js, I craft engaging and responsive user interfaces, making the web experience seamless and enjoyable.
Python Automation Testing with Selenium, Pytest, and Appium: My expertise in automation testing using Selenium, Pytest, and Appium ensures that software projects are thoroughly tested, reducing manual effort and improving the overall quality of the code.
AWS DevOps with Git, Jenkins, Docker, SonarQube, Terraform, Ansible, Kubernetes, and Maven: I've mastered various DevOps tools and practices on the AWS platform, streamlining the development and deployment process. Git, Jenkins, Docker, SonarQube, Terraform, Ansible, Kubernetes, and Maven are among the tools I'm proficient in.
IoT with Arduino and Raspberry Pi: Exploring the Internet of Things world, I've successfully worked with Arduino and Raspberry Pi to build exciting and innovative projects that leverage sensor data and real-world interactions.
Certified Ethical Hacking and Penetration Testing: My passion for cybersecurity has driven me to become a Certified Ethical Hacker. I conduct penetration testing to identify and fix security vulnerabilities, ensuring systems and applications are protected against potential threats.
Operators and Operands:
In Python, operators are symbols that represent specific actions or computations on operands. Operands, on the other hand, are the variables or values that operators act upon. Python supports a variety of operators, including arithmetic, comparison, assignment, logical, membership, and identity operators, each serving distinct purposes in programming.
Eg:

1) Arithmetic / Mathematical Operators:
Arithmetic operators are used to perform basic mathematical operations on numerical data types, such as integers and floating-point numbers. The commonly used arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation ().**
| Operator | Description | Example | Result |
| + | Addition: Adds two operands | 5 + 3 | 8 |
| - | Subtraction: Subtracts right operand from left | 10 - 4 | 6 |
| * | Multiplication: Multiplies two operands | 3 * 7 | 21 |
| / | Division: To find out the quotient, divide the left operand by the right operand | 15 / 5 | 3.0 |
| % | Modulus: Returns the remainder of the division | 20 % 3 | 2 |
| ** | Exponentiation: Raises left operand to the power of right | 2 ** 3 | 8 |
| // | Floor Division: To find out the quotient, round down to the nearest whole number | 13 // 5 | 2 |
Note: Remember that when performing arithmetic operations, the data types of the operands can affect the result. For example, division (/) always returns a float, even if both operands are integers. In contrast, floor division (//) returns an integer when both operands are integers.
Eg:

2) Assignment Operator:
The ' = '(Assignment) operator assigns the value from the right-hand side operand to the left-hand side variable. The assignment operator combines the effects of arithmetic and the assignment operator. Common assignment operators include equals (=), addition and assignment (+=), subtraction and assignment (-=), multiplication and assignment (*=), division and assignment (/=), and more.

Note: These assignment operators allow you to perform a specific operation on the variable on the left and update its value using the result of that operation. They provide a concise and efficient way to modify variables in Python.
3) Comparison / Relational Operators:
Comparison operators are used to compare two values or variables. They return a Boolean value (True or False) based on the comparison result. Common comparison operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
| Operator | Description | Example |
| \== | Equal to | 5 == 5 -> True |
| != | Not equal to | 5 != 3 -> True |
| > | Greater than | 10 > 5 -> True |
| < | Less than | 3 < 8 -> True |
| >= | Greater than or equal to | 5 >= 5 -> True |
| <= | Less than or equal to | 2 <= 4 -> True |
Equal to (==): Checks if the left operand is equal to the right operand. Returns
Trueif they are equal; otherwise, returnsFalse.Not equal to (!=): Checks if the left operand is not equal to the right operand. Returns
Trueif they are not equal; otherwise, returnsFalse.Greater than (>): Checks if the left operand is greater than the right operand. Returns
Trueif it is; otherwise, returnsFalse.Less than (<): Checks if the left operand is less than the right operand. Returns
Trueif it is; otherwise, returnsFalse.Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand. Returns
Trueif it is; otherwise, returnsFalse.Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand. Returns
Trueif it is; otherwise, returnsFalse.Note: Comparison operators are commonly used in conditional statements and loops to evaluate conditions and make decisions based on the results of these comparisons. They play a crucial role in controlling the flow of a program and implementing various logics.
Eg:

4) Logical Operators:
Logical operators are used to combine multiple conditions and perform logical operations. The common logical operators are and, or, and not.

and Operator:
The
andthe operator returns True only if both operands are True; otherwise, it returns False.It performs short-circuit evaluation, which means if the first operand is False, it won't check the second operand because the result will always be False.
Example: In the expression
True and False, the first operand is True, but the second operand is False. Since both operands are not True, the result is False.
or Operator:
The
orthe operator returns True if at least one of the operands is True; otherwise, it returns False.It performs short-circuit evaluation, which means if the first operand is True, it won't check the second operand because the result will always be True.
Example: In the expression
True or False, the first operand is True, so there's no need to check the second operand, and the result is True.
not Operator:
The
notthe operator returns the negation of the operand. If the operand is True,notwill make it False, and vice versa.It takes only one operand and flips its truth value.
Example: In the expression
not True, the operand is True, and the result is False. Innot False, the operand is False, and the result is True.
Note: In Python, logical operators can be used with any data type that can be evaluated as True or False (also known as truthy or falsy). For instance, numbers (except 0), non-empty strings, and non-empty containers are considered truthy, while 0, empty strings, and empty containers are considered falsy. This allows you to use logical operators beyond just Boolean values.
5) Membership Operators:
Membership operators are used to test whether a value is present in a sequence (like a string, list, tuple, etc.). The membership operators are in and not in.
in operator: It returns True if a character/substring exists in the given sequence (like a string, list, tuple, etc.)
not in operator: It returns True if does a character/substring not exist in the given sequence (like a string, list, tuple, etc.)
| Operator | Description | Example |
in | Returns True if the specified value is present in the specified sequence (like a string, list, tuple, or dictionary). | 5 in [1, 2, 3, 4, 5] will return True since 5 is present in the list. |
'l' in 'hello' will return True since the character 'l' is present in the string. | ||
'key' in {'key': 10, 'value': 20} will return True since 'key' is a key in the dictionary. | ||
'apple' in ['orange', 'banana'] will return False since 'apple' is not present in the list. | ||
not in | Returns True if the specified value is NOT present in the specified sequence. | 7 not in [1, 2, 3, 4, 5] will return True since 7 is not present on the list. |
'x' not in 'hello' will return True since the character 'x' is not present in the string. | ||
'key' not in {'key': 10, 'value': 20} will return False since 'key' is a key in the dictionary. | ||
'apple' not in ['orange', 'banana'] will return True since 'apple' is not present in the list. |
Membership operators (in and not in) are used to test if a value exists in a sequence or not. They are commonly used in conditional statements and loops to check for the presence of specific elements in lists, strings, tuples, and dictionaries.
6) Identity Operators:
Identity operators are used to compare the memory locations of two objects. The identity operators are is and is not.
is operator: It returns True if both variables are pointing to the same object.
is not operator: It returns True if both variables are not pointing to the same object.

Eg:

Previous Chapter: Chapter - 2
Next Chapter: Chapter - 4



