logical operator https://www.skillvertex.com/blog Thu, 11 Apr 2024 12:01:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png logical operator https://www.skillvertex.com/blog 32 32 Precedence and Associativity of Operators in Python https://www.skillvertex.com/blog/precedence-and-associativity-of-operators-in-python/ https://www.skillvertex.com/blog/precedence-and-associativity-of-operators-in-python/#respond Thu, 11 Apr 2024 12:01:34 +0000 https://www.skillvertex.com/blog/?p=7005 Read more]]>

Table of Contents

The arithmetic operator will take precedence over the logical operator. Initially, python will check the arithmetic operators. Relational operators will be checked next. At the end, logical operators will be evaluated.

Therefore, if multiple operators are present in an expression, then the higher precedence will be checked first. However, the associativity will be given more importance while considering the order of evaluation in the operator. Let us look into this article to learn more about the precedence and Associativity of operators in Python.

What is Operator Precedence in Python?

In Python, an expression is of variables, operators, and values. The Python interpreter will come across an expression that has various operations and that will be evaluated with the ordered hierarchy. This process is defined as operator precedence.

The table below has provided all the operators from the highest precedence to the lowest precedence.

PrecedenceOperatorsDescriptionAssociativity
1()ParenthesesLeft to right
2x[index], x[index:index]Subscription, slicingLeft to right
3await xAwait expressionN/A
4**ExponentiationRight to left
5+x, -x, ~xPositive, negative, bitwise NOTRight to left
6*, @, /, //, %Multiplication, matrix, division, floor division, remainderLeft to right
7+Addition and subtractionLeft to right
8<<, >>ShiftsLeft to right
9&Bitwise ANDLeft to right
10^Bitwise XORLeft to right
11|Bitwise ORLeft to right
12in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, membership tests, identity testsLeft to Right
13not xBoolean NOTRight to left
14andBoolean ANDLeft to right
15orBoolean ORLeft to right
16if-elseConditional expressionRight to left
17lambdaLambda expressionN/A
18:=Assignment expression (walrus operator)Right to left

Precedence of Python Operators

It is commonly used in the expression that has more than one operator with different precedence for identifying which operator will be required to do first.

Example

10 + 20 * 30

output

10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

Python code for the above example

# Precedence of ‘+’ & ‘*’

expr =10+20*30

print(expr)

Output

610

Precedence of logical operators in Python

The if block will be run in the code provided below even though the age is 0. The precedence of logicaland” will be greater than the logical “or“.

# Precedence of ‘or’ & ‘and’

name =”Alex”

age =0

ifname ==”Alex”orname ==”John”andage >=2:

    print(“Hello! Welcome.”)

else:

    print(“Good Bye!!”)

Output

Hello! Welcome.

Another way to execute the ‘else‘ block is the use of parenthesis (). It will be considered as their precedence and the highest among all the operators.

# Precedence of 'or' & 'and'
name = "John"
age = 0
 
if (name == " John" or name == " Albert") and age >= 2:
    print("Hello! Welcome.")
else:
    print("Good Bye!!")

Output

Good Bye!!

Associativity of the Python Operators

The expression has two or more operators with the same precedence, and then the operator associativity will determine it as either to be left or reft or from right to left.

Example

‘*’ and ‘ /’ consist of the same precedence and the associativity will be from Left to Right in the code below.

# Left-associative operators
a = 10 + 5 - 2  # Addition and subtraction are left-associative
print(a)  # Output: 13

b = 2 * 3 / 2  # Multiplication and division are left-associative
print(b)  # Output: 3.0

# Right-associative operators (only one example in Python, exponentiation)
c = 2 ** 3 ** 2  # Exponentiation is right-associative
print(c) 

Output

13
3.0
512

Operators’ Precedence and Associativity in Python

In Python, Operators’ precedence and Associativity are considered the two important characteristics of operators that will be used to check the order of subexpression in the absence of brackets.

Example

100 + 200 / 10 - 3 * 10

100 + 200 / 10 - 3 * 10 is calculated as 100 + (200 / 10) - (3 * 10)
and not as (100 + 200) / (10 - 3) * 10

Python code for the example above

expression = 100 + 200 / 10 - 3 * 10
print(expression)

Output

90.0

What are non-associative operators?

In Python, most of the operators have associativity and this indicates that it can be used to evaluate from left to right or right to left and have the same precedence.

Therefore, few operators are considered as non- associative and this means that they cannot be chained together.

a = 5
b = 10
c = 15
 
a = b = (a < b) += (b < c)

Output

a = b= (a < b) += (b < c)
                   ^^
SyntaxError: invalid syntax

Conclusion

To conclude, this article has listed about the operator precedence and associativity in Python. Moreover, several examples are also provided for a better understanding of the operator’s precedence. This can allow the students to improve their knowledge and skills regarding the topic.

Precedence and Associativity of Operators in Python- FAQs

Q1. Is Python right associative?

Ans. The associativity will be either from left to right or right to left.

Q2. What is precedence in Python?

Ans. It denotes the order of precedence. Python has divided the operators into several categories such as Arithmetic operators and assignment operators.

Q3. What is the highest precedence?

Ans. Parentheses will have the highest precedence. Higher precedence operators will be performed before the lower precedence operations.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/precedence-and-associativity-of-operators-in-python/feed/ 0
Python Logical Operators https://www.skillvertex.com/blog/python-logical-operators/ https://www.skillvertex.com/blog/python-logical-operators/#respond Thu, 11 Apr 2024 12:00:39 +0000 https://www.skillvertex.com/blog/?p=6978 Read more]]>

Table of Contents

The logical operators are used to combine the conditional statements. Let us check out this to learn more about the Python logical operators in Python.

What is the Logical Operators in Python?

In Python, Logical operators will function on the conditional statements. They will work on Logical AND, Logical OR, and Logical NOT operations.

What is Truth Table for Logical Operators in Python?

XYX and ZX or Ynot (X)not (Y)
TTTTFF
TFFFFT
FTTFTF
FFFFTT

What is the Logical AND operator in Python?

The Logical and AND operator will return True if both the operands are True otherwise, it will give the result as False.

Example

# Python program to demonstrate 
# logical and operator 
a = 10
b = 10
c = -10
if a > 0 and b > 0: 
    print("The numbers are greater than 0") 
if a > 0 and b > 0 and c > 0: 
    print("The numbers are greater than 0") 
else: 
    print("Atleast one number is not greater than 0")

Output

The numbers are greater than 0
Atleast one number is not greater than 0

Example 2

# Python program to demonstrate 
# logical and operator 
a = 10
b = 12
c = 0
if a and b and c: 
    print("All the numbers have boolean value as True") 
else: 
    print("Atleast one number has boolean value as False")

Output

Atleast one number has boolean value as False

What is the Logical OR operator in Python?

The logical OR operator will return a True value if the operands come true.

Example

# Python program to demonstrate 
# logical or operator 
a = 10
b = -10
c = 0
if a > 0 or b > 0: 
    print("Either of the number is greater than 0") 
else: 
    print("No number is greater than 0") 
if b > 0 or c > 0: 
    print("Either of the number is greater than 0") 
else: 
    print("No number is greater than 0")

Output

Either of the number is greater than 0
No number is greater than 0

What is the Logical NOT operator in Python?

The logical not operator will function with a single boolean value. Hence, if the boolean value will come True then it will return as False and Vice versa.

Example

# Python program to demonstrate 
# logical not operator 
a = 10
 
if not a: 
    print("Boolean value of a is True") 
if not (a%3 == 0 or a%5 == 0): 
    print("10 is not divisible by either 3 or 5") 
else: 
    print("10 is divisible by either 3 or 5")

Output

10 is divisible by either 3 or 5

What is Order of Precedence of Logical Operators?

Python will calculate the expression from left to right in multiple operators.

Example

# Python program to demonstrate 
# order of evaluation of logical 
# operators 
def order(x): 
    print("Method called for value:", x) 
    return True if x > 0 else False  
a = order 
b = order 
c = order 
if a(-1) or b(5) or c(10): 
    print("Atleast one of the number is positive")

Output

Method called for value: -1
Method called for value: 5
Atleast one of the number is positive

Conclusion

To conclude, these Python logical operators provide a straightforward way to make decisions in your code. “AND” ensures both conditions must be true for the overall expression to be true, while “or” requires at least one condition to be true.

On the other hand, “not” negates the truth value of a condition. Several examples are illustrated in this for more clarity on Logical And, Logical, Logical Not operators.

Python Logical Operators- FAQs

Q1. What are logic operators?

Ans. Logical operators work to compare the logical expressions that show results either as true or false.

Q2. What is called a logical AND operator?

Ans. This AND logical operator will check if both the operands are true.

Q3. What is the syntax of logical operators?

Ans.Logical OR operator: ||

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/python-logical-operators/feed/ 0