top of page

C, Python, and Molecules

Why Chemistry Was the Lesson I Didn’t Know I Needed


When I started studying Computer Engineering, I came from a liceo scientifico background, where I’d already encountered chemistry—though not at a university level. I had no programming experience at all, so I approached every subject with an open mind. But not all my peers shared that mindset.

Many had already mastered coding and saw chemistry as an irrelevant distraction. For them, it was a subject to push aside until the last possible moment. But for me, chemistry turned out to be a crucial lesson in more ways than one.


Chemistry and Computer Science
Chemistry and Computer Science


🧪 The Absurdity (At First Glance)


Let’s face it: putting chemistry in a Computer Engineering program seems bizarre. You sign up for courses like compilers and processors, not for periodic tables and chemical reactions. I wasn’t alone in questioning it; most of my peers felt the same way. 🤯


But as it turned out, this "absurd" subject ended up shaping me in ways I didn’t anticipate.



🎓 Why Chemistry Might Actually Matter


  1. Interdisciplinary Thinking 🔄In embedded systems, engineers often work in environments that overlap with physical, chemical, and biological domains. A basic understanding of these areas can make you a more effective problem solver.

  2. Cognitive Flexibility 🧠The challenge of switching between digital logic and chemical equations may seem random, but it strengthens your mental agility. It teaches you to adapt to new frameworks and think in ways you might not be used to.

  3. Growth Through Discomfort 🌱Chemistry was tough for me, but pushing through it — even when I wasn’t interested — taught me how to tackle uncomfortable challenges. That’s a muscle I use all the time now as an engineer.



✅ What I Gained


  • Broader scientific literacy 🔬

  • Respect for knowledge outside my main area of focus 🌍

  • Mental toughness from overcoming a challenging subject 💡


❌ What Still Doesn’t Sit Right


  • The timing: A first-year course might not be the best moment for chemistry 🕰️

  • The context: We never really understood why chemistry was part of the curriculum, and that’s a missed opportunity for deeper connection 🔍



💬 Final Thoughts


If you’re a Computer Engineering student staring at your chemistry textbook, don’t worry. You’re not alone. 😓

It’s not easy, and it doesn’t always feel necessary. But it’s a chance to grow — not as a chemist, but as someone who learns no matter the subject. And in the end, that’s what makes a great engineer. 🌟



💻 Example: Python Code for Chemical Analysis


To dive deeper into how chemistry and engineering can come together, let’s look at a simple Python script for calculating the molar mass of a chemical compound. This code uses a basic dictionary of atomic weights and parses a chemical formula to compute the compound’s total molar mass.


How It Works:


The function calculate_molar_mass takes a chemical formula (like C6H12O6) and splits it into individual elements and their counts. It then multiplies each element’s atomic weight by the number of atoms in the formula. For example, in glucose (C6H12O6), it will calculate the mass based on 6 carbon (C), 12 hydrogen (H), and 6 oxygen (O) atoms.

This simple yet powerful tool illustrates the intersection of programming and chemical analysis — a great example of engineering principles in action! 🌐


python

# Dictionary of atomic weights (g/mol)
atomic_weights = {
    'H': 1.008,  # Hydrogen
    'C': 12.011,  # Carbon
    'O': 15.999,  # Oxygen
    'N': 14.007,  # Nitrogen
    'Cl': 35.45,  # Chlorine
    'Na': 22.990,  # Sodium
}

def calculate_molar_mass(formula):
    """
    Calculate the molar mass of a chemical compound.
    Formula should be in the form of a string like 'H2O', 'C6H12O6', etc.
    """
    total_mass = 0
    i = 0
    while i < len(formula):
        element = formula[i]
        
        # Check for two-letter element symbols
        if i + 1 < len(formula) and formula[i + 1].islower():
            element += formula[i + 1]
            i += 1
        
        i += 1
        count = 0
        # Check for numbers after the element (subscript)
        while i < len(formula) and formula[i].isdigit():
            count = count * 10 + int(formula[i])
            i += 1
        if count == 0:
            count = 1  # Default to 1 if no subscript is given
        
        # Add the element's molar mass multiplied by the count
        if element in atomic_weights:
            total_mass += atomic_weights[element] * count
        else:
            print(f"Warning: Element {element} not found in atomic weights dictionary!")
    
    return total_mass

# Example usage
formula = 'C6H12O6'  # Glucose
molar_mass = calculate_molar_mass(formula)
print(f"The molar mass of {formula} is {molar_mass:.2f} g/mol")

Commenti


bottom of page