# Python – codewindow.in

## Related Topics ## Python Programing

#### Here are some examples of using the modulo operator in Python:

``````a = 10
b = 3

# Modulo operation to find the remainder of dividing a by b
c = a % b   # c is assigned the value of 1

# Another example
x = 25
y = 5
z = x % y   # z is assigned the value of 0, since 25 is evenly divisible by 5
``````

• #### Equal to: `==`

``````a = 5
b = 3
print(a == b)   # prints False
``````
• #### Not equal to: `!=`

``````a = 5
b = 3
print(a != b)   # prints True
``````
• #### Greater than: `>`

``````a = 5
b = 3
print(a > b)    # prints True
``````
• #### Less than: `<`

``````a = 5
b = 3
print(a < b)    # prints False
``````
• #### Greater than or equal to: `>=`

``````a = 5
b = 3
print(a >= b)   # prints True
``````
• #### Not equal to: `!=`

``````a = 5
b = 3
print(a != b)   # prints True
``````
• #### Less than or equal to: `<=`

``````a = 5
b = 3
print(a <= b)   # prints False
``````

• #### `and`: This operator returns `True` if both operands are `True`, and `False` otherwise.

``````a = True
b = False
print(a and b)    # prints False
``````
• #### `or`: This operator returns `True` if either operand is `True`, and `False` otherwise.

``````a = True
b = False
print(a or b)     # prints True
``````
• #### `not`: This operator returns the opposite Boolean value of the operand.

``````a = True
print(not a)      # prints False
``````

#### You can also chain together multiple logical operations using parentheses to control the order of evaluation. For example:

``````a = True
b = False
c = True
print((a and b) or c)   # prints True
``````

• #### Bitwise AND (`&`): This operator returns a new integer whose bits are set to 1 in both operands.

``````a = 0b1010  # binary representation of 10
b = 0b1100  # binary representation of 12
c = a & b   # bitwise AND of a and b
print(bin(c))  # prints 0b1000, which is 8 in binary
``````
• #### Bitwise OR (`|`): This operator returns a new integer whose bits are set to 1 in at least one of the operands.

``````a = 0b1010  # binary representation of 10
b = 0b1100  # binary representation of 12
c = a | b   # bitwise OR of a and b
print(bin(c))  # prints 0b1110, which is 14 in binary
``````
• #### Bitwise XOR (`^`): This operator returns a new integer whose bits are set to 1 in exactly one of the operands.

``````a = 0b1010  # binary representation of 10
b = 0b1100  # binary representation of 12
c = a ^ b   # bitwise XOR of a and b
print(bin(c))  # prints 0b0110, which is 6 in binary
``````

• #### Bitwise complement (`~`): This operator returns the complement of the input integer by flipping all of its bits.

``````a = 0b1010  # binary representation of 10
b = ~a      # bitwise complement of a
print(bin(b))  # prints -0b1011, which is -11 in binary
``````
• #### Bitwise shift (`<<` and `>>`): These operators shift the bits of an integer left or right by a specified number of positions. The `<<` operator shifts left, filling in the empty bits with 0’s, while the `>>` operator shifts right, filling in the empty bits with the original sign bit.

``````a = 0b1010  # binary representation of 10
b = a << 1  # shift a left by 1 position
print(bin(b))  # prints 0b10100, which is 20 in binary

c = 0b1100  # binary representation of 12
d = c >> 2  # shift c right by 2 positions
print(bin(d))  # prints 0b11, which is 3 in binary
``````

#### For example, consider the following expression:

``````result = 2 + 3 * 4
``````

#### However, if we wanted to evaluate the addition operation first, we could use parentheses to group the addition and multiplication operations together, like this:

``````result = (2 + 3) * 4
``````

1. #### Integer to float: To convert an integer to a float, use the `float()` function.

``````x = 5
y = float(x)
print(y)   # Output: 5.0
``````
1. #### Float to integer: To convert a float to an integer, use the `int()` function. Note that this will truncate the decimal portion of the float.

``````x = 3.14
y = int(x)
print(y)   # Output: 3
``````
1. #### Integer to string: To convert an integer to a string, use the `str()` function.

``````x = 42
y = str(x)
print(y)   # Output: "42"
``````
1. #### Float to string: To convert a float to a string, use the `str()` function.

``````x = 3.14
y = str(x)
print(y)   # Output: "3.14"
``````
1. #### String to integer: To convert a string to an integer, use the `int()` function. Note that the string must contain only digits, and can optionally have a negative sign at the beginning.

``````x = "42"
y = int(x)
print(y)   # Output: 42
``````
1. #### String to float: To convert a string to a float, use the `float()` function. Note that the string must contain a valid floating-point number.

``````x = "3.14"
y = float(x)
print(y)   # Output: 3.14
``````

#### It’s important to note that not all type conversions are possible. For example, you cannot convert a string that contains letters to an integer or float directly. Also, some type conversions may result in loss of data or precision, such as converting a float to an integer. #### Top Company Questions  #### Automata Fixing And More  ## We Loveto Support you

Go through our study material. Your Job is awaiting.