# Use comparison and logical operators in Python

# Use comparison and logical operators in Python

## 1. Use operators, operands and expressions to perform arithmetic operations like addition, subtraction, multiplication and division.

Use the following Arithmetic operators to perform various arithmetic operations. + Addition operator. For example: 100 + 45 = 145 - Subtraction operator. For example: 500 - 65 = 435 * Multiplication operator. For example: 25 * 4 = 100 / Float Division Operator. For example: 10 / 2 = 5.0 // Integer Division Operator. For example: 10 // 2 = 5 For example: >>> 6//3 2 >>> >>> 100//6 16

## 2. Use the ** operator to calculate a to the power of b.

For example: >>> 21**2 441 >>> >>> 5**2.2 34.493241536530384

## 3. Use the % operator to divide the left operand by the right operand and return the remainder.

Remainder Operator (%): Use % operator to divide the left operand by the right operand. For example: >>> 5%2 1

## 4. Increment or decrement the value of a variable and then reassign the value back to the same variable using compound assignment operators.

For example: x = 10 x = x + 5 Use a compound assignment operator to simplify the function: x += 5

## 5. To perform arithmetic operations with multiple data types, use type conversions.

For example, multiplying an integer variable with a float variable. >>> 45 * 3 135 # result is int >>> >>> 3.4 * 5.3 18.02 # result is float >>> >>> 88 * 4.3 378.4 # result is float >>> To convert data from one type to another, use functions like: int() accepts a string or number and returns a value of type integer. float() accepts a string or number and returns a value of type float. str() accepts any value and returns a value type string. For example: >>> int(2.7) # convert 2.7 to integer 2 >>> >>> float(42) # convert 42 to float 42.0 >>> >>> str(3.4) # convert 3.4 to str '3.4' >>>

## 6. Use the bool type to store a true or false value for a variable or validate an expression for a value of true or false.

Examples: >>> >>> var1 = True >>> var2 = False >>> >>> type(var1) # Checking var1 type >>> >>> type(var2) # Checking var2 type >>> >>> >>> var1 True >>> >>> var2 False >>>

## 7. Use relational operators to compare the values of two or more variables.

Relational operators include: Greater than = Greater than or equal to != Not equal to == Equal to For example: >>> >>> 3 >> >>> 90 > 450 False >>> >>> 10 >> >>> 31 >= 40 False >>> >>> 100 != 101 True >>> >>> 50 == 50 True >>>

## 8. Use logical operators to combine two or more boolean expressions and test whether they are true or false.

Logical operators: and or not For example: >>> >>> (10>3) and (15>6) True >>> >>> (1>5) and (43==6) False >>> >>> (1==1) and (2!=2) False >>>