python-天池-part1-01

变量、运算符与数据类型

变量

  • 在使用变量前,需要现对其赋值
  • 变量名可以包括字母、数字、下划线,但不能以字母开头
  • python 变量名大小写是敏感的
first = 2
second = 3
third = first + second
print(third)  # 5

运算符

  • 算数运算符

    加(+)、减(-)、乘(*)、除(/)、整除(//)、取余(%)、幂(**)

    print(1 + 1)  # 2
    print(2 - 1)  # 1
    print(3 * 4)  # 12
    print(3 / 4)  # 0.75
    print(3 // 4)  # 0
    print(3 % 4)  # 3
    print(2 ** 3)  # 8
    
  • 比较运算符

    大于(>)、大于等于(>=)、小于(<)、小于等于(<=)、等于(==)、不等于(!=)

    print(2 > 1)  # True
    print(2 >= 4)  # False
    print(1 < 2)  # True
    print(5 <= 2)  # False
    print(3 == 4)  # False
    print(3 != 5)  # True
    
  • 逻辑运算符

    与(and)、或(or)、非(not)

    print((3 > 2) and (3 < 5))  # True
    print((1 > 3) or (9 < 2))  # False
    print(not (2 > 1))  # False
    
  • 位运算符

    按位取反(~)、按位与(&)、按位或(|)、按位异或(^)、左移(<<)、右移(>>)

    print(bin(4))  # 0b100
    print(bin(5))  # 0b101
    print(bin(~4), ~4)  # -0b101 -5
    print(bin(4 & 5), 4 & 5)  # 0b100 4
    print(bin(4 | 5), 4 | 5)  # 0b101 5
    print(bin(4 ^ 5), 4 ^ 5)  # 0b1 1
    print(bin(4 << 2), 4 << 2)  # 0b10000 16
    print(bin(4 >> 2), 4 >> 2)  # 0b1 1
    
  • 三元运算符

    x, y = 4, 5
    small = x if x < y else y
    print(small)  # 4
    
  • 其他运算符

    存在(in)、不存在(not in)

    letters = ['A', 'B', 'C']
    if 'A' in letters:
        print('A' + ' exists')
    if 'h' not in letters:
        print('h' + ' not exists')
    
    # A exists
    # h not exists
    

    是(is)、不是(not is)

    # 比较的两个变量均指向可变类型
    a = ["hello"]
    b = ["hello"]
    print(a is b, a == b)  # False True
    print(a is not b, a != b)  # True False
    '''
    - is, is not 对比的是两个变量的内存地址
    - ==, != 对比的是两个变量的值
    - 比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。
    - 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。
    '''
    

数据类型

int整型 <class 'int'>
float浮点型<class 'float'>
bool布尔型<class 'bool'>

  1. 整型

    a = 1031
    print(a, type(a))
    # 1031 <class 'int'>
    
  2. 浮点型

    print(1, type(1))
    # 1 <class 'int'>
    
    print(1., type(1.))
    # 1.0 <class 'float'>
    
    a = 0.00000023
    b = 2.3e-7
    print(a)  # 2.3e-07
    print(b)  # 2.3e-07
    
    # 有时候我们想保留浮点型的小数点后 n 位。可以用 decimal 包里的 Decimal 对象和 getcontext() 方法来实现。
    decimal.getcontext().prec = 4
    c = Decimal(1) / Decimal(3)
    print(c)
    # 0.3333
    
  3. 布尔型

    # 在布尔(boolean)型变量中只有True和False两个值,在数字运算中分别用1和0表示
    print(True + True)  # 2
    print(True + False)  # 1
    print(True * False)  # 0
    
    
    # 除了对变量赋值True和False,还可以用bool(X),X可以是基本类型(int、float、bool)和容器类型(字符串、列表、元组、字典和集合),如果X是空的(数值——0,容器——没有元素),返回也是false
    print(type(0), bool(0), bool(1))
    # <class 'int'> False True
    
    print(type(10.31), bool(0.00), bool(10.31))
    # <class 'float'> False True
    
    print(type(True), bool(False), bool(True))
    # <class 'bool'> False True
    

在python中万物皆对象(object),基本数据类型也不例外,只要是对象就有相应的属性(attributes)和方法(methods)。

# 举例找一个整数的二进制表示,再返回其长度
a = 1031
print(bin(a))  # 0b10000000111
print(a.bit_length())  # 11

获取类型信息

  1. type()

    print(type(1)) # <class 'int'>
    print(type(5.2)) # <class 'float'>
    print(type(True)) # <class 'bool'>
    print(type('5.2')) # <class 'str'>
    
  2. isinstance()

    print(isinstance(1, int))  # True
    print(isinstance(5.2, float))  # True
    print(isinstance(True, bool))  # True
    print(isinstance('5.2', str))  # True
    
  3. 区别

    • type() 不会认为子类是一种父类类型,不考虑继承关系。
    • isinstance() 会认为子类是一种父类类型,考虑继承关系。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值