Python,while and for Loops
返回
>>> D1 = {'spam':1, 'eggs':3, 'toast':5}
>>> D1
{'toast': 5, 'eggs': 3, 'spam': 1}
>>> D1={}
>>> D1['spam'] = 1
>>> D1['eggs'] = 3
>>> D1['toast'] = 5
>>> D1
{'toast': 5, 'eggs': 3, 'spam': 1}
>>> keys = ['spam', 'eggs', 'toast']
>>> vals [1, 3, 5]
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'vals' is not defined
>>> vals = [1, 3, 5]
>>> zip(keys,vals)
[('spam', 1), ('eggs', 3), ('toast', 5)]
>>> D2 = {}
>>> for (k,v) in zip(keys,vals):D2[k] = v
...
>>> D2
{'toast': 5, 'eggs': 3, 'spam': 1}
>>>
>>> D3 = dict(zip(keys,vals))
>>> D3
{'toast': 5, 'eggs': 3, 'spam': 1}
>>>
>>> L = [1,2,3,4,5]
>>>
>>> for i in range(len(L)):
... L[i] += 1
...
>>> L
[2, 3, 4, 5, 6]
>>>
>>> i = 0
>>> while i < len(L):
... L[i] += 1
... i += 1
...
>>> L
[3, 4, 5, 6, 7]
>>>
>>> L1 = [1,2,3,4]
>>> L2 = [5,6,7,8]
>>>
>>> zip(L1,L2)
[(1, 5), (2, 6), (3, 7), (4, 8)]
>>>
>>> for (x,y) in zip(L1,L2):
... print x,y,'--',x+y
...
1 5 -- 6
2 6 -- 8
3 7 -- 10
4 8 -- 12
>>>
>>> T1, T2, T3 = (1,2,3), (4,5,6), (7,8,9)
>>> T3
(7, 8, 9)
>>> zip(T1,T2,T3)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>>
>>> S1 = 'abc'
>>> S2 = 'xyz123'
>>>
>>> zip(S1,S2)
[('a', 'x'), ('b', 'y'), ('c', 'z')]
>>>
>>> map(None, S1, S2)
[('a', 'x'), ('b', 'y'), ('c', 'z'), (None, '1'), (None, '2'), (None, '3')]
>>>
>>> for i in range(3):
... print i, 'Pythons'
...
0 Pythons
1 Pythons
2 Pythons
>>>
>>> X = 'spam'
>>> for item in X: print item,
...
s p a m
>>>
>>> i = 0
>>> while i < len(X):
... print X[i],: i += 1
File "", line 2
print X[i],: i += 1
^
SyntaxError: invalid syntax
>>> i = 0
>>> while i < len(X):
... print X[i]: i += 1
File "", line 2
print X[i]: i += 1
^
SyntaxError: invalid syntax
>>> i = 0
>>> while i < len(X):
... print X[i], i += 1:
File "", line 2
print X[i], i += 1:
^
SyntaxError: invalid syntax
>>>
>>> X
'spam'
>>> len(X)
4
>>> range(len(X))
[0, 1, 2, 3]
>>>
>>> for i in range(len(X)): print X[i],
...
s p a m
>>>
>>> S = 'abcdefghijk'
>>> range(0, len(S), 2)
[0, 2, 4, 6, 8, 10]
>>>
>>> for i in range(0, len(S), 2): print S[i],
...
a c e g i k
>>>
>>> seq1 = "span"
>>> seq2 = "scam"
>>>
>>> rec = []
>>> for x in seq1:
... if x in seq2:
... res.append(x)
...
Traceback (most recent call last):
File "", line 3, in ?
NameError: name 'res' is not defined
>>> for x in seq1:
... if x in seq2:
... rec.append(x)
...
>>> rec
['s', 'a']
>>>
>>> range(5), range(2,5), range(0,10,2)
([0, 1, 2, 3, 4], [2, 3, 4], [0, 2, 4, 6, 8])
>>>
>>> range(-5,5)
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> range(5,-5,-1)
[5, 4, 3, 2, 1, 0, -1, -2, -3, -4]
>>>
>>> items = ["aaa", 111, (4,5), 2.01]
>>> tests = [(4,5),3.14]
>>>
>>> for key in tests:
... for item in items:
... if item == key:
... print key, "was found"
... break
... else:
... print key, "not found"
...
(4, 5) not found
(4, 5) not found
(4, 5) was found
3.14 not found
3.14 not found
3.14 not found
3.14 not found
>>> for key in tests:
... if key in items:
... print key, "was found"
... else:
File "", line 4
else:
^
IndentationError: unindent does not match any outer indentation level
>>> for key in tests:
... if key in items:
... print key, "was found"
... else:
... print key, "not found!"
...
(4, 5) was found
3.14 not found!
>>>
>>> found = 0
>>> while x and not found:
... if match(x[0]):
... print 'Ni'
... found = 1
... else:
... x = x[1:]
... if not found:
File "", line 7
if not found:
^
SyntaxError: invalid syntax
>>>
>>> for x in ["spam", "eggs", "ham"]:
... print x,
...
spam eggs ham
>>> sum = 0
>>> for x in [1, 2, 3, 4]:
... sum = sum + x
...
>>> sum
10
>>> prod = 1
>>> for item in [1, 2, 3, 4]: prod *= item
...
>>> prod
24
>>> S,T = "lumberjack",("and", "I'm", "okay")
>>> for x in S: print x,
...
l u m b e r j a c k
>>> for x in T: print x,
...
and I'm okay
>>> T = [(1,2), (3,4), (5,6)]
>>> for (a,b) in T:
... print a,b
...
1 2
3 4
5 6
>>>
>>> while 1:
... name = raw_input('Enter name:')
... if name == 'stop': break
... age = raw_input('Enter age: ')
... print 'Hello', name, '=>', int(age) ** 2
...
Enter name:mel
Enter age: 40
Hello mel => 1600
Enter name:bob
Enter age: 30
Hello bob => 900
Enter name:stop
>>>
>>> x = 10
>>> while x:
... x = x - 1
... if x % 2 != 0: continue
... print x,
...
8 6 4 2 0
>>>
>>> x = 10
>>> while x:
... x = x - 1
... if x % 2 == 0:
... print x,
...
8 6 4 2 0
>>>
>>> y = 29
>>> x = y / 2
>>> while x > 1:
... if y %x == 0:
... print y, 'has factor', x
... break
... x = x - 1
... else:
... print y, 'is prime'
...
29 is prime
>>>
>>> while 1:
... print 'Type Ctrl-C to stop me!'
>>> x = 'spam'
>>> while x:
... print x,
... x = x[1:]
...
spam pam am m
>>> a=0; b=10
>>> while a < b:
... print a,
... a += 1
...
0 1 2 3 4 5 6 7 8 9
返回