Python語(yǔ)言快速上手學(xué)習(xí)方法
最近在學(xué)習(xí)Python,后面搞機(jī)器人項(xiàng)目需要用到,所以要快速上手,我使用的是PyCharm這個(gè)IDE,看起來(lái)就舒服,學(xué)習(xí)起來(lái)就有勁啦,作為一名有工作經(jīng)驗(yàn)的老司機(jī),我學(xué)習(xí)編程語(yǔ)言的方法不會(huì)像大學(xué)生那樣從頭到尾學(xué)一遍,我會(huì)選擇,夠用,能用,實(shí)用即可,拒絕晦澀的語(yǔ)法,在不影響效率的情況下,我會(huì)采取容易看懂,后期項(xiàng)目可維護(hù)性等的方式來(lái)學(xué)習(xí)和編程,至于如何靈活運(yùn)用Python語(yǔ)言,我認(rèn)為是需要在項(xiàng)目中,才能不斷精進(jìn)的,畢竟,作為一門(mén)編程語(yǔ)言,它僅僅只是工具而已。
如果要在python中寫(xiě)中文,則要在xx.py的最前面聲明
#coding:utf-8
一、基礎(chǔ)語(yǔ)法:變量,字符串,函數(shù),邏輯判斷,循環(huán)
varline = 2 ;
print(varline);
#打印字符串
print("hello Python");
print("你好,Python");
#整型和字符串的轉(zhuǎn)化
num1 = 100 ;
num2 = "100";
num3 = num1 + int(num2);
print(num3);
#字符串操作
str1 = "hello world" ;
str2 = str1 * 3 ;
string_count = len(str1);
print(string_count);
print(str2);
#字符串索引等價(jià)
print(str1[0]); print(str1[-11]) #===>h
print(str1[1]); print(str1[-10]) #===>e
print(str1[2]); print(str1[-9]) #===>l
#可以將字符串進(jìn)行分割
print(str1[0:5]);print(str1[6:11]); #===> hello world
print(str1[-4:]);
#函數(shù)的定義和使用
def Print():
print("hello world");
return "sss" ;
sss = Print();
print(sss);
def add(arg1 , arg2):
return arg1 + arg2 ;
print(add(1,2));
def getTempatuare(temp):
return temp *9/5 + 32 ;
print(str(getTempatuare(35)) + "'F");
#克轉(zhuǎn)千克算法
def print_kg(g):
return float(g / 1000) ;
print(str(print_kg(1)) + "kg");
#求直角三角形斜邊的長(zhǎng)度
def Line_print(arg1,arg2):
return ((arg1*arg1 + arg2 * arg2))**0.5
print("The right triangle third side's length is " + str(Line_print(3,4)));
#str_rp = str1.replace(str1[:3],'*'*9);
#print(str_rp)
str11 = "{} a word she can get what she {} for."
str12 = "{preposition} a word she can get what she {verb} for"
str13 = "{0} a word she can get what she {1} for."
str111 = str11.format('With','came');
str121 = str12.format(preposition = 'With',verb = 'came')
str131 = str13.format('With','came')
print(str111)
print(str121)
print(str131)
#單獨(dú)創(chuàng)建
file1 = open('F:\\'+'hello.txt','w')
file1.write("Hello world");
file1.close()
#使用函數(shù)創(chuàng)建
def text_create(name, msg):
desktop_path = 'F:\\'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print('Done')
text_create('Yang','hello world') # ????
#變量的比較
teststr1 = "Hello"
teststr2 = "World"
teststr3 = "Hello"
print(teststr1 in teststr2)
print(teststr1 is teststr3)
print(bool(teststr1))
print(bool(''))
print(not teststr1)
print(teststr1 < teststr3 and teststr2 > teststr1)
print(teststr1 > teststr2 or teststr3 < teststr1)
#python邏輯判斷學(xué)習(xí)
a = 1
b = 3
if a < b :
a = 3
b = 2
else:
a = 2
b = 3
print(a,b);
if a < b:
a = 3
b = 2
elif a > b:
a = 2
b = 3
else:
a = 100
b = 200
print(a,b)
for i in 1,2,3,4,5,6:
print(i)
for string_str in "hello","world","world":
print(string_str)
for str1111 in "Hello":
print(str1111)
二、Python數(shù)據(jù)結(jié)構(gòu):列表,元組,字典,集合
#python列表===>
#特點(diǎn):可以裝python的所有類(lèi)型,包括元組,列表,字典等
city = ['廣東','云南','廣西','江西','HongKong','Shenzhen',123456]
for i in 0,1,2,3,4,5,6:
print(city[i])
city.insert(1,'北京') #列表的插入
for i in 0,1,2,3,4,5,6:
print(city[i])
city.remove('HongKong') #列表的刪除
for i in 0,1,2,3,4,5,6:
print(city[i])
del city[0] #使用del方法刪除列表中的元素
for i in 0,1,2,3,4,5:
print(city[i])
#python元組 ===>
#特點(diǎn):不可修改,可被查看以及索引
num = ('1','2','3','4','5')
for i in 0,1,2,3,4:
print(num[i])
#python字典 ===>
#特點(diǎn):鍵值成對(duì)存在,鍵不可重復(fù),值可重復(fù),鍵不可改,值可以變,可以為任何對(duì)象
Dog = {'name':'sundy','age':18}
Dog.update({'tel':119}) #往字典中添加鍵值對(duì)
print(Dog)
del Dog['name'] #往字典中刪除鍵值對(duì)
print(Dog)
#集合
num_set = {1,2,3,4,1,5}
num_set.add(6) #往集合里添加元素
print(num_set)
num_set.discard(3) #從集合里刪除元素
print(num_set)
三、Python語(yǔ)言面對(duì)對(duì)象:類(lèi)的定義、使用以及類(lèi)的繼承
#coding:utf-8
#定義一個(gè)類(lèi)
class Anmial:
var = 100
Dog = ['runing','eat','sleep'] #Dog是這個(gè)類(lèi)的屬性
def function(self): #類(lèi)里的方法
if Anmial.var == 10:
print(Anmial.var)
else:
print(self+str(Anmial.Dog))
return Anmial.var
#實(shí)例化類(lèi)
Dog1 = Anmial()
print(Anmial.Dog)
#遍歷類(lèi)中的成員
for i in Anmial.Dog:
print(i)
#創(chuàng)建實(shí)例屬性===>類(lèi)似創(chuàng)建一個(gè)與Dog一樣的屬性
Anmial.log = '會(huì)飛','Hello','Monkey'
print(Anmial.log)
Anmial.function("屬性:")
class CocaCola():
formula = ['caffeine','suger','water','soda']
def __init__(self,local_name): #===>self相當(dāng)于可以用來(lái)訪問(wèn)類(lèi)中的成員或者創(chuàng)建屬性
self.logo_local = '橙汁'
if local_name == '可樂(lè)':
print(local_name)
elif local_name == '橙汁':
print(local_name)
else:
print('西瓜汁')
def drink(self): #===>調(diào)用該方法的時(shí)候等效于 coke = CocaCola.drink(coke)
print('Energy!')
coke = CocaCola('可樂(lè)')
coke1 = CocaCola('橙汁')
coke2 = CocaCola('梨汁')
#類(lèi)的繼承===>xuebi相當(dāng)于CocaCoal的子類(lèi),CocaCoal相當(dāng)于父類(lèi)
class xuebi(CocaCola):
formula = ['白色','黃色','綠色']
xuebi = xuebi(CocaCola) #將CocaCola放在括號(hào)中,表面xuebi集成于CocalCola
print(xuebi.formula)
xuebi.drink() #這樣子類(lèi)就可以調(diào)用父類(lèi)的方法,繼續(xù)延用了
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Python二叉搜索樹(shù)與雙向鏈表轉(zhuǎn)換實(shí)現(xiàn)方法
這篇文章主要介紹了Python二叉搜索樹(shù)與雙向鏈表轉(zhuǎn)換實(shí)現(xiàn)方法,涉及Python二叉搜索樹(shù)的定義、實(shí)現(xiàn)以及雙向鏈表的轉(zhuǎn)換技巧,需要的朋友可以參考下2016-04-04
Python3實(shí)現(xiàn)的簡(jiǎn)單三級(jí)菜單功能示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的簡(jiǎn)單三級(jí)菜單功能,涉及Python用戶交互以及針對(duì)json格式數(shù)據(jù)的遍歷、讀取、判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼
這篇文章主要介紹了python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼,幫助大家更好的利用python操作數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2020-09-09
python 自動(dòng)批量打開(kāi)網(wǎng)頁(yè)的示例
今天小編就為大家分享一篇python 自動(dòng)批量打開(kāi)網(wǎng)頁(yè)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
python Airtest自動(dòng)化測(cè)試工具的的使用
本文主要介紹了python Airtest自動(dòng)化測(cè)試工具的的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Python中使用多進(jìn)程來(lái)實(shí)現(xiàn)并行處理的方法小結(jié)
本篇文章主要介紹了Python中使用多進(jìn)程來(lái)實(shí)現(xiàn)并行處理的方法小結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

