Explain the use of in and not in operators in lists with suitable examples. Also, develop a program to find mean, variance, and standard deviation of a list of numbers.

Use of in and not in Operators in Lists

in Operator

Checks whether a value exists inside a list.
It returns True if found, otherwise False.

not in Operator

Checks whether a value is not present in the list.
It returns True if the value is missing from the list.

# List of greetings
greetings = ['hello', 'hi', 'howdy', 'hey']

print('howdy' in greetings)     # True
print('bye' in greetings)       # False
print('hello' not in greetings) # False
print('bye' not in greetings)   # True

Example Program: Check Pet Name

my_pets = ['Zophie', 'Pooka', 'Fat-tail']

print('Enter a pet name:')
name = input()

if name not in my_pets:
    print('I do not have a pet named ' + name)
else:
    print(name + ' is my pet.')

sample output:
Enter a pet name:
Tommy
I do not have a pet named Tommy

Program to Find Mean, Variance, and Standard Deviation

import math

# List of numbers
numbers = [10, 20, 30, 40, 50]

# Step 1: Mean
mean = sum(numbers) / len(numbers)

# Step 2: Variance
variance = sum((x - mean) ** 2 for x in numbers) / len(numbers)

# Step 3: Standard Deviation
std_deviation = math.sqrt(variance)

# Output
print("Numbers:", numbers)
print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", std_deviation)

sample output:

Numbers: [10, 20, 30, 40, 50]
Mean: 30.0
Variance: 200.0
Standard Deviation: 14.142135623730951

summary:

OperatorPurposeExample Result
inChecks if element exists in list'hi' in lstTrue
not inChecks if element doesn’t exist'bye' not in lstTrue

Leave a Reply

Your email address will not be published. Required fields are marked *