Fore­words

A while back I decided 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-friendly 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­ial sec­tion) I have put together a set of notes I made as I was learn­ing and hope­fully they can be use­ful to you as a quick list of how-to code snippets.

All the code snap­shots I’m show­ing here are taken 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 operations:

clip_image003

Set dif­fer­ence:

clip_image004

Set union:

clip_image005

Set inter­sect:

clip_image006

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

clip_image007

 

Dic­tio­nar­ies

Dic­tio­nar­ies are key value pairs indexed by keys, which can be any immutable type. To cre­ate an empty dictionary:

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 methods:

clip_image009

 

You can also use the del state­ment to delete a key value 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 value can be retrieved at the same time using the iteritems() method:

clip_image013

 

Loops

Use a while loops to gen­er­ate the Finobacci 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 statement:

Loops in Python can have an else clause which is exe­cuted 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­nated with the break statement:

clip_image005[10]

 

The con­tinue statement:

clip_image006[10]

 

The pass statement:

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

clip_image007[6]

 

Func­tions

Use the def key­word to define a function:

clip_image001[12]

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

 

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

Every func­tion has a return value, if noth­ing is returned then a spe­cial None value is returned. Writ­ing of the None value is usu­ally 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 function:

clip_image003[8]

 

Default argu­ment values:

clip_image004[8]

 

The default val­ues are eval­u­ated once and once only, at the point of func­tion definition:

clip_image005[12]

how­ever, it works dif­fer­ently 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 empty list? That’s because the object ref­er­enced by the vari­able l is muta­ble and its state was mutated 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­vided twice.

 

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

clip_image008[10]

 

Sim­i­larly, you can take in a dictionary:

clip_image009[6]

 

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

clip_image010[6]

 

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

clip_image011[6]

 

Lamb­das:

clip_image012[6]

Share

Leave a Reply