Python笔记: 类型和类型转换

类型和类型转换

  • 目前我们已经接触的数据类型

    1. 整型
    2. 浮点型
    3. 布尔型
    4. 字符串
    
  • 使用 type() 用来检查你所处理的任何变量的数据类型

    print(type(4)) # int
    print(type(3.7)) # float
    print(type('this')) # str
    print(type(True)) # bool
    

类型和类型转换[相关练习]

  • 你认为以下代码的结果是什么?

    "0" + 5
    

    将会报错:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be str, not int
    

    or

    0 + "5"
    

    将会报错:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    
  • 以下对象是什么类型:"12"

    str
    
  • 以下对象是什么类型:12.3

    float
    
  • 以下对象是什么类型:len("my_string")

    int
    
  • 以下对象是什么类型:"hippo" *12

    str
    
  • 计算总销量

    mon_sales = "121"
    tues_sales = "105"
    wed_sales = "110"
    thurs_sales = "98"
    fri_sales = "95"
    #TODO: Print a string with this format: This week's total sales: xxx. You will probably need to write some lines of code before the print statement.
    total = int(mon_sales) + int(tues_sales) + int(wed_sales) + int(thurs_sales) + int(fri_sales)
    print("This week\'s total sales: " + str(total)) # This week's total sales: 529
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值