Pyhon中变量不需要声明,只要在使用前赋值,变量赋值后才被创建。
Python变量没有类型,所指的类型只是内存中对象的类型。
Python有六种标准的数据类型:
- Numbers(数字)
- String(字符串)
- List(列表)
- Tuple(元组)
- Sets(集合)
- Dictionaries(字典)
Numbers(数字)
Python 3支持int、float、bool、complex(复数)等类型。运用很直观,使用起来就好象写文章一样,数字运用可作为计算器,如:
>>>
5
+
4
#加
9
>>>
4.3
-
2
#减
2.3
>>>
3
*
7
#乘
21
>>>
2
/
4
#除
0.5
>>>
2
/
/
4
#除后取整数
0
注意:
- 1、Python可以同时为多个变量赋值,如a, b = 1, 2。
- 2、一个变量可以通过赋值指向不同类型的对象。
- 3、数值的除法(/)总是返回一个浮点数,要获取整数使用//操作符。
- 4、在混合计算时,Pyhton会把整型转换成为浮点数。
String(字符串)
Python中的字符串str用单引号(' ')或双引号(" ")括起来,同时使用反斜杠(\)转义特殊字符。
>>> s
=
'Yes I don\'t'
#转义
>>>
print
(s,
type
(s))
Yes I don
't <class '
str
'>
>>>
print
(s,
type
(s),
len
(s))
Yes I don
't <class '
str
'>
11
如果想让反斜杠发生转义,可以在字符串前面添加一个r,表示原始字符串,反斜杠可以作为续行符,表示上一行是下一行的延续。
>>>
print
(
'c:\python\bin\local'
)
c:\pythoin\local
>>>
print
(r
'c:\python\bin\local'
)
c:\python\
bin
\local
字符串可以使用 + 运算符串连接在一起,或者用 * 运算符重复:
>>> book
=
'python'
+
'C++'
+
'Perl'
>>>
print
(book)
pythonC
+
+
Perl
>>> s
=
'good'
*
3
>>>
print
(s)
goodgoodgood
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147373.htm