小白python學習——學習基礎時遇到的函式、知識點總和(自己覺得比較重要的函式和知識點)
阿新 • • 發佈:2018-11-01
#字串——大小寫
def str():
s="abcde"
print(s.title()) # title : 每一個單詞首字母大寫,其他全小寫
print(s.upper()) # upper : 全部大寫
print(s.lower()) # lower : 全部小寫
#str()
#-------------------------------------------------------------
def _nt():
print("hello world\n\thello world") # \t 's function
print("Languages:\n\tPython\n\tJava\n\tC\C++") # \n\t 's function
#_nt()
#-------------------------------------------------------------
#字串——刪除空白
def _remove():
s=" hello world "
print(s)
print(s.lstrip()) # (開頭)首空格
print(s.rstrip()) # 末尾空格
print(s.strip()) # 首、尾空格
#_remove()
#--------------------------------------------------------------
#列表——插入元素
def list_append():
list=["I","LOVE","YOU"]
list.append("yh") #list.append(what) 列表末尾新增資料
print(list)
list.insert(0,"yh")
print(list) #list.insert(num,what) 列表具體位置,新增資料其他往後挪一位
list.insert(-1,"ZY")
print(list)
#list_append()
#--------------------------------------------------------------
#列表——刪除元素
def list_del():
s=["I","LOVE","YOU","ZY"]
del s[-1] #del s[i] 刪除已知位置的列表值
print(s)
s=["I","LOVE","YOU","ZY"]
print(s.pop()) #s.pop() 刪除並得到末尾的列表值
print(s)
s=["I","LOVE","YOU","ZY"]
s.remove("ZY") #s.remove("ZY") 刪除列表已知值
print(s)
#list_del()
#--------------------------------------------------------------
#列表——由小到大排序 / len()確定元素個數
def list_sort():
s=['a','d','c','e','b']
s.sort() # s.sort() 由小到大排序: 數字按數字 英文按字母表 abcde
print(s)
s.reverse()
print(s)
s.reverse() # s.reserve() 逆序排列,再逆序一次就可以返回原來的
print(s)
x=len(s)
print(x)
#list_sort()
#--------------------------------------------------------------
#列表——數字列表找最大最小值等
def MAX_AND_MIN():
num=[1,2,3,4,5,6,7,8,9,0]
print(max(num)) # max(self) : 找最大值
print(min(num)) # min(self) : 找最小值
print(sum(num)) # sum(self) : 求和
#MAX_AND_MIN()
#--------------------------------------------------------------
#列表——元組
def another_list():
s=(200,100)
print(s[0])
# print(s[-1]) #元組是無法改變的列表 這裡改變s【0】=50 is wrong!
# s[0]=50
# print(s[0])
#another_list()
#--------------------------------------------------------------
#If語句——列表
def _if():
s=["zy","yh","lx","cj"]
a="zy"
if a in s:
print("YES") # if 中直接not in 或者 in 來判斷是否在列表中
if a not in s:
print("NO")
#_if()
#-------------------------------------------------------------
#函式——import(模組)
import pizza as zy #from pizza import print.one 兩種呼叫方法 as是別名,把pizza叫做zy
#zy.print_one()
#--------------------------------------------------------------
#類——class XX():
class Restaurant():
def __init__(self,name,type):
self.name=name
self.type=type
def describe_restaurant(self):
print(self.name+"and"+self.type)
def open(self):
print("The restaurant is open")
retstaurant=Restaurant("AI BOAT","chinese food") #具體內容看書的簡介 P140
#retstaurant.describe_restaurant()
#retstaurant.open()
#--------------------------------------------------------------
def str():
s="abcde"
print(s.title()) # title : 每一個單詞首字母大寫,其他全小寫
print(s.upper()) # upper : 全部大寫
print(s.lower()) # lower : 全部小寫
#str()
#-------------------------------------------------------------
def _nt():
print("hello world\n\thello world") # \t 's function
print("Languages:\n\tPython\n\tJava\n\tC\C++") # \n\t 's function
#_nt()
#-------------------------------------------------------------
#字串——刪除空白
def _remove():
s=" hello world "
print(s)
print(s.lstrip()) # (開頭)首空格
print(s.rstrip()) # 末尾空格
print(s.strip()) # 首、尾空格
#_remove()
#--------------------------------------------------------------
#列表——插入元素
def list_append():
list=["I","LOVE","YOU"]
list.append("yh") #list.append(what) 列表末尾新增資料
print(list)
list.insert(0,"yh")
print(list) #list.insert(num,what) 列表具體位置,新增資料其他往後挪一位
list.insert(-1,"ZY")
print(list)
#list_append()
#--------------------------------------------------------------
#列表——刪除元素
def list_del():
s=["I","LOVE","YOU","ZY"]
del s[-1] #del s[i] 刪除已知位置的列表值
print(s)
s=["I","LOVE","YOU","ZY"]
print(s.pop()) #s.pop() 刪除並得到末尾的列表值
print(s)
s=["I","LOVE","YOU","ZY"]
s.remove("ZY") #s.remove("ZY") 刪除列表已知值
print(s)
#list_del()
#--------------------------------------------------------------
#列表——由小到大排序 / len()確定元素個數
def list_sort():
s=['a','d','c','e','b']
s.sort() # s.sort() 由小到大排序: 數字按數字 英文按字母表 abcde
print(s)
s.reverse()
print(s)
s.reverse() # s.reserve() 逆序排列,再逆序一次就可以返回原來的
print(s)
x=len(s)
print(x)
#list_sort()
#--------------------------------------------------------------
#列表——數字列表找最大最小值等
def MAX_AND_MIN():
num=[1,2,3,4,5,6,7,8,9,0]
print(max(num)) # max(self) : 找最大值
print(min(num)) # min(self) : 找最小值
print(sum(num)) # sum(self) : 求和
#MAX_AND_MIN()
#--------------------------------------------------------------
#列表——元組
def another_list():
s=(200,100)
print(s[0])
# print(s[-1]) #元組是無法改變的列表 這裡改變s【0】=50 is wrong!
# s[0]=50
# print(s[0])
#another_list()
#--------------------------------------------------------------
#If語句——列表
def _if():
s=["zy","yh","lx","cj"]
a="zy"
if a in s:
print("YES") # if 中直接not in 或者 in 來判斷是否在列表中
if a not in s:
print("NO")
#_if()
#-------------------------------------------------------------
#函式——import(模組)
import pizza as zy #from pizza import print.one 兩種呼叫方法 as是別名,把pizza叫做zy
#zy.print_one()
#--------------------------------------------------------------
#類——class XX():
class Restaurant():
def __init__(self,name,type):
self.name=name
self.type=type
def describe_restaurant(self):
print(self.name+"and"+self.type)
def open(self):
print("The restaurant is open")
retstaurant=Restaurant("AI BOAT","chinese food") #具體內容看書的簡介 P140
#retstaurant.describe_restaurant()
#retstaurant.open()
#--------------------------------------------------------------