What I Learned In Python This Week #4-5: Dictionaries (Conditionals, Loops, and More Next Week)

Tools I Used:

  • Geany
    • Still my favorite mini-IDE!
  • Python shell / bash shell
    • I use the Python shell to learn functions interactively, and the bash shell to execute the scripts I write (though I now use Geany’s F5 shortcut more often).


  • No new code, but cleaned up much of my old code and added some new features

Functions/Concepts I Learned:

  • Dictionaries
    • Dictionaries are pretty much what they sound like — two linked items, one being a key and the other being a value. More specifically, they are a mapping, and the only mapping type built in to Python.
    • You can use dict() to make a new dictionary from other types (strings, tuples, other dictionaries), but to make a completely new dictionary just declare it with a foo = {} type of statement.
    • Dictionary operations are much like string operations, only adapted to key/value rather than slice/item — so I won’t be giving a horrendously long and detailed list of what they actually are. It should be rather intuitive, so just look it up in the official Python documentation if you need such a list.
    • Dictionaries are awesome for string formatting — just use %(dictionary_key) for your conversion specifiers, and then use % dictionary_name to read the dictionary’s values into your string. Magic!
    • .clear() clears your dictionary of all items and returns None.
    • .copy() makes a shallow copy of your dictionary. This means values are the same in both the original and the copy, so replacing an item in the copy leaves the original alone, but modifying an item in the copy will likewise modify the original — and vice versa.
    • .fromkeys() creates a dictionary from your supplied keys, and sets the values as None.
    • .get() lets you access a nonexistent key and return None rather than crashing your program. You can also set a return value other than None if you want.
    • .has_key() is equivalent to in.
    • .items() reads a dictionary and returns a list consisting of key/value tuples. .iteritems() returns an iterator instead of a list.
    • .keys() reads a dictionary and returns a list of the keys in the dictionary. .iterkeys() returns an iterator instead.
    • .pop() returns the value of a specified key, then removes the key and its value from the dictionary.
    • .popitem() returns a random (because dictionaries don’t have any ordering at all) key/value pair and removes it from the dictionary. According to Hetland, this is a way to process key/value pairs efficiently without first retrieving a keys list.
    • .setdefault() either returns a value or, if the key/value pair doesn’t exist, it creates the key/value pair with the value set to None. You can also specify a value other than None if you want.
    • .update() updates the values of one dictionary with the values of another (if the keys are identical) or it creates new key/value pairs (if there are no corresponding keys in the dictionary you are modifying).
    • .values() reads a dictionary and returns a list of values in it. .itervalues() returns a iterator instead.
  • print can print multiple types of expressions if you separate them with a comma.
  • import can be used with as to make “aliases” of multiple functions with the same name, either by renaming the module or the specific functions. (Probably not technically correct, but that’s my mental shorthand for how it works.)

I also learned about loops and conditionals, but that’s going to be a really huge post in and of itself, so I’m saving it for next week — I’m likely going to need a while to digest what I’m learning about them, and I’d like to do some new coding (I’m working on a Morse code translator) to explore these things practically.

Honestly, I really am getting these concepts, but it’s hard to keep practicing them over and over, even with eight hours of coding a week, because that time also goes toward writing for this blog, and sometimes coding activities are preempted by some other necessity of life (stupid life!). However, even though my learning is sometimes quite protracted, this blog has really helped to keep me going forward no matter what, because I really want to write in it — if that makes any sense. Yeah, there’s only ~10 pageviews a day so far, but it’s nice to know that maybe my bumblings will help someone else.

So have an awesome weekend, all you Python coders — and good luck 😉 .


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s