Python的内置数据类型

# coding  = utf-8

print("内置的数据类型")
print("真值测试(if,while等的条件测试)")

cond = None
if not cond:
    print("None在条件测试中为False")

cond = False
if not cond:
    print("False在条件测试中为False")

cond = 0
if not cond:
    print("整数0在条件测试中为False")

cond = 0.0
if not cond:
    print("浮点数0.0在条件测中为False")

cond = ''
if not cond:
    print("空字符串在条件测试中为False")

cond = ()
if not cond:
    print("()在条件测试中为False")
cond = (1)
if cond:
    print("非空()在条件测试中为True")

cond = []
if not cond:
    print("[]在条件测试中为False")
cond = [2, 3, 4]
if cond:
    print("非空[]在条件测试中为True")

cond = {}
if not cond:
    print("空{}在条件测试中为False")
cond = {"code": "IF1701", "name": "jacky"}
if cond:
    print("非空{}在条件测试中为True")


class CondClass:
    def __init__(self):
        self.name = "没有定义__boolean()或__len__()"


cond = CondClass()
if cond:
    print(cond.name, "的条件测试为True")


class CondClassBoolean:
    def __init__(self):
        self.name = "定义了__bool__()方法且返回False"

    def __bool__(self):
        return False


cond = CondClassBoolean()
if not cond:
    print(cond.name, "的条件测试为False")


class CondClassLen:
    def __init__(self):
        self.name = "定义了__len__方法且返回0"

    def __len__(self):
        return 0


cond = CondClassLen()
if not cond:
    print(cond.name, "的条件测试为False")

print("逻辑运算符and, or, not")
x = True
y = True
print("True and True is", x and y)
print("True or True is", x or y)
x = False
y = False
print("False and False is", x and y)
print("False or False is", x or y)
x = False
y = True
print("False and True is", x and y)
print("False or True is", x or y)

print("比较操作符")
x = 10
y = 20
z = 20
print("下面的两个表达式效果相同")
print("x < y <= z is", x < y <= z)
print("(x < y) and (y <= z) is", (x < y) and (y <= z))

print("is和is not,测试的是对象地址是否一样。")
cond1 = CondClass()
cond2 = CondClass()
print("cond1 is cond2 的测试结果是", cond1 is cond2)
cond1 = cond2
print("cond1 is cond2 的测试结果是", cond1 is cond2)
x = 132
y = 132
print("x的地址是%s, y的地址是%s, x is y的条件测试结果是%s" % (hex(id(x)), hex(id(y)), x is y))

print()
print("数字类型,int, float, complex")
x = 110
y = 10
print("x = %d, y = %d" % (x, y))
print("x + y = %d" % (x + y))
print("x - y = %d" % (x - y))
print("x * y = %d" % (x * y))
print("x / y = %d" % (x / y))
print("121 / y = %f" % (121 / y))
print("pow(2,10) = %d" % (pow(2, 10)))
print("2 ** 10 = %d" % (2 ** 10))

print()
print("整数的操作")
x = -37
print("x = %d" % (x))
print("转换为二进制,不包含符号位 bin(x) = %s" % (bin(x)))
print("转换为二进制,不包含符号位 x.bit_length() = %s" % (x.bit_length()))

print("int.from_bytes(b'\\x00\\x10') = %d" % (int.from_bytes(b'\x00\x10', byteorder='big')))  # 相当于二进制00000000 00010000
print("int.from_bytes(b'\\x00\\x10') = %d" % (
int.from_bytes(b'\x00\x10', byteorder='little')))  # 相当于二进制00001000 00000000()顺序与00000000 00010000相反

print()
print("浮点数的其他方法")
print("(-20.0).is_integer() = %s" % ((-20.0).is_integer()))
print("(230.001).is_integer() = %s" % ((230.001).is_integer()))
print("float.hex(3470.0) = %s" % (float.hex(3470.0)))
print("float.fromhex('0x3.a7p10') = %f" % (float.fromhex('0x3.a7p10')))

print("数字类型的Hash算法")
print("hash(100) = %s" % (hash(100)))
print("hash(-100) = %s" % (hash(-100)))  # 负数的hash值也是有符号数
print("hash(200.120) = %s" % (hash(200.120)))
print("hash(-200.120) = %s" % (hash(-200.120)))

print()
print("序列类型list, tuple, range")
list = [10, 20, 30, 40, -10, -20, -15, 15, 200]
print("list = %s" % (list))
print("(-15 in list) = %s" % (-15 in list))
print("(1000 in list) = %s" % (1000 in list))
print("(-15 not in list) = %s" % (-15 in list))
print("(1000 not in list) = %s" % (1000 in list))
list2 = [20, 30, 100, 120, -120, -300, -15]
print("list2 = %s" % (list2))
print("list + list2 = %s" % (list + list2))
list3 = list + list2
print("list3 = %s" % (list3))
print("list3中第2个到20个,步长为2, list3[2:20:2] = %s" % (list3[2:20:2]))
print("-15在列表中出现的次数, list3.count(-15) = %d" % (list3.count(-15)))

print("[[]]*3 = %s" % ([[]] * 3))  ### [[],[],[]]
lists = [[] for i in range(3)]
print("[[] for i in range(3)] = %s" % (lists))

print("不可修改序列类型tuple")
lists = tuple([1, 3, 4, 10])
print("tuple序列类型 = ", (lists))

#print(list(range(2, 30)))
#print("list(range(5,20)) = %s, 最大值为19" % (list(range(5, 20))))
print("字符串数据类型")
print("单引号", '允许嵌入"双引号"')
print("双引号", "允许嵌入'单引号'")
print("三引号", '''三个单''引号
你可以写多行
''', """三个双引号dd
同样可以写多行
dd点点滴滴""")      ##三引号是什么样子???字符串中间''代表'
str = "hard work beats A TAlent"print("首字母大写,(%s).capitalize() = %s" %(str, str.capitalize()))print("casefold()和lower()的差别。。。。。。")print('"ß".casefold() =', "ß".casefold())print('"ß".lower() =', "ß".lower())print("('%s').center(50,'a') = %s" %(str, str.center(50, 'a')))str = "hard work beats A TAlent"print("'%s'.encode('utf-8') = %s" %(str, str.encode('utf-8')))print("字符串的函数太多,将另外单独处理。。。。。。。。")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值