def foo(x, y):
print x, y
alist = [1, 2]
adict = {'x': 1, 'y': 2}
foo(*alist) # 1, 2
foo(**adict) # 1, 2
>>> x = 3 >>> 1 < x < 5 True >>> 4 > x >=3 True
>>> def foo(x=[]): ... x.append(1) ... print x ... >>> foo() [1] >>> foo() [1, 1]
>>> def foo(x=None): ... if x is None: ... x = [] ... x.append(1) ... print x ... >>> foo() [1] >>> foo() [1] >>>
sum[value] = sum.get(value, 0) + 1
>>> print "Hello %(name)s !" % {'name': 'James'}
Hello James !
>>> print "I am years %(age)i years old" % {'age': 18}
I am years 18 years old
>>> print "Hello {name} !".format(name="James")
Hello James !
>>> for i in (1, 3, 5): ... if i % 2 == 0: ... break ... else: ... print "var i is always an odd" ... var i is always an odd >>>
>>> class Dict(dict): ... def __missing__(self, key): ... self[key] = [] ... return self[key] ... >>> dct = Dict() >>> dct["foo"].append(1) >>> dct["foo"].append(2) >>> dct["foo"] [1, 2]
>>> from collections import defaultdict
>>> dct = defaultdict(list)
>>> dct["foo"]
[]
>>> dct["bar"].append("Hello")
>>> dct
defaultdict(<type 'list'>, {'foo': [], 'bar': ['Hello']})
>>> Name = "Wang" "Hong" >>> Name 'WangHong'
>>> Name = "Wang" \ ... "Hong" >>> Name 'WangHong'[b]10. Python解释器中的”_” [/b] >>> range(4) [0, 1, 2, 3] >>> _ [0, 1, 2, 3]
import this
>>> [(i, j) for i in range(3) for j in range(i)] [(1, 0), (2, 0), (2, 1)][b]14. try/except/else [/b] try: put_4000000000_volts_through_it(parrot) except Voom: print "'E's pining!" else: print "This parrot is no more!" finally: end_sketch()
>>> print >> open("somefile", "w+"), "Hello World"
Python 3.2 (r32:88445, Oct 20 2012, 14:09:50) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ... Ellipsis
>>> class C(object): ... def __getitem__(self, item): ... return item ... >>> C()[1:2, ..., 3] (slice(1, 2, None), Ellipsis, 3) >>>
>>> a, b, *rest = range(10) >>> a 0 >>> b 1 >>> rest [2, 3, 4, 5, 6, 7, 8, 9] >>>
>>> first, second, *rest, last = range(10) >>> first 0 >>> second 1 >>> last 9 >>> rest [2, 3, 4, 5, 6, 7, 8]
>>> pow(4, 2, 2) 0 >>> pow(4, 2, 3) 1
>>> lst = ["a", "b", "c"] >>> list(enumerate(lst, 1)) [(1, 'a'), (2, 'b'), (3, 'c')]
>>> set([1,2,3])
>>> {1,2,3}
set([1, 2, 3])
>>> a = [1, 2, 3, 4, 5, 6, 7] >>> a[1:4] = [] >>> a [1, 5, 6, 7]
>>> a = [0, 1, 2, 3, 4, 5, 6, 7] >>> del a[::2] >>> a [1, 3, 5, 7]
>>> isinstance(1, (float, int))
True
>>> isinstance(1.3, (float, int))
True
>>> isinstance("1.3", (float, int))
False
>>> isinstance("1.3", (float, int, str))
True
>>> a, b = {}, {}
>>> a['b'] = b
>>> b['a'] = a
>>> a
{'b': {'a': {...}}}
>>> a, b = [], [] >>> a.append(b) >>> b.append(a) >>> a [[[...]]]
>>> int(u'1234') 1234
>>> class O(object):pass
...
>>> o = O()
>>> setattr(o, "can't touch this", 123)
>>> o.can't touch this
File "<stdin>", line 1
o.can't touch this
^
SyntaxError: EOL while scanning string literal
>>>
File "<stdin>", line 1
o.can't touch this
^
SyntaxError: EOL while scanning string literal
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有