Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 30 min

😊 Analyse Sentiment with Python and TextBlob

🎯 What You'll Build

A sentiment analyser that scores text as positive, neutral, or negative — tested on real product review data.

📋 What You'll Need

1

Basic sentiment scoring

TextBlob returns polarity (−1 to +1) and subjectivity (0 to 1).

from textblob import TextBlob

texts = [
    "I absolutely love this product — it changed my life!",
    "The delivery was OK but the packaging was damaged.",
    "Terrible quality. Would not recommend to anyone.",
]

for text in texts:
    blob = TextBlob(text)
    pol  = blob.sentiment.polarity
    label = "Positive" if pol > 0.1 else ("Negative" if pol < -0.1 else "Neutral")
    print(f"{label:8s} ({pol:+.2f})  {text[:50]}")
Positive (+0.65)  I absolutely love this product — it changed my life!
Neutral  (+0.08)  The delivery was OK but the packaging was damaged.
Negative (-0.70)  Terrible quality. Would not recommend to anyone.
2

Analyse a batch of reviews

Load a list of reviews and count positive/neutral/negative.

reviews = [
    "Fantastic! Exceeded all expectations.",
    "It's fine, nothing special.",
    "Broke after one week. Very disappointed.",
    "Best purchase I've made this year.",
    "Average product, average price.",
    "Absolutely awful. Returning immediately.",
]

counts = {"Positive": 0, "Neutral": 0, "Negative": 0}
for r in reviews:
    pol = TextBlob(r).sentiment.polarity
    key = "Positive" if pol > 0.1 else ("Negative" if pol < -0.1 else "Neutral")
    counts[key] += 1

print(counts)
{'Positive': 2, 'Neutral': 2, 'Negative': 2}
3

Visualise the results

A simple pie chart of sentiment distribution.

import matplotlib.pyplot as plt

plt.pie(counts.values(), labels=counts.keys(), autopct='%1.0f%%',
        colors=['#22c55e', '#94a3b8', '#ef4444'], startangle=90)
plt.title('Review Sentiment Distribution')
plt.savefig('sentiment.png', dpi=150)
print("Saved sentiment.png")
💡 Tip: For production-grade sentiment on social media text, upgrade to `transformers` and a fine-tuned BERT model — accuracy jumps significantly.

🎉 You Did It!

You now have a working sentiment pipeline. Apply it to real product reviews, tweets, or customer feedback to surface insights automatically.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.