# this is the first comment SPAM = 1 # and this is the second comment # ... and now a third! SPRINT = "# This is not a comment." 2+2 (50-5*6)/4 7/3 7/-3 width = 20 height = 5*9 width * height x = y = z = 0 x y z x, y, z 3 * 3.75 / 1.5 7.0 / 2 1j * 1J 1j * complex(0,1) 3 - 1j*3 (3 - 1j)*3 (1+2j)/(1+1j) a=1.5+0.5j a.real a.imag a = 3.0 + 4.0j float(a) a.real a.imag abs(a) # sqrt(a.real**2 + a.imag**2) # in interactive mode, the last printed expression is assigned to the variable, _ tax = 12.5 / 100 price = 100.50 price * tax _ price + _ round(_, 2) ====== STRING ====== 'spam eggs' 'doesn\'t' "doesn't" '"Yes," he said.' "\"Yes,\" he said." '"Isn\'t, " she said.' hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." print hello hello = r"This is a rather long string containing\n\ several lines of text just as you would do in C." print hello print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """ word = 'Help' + 'A' word '<' + word * 5 + '>' 'str' 'ing' 'str'.strip() + 'ing' 'str'.strip() 'ing' # <- This is invalid word[4] word[0:2] word[2:4] word[:2] word[2:] word[:2] + word[2:] word[:] word word[0] = 'x' # <- Python strings cannot be changed 'x' + word[1:] 'Splat' + word[4] word[1:100] word[10:] word[2:1] word[-1] # The last character word[-2] # The last-but-one character word[-2:] # The lqast two characters word[:-2] # Everything except the last two characters word[-0] word[0] word[-10] # <- string index out of range ==== LIST ==== a = ['spam', 'eggs', 100, 1234] a a[0] a[3] a[-2] a[1:-1] a[:2] + ['bacon', 2*2] 3*a[:3] + ['Boo!'] a a[2] = a[2] + 23 # <- Unlike strings, wihci are immutable, it is possible to change individual elements of a list # replace some items a[0:2] = [1, 12] a # remove some a[0:2] = [] a # insert some a[1:1] = ['bletch', 'xyzzy'] # insert (a copy of ) itself a the beginning a[:0] = a a # clear the list: replace all items with an empty list a[:] = [] a