小甲鱼python视频005闲聊之Python的数据类型--笔记 2018.1.1

知识点1. e记法(针对特别大或者特别小的数)

>>> a = 0.0000000000000000000000000000000000000025
>>>a
2.5e-39 (10的负39次方)

>>>1.5e11

150000000000.0

e就是10的意思,后面11是指10的11次方。

知识点2. 布尔类型

《笨办法学python》习题27+28

参考《Python编程快速上手让繁琐工作自动化》P17 

True = 1,False = 0

>>> True + True

2

>>> True + False

1

>>>True / False
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-5-dcacc0b7b97e>", line 1, in <module>
    True / False
ZeroDivisionError: division by zero

False = 0, 0不能作为除数

知识点3:类型转换

小甲鱼python视频005闲聊之Python的数据类型--笔记 2018.1.1

>>> c = str(5e19)   ----- 自动添加单引号

>>> c

'5e+19'

>>> str = "I love fishc"
str
'I love fishc'
>>> c = str(5e19)
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-49a656b321ee>", line 1, in <module>
    c = str(5e19)
TypeError: 'str' object is not callable

str被当作变量名,就等于被赋予了另外一种身份,当你再度使用时,会报错。

知识点4:type()函数 -- 获得关于类型的信息

>>>a = '520'
>>>type (a)
str

>>>type(True)
bool

知识点5:isinstance()函数 -- 获得关于类型的信息

>>> a = '小甲鱼'
>>> isinstance(a,str)
True

>>> isinstance(320,float)
False

小甲鱼python视频005闲聊之Python的数据类型--笔记 2018.1.1

课后习题

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

对于正数来说,向下取整。(5.5向上取整为6,向下取整为5)

负数则是相反。

3. 有什么办法使得int()按照“四舍五入”的方式取整 ?

5.4

int(5.4+0.5) == 5

5.6

int(5.6+0.5) == 6

动动手:

import random

times = 3
secret = random.randint(1, 10)

print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")

while (guess != secret) and (times > 0):
    temp = input()

    if temp.isdigit(): #所有字符都是数字,为真返回True
        guess = int(temp)
        if guess == secret:
            print("我草,你是小甲鱼心里的蛔虫吗?!")
            print("哼,猜中了也没有奖励!")
        else:
            if guess > secret:
                print("哥,大了大了~~~")
            else:
                print("嘿,小了,小了~~~")
            if times > 1:
                print("再试一次吧:", end='')
            else:
                print("机会用光咯T_T")
    else:
        print("抱歉,您的输入有误,请输入一个整数:", end='')

    times = times - 1  # 用户每输入一次,可用机会就-1

print("游戏结束,不玩啦^_^")

写一个程序,判断是否为闰年。

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 + ' 不是闰年!')