Python入门学习03

Python基础入门学习小总结

Python类型转换

  1. str --> int
    a = ‘123’
    print(type(a))
    b = int(a)
    print(type(b))
  2. float --> int
    c = 3.1415
    print(type©)
  3. float 转整数返回的小数点前面的数据
    d = int©
    print(d)
    print(type(d))
  4. boolean --> int False – 0 True – 1
    x = True
    y = int(x)
    print(y)
    print(type(y))

当字符串中包含非法字符,则不能转换

  1. a = ‘1.123’ or a = ‘123dfg’
    b = int(a)
    print(b)

当获取的数据是字符串类型

  1. 字符串转浮点型
    a=‘12.34’
    b=float(a)
    print(type(b))
  2. 整型转浮点型
    t = 111
    k = float(t)
    print(type(k))
    print(k)

强制类型转换 方法是 str()

  1. 整型转字符串
    r = 80
    s = str®
    print(s)
    print(type(s))

  2. 浮点型转字符串
    m = 2.32
    n = str(m)
    print(n)
    print(type(n))

  3. 布尔类型转字符串
    g = True
    h = str(g)
    print(h)
    print(type(h))

  4. 转换为布尔类型
    整数非0的整数和负数 – True; 0 转布尔为 False
    q = 3
    p = bool(q)
    print§
    print(type§)
    浮点数里非0的 – True; 0.0 转布尔为 False
    q1 = 3.9
    p2 = bool(q)
    print§
    print(type§)
    字符串转布尔 字符串有内容是 True; 没内容是 False
    test = ‘’ #‘werqree’ or ’ ’
    test1 = bool(test)
    print(test1)
    print(type(test1))
    列表 有数据是 – True 和 [] – False
    List = [a,‘b’,c,d]
    List1 = bool(List)
    print(List)
    print(type(List1))
    元组 有数据是 – True 和 () – False
    tuple = (‘w’,‘r’,‘t’)
    tuple1 = bool(tuple)
    print(tuple1)
    print(type(tuple1))
    字典 有数据是 – True 和 {} – False
    dict = {“a”:‘a’}
    dict1 = bool(dict)
    print(dict1)
    print(type(dict1))

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值