python程序執(zhí)行
- python是解釋性語言,不需要先編譯成二進(jìn)制語言壶谒。
- python不需要main函數(shù)也能執(zhí)行丙曙。
- 順序執(zhí)行
test()函數(shù)在main()上方,則先執(zhí)行的是test函數(shù) - __name__是系統(tǒng)內(nèi)置變量掏婶,代表所在模塊名字,也即所在文件名潭陪。
- if __name__ == '__main__'
當(dāng).py文件被直接執(zhí)行時(shí)雄妥,該代碼后的代碼將被執(zhí)行最蕾;
當(dāng).py文件被作為模塊導(dǎo)入時(shí),改代碼后的代碼不被執(zhí)行老厌。 - python中的def如果不被調(diào)用瘟则,就不會(huì)被執(zhí)行。
——————————————————————————————————————————
python的七個(gè)標(biāo)準(zhǔn)數(shù)據(jù)類型
Bool(布爾):
True和False
Number(數(shù)字)[不可變數(shù)據(jù)]:
分為int(整數(shù))枝秤、long(長(zhǎng)整數(shù))壹粟、float(浮點(diǎn)數(shù))、complex(復(fù)數(shù))
默認(rèn)17位小數(shù)精度宿百,即小數(shù)點(diǎn)后16位是準(zhǔn)確的。
String(字符串)[不可變數(shù)據(jù)]:
轉(zhuǎn)義符 " \ "引入r可不轉(zhuǎn)義:
print(r"\\\\t\\") # \\\t\\
Tuple(元組)[不可變數(shù)據(jù)]:
與列表相似洪添,但元組內(nèi)的元素不可修改/刪除垦页,但可以將整個(gè)元組刪除。
創(chuàng)建元組:
tup1=() ##空元組
tup2=(30,) ##只含一個(gè)元素需要加逗號(hào)
List(列表):
創(chuàng)建列表:
list0 = ['hello', 'world', 1111, 2222]
list1 = [1, 2, 3, 4, 5 ]
list2 = ["a", "b", "c", "d"]
list3= [] ##空列表
更新列表:
list = []
list.append('Hello') ## 使用 append() 添加元素
list.append('World')
print list
##輸出:
['Hello', 'World']
刪除元素:
list = ['a', 'b', 1111, 2222]
del list[2]
print list
##結(jié)果:
## ['a', 'b', 2222]
Set(集合):
一個(gè)無序的不重復(fù)元素序列干奢。
空集用set()痊焊,不能用{},{}是用來創(chuàng)建空字典的忿峻。
創(chuàng)建集合:
set1 = {'hello','world','hello'} #方式1
set2 = set('abcdefg') #方式2
print(set1)
print(set2)
##結(jié)果:
##{'helllo','world'} ##有去重功能
##{'a','b','c','d','e','f','g'}
Dictionary(字典):
{}空字典
關(guān)鍵值+對(duì)應(yīng)數(shù)據(jù)
dict = {'a': 'Acc', 'b': 666, 'c': 'ccc'}
print "dict['a']: ", dict['a']
print "dict['b']: ", dict['b']
dict['b'] = 999 ## 更新
dict['d'] = "ddd" ## 添加
print "dict['b']: ", dict['b']
print "dict['d']: ", dict['d']
##結(jié)果:
##dict['a']: Acc
##dict['b']: 666
##dict['b']: 999
##dict['d']: ddd
——————————————————————————————————————————
for in [ ]
for a in [1]:
print(a)
##輸出: 1
for a in [1,2,3]:
print(a)
##輸出:
##1
##2
##3
——————————————————————————————————————————
os.path.join的用法
import os
p1 = 'first'
p2 = 'second'
name = 'ccc.txt'
p3 = os.path.join(p1,"ABCDE",p2+name)
print ( p3 )
##結(jié)果:
##first/ABCDE/secondccc.txt
——————————————————————————————————————————
python面向?qū)ο?/h2>
類的私有屬性
__xxxx:類的屬性名中薄啥,兩個(gè)下劃線開頭,則該屬性為私有逛尚,不能在類的外部被使用或直接訪問垄惧。
類的繼承
class student(people): ##類student繼承類people
class sample(speaker,student): ##多重繼承
__init__ : 構(gòu)造函數(shù),在生成對(duì)象時(shí)調(diào)用
——————————————————————————————————————————
[]用法
a='python'
d=a[:-1] #從位置0到位置-1之前的數(shù)
print(d) #pytho
e=a[:-2] #從位置0到位置-2之前的數(shù)
print(e) #pyth