Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Python Comparis

comparison

Uploaded by

epimacklaurent
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Comparis

comparison

Uploaded by

epimacklaurent
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

INTRODUCTION

Operators

The operator can be defined as a symbol which is responsible for a particular operation between two
operands. Operators are the pillars of a program on which the logic is built in a particular programming
language.

Python Comparison Operators

In Python, comparison operators are used to compare two values. These operators return a Boolean
value (True or False) based on the comparison result. The following are the Python comparison
operators:

Equal to (==): This operator checks if the values of two operands are equal. If they are, it returns True;
otherwise, it returns False.

Not equal to (!=): It checks if the values of two operands are not equal. If they are not equal, it returns
True; otherwise, it returns False.

Greater than (>): This operator checks if the left operand is greater than the right operand. If it is, it
returns True; otherwise, it returns False.

Less than (<): It checks if the left operand is less than the right operand. If it is, it returns True;
otherwise, it returns False.

Greater than or equal to (>=): This operator checks if the left operand is greater than or equal to the
right operand. If it is, it returns True; otherwise, it returns False.

Less than or equal to (<=): It checks if the left operand is less than or equal to the right operand. If it is, it
returns True; otherwise, it returns False.

Usage of Comparison Operators in Python

Comparison operators are used in conditional statements and loops to make decisions based on certain
conditions. Here are some examples of how comparison operators are used in Python:

Example 1: Using comparison operators in an if statement

x = 10

y=5

if x > y:

print("x is greater than y")


Example 2: Using comparison operators in a while loop

count = 0

while count <= 5:

print(count)

count += 1

In these examples, the comparison operators (>, <=) are used to compare values and make decisions
based on the results.

Logical Operators in Python

Logical operators in Python are used to combine conditional statements. The following are the logical
operators used in Python:

and: Returns True if both statements are true.

or: Returns True if at least one of the statements is true.

not: Returns True if the statement is false and vice versa.

Usage of Logical Operators in Python

Logical operators are used to combine multiple conditions in conditional statements and loops. Here’s
an example of how logical operators are used in Python:

Example: Using logical operators in an if statement

x = 10

y=5

z=7

if x > y and x > z:

print("x is greater than y and z")

In this example, the and logical operator is used to combine two conditions.
Difference between Logical Operators and Comparison Operators

The main difference between logical operators and comparison operators lies in their functionality.
Comparison operators are used to compare two values and return a Boolean result based on the
comparison, while logical operators are used to combine multiple conditional statements and return a
single Boolean result based on these combinations.

In summary, comparison operators compare values and return a Boolean result, while logical operators
combine conditional statements and return a single Boolean result based on these combinations.
REFERENCE

You might also like