1.字串
字串就是一系列字符,在Python中,用引號括起的都是字串,其中的引號可以是單引號>>> name="ada lovelace" >>> print(name.title()) #首字母改成大寫 Ada Lovelace >>> print(name.upper()) #全改成大寫 ADA LOVELACE >>> print(name.lower()) #全改成小寫 ada lovelace >>>
用函式str()避免型別錯誤避免型別
age = 23 #此時沒有指定型別,它可能是整數23,也可能是字符2和3. message = "Happy " + str(age) + "rd Birthday!"#要指定str型別,否則會報錯 print(message) Happy 23rd Birthday
合并字串
>>> first_name="ada" >>> last_name="lovelace" >>> full_name=first_name+""+last_name >>> print(full_name) adalovelace >>> print("hello,"+full_name.title ()+"!") hello,Adalovelace!
使用制表符或換行符來添加空白
>>> print("Python") Python >>> print("\tPython") #添加制表符,可使用字符組合\t Python >>> print("Languages:\nPython\nC\nJavaScript") #字串中添加換行符,可使用字符組合\n Languages: Python C JavaScript
洗掉空白
>>> favorite_language='python ' >>> favorite_language 'python ' >>> favorite_language.rstrip() #要確保字串末尾沒有空白,可使用方法rstrip() 'python' >>> favorite_language 'python '
要永久洗掉這個字串中的空白,必須將洗掉操作的結果存回到變數中:
>>> favorite_language=favorite_language.rstrip() >>> favorite_language 'python' >>> favorite_language=' python ' >>> favorite_language.rstrip() #剔除尾部空白 ' python' >>> favorite_language.lstrip() #剔除開頭空白 'python ' >>> favorite_language.strip() #剔除兩端空白 'python' >>>View Code
在Python 2中,無需將要列印的內容放在括號內 在Python中,可對整數執行加(+)減(-)乘(*)除(/)運算, 在終端會話中,Python直接回傳運算結果,Python使用兩個乘號表示乘方運算 2.串列 串列由一系列按特定順序排列的元素組成
1 >>> bicycles=['trek', 'cannondale', 'redline' ,'specialized'] 2 >>> print(bicycles) 3 ['trek', 'cannondale', 'redline', 'specialized'] 4 >>> print(bicycles[0]) #提取第一輛自行車 5 trek 6 >>> print(bicycles[0].title()) #首字母大寫 7 Trek 8 >>> print(bicycles[-1]) #通過將索引指定為-1,可讓Python回傳最后一個串列元素 9 specializedView Code 在Python中,第一個串列元素的索引為0,而不是1, 要修改串列元素,可指定串列名和要修改的元素的索引,再指定該元素的新值
1 >>> motorcycles=['honda', 'yamaha', 'suzuki'] 2 >>> print(motorcycles) 3 ['honda', 'yamaha', 'suzuki'] 4 >>> motorcycles[0]='ducati' 5 >>> print(motorcycles) 6 ['ducati', 'yamaha', 'suzuki'] 7 8 >>> motorcycles.append('honda') #方法append()將元素'honda'添加到了串列末尾 9 >>> print(motorcycles) 10 ['ducati', 'yamaha', 'suzuki', 'honda'] 11 12 >>> motocycles=['honda','yamaha','suzuki'] 13 >>> motocycles.insert(0,'ducati') #使用方法insert()可在串列的任何位置添加新元素, 14 >>> print(motocycles) 15 ['ducati', 'honda', 'yamaha', 'suzuki'] 16 >>> del motocycles[0] #使用del陳述句洗掉元素 17 >>> print(motocycles) 18 ['honda', 'yamaha', 'suzuki'] 19 20 21 >>> cars=['bmw','audi','toyota','subaru'] 22 >>> cars.sort() #使用sort方法將串列按字母排序 23 >>> print(cars) 24 ['audi', 'bmw', 'subaru', 'toyota'] 25 26 >>> cars=['bmw','audi','toyota','subaru'] 27 >>> cars.sort(reverse=True) #按字母反方向排序 28 >>> print(cars) 29 ['toyota', 'subaru', 'bmw', 'audi']View Code
使用函式sorted()對串列進行臨時排序對串列進行臨時排序
1 >>> cars=['bmw','audi','toyota','subaru'] 2 >>> print("Here is the original list:") 3 Here is the original list: 4 >>> print(cars)#正常排序 5 ['bmw', 'audi', 'toyota', 'subaru'] 6 >>> 7 >>> print("\nHere is the sorted list:") 8 9 Here is the sorted list: 10 >>> print(sorted(cars))#臨時排序 11 ['audi', 'bmw', 'subaru', 'toyota'] 12 >>> 13 >>> print("\nHere is the original list again:") 14 15 Here is the original list again: 16 >>> print(cars)#正常排序 17 ['bmw', 'audi', 'toyota', 'subaru'] 18 19 >>> cars.reverse() #反向排序 20 >>> print(cars) 21 ['subaru', 'toyota', 'audi', 'bmw'] 22 23 24 >>> len(cars) #確定串列的長度View Code
訪問最后一個元素
1 >>> motorcycles=['honda','yamaha','suzuki'] 2 >>> print(motorcycles[-1]) 3 suzukiView Code
取最值
1 >>> digits=[1,2,3,4,5,6,7,8,9,0] 2 >>> min(digits) #取最小值 3 0 4 >>> max(digits)#取最大值 5 9 6 >>> sum(digits)#取最小值 7 45View Code
元素增刪
1 num=[10,11,12,13,14] 2 print(num) 3 num.append(15)#append方法給陣列后面加元素 4 print(num) 5 num.insert(0,9)#insert方法給陣列指定位置加元素 6 print(num) 7 num.remove(15)#洗掉陣列中元素15 8 print(num) 9 del num[0]#洗掉第一個元素 10 print(num) 11 num.pop(0)#洗掉第一個元素 12 print(num)View Code
輸出結果:
1 [10, 11, 12, 13, 14] 2 [10, 11, 12, 13, 14, 15] 3 [9, 10, 11, 12, 13, 14, 15] 4 [9, 10, 11, 12, 13, 14] 5 [10, 11, 12, 13, 14] 6 [11, 12, 13, 14]View Code
切片:
1 >>> players=['charles','martina','michael','florence','eli'] 2 >>> print(players) 3 ['charles', 'martina', 'michael', 'florence', 'eli'] 4 >>> print(players[0:3]) 5 ['charles', 'martina', 'michael'] 6 >>> print(players[1:4]) 7 ['martina', 'michael', 'florence'] 8 >>> print(players[:4]) 9 ['charles', 'martina', 'michael', 'florence'] 10 >>> print(players[2:]) 11 ['michael', 'florence', 'eli'] 12 >>> print(players[-3:]) #取后三位 13 ['michael', 'florence', 'eli']View Code
元組:
1 >>> dimensions=(200,50) #用括號表示 2 >>> print(dimensions[0]) 3 200 4 >>> print(dimensions[1]) 5 50 6 >>> dimensions[0]=250 #當修改元組的值時報錯,修改元組的操作是被禁止的,因此Python指出不能給元組的元素賦值 7 Traceback (most recent call last): 8 File "<pyshell#3>", line 1, in <module> 9 dimensions[0]=250 10 TypeError: 'tuple' object does not support item assignment 11 >>>View Code
比較兩個串列的用戶名:
1 current_users=['A','B','C','D','E'] 2 new_users=['D','E','F','G','H'] 3 for users_1 in new_users:#遍歷新的用戶名組 4 if users_1.upper() in [current_user.upper() for current_user in current_users]:#【遍歷原用戶名組】得到的數大寫后與users_1大寫比較 5 print(users_1+"用戶名已使用") 6 else: 7 print(users_1+"可以使用該用戶名")View Code
運行結果:
1 D用戶名已使用 2 E用戶名已使用 3 F可以使用該用戶名 4 G可以使用該用戶名 5 H可以使用該用戶名View Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/147262.html
標籤:Python
