博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《python基础教程》答案(第四章)
阅读量:4167 次
发布时间:2019-05-26

本文共 1882 字,大约阅读时间需要 6 分钟。

《python基础教程》答案(第四章)

4.1

# 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("")

4.2

# 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)

4.3

# 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))

4.4

# 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")

4.5

# 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")

4.6

# 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/

你可能感兴趣的文章
二进制详解:世界上有10种人,一种懂二进制,一种不懂。
查看>>
c语言一个字符变量存储多个字符
查看>>
java接口中方法的默认访问修饰符为public
查看>>
java多线程之并发synchronized
查看>>
java多线程之并发Lock
查看>>
微信公众平台基础配置
查看>>
jpa 和 hibernate 的联系
查看>>
SpringBoot之@SpringBootApplication注解
查看>>
ajax 传JSON 写法
查看>>
SpringBoot之web发展史
查看>>
SpringBoot之开发web页面
查看>>
SpringBoot之快速部署
查看>>
springBoot之jar包在后台(运行:编写start、stop脚本)
查看>>
redis学习
查看>>
SpringBoot之application.properties文件能配置的属性
查看>>
javaWeb监听器、过滤器、拦截器
查看>>
RESTFUL风格的接口
查看>>
后台参数验证配置
查看>>
SpringBoot之外置Tomcat配置
查看>>
java 删除 list 中的元素
查看>>