This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by msarlitt
almost 9 years

The NPR API 33/34: Why do we use a "==" in parse_station_json()?

Here is the correct parse_station_json() function from exercise 33:

def parse_station_json(json_obj):
    for station in json_obj['station']:
        print station['callLetters']['$text'] + ": " + station['marketCity']['$text'] + ", " + station['state']['$text']
        print "Frequency: " + station['frequency']['$text'] + station['band']['$text']
        
        if 'url' in station:
            print "MP3 Streams: "
            for link in station["url"]:
                if link["type"] == "Audio MP3 Stream":
                    print "\t" + link["title"] + " - " + link["$text"]

In the final if statement, why do we use the double equals (==) rather than a single one (=)? When checking whether a value is equal to something in an if statement, I’ve always been using a single =. Can’t figure out why this one is different.

Answer 5570018dd3292f4c610007a3

0 votes

Permalink

Hey there, That’s a bit confusing! In Python “==” has always been a comparison operator, whereas “=” is the assignment operator. You can read about the operators and their difference here, for example: http://www.tutorialspoint.com/python/python_basic_operators.htm For me trying to write something like: if a = b: throws a SyntaxError, as it should.

points
Submitted by Borys Vengerov
almost 9 years