Page 469 - ComputerScience_Class_11
P. 469
12.1.2 Binary
A binary operator in Python is a type of operator that works on two operands to perform an operation. Common
binary operators include + (addition), - (subtraction), * (multiplication), / (division) and (logical AND) or (logical
OR), == (equal to) and > (greater than). They are used to perform mathematical calculations, comparisons, logical
operations or bitwise manipulations between two values.
Program 2: To display the output of Binary operator.
Program 2.py
File Edit Format Run Options Window Help
# Assign value 10 to variable a
a = 10
# Assign value 5 to variable b
b = 5
# Perform addition of a and b and print the result
print("a + b =", a + b) # Output: 15
# Perform subtraction of b from a and print the result
print("a - b =", a - b) # Output: 5
# Perform multiplication of a and b and print the result
print("a * b =", a * b) # Output: 50
# Perform division of a by b and print the result
print("a / b =", a / b) # Output: 2.0
Output
a + b = 15
a - b = 5
a * b = 50
a / b = 2.0
12.1.3 Ternary
A ternary operator in Python is a type of operator that works on three operands to perform a conditional operation. It
allows selecting a value based on a condition in a single line, the ternary operator allows you to choose between two
values based on a condition using only one line of code.
Syntax of ternary is as follows:
value_if_true if condition else value_if_false
Operators and Expressions 467

