发布时间:2019-09-23 17:01:06编辑:auto阅读(1918)
一、变量
变量格式:
变量名 = 变量值
例子:[root@localhost~]# python
Python2.7 (r27:82500, Jul 28 2016, 02:42:00)
[GCC4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type"help", "copyright", "credits" or "license"for more information.
>>>a = 2 #定义变量a 等号两边有空格
>>> a #查看变量的值方法1
2
>>> print a #查看变量的值方法2
2
第1种查看变量值的方法在python的命令行中是可行的。
但是写在脚本中,执行脚本是不能打印出变量a的值的。
注意:变量名称不能以数字或是Python的关键字开头。
二、数据类型
python常见的数据类型有:
查看数据类型的函数
type (变量名称) 这样就可以查看出变量的数据类型
1. 数字:
(a)整型(int):
>>> a = 123
>>> type (a)
<type 'int'>
(b) 长整型(ling):
>>> b =12345678901234567890
>>> type (b)
<type 'long'>
(c) 浮点型 (float):
浮点型就是小数
>>> a = 1.2
>>> type (a)
<type 'float'>
(d) 布尔型(False和True): 引用的时候,首字母大写
>>> 2 > 1
True
>>> 2 < 1
False
>>>
3. 字符串:
当变量的值 #需要注意的是当字符串作为变量的值的时候,需要用引号引起来
>>> a = 'abc'
>>> type (a)
<type 'str'>
三、类型转换
(1) str(),repr()或是format()用这个3个内置函数可以将非字符串类型转换为字符串。
例子:>>> a = 1
>>> type(a)
<type 'int'>
>>> a=str(a)
>>> type(a)
<type 'str'>
(2)强制转换为整型
int()
(3)转为为浮点型
float()
(4)将字符串转换为列表
list()
(5)将字符转转换为元组
tuples()
(6)将字符串转换为集合
set()
上一篇: ns3仿真
下一篇: Python 3 学习笔记:序列
48706
47728
38511
35712
30150
26888
25905
20759
20519
18916
308°
376°
415°
423°
410°
418°
464°
527°
653°
655°