FIsh论坛《零基础入门学习Python》| 第005讲 课后测试题及答案

第005讲:闲聊之Python的数据类型 | 课后测试题及答案

1.课堂

#e记法:
print(15e10)

#直接去除小数,不进行四舍五入操作

b=int(4.99)
print(b)
#type查询数据的类型
print(type(0.23))
#isinstance判断数据的类型
print(isinstance(0.59,float))

在这里插入图片描述

2.测试题:

0 在 Python 中,int 表示整型,那你还记得 bool、float 和 str 分别表示什么吗?

布尔类型、浮点型、字符串

1.你知道为什么布尔类型(bool)的 True 和 False 分别用 1 和 0 来代替吗

计算机只认识二进制数,所以所有的编程语言最终都会转换成简单的二进制序列给 CPU 按照一定的规则解析。由于二进制只有两个数:0 和 1,因此用 0 和 1 来表示 False 和 True 再适合不过了,因为不用浪费资源在转换的过程上!

2.使用 int() 将小数转换为整数,结果是向上取整还是向下取整呢?

print(int(3.16))
print(int(-3.16))
输出结果:
3
-3

3 我们人类思维是习惯于“四舍五入”法,你有什么办法使得 int() 按照“四舍五入”的方式取整吗?

5.4 “四舍五入”结果为:5,int(5.4+0.5) == 5
5.6 “四舍五入”结果为:6,int(5.6+0.5) == 6**

print(int(3.56+0.5))
print(int(-3.56+0.5))
输出结果:
4
-3

4 取得一个变量的类型,视频中介绍可以使用 type() 和 isinstance(),你更倾向于使用哪个?

type,更方便(×)
建议使用 isinstance(),因为它的返回结果比较直接,另外 type() 其实并没有你想象的那么简单,我们后边会讲到。

5 Python3 可以给变量命名中文名,知道为什么吗?
Pyhton3 源码文件默认使用utf-8编码(支持中文)

3.编程题

0.参考案例改造游戏:

s = 'I LOVE FISHC'
print(s.isupper())
输出:
True
小知识:

s 为字符串

s.isalnum() 所有字符都是数字或者字母,为真返回 True,否则返回 False。

s.isalpha() 所有字符都是字母,为真返回 True,否则返回 False。

s.isdigit() 所有字符都是数字,为真返回 True,否则返回 False。

s.islower() 所有字符都是小写,为真返回 True,否则返回 False。

s.isupper() 所有字符都是大写,为真返回 True,否则返回 False。

s.istitle() 所有单词都是首字母大写,为真返回 True,否则返回 False。

s.isspace() 所有字符都是空白字符,为真返回 True,否则返回 False。
改写代码:

temp = input("请输入一个整数:")
while temp.isalpha():  #只能判断字符,如果是浮点型不能判断
    print("输入不合法")
    temp = input("请输入一个整数:")
print("好滴")

参考代码:

print("请输入一个整数:",end="")
temp = input()
while not temp.isdigit(): #这里用到的是isdigit,这样浮点型也可以识别
    print("输入不合法")
    temp = input("请输入一个整数:")
print("好滴")
# not操作符的作用是将布尔类型的结果翻转:即取反的意思,not True == Flase

1.写一个程序,判断给定年份是否为闰年。(注意:请使用已学过的 BIF 进行灵活运用)
这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年。
a.自己的代码:

print("请输入一个年份:", end="")
year = int(input())
c = (year % 400)
a = (year % 4)
b = (year % 100)
if (year) > 0:
    if (c) == 0:
        print("能被400整除,闰年")
        print("a=", a, "b=", b, "c=", c)
    elif ((a == 0) and (not (b == 0))):
        print("能被4整除,不能被100整除,闰年")
        print("a=", a, "b=", b, "c=", c)
    else:
        print("平年")
        print("a=", a, "b=", b, "c=", c)
if (year) == 0:
    print("公元0年不存在,如果存在,为闰年;")

if (year) < 0:#公元前1年(-1年)为公元0年
    year+=1
    c = (year % 400)
    a = (year % 4)
    b = (year % 100)
    if (c) == 0:
        print("能被400整除,闰年")
        print("a=", a, "b=", b, "c=", c)
    elif ((a == 0) and (not (b == 0))):
        print("能被4整除,不能被100整除,闰年")
        print("a=", a, "b=", b, "c=", c)
    else:
        print("平年")
        print("a=", a, "b=", b, "c=", c)

b.参考代码:

temp = input('请输入一个年份:')
while not temp.isdigit():
    temp = input("抱歉,您的输入有误,请输入一个整数:")

year = int(temp)
if year/400 == int(year/400):
    print(temp + ' 是闰年!')
else:
    if (year/4 == int(year/4)) and (year/100 != int(year/100)):
        print(temp + ' 是闰年!')
    else:
        print(temp + ' 不是闰年!')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值