Chapter 1: Basic knowledge
>>> From _ future _ import division # implements the slash/division, instead of division, >>> 1 // 2 # implement division> 2 ** 3 # implement index> pow (2, 3) # implement an exponential function> 0xAF # hexadecimal> 010 #> >>> x = input ("x:") # enter an exponential function with a format, enclose the string with quotation marks >>> x = raw_input ("y:") # The format is not required for input, directly convert to a string >>> import math # import module >>> math. floor (342.34) 342.0 >>> from math import sqrt # Do not add math below >>> sqrt (9) 3.0 >>>> import cmath # plural >>> cmath. sqrt (-1) 1j >>> print "Hello" # single quotation marks and double quotation marks can be used in combination, both of which can be Hello >>> prin T 'hello' Hello >>> print "Let's go! "# Use dual-Let's go if there is a single string! >>> Print '"Hello! "'# Use a single string if the string is double." Hello! ">>> Print 'let \'s go '# Escape Character Let's go >>> 1 + 2 + 3 \ # The backslash can be used as a line break + 4 + 515>> print 'hello, \ nworld' # \ n is used as the line break Hello, world >>> print r'c: \ nowhere' # Add the first r character to implement the original character. escape characters are not considered. C: \ nowhere >>> print r'c: \ Program Files \ fnord 'C: \ Program Files \ fnord
New functions in this Chapter
Chapter 2: List and metadata
Reference: http://blog.sina.com.cn/s/blog_4b5039210100e9yd.html
>>> Name = 'Alex '>>> name [0] # The first 'A' >>> name [-1] # reverse from the last one to 'X' >>> 'Hello' [1] # Use 'E'> fourth = raw_input ('year: ') [3] Year: 2005 >>> fourth '5'
2-1
months = ['January','Feburuary','March','April','May','June','July','August','September','October','November','December']endings=['st','nd','rd'] + 17*['th']\ +['st','nd','rd'] + 7 * ['th']\ +['st']year = raw_input('Year:')month = raw_input('Month(1-12):')day = raw_input('Day(1-31):')month_number = int(month)day_number = int(day)month_name = months[month_number-1]ordinal = day + endings[day_number-1]print month_name + ' ' + ordinal + ', ' + year
The running result is as follows (I have to say today is 11.11.11)
Year:2011Month(1-12):11Day(1-31):11November 11th, 2011
Sharding (large format)
>>> tag = '<a href="http://www.python.org">Python web site</a>'>>> tag[9:30]'http://www.python.org'>>> tag[32:-4]'Python web site'
>>> numbers = [1,2,3,4,5,6,7,8,9,10]>>> numbers[3:6][4, 5, 6]>>> numbers[0:1][1]>>> numbers[8:][9, 10]>>> numbers[-3:][8, 9, 10]>>> numbers[:][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> numbers[:3][1, 2, 3]
2-2
# Split up a URL of the form http://www.something.comurl = raw_input('Please enter the URL: ')domain = url[11:-4]print "Domain name: " + domain
Please enter the URL: http://www.baidu.comDomain name: baidu
Larger step size
>>> numbers = [1,2,3,4,5,6,7,8,9,10]>>> numbers[0:10:1][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> numbers[0:10:2][1, 3, 5, 7, 9]>>> numbers[0:10:3][1, 4, 7, 10]>>> numbers[0:10:-2][]>>> numbers[::-2][10, 8, 6, 4, 2]>>> numbers[5::-2][6, 4, 2]>>> numbers[:5:-2][10, 8]
Sequence Addition
>>> [1,2,3]+[4,5,6][1, 2, 3, 4, 5, 6]>>> a = [1,3,5]>>> b = [2,4,6 ]>>> c = a + b>>> c[1, 3, 5, 2, 4, 6]
Multiplication
>>> 'python'*5'pythonpythonpythonpythonpython'>>> [42]*10[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
Initialize a list with a length of 10
>>> sequence = [None] * 10>>> sequence[None, None, None, None, None, None, None, None, None, None]
2-3 Series (string) multiplication example
Sentence = raw_input ("Sentence:") screen_width = 80text_width = len (sentence) box_width = text_width + 6left_margin = (screen_width-box_width) // 2 printprint ''' * left_margin + '-' * (box_width-2) + '+ 'print ''' * left_margin +' | '+ ''' * text_width +' | '# Note space print ''' * left_margin +' | '+ sentence +' |' print ''' * left_margin + '|' + ''' * text_width + '| 'print ''' * left_margin +'-'* (box_width-2) + 'print
Membership (in)
>>> permissions = 'rw'>>> 'w' in permissionsTrue>>> 'x' in permissionsFalse>>> users = ['mlh','foo','bar']>>> raw_input('Enter your user name:') in usersEnter your user name:mlhTrue>>> subject = '$$Get rich now!!!$$$'>>> '$$$' in subjectTrue
2-4 Sequence member qualification example
database = [ ['albert', '1234'], ['dilbert', '4242'], ['smith', '7524'], ['jones', '9843']]username = raw_input('User name: ')pin = raw_input('PIN code: ')if [username, pin] in database: print 'Access granted'
User name: smithPIN code: 7524Access granted
Built-in functions
>>> numbers = [100,34,679]>>> len(numbers)3>>> max(numbers)679>>> min(numbers)34>>> max(2,3)3>>> min(8,3,4,2)2
List Function
>>> list('hello')['h', 'e', 'l', 'l', 'o']>>> lists = list('hello')>>> lists['h', 'e', 'l', 'l', 'o']
>>> x = [1,1,1]>>> x[1]1>>> x[2] = 2>>> x[1, 1, 2]
Delete Element
>>> names = ['Alice','Beth','Cecil','Dee-Dee','Earl']>>> del names[2]>>> names['Alice', 'Beth', 'Dee-Dee', 'Earl']
Partition assignment, insert new elements
>>> name = list('Perl')>>> name['P', 'e', 'r', 'l']>>> name[2:] = list('ar')>>> name['P', 'e', 'a', 'r']>>> name[1:] = list('ython')>>> name['P', 'y', 't', 'h', 'o', 'n']>>> numbers = [1,5]>>> numbers[1:1] = [2,3,4]>>> numbers[1, 2, 3, 4, 5]>>> numbers[1:4] = []>>> numbers[1, 5]
List Method
Append
>>> lst = [1,2,3]>>> lst.append(4)>>> lst[1, 2, 3, 4]
Count
>>> [2,4,3,2,4,5,6,2,6].count(2)3>>> x = [[2,3],[3,4],[2,5]]>>> x.count(1)0>>> x.count([2,3])1
Extend
>>> a = [1,2,3]>>> b = [4,5,6]>>> a.extend(b)>>> a[1, 2, 3, 4, 5, 6]
Index
>>> knights = ['We','are','the','knight','who','say','in']>>> knights.index('who')4
Insert
>>> numbers = [1,2,3,4,5,6]>>> numbers.insert(3,'four')>>> numbers[1, 2, 3, 'four', 4, 5, 6]>>> numbers[4:4] = ['four']>>> numbers[1, 2, 3, 'four', 'four', 4, 5, 6]>>>
Pop (stack Operation)
>>> x = [1,2,3]>>> x.append(x.pop())>>> x[1, 2, 3]
Remove
>>> x = ['to','be','or','not','to','be']>>> x.remove('be')>>> x['to', 'or', 'not', 'to', 'be']
Reversed and list Functions
>>> x = [1,2,3]>>> list(reversed(x))[3, 2, 1]
Sort
>>> x = [4,6,2,3,8,1]>>> x.sort()>>> x[1, 2, 3, 4, 6, 8]
Save to copy
>>> x = [4,6,2,1,7,9]>>> y = x[:]>>> y.sort()>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]
Comparison
>>> x = [3,2,5,4,6]>>> y = x>>> y.sort()>>> x[2, 3, 4, 5, 6]>>> y[2, 3, 4, 5, 6]
Use sorted Functions
>>> x = [4,6,2,1,7,9]>>> y = sorted(x)>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]
Advanced sorting
>>> Cmp (99,100) 1 >>> cmp ()-1 >>> numbers = [,] >>>> numbers. sort (cmp) >>> numbers [3, 5, 7, 9] >>> x = ['aadkfjl ', 'dkfjl', 'eori ', 'dkf']> x. sort (key = len) >>> x ['dkf', 'eori ', 'dkfjl', 'aadkfjl '] >>>> x = [3, 4, 6, 2, 5]> x. sort (reverse = True) # case sensitive! >>> X [6, 5, 4, 3, 2]
Tuples: immutable Sequence
>>> 1,2,4(1, 2, 4)>>> 4343>>> 34,(34,)>>> (1,2,5)(1, 2, 5)>>> (43,)(43,)
Tuple Function
>>> tuple([1,2,4])(1, 2, 4)>>> tuple('abc')('a', 'b', 'c')>>> tuple((1,2,3))(1, 2, 3)
Basic tuples
>>> x = 1,2,4>>> x(1, 2, 4)>>> x[1]2>>> x[0:2](1, 2)