Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// Author: Huahua import re import io # Example 1: a = [0] * 100 if len(a) > 10: print(f"List is too long ({len(a)} elements, expected <= 10)") n = len(a) if n > 10: print(f"List is too long ({n} elements, expected <= 10)") if (x := len(a)) > 10: print(f"List is too long ({x} elements, expected <= 10)") # Example 2, bad ads = "Now 20% off till 6/18" m1 = re.search(r'(\d+)% off', ads) discount1 = float(m1.group(1)) / 100 if m1 else 0.0 discount2 = float(m2.group(1)) / 100 if (m2 := re.search(r'(\d+)% off', ads)) else 0.0 print(f'{discount1 = }, {discount2 = }') # Example 3 f1 = io.StringIO("123456789") x1 = f1.read(4) while x1 != '': print(x1) x1 = f1.read(4) f2 = io.StringIO("123456789") while (x2 := f2.read(4)) != '': print(x2) # Example 4 prog_langs = {'c++', 'python', 'java'} langs = ['C++', 'Java', 'PYthon', 'English', '中文'] l1 = [lang.lower() for lang in langs if lang.lower() in prog_langs] print(l1) l2 = [l for lang in langs if (l := lang.lower()) in prog_langs] print(l2) |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment