The ABCs of Python: “del,” “if,” “else,” “elif,” and teeny cars

Here are four more Python keywords:

del

This means, as you might expect, “delete.” I became a fan of del after I read a discussion in which coders debated whether it’s an essential word. Here’s how one side of the argument went: “I can’t really think of any reason why python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it.” I love this because it so coder-like. Rather than using a term that’s something like regular English, some keyword-frugal folks suggest abandoning it, going with the system of assigning None to things. This is a minor insight in the mind of programmers.

n = 'Nazi'
print(n)
del n
print(n) 
#when print runs the 2nd time, Nazi will be deleted 
#so you should get an error

I should note that assigning None to something does not do the same thing as deleting it. A variable to which you assign None still exists. A variable you delete doesn’t.

 if

I’m jumping ahead again in our alphabetical list because it’s best if we pair if with elif, which comes next. As we noted in When Does Your Code Get Iffy?, if is the key word in conditional statements. If your code contains a contingency or a test, then you likely need an if:

def gasMileage(mpg):
    if mpg >= 100:
        comment = 'need a can opener to get in that thing?'
    else:
        if mpg >= 80 and mpg < 99:
            comment = "that's frigging amazing"
        else: 
            if mpg >= 40 and mpg < 80:
                comment = "huh, that's pretty good"
            else:    
                if mpg >= 25 and mpg < 40:
                    comment = 'my bmw wagon does better'
                else:
                    comment = "that a global warmer, baby"
    return comment
print(gasMileage (25))

 else

Yes, this word also means what it says, working hand-in-glove with if, as we saw in the previous example. You can look at the code above if you want to see how else is used in actual code.

 elif

This is a not-too-tricky combination of the words “else” and “if.” It sounds like it might be some phony, steroetyped Hollywood version of a Southern dialect: “If’n ya’ll want to get to the Piggly Wiggly, you drive on up County Road 40 a couple of miles. Elif you miss it, there’ll be a Kash n Kary just another half mile or so afore ya’ll cross the railroad tracks. Elif you cross those tracks, you better turn back because from there you’ve got 30 miles or so of pure Ocala Forest road with nary a store nor gas station to be seen.”

By using elif, you don’t need to have a multitude of indents in some code with lots of conditionals. Remember that code we used in when discussing if? Well, here’s another version:

def gasMileage(mpg):
    if mpg >= 100:
        comment = 'need a can opener to get in that thing?'
    elif mpg >= 80 and mpg < 99:
        comment = "that's frigging amazing"
    elif mpg >= 40 and mpg < 80:
        comment = "huh, that's pretty good"
    elif mpg >= 25 and mpg < 40:
        comment = 'my bmw wagon gets that kind of mileage'
    else:
        comment = "that's why FL is gonna be underwater soon"
    return comment
print(gasMileage (150))

It’s pretty neat, in more ways than one.

Featured image: American Automobile Culture: Cars from the Lane Motor Museum. Philip (flip) Kromer from Austin, TX. https://commons.wikimedia.org/wiki/File:1965_Peel_P50,_The_World%27s_Smallest_Car_ (Lane_Motor_Museum).jpg