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

When Does Your Code Get Iffy?

On using conditional if-else statements in Python

We live in a contingent world. That’s why it’s so hard to make predictions with any accuracy. Are you going to live in your current house for the next ten years? Well, that depends. If  we don’t inadvertently produce more kids and if we can afford to add on a new adults-only bathroom and if my boss doesn’t continue to drive me bonkers and if interest rates continue to climb like kudzu and if… Well, you get the idea. It’s all about how various conditions shape up.

Programmers need to worry about lots of contingencies as well. That’s why, in the world of programming, we have “conditional statements.” That’s usually just a fancy name for lines of code that begin with the world “if.” The programmer has to work out the possible conditions in advance.

Let’s say you want to write a program that automatically gives people their horoscopes. Well, that horoscope will be contingent on what year the person was born, what time they were born, what hour they were born, whether Venus was in retrograde (whatever that even means), etc. In short, there are a whole lot of “ifs” here.

Let’s start at a basic level. Write the following short code in a text editor and run it. (Don’t forget the indents because Python is extremely sensitive to those, which is a subject for another post!)

height = 8
if height == 8:
     print ("Hiya, Charlie! Nice stilts!")
 else:
     print ("Hey, Charlie, no stilts today?")

You should get the message, “Hiya, Charlie! Nice stilts!” Now, go ahead and assign the variable “height” to a smaller value such as 5 or 6 and run the code again. Because the condition of height changed, you got a different response. Such is the power of “if” and it’s sidekick  “else”.  If you can master “if,” you can prepare your code for a world in which conditions change and the future always looks iffy.

PS – If you want more information on conditionals, I recommend the Hands-on Python Tutorial.

Featured image from Tall Camp. Author Jennifer Morrow from San Francisco. Wikimedia Commons.