Learning Python – Part 2

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.

Lists

To cre­ate a new list:

clip_image001

 

Lists are NOT immutable:

clip_image002

 

Use the in key­word to check whether an ele­ment is in the spec­i­fied list:

clip_image003

 

Nest­ing lists:

clip_image004

 

The min and max func­tions:

clip_image005

 

The list func­tion – you can use the list() func­tion to con­vert a tuple to a list:

clip_image006

 

Ele­ment val­ues of a tuple can­not be changed and tuple ele­ments are put between paren­the­sis instead of square brack­et:

clip_image007

 

Delet­ing an item from list:

clip_image008

or you can use the remove() func­tion:

clip_image009

 

Replace por­tion of list with slic­ing:

clip_image010

 

Insert a list into anoth­er list with slic­ing:

clip_image011

 

Delete a por­tion of list with slic­ing:

clip_image012

 

Append­ing to a list by using sim­ple con­cate­na­tion:

clip_image013

or use append or extend, the dif­fer­ence being append adds a sin­gle ele­ment to the list where as extend works like the con­cate­na­tion above.

clip_image014

now com­pare this to extend:

clip_image015

 

Like in Javascript, you can use a list like a stack (FILO) too:

clip_image016

 

You can also use a list as a queue (FIFO) using the collections.deque func­tion:

clip_image017

 

Sort­ing a list:

clip_image018

you can do the same to a string too using the sort­ed func­tion:

clip_image019

 

To con­struct an emp­ty tuple:

clip_image020

To con­struct a tuple with a sin­gle item:

clip_image021

 

You can unpack a tuple or list (like the pat­tern match­ing in F#):

clip_image022

clip_image023

 

There must be the same num­ber of ele­ments on the left as the tuple on the right:

clip_image024

 

Use the range() func­tion to gen­er­ate a range of inte­gers:

clip_image025

 

Use the fil­ter() func­tion to fil­ter a list:

clip_image026

 

Use the map() func­tion to project a sequence’s items to some­thing else:

clip_image027

you can also use it like the zip() method in F# by pass­ing in mul­ti­ple sequences:

clip_image028

if the lists are not of equal length, None is used to fill in the gap:

clip_image029

 

Use the reduce() func­tion to return a sin­gle val­ue from a list of ele­ment, e.g. to sum the num­bers 1–4:

clip_image030

you can also pass in a third argu­ment to indi­cate the start­ing val­ue of the accu­mu­la­tor:

clip_image031

 

You can remove an item from a list using its index with the del state­ment:

clip_image032

note that del state­ment doesn’t return any val­ues.

You can also use it to delete the entire list or part of the list:

clip_image033

or to delete the vari­able itself:

clip_image034

 

List com­pre­hen­sions (sim­i­lar to those in F#):

clip_image035

 

If the result is a tuple, then it must be paren­the­sized:

clip_image036

 

You can add addi­tion­al fil­ters:

clip_image037

 

Or you can have a loop inside anoth­er loop:

clip_image038

 

Nest­ed List Com­pre­hen­sions, e.g. to turn the columns of a matrix into rows:

clip_image039

remem­ber, read nest­ed com­pre­hen­sions from right to left!

Nest­ed com­pre­hen­sions is a pow­er­ful tool but adds com­plex­i­ty, where pos­si­ble, use built-in func­tions. E.g. the above can be done using zip():

clip_image040

 

When loop­ing through a sequence, the posi­tion index and cor­re­spond­ing val­ue can be retrieved at the same time using the enu­mer­ate() func­tion:

clip_image041

 

You can also use zip() func­tion to loop over two or more sequences at the same time:

clip_image042