Python,Assignment, Expressions, and Print
返回
>>> branch = {'spam':1.25,
... 'ham':1.99,
... 'eggs':0.99}
>>> print branch.get('spam', 'Bad choice')
1.25
>>> print branch.get('bacon', 'Bad choice')
Bad choice
>>>
>>> 2 < 3, 3 < 2
(True, False)
>>> 2 or 3, 3 or 2
(2, 3)
>>> [] or 3
3
>>> [] or {}
{}
>>>
>>> 2 and 3, 3 and 2
(3, 2)
>>> [] and {}
[]
>>> 3 and []
[]
>>>
>>> x = 'a'
>>> y = 'b'
>>> print x,y
a b
>>> print x + y
ab
>>> print '%s...%s' % (x,y)
a...b
>>> print 'hello world'
hello world
>>> 'hello world'
'hello world'
>>> import sys
>>> sys.stdout.write('hello world\n')
hello world
>>>
>>> temp = sys.stdout
>>> sys.stdout = open('log.txt', 'a')
>>>
>>> print x, y, x
>>> sys.stdout = temp
>>> print a, b, c
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'a' is not defined
>>>
>>> x = 0
>>> x = "Hello"
>>> x = [1, 2, 3]
>>> x
[1, 2, 3]
>>>
>>> x = 1
>>> x = x + 1
>>> x
2
>>> x += 1
>>> x
3
>>> X = "spam"
>>> X += "SPAM"
>>> X
'spamSPAM'
>>> L = [1, 2]
>>> L = L + [3]
>>> L
[1, 2, 3]
>>> L.append(4)
>>> L
[1, 2, 3, 4]
>>> L = L + [5,6]
>>> L
[1, 2, 3, 4, 5, 6]
>>> L.extend([7,8])
>>> L
[1, 2, 3, 4, 5, 6, 7, 8]
>>> L += [9,10]
>>> L
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> nudge = 1
>>> wink = 2
>>> A, B = nudge, wink
>>> A, B
(1, 2)
>>> [C,D] = [nudge,wink]
>>> C,D
(1, 2)
>>>
>>> nudge = 1
>>> wink = 2
>>> nudge, wink = wink, nudge
>>> nudge, wink
(2, 1)
>>>
>>> [a,b,c] = (1,2,3)
>>> a,c
(1, 3)
>>> (a,b,c) = "ABC"
>>> a,c
('A', 'C')
>>> red, green, blue = range(3)
>>> red, blue
(0, 2)
>>> range(3)
[0, 1, 2]
>>>
返回