I am stuck, I don't understand a line of code (Python for DS: Intermediate>Cleaning and Preparing Data in Python>5. String Capitalization)

Screen Link:

https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/5/string-capitalization

My Code:

for row in moma:
    gender = row[5]
    gender = gender.title()
    # if there is no gender, set
    # a descriptive value
    if not gender:
        gender = "Gender Unknown/Other"
    row[5] = gender

Hi there!
I don’t get what this line of code means: “if not gender”
The reason why I don’t understand it is because we have assigned row[5] as gender. How can it not be gender? It is more intuitive to say if gender == “”. I would appreciate if someone can help me out this. I have pasted the url link above.

Many thanks for your time and helping me out!

You are correct, it may seem to be more intuitive to write if gender == "" but it’s considered to be more readable to use if not gender. Now the tricky part: how does it work? Python uses what’s called “truthy-ness” where if your variable isn’t of type bool then python will coerce it as such.

Try these little snippets to see what I mean:

empty_string = ""
print(bool(empty_string))
non_empty_string = "testing"
print(bool(non_empty_string))
zero = 0
print(bool(zero))
non_zero = 314
print(bool(non_zero))

When you use a non-boolean variable in an if statement, python automatically determines the “truthy-ness” of the variable and (I think) makes the code a lot easier to read.

1 Like

Hi Mike!
You have come to my rescue yet again!! Thank you for your time and the detailed explanation! I think I get it now. I saw that my question was read over 5 times and nobody had replied. I almost gave up and thought nobody was going to reply. How do I increase my chances of being answered from your experience?

I also did this to understand what you meant:

gender = ""
if not gender:
    print("Not gender")

gender = "Male"

if not gender:
    print("Not gender")
else:
    print("gender")

Thank you breaking it down for me! I hope I can contribute to this community some day just like you! Keep up the great work Mike!!

Glad to be of help! Honestly, I really enjoy helping others as it gives me a chance to review concepts or learn something entirely new myself. So, I appreciate your appreciation but it really should be me thanking you for the teaching/learning experience you provide by posting questions :sunglasses:

This will happen. Perhaps those 5 people couldn’t answer the question and you just have to wait until someone who can help reads your post. Or maybe they could but just didn’t have the time/resources available to answer your question at that time. Sometimes it can take days before someone replies and there’s always a chance that no one will :frowning_face: Personally, I like to look for posts with zero replies to see if there is a way for me to help because there’s nothing worse than being stuck/frustrated while learning.

This is a fantastic question and I’m sure answers can vary depending on the reader. But for me, if you follow the recommendations in this post then I am much more likely to be able to help.

I am sure it will happen for you. It just takes time. The more you learn, the more you can share and help out in the community. That’s what it’s all about, my friend! :grinning_face_with_smiling_eyes:

Happy coding!

1 Like