Python入门教程-02 变量

来源:互联网 发布:韩国扫码查真伪软件 编辑:程序博客网 时间:2024/05/20 06:26

1. 代码示例

继续上节给出的例子进行说明:

flying-bird@flyingbird:~/examples/python$ cat test.py #!/usr/bin/env pythonx = 5y = 3z = x + yprint "%d + %d = %d\n" % (x, y, z)flying-bird@flyingbird:~/examples/python$ 

2. 变量

在这个例子中,xyz均属于变量。在Python中,变量使用之前无需声明,这也是大多脚本语言或解释性语言的共同特点。


此外,变量没有固定的数据类型;换句话讲,同一个名称的变量可用于记录不同数据类型的数值。如:

flying-bird@flyingbird:~/examples/python$ cat test.py#!/usr/bin/env pythonx = 5print xx = "This is five."print xx = [5, 6, 7]print xflying-bird@flyingbird:~/examples/python$ ./test.py 5This is five.[5, 6, 7]flying-bird@flyingbird:~/examples/python$ 

在这个例子中,变量x依次用于存储三种类型的数据:整型、字符串、列表。


在编译型语言中,因为一开始需要对变量进行声明,即会指定一个数据类型,因此之后就不能赋值为其他类型的数据,否则就会有编译错误。


3. 变量名称

关于变量名称,只需要掌握最基本的规则即可,即字母开头,后面可以跟字母、数字、下划线;区分大小写。示例:

flying-bird@flyingbird:~/examples/python$ pythonPython 2.7.3 (default, Feb 27 2014, 20:00:17) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 5 = 3  File "<stdin>", line 1SyntaxError: can't assign to literal>>> 5x = 3  File "<stdin>", line 1    5x = 3     ^SyntaxError: invalid syntax>>> x5 = 3>>> this is a varible = 3  File "<stdin>", line 1    this is a varible = 3                    ^SyntaxError: invalid syntax>>> this_is_a_variable = 3>>> "this is a variable" = 5  File "<stdin>", line 1SyntaxError: can't assign to literal>>> FIVE = 5>>> FIVE5>>> X5=50>>> print x5, X53 50>>> 

4. 数据类型

如同前面所描述的,在Python中,没有变量声明一说,自然也没有所谓的关键字来定义变量的类型。即,如C语言中的int bool long char等,在Python中是不存在的。


但是否就说明Python不存在数据类型的说法呢?恰恰相反,Python的的确确有数据类型一说。但数据类型不是通过声明变量的方式去定义的,而是通过给变量赋值的具体数值来确定的。因为数据类型的多样性,我们这里先介绍几种简单的,因为后续在介绍各个专题的时候还会详细分析。


4.1 整型

通过下面的例子即可说明,唯一需要提及的是Python中有所谓的长整数,后面以小写或大写字母L表示。

>>> x = 5>>> y = x * 100>>> y500>>> >>> x = 12345678901234567890>>> x12345678901234567890L>>> x * 561728394506172839450L>>> >>> 

4.2 字符串

对于字符串只需要提及如下几点:

  • 字符串可以用双引号或单引号来表示;
  • 双引号表示的字符串内部可以用单引号,或者相反;
  • 字符串的拼接用加号+;——有些脚本语言使用&符号,但Python不支持;
  • 字符串中可以有转义字符,如\n;——其他常见的转义字符是可以想象到的,所以不一一列举;
  • 如同C语言一样,字符串如果太长,可以分开一个个定义,Python会自动连接在一起。

>>> s = "Hello, world!">>> s'Hello, world!'>>> >>> s = s + "\nHello, Python!">>> print sHello, world!Hello, Python!>>> s = s & "\nHello, programmer!"Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for &: 'str' and 'str'>>> s = 'First line.\n' 'Second line.'>>> s'First line.\nSecond line.'>>> s = "a b c 'd e' f g">>> s"a b c 'd e' f g">>> s = 'x y z " r s t "'>>> s'x y z " r s t "'>>> s[0]'x'>>> 

4.3 布尔类型

布尔类型是True或False,主要用于条件判断,后面学习if-else的时候会再次提到。

>>> x = True>>> xTrue>>> y = not x>>> yFalse>>> z = 5 == 3>>> zFalse>>> 

4.4 None

要点:

  • 可以给一个变量赋值为None,这是一个特殊的数据。
  • 可以用is None来判断一个变量是否为None。

>>> x = None>>> x>>> print xNone>>> if x is None:...   print "x is None"... else:...   print "x is not None"... x is None>>> x = 5>>> if x is None:...   print "x is None"... else:...   print "x is not None"... x is not None>>> 

None主要的使用场景是什么呢?主要还是用于判断,如同上面的布尔类型一样。通常有3个步骤:

  • 把一个变量(不妨称为x)初始化为None;
  • 调用一个函数、执行一段语句,其中可能对x赋值;
  • 判断x是否为None,以此决定接下来的执行流程。

目前先了解这么多,后续我们做练习的时候会给出具体的例子。


4.5 type()函数

前面是给出了几个简单数据类型的例子。那么严格意义上给出每个数据或变量的类型,则需要调用type()函数。

>>> type(3)<type 'int'>>>> type(1234567890123456789)<type 'long'>>>> type(1.0)<type 'float'>>>> type('hello')<type 'str'>>>> type(False)<type 'bool'>>>> type(None)<type 'NoneType'>>>> 

4.6 复杂数据类型

当然,除了前面列出的几种非常简单的、常见的数据类型之外,Python也支持更多的复杂的数据类型。比如,list set dictionary class等等。——我们后面会分专题进行介绍。



0 0