Bigdata – Knowledge Base

Python – Arithmetic and Logical Operations

Python – Arithmetic and Logical Operations #

Python provides a range of operators that allow you to perform arithmetic and logical operations. These operators help in manipulating values, performing calculations, and making decisions.

1. Arithmetic Operations in Python #

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and more.

1.1 List of Arithmetic Operators #

OperatorOperationDescription
+AdditionAdds two numbers.
-SubtractionSubtracts the second number from the first.
*MultiplicationMultiplies two numbers.
/DivisionDivides the first number by the second.
%ModulusReturns the remainder of the division.
**ExponentiationRaises the first number to the power of the second.
//Floor DivisionReturns the integer result of division (ignores the remainder).

1.2. Basic Arithmetic Examples #


1.3. Division and Floor Division #

  • Normal Division (/) gives the result in floating-point form, even if both numbers are integers.
  • Floor Division (//) returns the largest integer smaller than or equal to the division result.

Example: #


2. Logical Operations in Python #

Logical operators are used to combine conditional statements and check conditions based on Boolean values (True or False).

2.1 List of Logical Operators #

OperatorOperationDescription
andLogical ANDReturns True if both operands are true.
orLogical ORReturns True if at least one operand is true.
notLogical NOTReturns True if the operand is false, and False if true.

2.2. Examples of Logical Operations #


3. Comparison (Relational) Operators #

Comparison operators are used to compare two values. These operations return either True or False.

3.1 List of Comparison Operators #

OperatorOperationDescription
==Equal toReturns True if both values are equal.
!=Not equal toReturns True if the values are not equal.
>Greater thanReturns True if the left value is greater.
<Less thanReturns True if the left value is smaller.
>=Greater than or equalReturns True if the left value is greater or equal.
<=Less than or equalReturns True if the left value is smaller or equal.

3.2. Examples of Comparison Operators #


4. Combining Logical and Comparison Operators #

You can combine logical and comparison operators to form more complex expressions.

Example: #

In this example, both conditions age >= 18 and has_license must be True for the person to be able to drive.


5. Arithmetic with Assignment Operators #

Assignment operators are used to assign values to variables. In Python, you can perform arithmetic operations and assign the result to a variable in a single step.

5.1 List of Arithmetic Assignment Operators #

OperatorOperationEquivalent to
+=Addition assignmentx = x + y
-=Subtraction assignmentx = x - y
*=Multiplication assignmentx = x * y
/=Division assignmentx = x / y
%=Modulus assignmentx = x % y
**=Exponentiation assignmentx = x ** y
//=Floor division assignmentx = x // y

5.2. Examples of Assignment Operators #


6. Bitwise Operators in Python #

Bitwise operators perform operations on the binary representations of numbers. These operators treat numbers as a sequence of bits and perform operations like AND, OR, and NOT at the bit level.

6.1 List of Bitwise Operators #

OperatorOperationDescription
&Bitwise ANDPerforms AND operation on each bit.
``Bitwise OR
^Bitwise XORPerforms XOR operation on each bit.
~Bitwise NOTFlips all the bits (1 becomes 0, 0 becomes 1).
<<Left shiftShifts bits to the left by a given number.
>>Right shiftShifts bits to the right by a given number.

6.2. Examples of Bitwise Operations #


7. Conclusion #

Python provides a rich set of operators for performing arithmetic, logical, comparison, and bitwise operations. By mastering these operations, you can handle various mathematical calculations, logical decisions, and data manipulation tasks efficiently in your programs.

What are your feelings
Updated on September 5, 2024