Python 3.1
1. print為一個function,要用print(),基本的數值處理,example
a = 2
b = 3
a*b
6
print(a+b)
5
2. string index是從0開始,且不能修改(重新assign)example:
word = 'Help' + 'A'
則
word[0:2]
'He'
word[0:]
'HelpA'
word[:3]
'Hel'
string index可以是負值,此時從右方算起,例如:
word[-1]
'A'
word[-2]
'p'
word[:-2]
'Hel'
3. List index也是從0開始,且可被修改,example:
a = ['spam', 'eggs', 100, 1234]
a
['spam', 'eggs', 100, 1234]
a[0]
'spam'
a[-2]
100
a[0] = 'cool'
a
['cool', 'eggs', 100, 1234]
List中的元素也可以是List,例如
q = [2, 3]
p = [1, q, 27]
p
[1, [2, 3], 27]
p[1]
[2, 3]
詳參:
沒有留言:
張貼留言