What Are Relational, Logical & Bitwise Expressions in Python?

TL;DR
Relational, logical, and bitwise expressions all return Boolean values, while combinational expressions mix several operator types in one statement. To evaluate any expression, apply operator precedence: brackets first, then power, arithmetic, shifts, bitwise, relational, membership, logical, and finally assignment, which always runs last.
Transcript
foreign we started our discussion on expressions in Python we have understood what our expressions in Python and we have seen some types of Expressions as well we have understood what is an integral expression what is a constant expression what is an arithmetic expression and what is a floating Point expression now in this presentation we will cont... Read More
Key Insights
- Relational expressions are also called Boolean expressions because they always return a Boolean value. They use relational operators, and their operands can be arithmetic expressions rather than single values, such as '10 + 13 <= 2 + 3' evaluating to False.
- Logical expressions consist of relational expressions connected by logical operators like 'and' and 'or', and they always return a Boolean value. Both conditions in '10 < 13 and 1 == 1' are true, so the whole expression returns True.
- Bitwise expressions always contain bitwise operators, and their computations are performed at the bit level because bitwise operators can only work with bits. This forces evaluation to happen on the binary representation of the operands.
- The left shift operator works by multiplying the left operand by 2 raised to the power of the right operand. So '10 << 2' equals 10 times 2 to the power 2, which is 10 times 4, giving 40.
- Combinational expressions are a combination of different types of expressions and operators. A single statement can contain assignment, arithmetic (plus, minus, multiply), and bitwise operators all at once, making it both a bitwise and arithmetic expression.
- Operator precedence in Python, from highest to lowest, is: brackets, power, unary plus/minus, multiplication/division/floor-division/modulus, binary plus/minus, shifts, bitwise operators, relational, equality, membership, logical (not/and/or), then assignment.
- Brackets always have the highest priority among all operators, so any operation inside parentheses is solved first. In 'X << 1' inside parentheses, the shift runs before multiplication even though shift normally has lower precedence.
- Assignment operators have the lowest precedence of all operators, so the entire right-hand expression is fully evaluated before the value is assigned. In 'Z = Y + X << 1 - X * 3', the assignment to Z happens last.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is a relational expression in Python?
A relational expression, also called a Boolean expression, is an expression built with relational operators such as less than, greater than, less than or equal to, greater than or equal to, double equal to, or not equal to. It always returns a Boolean value, either True or False. Its operands do not have to be single values; they can be arithmetic expressions. For example, '10 + 13 <= 2 + 3' compares 23 and 5 and returns False.
Q: What is a logical expression and what does it return?
A logical expression consists of relational expressions connected using logical operators, and it always returns a Boolean value. A logical expression must contain a logical operator, and while it usually connects relational expressions, it can also use single valued operands. For example, '10 < 13 and 1 == 1' joins two relational expressions with the 'and' operator; because both conditions are satisfied, the expression returns True.
Q: How does the left shift bitwise operator work in Python?
The left shift operator is a bitwise operator, so its computations are performed at the bit level. The trick to evaluate it is to multiply the left operand by 2 raised to the power of the right operand. For example, in '10 << 2', you multiply 10 by 2 to the power 2. Since 2 to the power 2 is 4, and 4 multiplied by 10 is 40, the expression returns 40.
Q: What is a combinational expression in Python?
A combinational expression is simply a combination of different types of expressions and operators within a single statement. For example, 'Z = Y + X << 1 - X * 3' contains an assignment operator, arithmetic operators like plus, minus, and multiply, and a left shift bitwise operator. It includes a bitwise expression because of the shift operator and an arithmetic expression because of the multiplication, so the whole statement qualifies as combinational.
Q: What is the order of operator precedence in Python?
From highest to lowest priority, the order is: brackets, the power operator, unary plus and unary minus, multiplication/division/floor division/modulus, binary plus and binary minus, left shift and right shift, bitwise and/xor/or, relational operators (greater than, less than, and their equal-to versions), equality and not equal to, membership operators (is, is not, in, not in), logical operators (not, and, or), and finally assignment operators, which have the lowest precedence.
Q: Why do relational and logical expressions always return a Boolean value?
Relational expressions are also known as Boolean expressions, so by definition they evaluate a comparison and produce either True or False. Logical expressions connect relational expressions with logical operators like 'and' and 'or', and asking whether the combined conditions hold also yields a True or False result. Because both expression types are fundamentally asking yes-or-no questions about their operands, they always return a Boolean value rather than a number.
Q: How do you evaluate 'Z = Y + X << 1 - X * 3' when X is 10 and Y is 20?
First, evaluate the parenthesized 'X << 1', which has highest priority due to brackets: 10 multiplied by 2 to the power 1 equals 20. Next, multiplication has higher precedence than plus and minus, so 'X * 3' is 10 times 3, which is 30. The expression becomes 'Y + 20 - 30', or '20 + 20 - 30'. Since plus and minus share precedence, evaluate left to right: 20 plus 20 is 40, and 40 minus 30 is 10. So Z points to 10.
Q: Why is the assignment operation performed last in an expression?
Assignment operators have the lowest precedence among all operators in Python. This means the entire expression on the right-hand side must be fully evaluated before its resulting value is assigned to the variable on the left. In 'Z = Y + X << 1 - X * 3', all the arithmetic, shift, and bracket operations resolve to a single value of 10 first, and only then does the assignment operation store that value in Z.
Summary & Key Takeaways
-
This part-two lesson covers five expression types in Python: relational, logical, bitwise, combinational, and operator precedence. Relational expressions, also called Boolean expressions, use relational operators and always return a Boolean value, with operands that can themselves be arithmetic expressions like '10 + 13 <= 2 + 3'.
-
Logical expressions connect relational expressions using logical operators such as 'and' and always return Booleans, as in '10 < 13 and 1 == 1' returning True. Bitwise expressions contain bitwise operators and compute at the bit level; for example '10 << 2' multiplies 10 by 2 to the power 2 to give 40.
-
Combinational expressions mix multiple expression types and operators. Evaluating 'Z = Y + X << 1 - X * 3' with X=10 and Y=20 requires operator precedence: the parenthesized shift gives 20, multiplication gives 30, then left-to-right addition and subtraction yield 10. Assignment always executes last.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Neso Academy 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator





