Learning Python – Part 1

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.

Basics

Com­ments:

clip_image001[10]

 

Vari­able assign­ment:

clip_image002[10]

 

Arith­metic:

clip_image003[10]

 

Pow­er:

clip_image004[8]

 

Absolute val­ue:

clip_image005[8]

 

Get­ting user input:

clip_image006[8]

 

raw_input vs input:

raw_input always con­tains string, input can con­tain any object, even a cal­cu­la­tion:

clip_image007[8]

 

Import mod­ules:

clip_image008[8]

 

Func­tions as first class objects:

clip_image009[8]

 

If-elseif-else:

clip_image028[8]

 

The is oper­a­tor checks if two vari­ables refers to the SAME object:

clip_image029[8]

on the oth­er hand:

clip_image030[8]

The is not oper­a­tor does the reverse.

 

The and and or log­i­cal oper­a­tors, same as && and || in C# respec­tive­ly. You can use the not oper­a­tor to negate the out­come of a boolean com­par­i­son.

 

You can chain com­par­isons, e.g. is the val­ue of x greater than or equal to 5 and less than or equal to 10?

clip_image031[8]

 

You may com­pare sequence objects of the same type, which uses lex­i­co­graph­i­cal order­ing — com­pare the first two, and if they dif­fer then that’s the out­come of the com­par­i­son, else com­pare the next two, and so on:

clip_image032[8]

 

Strings

Strings can use dou­ble or sin­gle quotes inter­change­ably:

clip_image010[8]

Escape char­ac­ter:

clip_image011[8]

Span­ning across mul­ti­ple lines – a back­slash (\) as the last char­ac­ter on the line indi­cates that the next time is a log­i­cal con­tin­u­a­tion of this line:

clip_image012[8]

or you can sur­round them in a pair of match­ing triple quotes: “”” or ”’:

clip_image013[8]

 

String con­ver­sion using the str() func­tion:

clip_image014[8]

The repr func­tion – the repr func­tion returns a canon­i­cal string rep­re­sen­ta­tion of the object, back-ticks () do the same thing (they are sim­i­lar to the ToString() method on C#‘s objects:

clip_image015[8]

 

String con­cate­na­tion:

clip_image016[8]

 

Slic­ing a string:

clip_image017[8]

You can also use neg­a­tive index, in which case it starts count­ing from the right:

clip_image018[8]

note: message[0] = message[-0], see how the indices are mapped:

clip_image019[8]

you can also set up steps in the slic­ing:

clip_image020[8]

sim­i­lar­ly to before, you can slice back­wards too:

clip_image021[8]

 

Get length of string:

clip_image022[8]

 

Strings are IMMUTABLE!

 

For­mat­ting strings:

clip_image023[8]

 

Find­ing sub­string (returns the index of the start of the first match):

clip_image024[8]

 

Join­ing strings:

clip_image025[8]

 

Chang­ing the case of strings:

clip_image026[8]

 

Replac­ing por­tions of a string:

clip_image027[8]