Python語言快速上手
掃描二維碼
隨時隨地手機看文章
微信公眾號:嵌入式開發(fā)圈
關(guān)注可了解更多的教程。問題或建議,請公眾號留言;
如果你覺得本文對你有幫助,歡迎在評論區(qū)下方留言討論!如要尋找本平臺合作,請加微信:morixinguan
Python語言快速上手
最近在學習Python,后面搞機器人項目需要用到,所以要快速上手,我使用的是PyCharm這個IDE,看起來就舒服,學習起來就有勁啦,作為一名有工作經(jīng)驗的老司機,我學習編程語言的方法不會像大學生那樣從頭到尾學一遍,我會選擇,夠用,能用,實用即可,拒絕晦澀的語法,在不影響效率的情況下,我會采取容易看懂,后期項目可維護性等的方式來學習和編程,至于如何靈活運用Python語言,我認為是需要在項目中,才能不斷精進的,畢竟,作為一門編程語言,它僅僅只是工具而已。
下面開始吧,首先:
如果要在python中寫中文,則要在xx.py的最前面聲明
1#coding:utf-8
一、基礎(chǔ)語法:變量,字符串,函數(shù),邏輯判斷,循環(huán)
1varline = 2 ;
2print(varline);
3
4#打印字符串
5print("hello Python");
6print("你好,Python");
7
8#整型和字符串的轉(zhuǎn)化
9num1 = 100 ;
10num2 = "100";
11num3 = num1 + int(num2);
12print(num3);
13
14#字符串操作
15str1 = "hello world" ;
16str2 = str1 * 3 ;
17string_count = len(str1);
18print(string_count);
19print(str2);
20
21#字符串索引等價
22print(str1[0]); print(str1[-11]) #===>h
23print(str1[1]); print(str1[-10]) #===>e
24print(str1[2]); print(str1[-9]) #===>l
25#可以將字符串進行分割
26print(str1[0:5]);print(str1[6:11]); #===> hello world
27print(str1[-4:]);
28#函數(shù)的定義和使用
29def Print():
30 print("hello world");
31 return "sss" ;
32
33sss = Print();
34print(sss);
35
36def add(arg1 , arg2):
37 return arg1 + arg2 ;
38print(add(1,2));
39
40def getTempatuare(temp):
41 return temp *9/5 + 32 ;
42print(str(getTempatuare(35)) + "'F");
43
44#克轉(zhuǎn)千克算法
45def print_kg(g):
46 return float(g / 1000) ;
47print(str(print_kg(1)) + "kg");
48#求直角三角形斜邊的長度
49def Line_print(arg1,arg2):
50 return ((arg1*arg1 + arg2 * arg2))**0.5
51print("The right triangle third side's length is " + str(Line_print(3,4)));
52
53#str_rp = str1.replace(str1[:3],'*'*9);
54#print(str_rp)
55str11 = "{} a word she can get what she {} for."
56str12 = "{preposition} a word she can get what she {verb} for"
57str13 = "{0} a word she can get what she {1} for."
58str111 = str11.format('With','came');
59str121 = str12.format(preposition = 'With',verb = 'came')
60str131 = str13.format('With','came')
61print(str111)
62print(str121)
63print(str131)
64
65#單獨創(chuàng)建
66file1 = open('F:\\'+'hello.txt','w')
67file1.write("Hello world");
68file1.close()
69
70#使用函數(shù)創(chuàng)建
71def text_create(name, msg):
72 desktop_path = 'F:\\'
73 full_path = desktop_path + name + '.txt'
74 file = open(full_path,'w')
75 file.write(msg)
76 file.close()
77 print('Done')
78text_create('Yang','hello world') # ????
79
80#變量的比較
81teststr1 = "Hello"
82teststr2 = "World"
83teststr3 = "Hello"
84print(teststr1 in teststr2)
85print(teststr1 is teststr3)
86print(bool(teststr1))
87print(bool(''))
88print(not teststr1)
89print(teststr1 < teststr3 and teststr2 > teststr1)
90print(teststr1 > teststr2 or teststr3 < teststr1)
91
92#python邏輯判斷學習
93a = 1
94b = 3
95if a < b :
96 a = 3
97 b = 2
98else:
99 a = 2
100 b = 3
101print(a,b);
102
103if a < b:
104 a = 3
105 b = 2
106elif a > b:
107 a = 2
108 b = 3
109else:
110 a = 100
111 b = 200
112print(a,b)
113
114
115for i in 1,2,3,4,5,6:
116 print(i)
117for string_str in "hello","world","world":
118 print(string_str)
119for str1111 in "Hello":
120 print(str1111)
二、Python數(shù)據(jù)結(jié)構(gòu):列表,元組,字典,集合
1#python列表===>
2#特點:可以裝python的所有類型,包括元組,列表,字典等
3city = ['廣東','云南','廣西','江西','HongKong','Shenzhen',123456]
4for i in 0,1,2,3,4,5,6:
5 print(city[i])
6city.insert(1,'北京') #列表的插入
7for i in 0,1,2,3,4,5,6:
8 print(city[i])
9city.remove('HongKong') #列表的刪除
10for i in 0,1,2,3,4,5,6:
11 print(city[i])
12del city[0] #使用del方法刪除列表中的元素
13for i in 0,1,2,3,4,5:
14 print(city[i])
15
16#python元組 ===>
17#特點:不可修改,可被查看以及索引
18num = ('1','2','3','4','5')
19for i in 0,1,2,3,4:
20 print(num[i])
21
22#python字典 ===>
23#特點:鍵值成對存在,鍵不可重復,值可重復,鍵不可改,值可以變,可以為任何對象
24Dog = {'name':'sundy','age':18}
25Dog.update({'tel':119}) #往字典中添加鍵值對
26print(Dog)
27del Dog['name'] #往字典中刪除鍵值對
28print(Dog)
29
30#集合
31num_set = {1,2,3,4,1,5}
32num_set.add(6) #往集合里添加元素
33print(num_set)
34num_set.discard(3) #從集合里刪除元素
35print(num_set)
三、Python語言面對對象:類的定義、使用以及類的繼承
1#coding:utf-8
2#定義一個類
3class Anmial:
4 var = 100
5 Dog = ['runing','eat','sleep'] #Dog是這個類的屬性
6 def function(self): #類里的方法
7 if Anmial.var == 10:
8 print(Anmial.var)
9 else:
10 print(self+str(Anmial.Dog))
11 return Anmial.var
12
13#實例化類
14Dog1 = Anmial()
15print(Anmial.Dog)
16#遍歷類中的成員
17for i in Anmial.Dog:
18 print(i)
19#創(chuàng)建實例屬性===>類似創(chuàng)建一個與Dog一樣的屬性
20Anmial.log = '會飛','Hello','Monkey'
21print(Anmial.log)
22Anmial.function("屬性:")
23
24class CocaCola():
25 formula = ['caffeine','suger','water','soda']
26 def __init__(self,local_name): #===>self相當于可以用來訪問類中的成員或者創(chuàng)建屬性
27 self.logo_local = '橙汁'
28 if local_name == '可樂':
29 print(local_name)
30 elif local_name == '橙汁':
31 print(local_name)
32 else:
33 print('西瓜汁')
34 def drink(self): #===>調(diào)用該方法的時候等效于 coke = CocaCola.drink(coke)
35 print('Energy!')
36
37coke = CocaCola('可樂')
38coke1 = CocaCola('橙汁')
39coke2 = CocaCola('梨汁')
40
41#類的繼承===>xuebi相當于CocaCoal的子類,CocaCoal相當于父類
42class xuebi(CocaCola):
43 formula = ['白色','黃色','綠色']
44
45xuebi = xuebi(CocaCola) #將CocaCola放在括號中,表面xuebi集成于CocalCola
46print(xuebi.formula)
47xuebi.drink() #這樣子類就可以調(diào)用父類的方法,繼續(xù)延用了
免責聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!