Mastering Python Operators: A Simple and Easy Guide with Code Examples

codepythonic
0

 Easy-to-learn Python operator notes with examples:


1. Arithmetic Operators:

   - Addition (+): Adds two numbers.

   - Subtraction (-): Subtracts the second number from the first.

   - Multiplication (*): Multiplies two numbers.

   - Division (/): Divides the first number by the second.

   - Modulus (%): Returns the remainder after division.

   - Exponentiation (**): Raises the first number to the power of the second.


   Example:

   ```python

   a = 10

   b = 3

   print(a + b) # Output: 13

   print(a - b) # Output: 7

   print(a * b) # Output: 30

   print(a / b) # Output: 3.333...

   print(a % b) # Output: 1

   print(a ** b) # Output: 1000

   ```


2. Assignment Operators:

   - Equals (=): Assigns the value on the right to the variable on the left.

   - Add and Assign (+=): Adds the right value to the variable and assigns the result.

   - Subtract and Assign (-=): Subtracts the right value from the variable and assigns the result.

   - Multiply and Assign (*=): Multiplies the variable by the right value and assigns the result.

   - Divide and Assign (/=): Divides the variable by the right value and assigns the result.

   - Modulus and Assign (%=): Performs modulus operation on the variable and assigns the result.

   

   Example:

   ```python

   x = 5

   x += 2 # Equivalent to x = x + 2

   print(x) # Output: 7


   y = 10

   y -= 3 # Equivalent to y = y - 3

   print(y) # Output: 7

   ```


3. Comparison Operators:

   - Equal to (==): Checks if two values are equal.

   - Not equal to (!=): Checks if two values are not equal.

   - Greater than (>): Checks if the left value is greater than the right value.

   - Less than (<): Checks if the left value is less than the right value.

   - Greater than or equal to (>=): Checks if the left value is greater than or equal to the right value.

   - Less than or equal to (<=): Checks if the left value is less than or equal to the right value.


   Example:

   ```python

   a = 5

   b = 7

   print(a == b) # Output: False

   print(a != b) # Output: True

   print(a > b) # Output: False

   print(a < b) # Output: True

   ```


4. Logical Operators:

   - and: Returns True if both operands are True.

   - or: Returns True if at least one operand is True.

   - not: Returns True if the operand is False, and vice versa.


   Example:

   ```python

   x = 5

   y = 10

   print(x > 0 and y < 15) # Output: True

   print(x > 7 or y < 8) # Output: False

   print(not x > 0) # Output: False

   ```


5. Identity Operators:

   - is: Returns True if both variables refer to the same object.

   - is not: Returns True if both variables do not refer to the same object.


   Example:

   ```python

   p = [1, 2, 3]

   q = p

   r = [1, 2, 3]

   print(p is q) # Output: True

   print(p is r) # Output: False

   print(p is not r) # Output: True

   ```


6. Membership Operators:

   - in: Returns True if a value is present in a sequence.

   - not in: Returns True if a value is not present in a sequence.


   Example:

   ```python

   list_numbers = [1, 2, 3, 4, 5]

   print(3 in list_numbers) # Output: True

   print(6 not in list_numbers) # Output: True

   ```


I hope these notes and examples are helpful for your Python tutorial blog! Happy coding and blogging!

Post a Comment

0Comments

Post a Comment (0)