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 "<stdin>", 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] >>>
返回
双桥区| 宝清县| 江安县| 鹤峰县| 怀远县| 武安市| 都江堰市| 锡林浩特市| 成都市| 广东省| 揭东县| 宾川县| 图木舒克市| 贞丰县| 顺平县| 阿坝县| 乐清市| 竹北市| 宝兴县| 平凉市| 罗田县| 新民市| 全州县| 万全县| 潼南县| 丹凤县| 浦城县| 施秉县| 班戈县| 松桃| 子洲县| 延安市| 大英县| 磐安县| 新源县| 中江县| 汶上县| 中牟县| 济宁市| 筠连县| 改则县|