Python 有点意思
基本语法
>>> width = 20
>>> height = 2 * 3
>>> width * height
120
>>> x = y = z = 0
>>> x
0
>>> y
0
>>> z
0
变量在使用前,必须定义
>>> n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
对浮点数支持很好
>>> 7.0 / 2
3.5
>>> 7 / 2
3
>>> hello = "This is a string\n Hello Python."
>>> print hello
This is a string
Hello Python.
>>> hello
'This is a string\n Hello Python.'
print 可以输出内容。
字符串可以由 + 操作符连接(粘到一起),可以由 * 重复。
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> word * 5
'HelpAHelpAHelpAHelpAHelpA'
这个厉害了。
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>> word[:2]
'He'
>>> word[2:]
'lpA'
>>> word[1:100]
'elpA'
>>> word[10:0]
''
>>> word[2:1]
''
>>> word[-1]
'A'
>>> word[-2]
'p'
>>> word[-2:]
'pA'
>>> word[:-2]
'Hel'
+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4
-5 -4 -3 -2 -1
>>> s = "hello world"
>>> len(s)
11
>>> u'Hello\u0020World !'
u'Hello World !'
转码序列 \u0020 表示在指定位置插入编码为 0x0020 的 Unicode 字符(空格)。
>>> h = u'Hello\u0020World !'
>>> print h
Hello World !
>>> h
u'Hello World !'
列表
>>> a = ['hello','world',100,20]
>>> a
['hello', 'world', 100, 20]
>>> len(a)
4
>>> a[0]
'hello'
>>> a[3]
20
>>> a[0:2] + ['python',2*2]
['hello', 'world', 'python', 4]
编程
>>> a,b = 0,1
>>> a
0
>>> b
1
>>> while b < 10:
... print b
... a,b = b,a+b
...
1
1
2
3
5
8
>>> i = 2 * 2
>>> print 'The value of i is',i
The value of i is 4
学习给我快乐,没有别的目的。