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

学习给我快乐,没有别的目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值