Python之2与3_is与==的区别_小数据池

一.Python2和Python3区别:
   Python2
     打印输出
       print('hello') -- 支持
       print 'hello' -- 支持
       print('hello'), -- 不换行输出
     用户输入
       raw_input() -- 所有输入均转换为字符串类型,无需引号括起
       input() -- 输入的字符串需要用引号括起
   Python3
     打印输出
     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
         可以设置分隔符,默认空格
         
         可以设置行分割符,默认换行
         
         file参数可以设置输出的位置,默认标准输出
         
         flush = True 实时刷新内存内容显示,进度条时用到
            for i in range(51):
               show_str = "[%-50s%d%%]" %('#' * i,i/50*100)
               print('\r' + show_str,end="",flush=True)
               time.sleep(0.2)
            else:print()
     print('hello') -- 支持
     print 'hello' -- 异常
     
     用户输入
       input()
       
二.is 和 == 的区别
    == 比较两个对象的value值
    is 比较两个对象的地址值
    例1:
      >>> str1 = 'hello'
      >>> str2 = 'hello'
      >>> str1 == str2
      True
      >>> str1 is str2
      True
      >>> print(id(str1),id(str2))
      2817672418616 2817672418616
    例2:
      >>> lst1 = [1,2,3]
      >>> lst2 = [1,2,3]
      >>> lst1 == lst2
      True
      >>> lst1 is lst2
      False
      print(id(lst1),id(lst2))
      2817675688392 2817675670024
   
   Python 的小数据池--定义多个变量时,若值在小数据池范围内,则id值想同
     对于数字 -- -5~256
     int1 = 50
     int2 = 50
     print(id(int1),int(int2))
        1886941744 1886941744
     对于字符串:
       不能有特殊字符
         

猜你喜欢

转载自blog.csdn.net/qq_40199698/article/details/87523266