本文共 1882 字,大约阅读时间需要 6 分钟。
# created by swy"""solutions to beginning python"""def printTrueFalse(X): if X: print("%s is true" % str(X)) else: print("%s is false" % str(X))printTrueFalse(0)printTrueFalse(1)printTrueFalse(2)printTrueFalse(3)printTrueFalse("abc")printTrueFalse("")
# created by swy"""solutions to beginning python"""def inRange(X): if X in range(3, 9): print("%s is in the range" % str(X))inRange(5)
# created by swy"""solutions to beginning python"""lookFor = "milk"lookIn = ["butter", "milk", "cheese"]if lookFor == lookIn[0]: print("found it")elif lookFor == lookIn[1]: print("found it ...")else: print("did not find it")if lookFor in lookIn: print("it is there")else: print("it is not there")print(lookIn.index(lookFor))
# created by swy"""solutions to beginning python"""fridge = { "Milk": "Thise milk", "Cheese": "Emmentaler", "Butter": "Unsalted butter"}food_sought = "Butter"for foodKey in fridge: print("\nlooking at : %s", foodKey) if foodKey == food_sought: print("key: %s \tValue: %s" % (foodKey, fridge[food_sought])) breakelse: print("it was not there")
# created by swy"""solutions to beginning python"""fridge = { "Milk": "Thise milk", "Cheese": "Emmentaler", "Butter": "Unsalted butter"}food_sought = "Butter"fridge_list = []for foodKey in fridge: fridge_list.append(foodKey)print(fridge_list)while fridge_list: if fridge_list.pop() == food_sought: print("found it again") break else: print("not found")
# created by swy"""solutions to beginning python"""fridge = { "Milk": "Thise milk", "Cheese": "Emmentaler", "Butter": "Unsalted butter"}food_sought = "Butter"try: wrongKey = fridge["Beer"]except KeyError: print("We are out of beer")try: goodKey = fridge["Milk"]except KeyError: print("We are out of Milk")
转载地址:http://dhwai.baihongyu.com/