03-01概念知识-常量变量和类型系统

以实例入学

hello world

学语言第一个程序必定为hello world

(pdf) [root@centos7 PyPDF2.bak]# ipython
Python 3.7.7 (default, Apr  3 2020, 15:51:41)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: print('hello world')                                                                                                                                                      
hello world

常量/变量

常量(仅做了解)

  • 常量:一旦赋值, 就不可以改变, 就是不能重新赋值, python中没有真正的常量,所以只需要稍微了解
  • 字面常量:一个单独出现的量, 未赋值给任何变量或常量(例如上述"hello world")
  • 关于空格:在python中, 除行首的空格外, 其他空格无意义

变量

变量:是一个名字,在赋值符号的左边,这个名字可以指代赋值符号右边的内容
举例说明,如下i就是一个变量

In [2]: i = 3                                                                                                                                                                     

In [3]: print(i)                                                                                                                                                                  
3

类型系统

强类型

Python是强类型的动态语言
强类型:一般和弱类型做对比,指不同类型之间不能相互计算,运算的时候会做类型检查。参考如下例子(python是强类型、shell是弱类型)

  • 在python中执行
In [4]: 4 + 4                                                                                                                                                                     
Out[4]: 8

In [5]: 4 + '4'                                                                                                                                                                   
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-ae36418677da> in <module>
----> 1 4 + '4'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

两个4是不同类型
In [6]: type(4)                                                                                                                                                                   
Out[6]: int

In [7]: type('4')                                                                                                                                                                 
Out[7]: str
  • 在shell中执行
[root@centos7 ~]# expr 4 + 4
8
[root@centos7 ~]# expr 4 + '4'
8

动态类型

动态类型:变量可以重新赋值为其他类型,请参考如下示例(变量i已经赋值了,但也可以重新赋值)

In [8]: i = 4

In [9]: type(i)
Out[9]: int

In [10]: i = '4'

In [11]: type(i)
Out[11]: str

基本类型

基本类型介绍:总结规律,发现基本类型无法当变量名

  • int:整形
  • float:浮点数
  • bool:布尔类型
  • None

整型和长整型

In [16]: type(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ** 10)
Out[16]: int

Python 2.7.5 (default, Nov  6 2016, 00:28:07)     # python2中有长整型概念, 在Python3中就没有
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ** 10)
<type 'long'>

另外有些语言会有长度限制, 如果超长了就会溢出
python int类型没有长度限制, 受限物理内存

浮点数

In [17]: type(12.3)
Out[17]: float

In [19]: 12.00000000000000000000000000000000000000001 == 12   浮点数有精度丢失
Out[19]: True

财务、金融、以及科学计算都必须高精度, 可以先转换为整型计算然后换算为小数部分

布尔类型

In [17]: True                                                                                                                                                                     
Out[17]: True

In [18]: False                                                                                                                                                                    
Out[18]: False

None类型

In [19]: None

复合类型

先做简单介绍,后面都会单独讲解

  • str:字符串
  • list
  • 其他

猜你喜欢

转载自www.cnblogs.com/cishi/p/12942144.html