In Python, “//” is an operator which stands for floor division in Python, also known as integer division. It divides one integer by another and then rounds off the quotient to the nearest whole number.
It lies under the arithmetic operators in Python. Similarly, many other operators are used in Python programming.
These operators help to build the code and provide the functionality to it. These operators are divided into categories like arithmetic operators, comparison operators, logical operators, identity operators, and many more.
So let’s discuss all the types of operators in Python with their signs and uses.
The Role of Operators in Python
In Python, operators play a fundamental role in performing various operations on data, variables, and values. These operators are symbols or special keywords that allow you to manipulate and interact with different types of data in a concise and efficient manner.
Python supports a wide range of operators, including arithmetic operators (such as +, -, *, /), comparison operators (like ==, !=, <, >), logical operators (such as and, or, not), assignment operators (like =, +=, -=), and more.
These operators enable you to perform tasks like mathematical calculations, making decisions based on conditions, combining and manipulating strings, and much more.
Understanding and effectively using operators is essential for writing efficient and expressive Python code, as they form the building blocks for performing diverse operations within programs.
Types of Operators in Python
Arithmetic operators in Python perform arithmetic functions in a program like Addition, subtraction, division, etc. Here are the arithmetic operators present in Python.
- Unary Addition: “+” is called the unary positive operator. The use of the unary operator looks like “+a.” However, it doesn’t add any meaning to the code. It is similar to writing 1 as +1 in everyday mathematics.
- Unary Negation: “-” is called the unary Negation. It is used to write a negative term in Python. For example, “-a” is the negative of a, as the only change here is the sign.
- Addition: “+” is the addition operator in Python. It is used for Addition in Python, such as “a+b.”
- Subtraction: “-” is the subtraction operator in Python used for subtraction. For example, “a-b,” here b will get subtracted from a.
- Multiplication: “*” is the multiplication operator in Python. It is used for multiplication, such as “a*b.”
- Division: “/” is known as the division operator. It divides one integer by another. For example, “a/b.”
- Floor Division in python: “//” is also a division operator. However, when a//b, the quotient will get rounded off to the nearest whole number.
- Modulus: “%” also acts as a division operator, but as a result, it declares the remainder and not the quotient.
- Exponentiation: “**” is used to raise a number to a power. For example, a**b is equal to ab.
- Equal to: “==” compares two numbers and states them as equal. For example, a==b.
- Not equal to: “!=” Is the operator which compares two numbers and states them as unequal. For example, a!=b.
- Greater than: “>” is used to compare a bigger number on the left to a smaller number on the right. For example, a>b.
- Less than: “< “is used to compare a smaller number on the left to a bigger number on the right. For example, a<b.
- Greater than or equal to: “>=” is used for a greater or equal number on the left. For example, a >= b.
- Less than or equal to: “<=” is used for a lesser or equal number on the left. For example, a <=b.
Logical operators in Python are used for performing logical operations on boolean values or expressions. Boolean values are the values that can either be true or false.
Moreover, based on the combination of logical operators with boolean values, the flow of the program is controlled.
- Logical AND: “and” logical operator returns true only if both the operands are true. Otherwise, it returns false if any of the two operands is false or both are false.
- Logical OR: “or” operator returns true if any of the two operands is true. Moreover, it only returns false if both operands are false.
- Logical NOT: “not” operator is used to negate the boolean value. If the operand is true, the result will be false. Similarly, if the operand is false, the result will be true. Exactly the opposite of the initial state of the boolean value.
As the name suggests, bitwise operators operate on operands bitwise. Here are the bitwise operators in Python.
- Bitwise AND (&): Performs a bitwise AND operation on individual bits of two integers. It sets each bit to 1 only if both corresponding bits in the operands are 1.
- Bitwise OR (|): Performs a bitwise OR operation on individual bits of two integers. It sets each bit to 1 if at least one of the corresponding bits in the operands is 1.
- Bitwise XOR (^): Performs a bitwise exclusive OR (XOR) operation on individual bits of two integers. It sets each bit to 1 if only one of the corresponding bits in the operands is 1.
- Bitwise NOT (~): Performs a bitwise NOT operation (complement) on individual bits of an integer. It inverts all the bits, turning 0s into 1s and 1s into 0s. The result is the two’s complement representation of the negative of the number minus one.
- Left Shift (<<): Shifts the bits of an integer to the left by a specified number of positions. It effectively multiplies the integer by 2, raised to the power of the shift amount.
- Right Shift (>>): Shifts the bits of an integer to the right by a specified number of positions. It effectively divides the integer by 2, raised to the power of the shift amount, discarding the remainder.
What is Binary Search Python?
Binary search Python algorithm is used to search for an element’s position in a sorted array. Binary search Python algorithms can be of two types: iterative and recursive.
Here is an example of the binary search Python algorithm.
- Binary Search Python (Iterative)
Sorted Array: [2, 5, 8, 12, 16, 23, 38, 45, 56, 72, 91]
Target Element: 23
Operators in Python are important components to write code. Learning about all the operators in Python is necessary to write correct and concise codes.
Moreover, you should also know these operators’ meanings and proper use. Otherwise, it may lead to errors while running the program. So, keep learning and keep coding.