Logical Operators in Python with ExamplesIntroduction:In this tutorial, we are learning about the Logical Operators in Python with Examples. Python logical operators are used to create Boolean expressions. Each operation of these logical operators is itself a Boolean operator. Operators are used to perform functions of values and variables. These are special characters for arithmetic and logical computations. The values that the operator operates on are called operands. Here, we look at some Python logical operator methods to understand their meaning. Example: Besides false expressions, Python falsely interprets zero integers of all types, empty sequences (strings, definitions, names), empty dictionaries, and empty sets as false. All other values are assumed to be True. There are 3 logical operators in Python. These are "and", "or" and "not". They must be represented in lowercase. Logical operator:In Python, the Logical operator is used for conditional statements (True or False). They can perform logical AND, logical OR, and logical NOT operations. The chart of these operators is given below -
The truth table of the logical operator:
Logical AND operator in Python:For a compound Boolean expression to be true, both operands must be true. If one or both operands are evaluated as False, the expression returns False. Program Code 1: Here, we give a program code of logical "and" operators in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - The numbers of two operands are greater than 10 At least one number of the operand is not greater than 10 Program Code 2: Here, we give another program code of logical "and" operators in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - At least the value of two operands is true Logical OR operator in Python:The variable or operator returns true if any of its operands are true. For a compound Boolean expression to be False, both operands must be False. Program Code 1: Here, we give a program code of logical "or" operator in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - One of the operands is greater than 10 Not any operand is greater than 10 Program Code 2: Here, we give another program code of logical "or" operator in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - At least one number has a Boolean value is True Program Code 3: Here, we give another program code of logical "or" operator in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - a or b: 5 c or a: 20 b or c: 20 Logical NOT operator in Python:This is a unary operator. The state of subsequent Boolean operands is reversed. Therefore, no true will be false, and no false will be true. Program Code 1: Here, we give a program code of the logical "not" operator in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - The boolean value of x is divisible by either 2 or 6 Program Code 2: Here, we give another program code of the logical "not" operator in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - not (a+b > 20): True Use of multiple operators:In the case of multiple operators, firstly, Python always evaluates the given expressions from left to right. We can understand the importance of Python logical operators from the examples below. Program Code 1: Here we give a program code of multiple operators in Python. The code is given below - Output: Now, we run the above code and find the result. The result is given below - x > 0 and y < 10 and z > 0: False Program Code 2: Here we give another program code of multiple operators in Python. The code is given below- Output: Now, we run the above code and find the result. The result is given below - The value of the method called is: 2 At least one of the operands is positive Next TopicOlympic data analysis in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India