Tag Archives: Python 101

Python 101: All About Dictionaries

The Python programming language has several built-in types that it supports. One of my favorites is the dictionary. A dictionary is a mapping object maps hashable values to arbitrary objects (source). Other languages call dictionaries “hash tables”. They are mutable objects that you can change whenever you want to, unlike tuples. A dictionary’s keys must be hashable or immutable, which means that you cannot use a list or another dictionary as a key. Note that dictionaries before Python 3.6 are not ordered. In Python 3.6, they changed the dict’s implementation so that it is now ordered, although there is a warning mentioned that you should not rely on it being ordered. What this means is that when you iterate over a dictionary, you may not extract the values in the same order that you inserted them except in Python 3.6 and potentially future versions of Python.

In this article, we will take some time learning about some of the many things you can do with a dictionary.

Creating Dictionaries

A dictionary is a key:value pair. In Python, these key:value pairs are enclosed inside of curly braces with commas between each pair. Creating dictionaries in Python is really easy. Here are the three ways to create a dictionary:

>>> my_dict = {}
>>> my_other_dict = dict()
>>> my_other_other_dict = {1: 'one', 2: 'two', 3: 'three'}

In the first example, we create an empty dictionary by just assigning our variable to a pair of empty curly braces. You can also create a dictionary object by calling Python’s built-in dict() keyword. I have seen some people mention that calling dict() is slightly slower than just doing the assignment operator. The last example shows how to create a dictionary with some predefined key:value pairs. You can have dictionaries that contain mappings of various types, including mapping to functions or objects. You can also nest dictionaries and lists inside your dictionaries!

Continue reading Python 101: All About Dictionaries

Python 101: An Intro to ftplib

The File Transfer Protocol (FTP) is used by many companies and organizations for sharing data. Python provides an File Transfer Protocol module in its standard library called ftplib that implements the client side of the FTP protocol. You can learn all about the File Transfer Protocol by reading the RFC 959 document on the Internet. However the full specification is outside the scope of this article. Instead we will focus on the following topics:

  • Connecting to an FTP server
  • Navigating it’s structure
  • Downloading files from the FTP server
  • Uploading files to an FTP server

Let’s get started!

Continue reading Python 101: An Intro to ftplib

Python 101 Screencast Released

The Python 101 Screencast has been finished for a little over a month now and I am now releasing it for general consumption.


The Python 101 Screencast is based on my book, Python 101. I went through all 44 chapters of the book and turned each of them into a standalone screencast. In other words, the screencast covers 44 videos. You can view the first eleven videos on my blog’s youTube page.

What you get:

A minimum of 44 episodes + you will receive a PDF, MOBI and EPUB version of the Python 101 book with your purchase. You will also receive a PDF copy formatted to A4 size.

Part One

The first part is the beginner section. In it you will learn all the basics of Python. From Python types (strings, lists, dictionaries) to conditional statements to loops. You will also learn about comprehensions, functions and classes and everything in between!

Part Two

This section is a curated tour of the Python Standard Library. The intent isn’t to cover everything in it, but instead it is to show the reader that you can do a lot with Python right out of the box. We’ll be covering the modules I find the most useful in day-to-day programming tasks, such as os, sys, logging, threads, and more.

Part Three

An all intermediate section covering lambda, decorators, properties, debugging, testing and profiling.

Part Four

Now things get really interesting! In part four, we will be learning how to install 3rd party libraries (i.e. packages) from the Python Package Index and other locations. We will cover easy_install and pip. This section will also be a series of tutorials where you will learn how to use the packages you download. For example, you will learn how to download a file, parse XML, use an Object Relational Mapper to work with a database, etc.

Part Five

The last section of the series will cover how to share your code with your friends and the world! You will learn how to package it up and share it on the Python Package Index (i.e. how to create an egg or wheel). You will also learn how to create executables using py2exe, bb_freeze, cx_freeze and PyInstaller. Finally you will learn how to create an installer using Inno Setup

Buy Now

Python 101: Lambda Basics

Many programming languages have the concept of the lambda function. In Python, the lambda is an anonymous function or unbound function. The syntax for them looks a bit odd, but it’s actually just taking a simple function and turning it into a one-liner. Let’s look at a regular simple function to start off:

def doubler(x):
    return x*2

All this function does is take an integer and double it. Technically, it will also double other things too since there’s no type checking but that is its intent Now let’s turn it into a lambda function!

Continue reading Python 101: Lambda Basics

Python 101 Back-to-School Sale

I am launching a back-to-school sale of my Python 101 course. You can get the Python 101 book now for 50% by using the following offer code: fall2015. This offer is good until September 15, 2015.

You can also get my Python 101 Screencast for 50% off (i.e. $25 no offer code required). It includes the book, but it should be noted that it won’t be completed until December 2015. There are 15 videos completed at this time. The first 11 are available free of charge on Youtube, so you can try before you buy.

Python 101 Screencast Available for Pre-Order

The Python 101 Screencast is now available for Pre-Order. If you pre-order the screencast series, then you will receive what I currently have finished (12 videos + the eBook) and then receive updates as I add new ones. There will be a minimum of 44 videos. Upon purchase, you will be able to stream or download the videos at any time.

The screencasts are based off my book, Python 101. Each screencast is based on a chapter from the book. The first 11 videos are available free of charge so you can try-before-you-buy! You can check them out on Youtube here.