Page 54 - Computer - 7
P. 54
Logical Operators
Logical operators are used to combine two or more expressions containing relational operators. These
operators evaluate to either True or False. These are used to test conditions and control the flow of a
program based on the result of the condition. The Logical operators available in Python are described
in the table given below.
Example
Operator Description Output
(X = 9, Y = 6)
If both expressions evaluate to True, returns True, else
and (X > 5) and (Y > 4) True
returns False.
If any one of the expressions evaluates to True, returns
or (X > 5) or (Y > 10) True
True, else returns False.
If expression evaluates to True, returns False, and vice-
not not(X > Y) False
versa (reverses the result of the expression).
Lab Activity 5 Experiential Learning
Program to illustrate the use of Relational and Logical operators.
1. Type the code as shown in the image in the Python Editor.
2. Press the F5 key to execute the code. Sample output of the program is given below.
String Operators
String operators are used to perform operations on string values. The String operators available in
Python are described in the table given below.
Operator Name Description Example Output
Concatenates or joins two
+ Concatenation “Amit” + “Gupta” “AmitGupta”
string values into one string.
Repeats the specified string a
* Replication “A”*3 A A A
specified number of times .
52