Learning Python – Part 3

Forewords

A while back I decid­ed to try and learn Python for the hell of it as it seems like an inter­est­ing lan­guage and has some of the most con­cise and user-friend­ly syn­tax. Hav­ing spent some time going through a num­ber of dif­fer­ent learn­ing sources and mate­ri­als (like the offi­cial site python.org which has a very help­ful tuto­r­i­al sec­tion) I have put togeth­er a set of notes I made as I was learn­ing and hope­ful­ly they can be use­ful to you as a quick list of how-to code snip­pets.

All the code snap­shots I’m show­ing here are tak­en from the IDLE Python shell.

Sets

A set is an unordered col­lec­tion with no dupli­cate ele­ments. To cre­ate a set:

clip_image001

notice how the dupli­cate was removed?

 

You can also use a string as a sequence of char objects and turn it into a set:

clip_image002

 

Now, let’s use this two sim­ple sets to illus­trate some basic set oper­a­tions:

clip_image003

Set dif­fer­ence:

clip_image004

Set union:

clip_image005

Set inter­sect:

clip_image006

Exclu­sive (in one or the oth­er but not both):

clip_image007

 

Dictionaries

Dic­tio­nar­ies are key val­ue pairs indexed by keys, which can be any immutable type. To cre­ate an emp­ty dic­tio­nary:

clip_image008

 

You access the ele­ments in a dic­tio­nary using square brack­ets [], you can find out all the keys and val­ues in a dic­tio­nary using the keys and val­ues meth­ods:

clip_image009

 

You can also use the del state­ment to delete a key val­ue pair:

clip_image010

 

You can cre­ate a dic­tio­nary using the dict() method and pass in a sequence of tuples:

clip_image011

if the keys are all strings, you can even do this:

clip_image012

 

To loop through dic­tio­nar­ies, the key and cor­re­spond­ing val­ue can be retrieved at the same time using the iteritems() method:

clip_image013

 

Loops

Use a while loops to gen­er­ate the Finobac­ci sequence:

clip_image001[10]

 

For loops:

clip_image002[10]

 

Muta­ble sequences like lists should not be mod­i­fied at the same time as loop­ing through it, and instead should be done on a copy:

clip_image003[6]

what would hap­pen if we don’t use a copy here? This code will get stuck in an infi­nite loop:

clip_image004[6]

 

The else and break state­ment:

Loops in Python can have an else clause which is exe­cut­ed when the loop ter­mi­nates through exhaus­tion of the list (for for loops) or when the loop con­di­tion becomes false (for while loops), but it won’t exe­cute if the loop is ter­mi­nat­ed with the break state­ment:

clip_image005[10]

 

The con­tin­ue state­ment:

clip_image006[10]

 

The pass state­ment:

It does noth­ing, often used to cre­ate a min­i­mal class..

clip_image007[6]

 

Functions

Use the def key­word to define a func­tion:

clip_image001[12]

Note that the first string is a doc­string that can be used by some tools to gen­er­ate doc­u­men­ta­tions.

 

Argu­ments are passed using call by ref­er­ence, as the argu­ment is always an object ref­er­ence not the val­ue of the object.

Every func­tion has a return val­ue, if noth­ing is returned then a spe­cial None val­ue is returned. Writ­ing of the None val­ue is usu­al­ly sup­pressed by the inter­preter, but you can see it using print:

clip_image002[12]

 

Use the return key­word to return val­ues from a func­tion:

clip_image003[8]

 

Default argu­ment val­ues:

clip_image004[8]

 

The default val­ues are eval­u­at­ed once and once only, at the point of func­tion def­i­n­i­tion:

clip_image005[12]

how­ev­er, it works dif­fer­ent­ly with muta­ble objects such as lists, dic­tio­nar­ies etc.

clip_image006[12]

notice how the sec­ond func­tion call didn’t use the default emp­ty list? That’s because the object ref­er­enced by the vari­able l is muta­ble and its state was mutat­ed by the first add_item call.

 

You can also call the func­tion using key­word argu­ments if they appear AFTER the reg­u­lar para­me­ters have been passed in:

clip_image007[8]

note that key­word argu­ments can­not be pro­vid­ed twice.

 

You can define an arbi­trary argu­ment list which takes in a tuple:

clip_image008[10]

 

Sim­i­lar­ly, you can take in a dic­tio­nary:

clip_image009[6]

 

Inter­est­ing­ly, the reserve of the above is true too! You can take a tuple and unpack­ing it with the * oper­a­tor:

clip_image010[6]

 

Or take a dic­tio­nary and unpack­ing it with the ** oper­a­tor:

clip_image011[6]

 

Lamb­das:

clip_image012[6]