Python学习
$ python [15:50:40] Python2.7.6(default,Mar222014,22:59:56) [GCC 4.8.2] on linux2 Type"help","copyright","credits"or"license"for more information. >>>print('hello world'); hello world >>>
1 print('hello world');
1 shiyanlou:~/ $ python hello.py [15:55:05] 2 python: can't open file 'hello.py': [Errno 2] No such file or directory 3 shiyanlou:~/ $ cd desktop [15:55:15] 4 cd:cd:13: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55: desktop 5 shiyanlou:~/ $ cd Desktop [15:55:30] 6 shiyanlou:Desktop/ $ python hello.py [15:55:42] 7 hello world 8 shiyanlou:Desktop/ $
1 #!/usr/bin/env python 2 print('hello world');
1 shiyanlou:Desktop/ $ chmod 755 hello.py [16:02:17] 2 shiyanlou:Desktop/ $ ./hello.py [16:02:40] 3 hello world 4 shiyanlou:Desktop/ $ [16:02:53]
>>> a =100 >>>print a >>>print type(a)
s1 =(2,1.3,'love',5.6,9,12,False)# s1是一个tuple s2 =[True,5,'smile']# s2是一个list print s1,type(s1) print s2,type(s2)
一个序列作为另一个序列的元素:
1 s3 =[1,[3,4,5]]
空序列:
1 s4 =[]
序列元素的下标从0开始:
1 print s1[0] 2 print s2[2] 3 print s3[1][2]
由于list的元素可变更,你可以对list的某个元素赋值:
1 s2[1]=3.0 2 print s2
如果你对tuple做这样的操作,会得到错误提示。
所以,可以看到,序列的引用通过s[int]实现,(int为下标)。
print s1[:5]# 从开始到下标4 (下标5的元素 不包括在内) print s1[2:]# 从下标2到最后 print s1[0:5:2]# 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素) print s1[2:0:-1]# 从下标2到下标1
print s1[-1]# 序列最后一个元素 print s1[-3]# 序列倒数第三个元素
1 str ='abcdef' 2 print str[2:4]
shiyanlou:~/ $ cd Desktop[19:12:53] shiyanlou:Desktop/ $ python hello.py [19:12:59] cd
1 print1+9 2 print1.3-4 3 print3*4 4 print4.5/1.5 5 print3**2 6 print10%3
shiyanlou:Desktop/ $ python hello.py [19:19:43]
10
-2.7
12
3.0
9
1
1 print5==6 2 print5!=6 3 print3<3,3<=3 4 print4>5,4>=0 5 print5in[1,2,3]
shiyanlou:Desktop/ $ python hello.py [19:20:26]
False
True
FalseTrue
FalseTrue
False
1 print True and False,True and True 2 print True or False 3 print not True
shiyanlou:Desktop/ $ python hello.py [20:59:38]
FalseTrue
True
False
1 i =1 2 if i >0: 3 print'x>0'
shiyanlou:Desktop/ $ python hello.py [21:05:24]
x>0
1 i =1 2 if i >0: 3 print'positive i' 4 i = i+1 5 elif i ==0: 6 print'i is 0' 7 i = i*10 8 else: 9 print'negative i' 10 i = i-1 11 print'new i : ',i
shiyanlou:Desktop/ $ python hello.py [21:07:41]
positive i
new i :2