Python - Operators

9.
Explain the use of the membership operators (in and not in) in Python.

In Python, the membership operators in and not in are used to test whether a value is present in a sequence (such as a list, tuple, or string) or not.

Example: Using membership operators in Python.

# Example with the 'in' operator
fruits = ['apple', 'banana', 'orange']

# Check if 'banana' is in the list
is_banana_in_list = 'banana' in fruits

# Check if 'grape' is in the list
is_grape_in_list = 'grape' in fruits

# Example with the 'not in' operator
# Check if 'watermelon' is not in the list
is_watermelon_not_in_list = 'watermelon' not in fruits

# Check if 'orange' is not in the list
is_orange_not_in_list = 'orange' not in fruits

print("'banana' in fruits:", is_banana_in_list)
print("'grape' in fruits:", is_grape_in_list)
print("'watermelon' not in fruits:", is_watermelon_not_in_list)
print("'orange' not in fruits:", is_orange_not_in_list)
'banana' in fruits: True
'grape' in fruits: False
'watermelon' not in fruits: True
'orange' not in fruits: False

In this example, the membership operators are used to check whether specific elements are present in the list fruits or not. The outputs demonstrate the results of these membership checks.


10.
What is the purpose of the identity operators (is and is not) in Python?

In Python, the identity operators is and is not are used to test object identity. They check if two variables refer to the same object in memory or not.

Example: Using identity operators in Python.

# Example with the 'is' operator
x = [1, 2, 3]
y = [1, 2, 3]
z = x

# Check if x and y refer to the same object
are_x_and_y_same = x is y

# Check if x and z refer to the same object
are_x_and_z_same = x is z

# Example with the 'is not' operator
# Check if x and y do not refer to the same object
are_x_and_y_not_same = x is not y

# Check if x and z do not refer to the same object
are_x_and_z_not_same = x is not z

print("x is y:", are_x_and_y_same)
print("x is z:", are_x_and_z_same)
print("x is not y:", are_x_and_y_not_same)
print("x is not z:", are_x_and_z_not_same)
x is y: False
x is z: True
x is not y: True
x is not z: False

In this example, the identity operators are used to check if variables x, y, and z refer to the same object in memory or not. The outputs demonstrate the results of these identity checks.


11.
Discuss the role of the ternary conditional operator (x if condition else y) in Python.

The ternary conditional operator, also known as the conditional expression, is a shorthand way to write an if-else statement in a single line. It has the form x if condition else y. If the condition is True, the expression evaluates to x; otherwise, it evaluates to y.

Example: Using the ternary conditional operator in Python.

# Example of the ternary conditional operator
temperature = 25
weather = "Sunny" if temperature > 20 else "Cloudy"

print("Weather today:", weather)
Weather today: Sunny

In this example, the ternary conditional operator is used to determine the value of the weather variable based on the temperature. If the temperature is greater than 20, the weather is considered "Sunny"; otherwise, it is "Cloudy". The output demonstrates the result of this ternary expression.


12.
How are comparison operators (<, >, <=, >=, !=, ==) used in Python?

Comparison operators are used to compare values in Python. They return True or False based on the comparison result. The common comparison operators are < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), != (not equal to), and == (equal to).

Example: Using comparison operators in Python.

# Example of using comparison operators
x = 5
y = 10

# Less than
result_lt = x < y

# Greater than
result_gt = x > y

# Less than or equal to
result_lte = x <= y

# Greater than or equal to
result_gte = x >= y

# Not equal to
result_ne = x != y

# Equal to
result_eq = x == y

# Output the results
print("x < y:", result_lt)
print("x > y:", result_gt)
print("x <= y:", result_lte)
print("x >= y:", result_gte)
print("x != y:", result_ne)
print("x == y:", result_eq)
x < y: True
x > y: False
x <= y: True
x >= y: False
x != y: True
x == y: False

In this example, the comparison operators are used to compare the values of x and y. The output demonstrates the results of these comparisons.