Posts

Showing posts with the label magnus lie hetland

Chapter 3.Working With Strings. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

String formatting operator >>> print format % value Hello, Pit, How do you do? >>> format = 'Hello, %s. %s do you do?' >>> values = ('Pit','How') >>> print format % values Hello, Pit. How do you do? ____________________________________ formatting real numbers (floats) >>> format = 'Pi with four decimals: %.4f' >>> from math import pi >>> print format % pi Pi with four decimals: 3.1416 _______________________________________ Template strings/Template method SUBSTITUTE >>> from string import Template >>> s = Template('$x, glorious $x') >>> s.substitute(x = 'slurm') 'slurm, glorious slurm' ___________________________ If the replacement field is part of a word, x should be enclosed in braces >>> from string import Template >>> s = Template("It's ${x}tastic") >>> s.substitute(x=...

Chapter 2. LISTS & TUPLES. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

FUNCTIONS of LISTS >>> list ('Hello') ['H', 'e', 'l', 'l', 'o']' __________________ Item assignments >>> x = [1,1,1] >>> x[1]=2 >>> x [1, 2, 1] _________________________ Deleting elements >>> names = ['debora','Alice','Trinity','Sofia'] >>> del names[2] >>> names ['debora', 'Alice', 'Sofia'] _______________________________________ Slicing >>> name = list('Perl') >>> name ['P', 'e', 'r', 'l'] >>> name[2:]=list('ar') >>> name ['P', 'e', 'a', 'r'] ________________________________________ Different length of initial and final list after slicing >>> name[1:]=list('ython') >>> name ['P', 'y', 't', 'h', 'o', 'n...

Chapter 2. LISTS & TUPLES.SEQUENCES 'Beginning Python From Novice to Professional'

>>> ed = ['Edward Snowden',42] >>> steve = ['Steven Jobs',55] >>> gven = ['Gven Stephany',35] >>> barak = ['Barak Obama',56] >>> database = [ed,steve,gven,barak] >>> database [['Edward Snowden', 42], ['Steven Jobs', 55], ['Gven Stephany', 35], ['Barak Obama', 56]] >>> _________________________________________________ Common Sequence Operations These operations include indexing, slicing, adding, multiplying, and checking for membership . INDEXING >>>print 'losos'[4] s >>> 'losos'[-1] 's' >>> third = raw_input('Who? ')[3] Who? Kozel >>> third 'e' ____________________________________________ >>> endings = ['st', 'nd', 'rd'] + 17 * ['th'] \ + ['st', 'nd', 'rd'] + 7 * ['th'] \ + ['st...