#hello world
print("Hello world!")
#基本数据类型
x = int("32") print(x) print(type(x)) print(type("Hello world!")) arr = "doris", "angel" print(len(arr)) arr = "teapot", print(len(arr)) arr = ["angel", "foo", "teapot", 45, 33] print(type(arr), arr[1], type(arr[1]), type(arr[3])) print(len("automatically")) arr.append("doris") print(arr) a = None print(a is None) # while True: # i = input("Input a number:") # try: # i = int(i) # if i == 0: # break # print("Input number is:", i) # except ValueError as err: # print(err) print(random.choice(arr))
#字符串
s = "The sword of truth" print("{0}".format(s)) print("{0:25}".format(s)) print("{0:>25}".format(s)) print("{0:^25}".format(s)) print("{0:-^25}".format(s)) print("{0:.<25}".format(s)) print("{0:.10}".format(s)) maxwidth = 12 print("{0}".format(s[:maxwidth])) print("{0:.{1}}".format(s, maxwidth)) print("{0:-^25}".format("number")) # data = 8749203 print("{0:0=12}".format(8749203)) print("{0:0=12}".format(-8749203)) print("{0:012}".format(8749203)) print("{0:012}".format(-8749203)) print("{0:*<15}".format(18340427)) print("{0:*>15}".format(18340427)) print("{0:*^15}".format(18340427)) print("{0:*^15}".format(-18340427)) print("[{0:}][{1:}]".format(123456, -123456)) print("[{0:+}][{1:+}]".format(123456, -123456)) print("[{0:-}][{1:-}]".format(123456, -123456)) print("{0:b} {0:o} {0:x} {0:X}".format(4613198)) print("{0:#b} {0:#o} {0:#x} {0:#X}".format(4613198)) print("{0:,}".format(int(2.39432185e6))) print("{0:*>13,}".format(int(2.39432185e6))) print("{who} turned {age} this year!".format(who="Doris", age=20)) locale.setlocale(locale.LC_ALL, "") x, y = (1234567890, 1234.56) locale.setlocale(locale.LC_ALL, "C") c = "{0:n} {1:n}".format(x, y) print(c) # locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # en = "{0:n} {0:n}".format(x, y) # print(en) # locale.setlocale(locale.LC_ALL, "de_DE.UTF-8") # de = "{0:n} {0:n}".format(x, y) # print(de) a = "a" print(a * 10) amount = (10 ** 3) * math.pi print("[{0:12.2e}][{0:12.2f}]".format(amount)) print("[{0:*>12.2e}][{0:*>12.2f}]".format(amount)) print("[{0:*>+12.2e}][{0:*>+12.2f}]".format(amount)) print("{:,.6f}".format(decimal.Decimal("1234567890.1234567890")))
#collection
import math import collections for x, y in ((-3, 4), (5, 12), (28, -45)): print(math.hypot(x, y)) Sale = collections.namedtuple("Sale", "productid customerid date quantity price") sales = [] sales.append(Sale(432, 921, "2008-09-14", 3, 7.99)) sales.append(Sale(419, 874, "2008-09-15", 1, 18.49)) total = 0 for sale in sales: print("sale:{0} {1} {2} {3} {4}".format(sale.productid, sale.customerid, sale.date, sale.quantity, sale.price)) total += sale.quantity * sale.price print("Total ${0:.2f}".format(total)) Aircraft = collections.namedtuple("Aircraft", "manufacturer model seating") Seating = collections.namedtuple("Seating", "minimum maximum") aircraft = Aircraft("Airbus", "A320-200", Seating(100,220)) print(aircraft.seating.maximum) print("{0} {1}".format(aircraft.manufacturer, aircraft.model)) print("{0.manufacturer} {0.model}".format(aircraft)) print("{0:-^25}".format("")) first, *rest = "This is a list test".split() print(first, rest) # for ... in ... leaps = [] for year in range(1900, 1940): if(year % 4 == 0 and year % 100 != 0) or year % 400 == 0: leaps.append(year) print("leaps:", leaps) leaps = [y for y in range(1900, 1940) if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)] print("leaps:", leaps) codes = [] for sex in "MF": for size in "SMLX": for color in "RGB": codes.append(sex + size + color) print(codes) codes = [s + z + c for s in "MF" for z in "SMLX" for c in "RGB"] print(codes) #set list lists = list("apple") set_test = set("apple") print(lists) print(set_test) set_test.add("s") print(set_test) set_test.pop() print(set_test) #Dictionary d1 = dict({"id": 1984, "name": "Washer", "size": 3}) #字典字面值创建 d2 = dict(id=1984, name="Washer", size=3) #关键字参数创建,最紧凑最方便 d3 = dict([("id", 1984), ("name", "Washer"), ("size", 3)]) #从序列中创建 d4 = dict(zip(("id", "name", "Washer"), (1984, "Washer", 3))) #从序列中创建 d5 = {"id": 1984, "name": "Washer", "size": 3} #从字典字面值创建 for d in d1.items(): print(d[0], d[1]) for key, value in d1.items(): print(key, value) for value in d1.values(): print(value)