最近在學習Python,后面搞機器人項目需要用到,所以要快速上手,我使用的是PyCharm這個IDE,看起來就舒服,學習起來就有勁啦,作為一名有工作經驗的老司機,我學習編程語言的方法不會像大學生那樣從頭到尾學一遍,我會選擇,夠用,能用,實用即可,拒絕晦澀的語法,在不影響效率的情況下,我會采取容易看懂,后期項目可維護性等的方式來學習和編程,至于如何靈活運用Python語言,我認為是需要在項目中,才能不斷精進的,畢竟,作為一門編程語言,它僅僅只是工具而已。
如果要在python中寫中文,則要在xx.py的最前面聲明
#coding:utf-8
一、基礎語法:變量,字符串,函數,邏輯判斷,循環
varline = 2 ;print(varline);#打印字符串print("hello Python");print("你好,Python");#整型和字符串的轉化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);#字符串索引等價print(str1[0]); print(str1[-11]) #===>hprint(str1[1]); print(str1[-10]) #===>eprint(str1[2]); print(str1[-9]) #===>l#可以將字符串進行分割print(str1[0:5]);print(str1[6:11]); #===> hello worldprint(str1[-4:]);#函數的定義和使用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");#克轉千克算法def print_kg(g): return float(g / 1000) ;print(str(print_kg(1)) + "kg");#求直角三角形斜邊的長度def Line_print(arg1,arg2): return ((arg1*arg1 + arg2 * arg2))**0.5print("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)#單獨創建file1 = open('F://'+'hello.txt','w')file1.write("Hello world");file1.close()#使用函數創建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邏輯判斷學習a = 1b = 3if a < b : a = 3 b = 2else: a = 2 b = 3print(a,b);if a < b: a = 3 b = 2elif a > b: a = 2 b = 3else: a = 100 b = 200print(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)
新聞熱點
疑難解答