"yahoo!" if 3 > 2 else 2 # => "yahoo!" with open("myfile.txt") as f: for line in f: print line
#################################################### # Functions #################################################### def varargs(*args): return args varargs(1, 2, 3) # => (1, 2, 3) def keyword_args(**kwargs): return kwargs keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} def all_the_args(*args, **kwargs): print args print kwargs """ all_the_args(1, 2, a=3, b=4) prints: (1, 2) {"a": 3, "b": 4} """####################################################
values = (-x for x in [1, 2, 3, 4, 5])for x in values:print(x) # prints -1 -2 -3 -4 -5 to console/terminal# You can also cast a generator comprehension directly to a list. values = (-x for x in [1, 2, 3, 4, 5]) gen_to_list = list(values) print(gen_to_list) # => [-1, -2, -3, -4, -5]
Python 基础与笔记(4)
最新推荐文章于 2022-11-21 18:37:21 发布