>>> s = 'abcdefghijklmnop' >>> s[1:10:2] 'bdfhj' >>> s[::2] 'acegikmo'
修改
1 2 3 4 5 6 7 8 9 10 11 12 13
>>> s = 'spam' >>> s[0] = 'x' Traceback (most recent call last): File "<interactive input>", line 1, in <module> TypeError: 'str' object does not support item assignment
>>> s = 'spam' >>> s = s + 'SPAM' >>> s 'spamSPAM' >>> s = s[:4] + 'OK!' + s[-1] >>> s 'spamOK!M'
if <test1>: <statements1> elif <test2>: <statements2> else: <statements3>
例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#coding:utf-8 number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block ... else: print 'No, it is a little lower than that' # you must have guess > number to reach here print 'Done' # This last statement is always executed, after the if statement is executed
while <test>: #Loop test <statements1> #Loop body else: #Optional else <statements2> #Run if didn't exit loop with break
例1
1 2 3 4 5
a = 0 b = 10 while a<b: print a a = a + 1
例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
number = 23 running = True while running: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number: print 'No, it is a little lower than that' else: print 'No, it is a little higher than that' else: print 'The while loop is over.' # Do anything else you want to do here print 'Done'
for语句
基本结构1
1 2 3 4
for <target> in <object>:#Assign object items to target <statements> #Repeated loop body:use target else: <statements> #If we didn't hit a 'break'
基本结构2
1 2 3 4 5 6 7 8 9 10 11 12
for <target> in <object>:#Assign object items to target <statements> #Repeated loop body:use target if <test>: break #Exit loop now, skip else if <test>: continue #Go to top of loop now else: <statements> #If we didn't hit a 'break' for <target> in <object>:#Assign object items to target <statements> #Repeated loop body:use target if <test>: break #Exit loop now, skip else if <test>: continue #Go to top of loop now else: <statements> #If we didn't hit a 'break'
例1
1 2 3 4 5 6 7
def text_create(): path = 'E:/python/' for text_name in range(1,11): with open (path + str(text_name) + '.txt','w') as text: text.write(str(text_name+9)) text.close() print('Done‘)
例2
1 2 3 4 5 6
file = open('E:/python/1.txt') try: data = file.read() finally: file.close() print data
1 2 3
with open('E:/python/1.txt') as file: data = file.read() print data
break/continue
1 2 3 4 5 6 7 8
while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: print 'Input is of sufficient length' continue # Do other kinds of processing here...
Data Structures
List Methods
list.append(x) Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
list.index(x) Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x) Return the number of times x appears in the list.
list.sort(cmp=None, key=None, reverse=False) Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
list.reverse() Reverse the elements of the list, in place.