Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • What Are Membership Operators in Python?
  • Step-by-Step Usage
  • ML Utility Summary
  • Keywords
  1. Artificial Intelligence
  2. Data Science Foundation
  3. Python Programming
  4. Operators

Membership Operators

Nerd Cafe

What Are Membership Operators in Python?

Python provides two membership operators to test whether a value is in a sequence (like a list, string, tuple, etc.) or not:

Operator
Meaning
Example

in

Returns True if present

"cat" in "category"

not in

Returns True if not present

"dog" not in "cat"

Step-by-Step Usage

1. Using in with Lists

features = ['height', 'weight', 'age']
print('age' in features)      # True
print('income' in features)   # False

ML Tip: Useful when selecting features dynamically from a dataset.

2. Using not in with Lists

model_params = ['learning_rate', 'max_depth']
print('gamma' not in model_params)  # True

ML Tip: Before tuning, check if a parameter is valid for your model.

3. Using with Strings

sentence = "Machine learning is awesome"
print("learning" in sentence)       # True
print("AI" not in sentence)         # True

NLP Example: You might want to detect keywords in a sentence.

4. Using with Tuples

allowed_metrics = ('accuracy', 'precision', 'recall')
print('f1-score' in allowed_metrics)      # False

Practical ML Tip: Use this in model evaluation settings.

5. Using with Dictionaries (Only Keys are Checked)

params = {'n_estimators': 100, 'max_depth': 5}
print('max_depth' in params)         # True
print('learning_rate' not in params) # True

Tip: in only checks keys in a dictionary, not values.

ML Utility Summary

Use Case
Membership Operator
Example

Feature selection

in

if 'age' in df.columns:

Parameter check

in, not in

'max_depth' in model_params

Word/keyword search in NLP

in

'data' in sentence

Model metric validation

in

'accuracy' in metrics_list

Keywords

python, membership operators, in, not in, list, string, tuple, dictionary, data preprocessing, feature selection, machine learning, model parameters, pandas, dataframe, NLP, keyword search, boolean logic, conditional checks, data validation, error handling, nerd cafe

PreviousCompound OperatorsNextIdentity Operators

Last updated 2 months ago