|
| 1 | +x = 1 if True else 2 |
| 2 | + |
| 3 | +num1 = 1_000_000_000_000_000 |
| 4 | +num2 = 100_000_000_000 |
| 5 | + |
| 6 | +total = num1 + num2 |
| 7 | + |
| 8 | +print(f"{total:,}") |
| 9 | + |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +script_location = Path(__file__).absolute().parent |
| 14 | +file_location = script_location / "bp.py" |
| 15 | + |
| 16 | +with open(file_location, "r") as f: |
| 17 | + f_contents = f.read() |
| 18 | + |
| 19 | +words = f_contents.split(" ") |
| 20 | +word_count = len(words) |
| 21 | +print(word_count) |
| 22 | + |
| 23 | +name = ["John", "Doe"] |
| 24 | + |
| 25 | +for i, n in enumerate(name, start=1): |
| 26 | + print(i, n) |
| 27 | + |
| 28 | + |
| 29 | +name = ["John", "Doe", "Jane", "Smith", "David"] |
| 30 | +heros = ["Spiderman", "Superman", "Batman", "Wonder Womna"] |
| 31 | +universe = ["Marvel", "DC", "DC", "DC"] |
| 32 | +for n, h, u in zip(name, heros, universe): |
| 33 | + print(f"{n} is actually {h} from {u}") |
| 34 | + |
| 35 | +# unpacking |
| 36 | +a, b, *c = (1, 2, 3, 4, 5) |
| 37 | +print(a) |
| 38 | +print(b) |
| 39 | +print(c, type(c)) |
| 40 | + |
| 41 | +# if you dont care the rest |
| 42 | +a, _ = (1, 2) |
| 43 | +print(a) |
| 44 | + |
| 45 | +# this will throw exception |
| 46 | +# a, b, c = (1, 2) |
| 47 | + |
| 48 | +a, b, *c, d = (1, 2, 3) |
| 49 | +print(a, b, c, d) |
| 50 | + |
| 51 | + |
| 52 | +class Person: |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +person = Person() |
| 57 | + |
| 58 | +person.firstname = "zhang" |
| 59 | +person.lastname = "san" |
| 60 | + |
| 61 | +first_key = "first" |
| 62 | +first_val = "li 4" |
| 63 | + |
| 64 | +setattr(person, first_key, first_val) |
| 65 | + |
| 66 | +first = getattr(person, first_key) |
| 67 | + |
| 68 | +print(first) |
| 69 | + |
| 70 | +print(person) |
| 71 | + |
| 72 | + |
| 73 | +person_info = {"age": 20, "address": "shanghai"} |
| 74 | + |
| 75 | +for key, value in person_info.items(): |
| 76 | + setattr(person, key, value) |
| 77 | + |
| 78 | +for key in person_info.keys(): |
| 79 | + print(getattr(person, key)) |
| 80 | + |
| 81 | +from datetime import datetime |
| 82 | + |
| 83 | +print(dir(datetime)) |
| 84 | + |
| 85 | +print(dir(datetime.today)) |
| 86 | + |
| 87 | +print(datetime.today) |
0 commit comments