pycharm的簡單配置
settings->editor->file and code temlates
#-*- codeing = utf-8 -*- 編碼#@Time : ${DATE} ${TIME} 時間#@Author : chengcheng 作責#@File : ${NAME}.py 文件名#@Software : ${PROJECT_NAME} File runtime environment
Comments automatically generated for new files after modifying the settings
#-*- codeing = utf-8 -*-#@Time : 2020/9/27 19:00#@Author : chengcheng#@File : day01test02.py#@Software : python_test01''''多行注釋'''
python基礎知識補充:
forLoops can iterate over many basic data types.
name = "123"lists = ["1", 2]for x in name: print(x, end="") for strings in lists: #It is also possible to traverse lists and other data storage structures that use indexes print(strings)
while循環可以和else子句一起使用
while True: print(1)else: print(2)
pythonData elements that support indexing can use negative slicing
#列表,元組,字符串
break 結束while,for循環,continue 結束本次循環,pass 占位符
r 與C#中的@功能一致
print(r"12\n23")
python字符串常用方法
lst = [1,2,3,4]lst.append(12) #末尾追加lst.append(["1", 5]) #Add the whole directlylst.extend([2,3]) #擴展添加for i in lst: print(i)
lst = [1,2,3,4,5,6,7,8,7]lst.pop() #默認彈出最後一個元素lst.pop(1) #彈出指定位置的元素lsts = []lsts = lst.remove(8) #移除指定元素del lst[1] #刪除指定位置的元素for i in lst: print(i, end="")for x in lsts: print(x, end="")
lst = [1,1,1,1]lst[0] = 2 #修改指定位置的元素值lst[1] = 3for i in lst: print(i)
select_name = 5lst = [1,2,3,4,5]if select_name in lst: #Implies a traversal of the list print("OK")else: print("NO")
lst = [1,2,3,4,5]print(lst.index(3, 1, 3)) #從1到3,查詢3的位置print(lst.index(3, 1, 2)) #如果查詢不到,會報錯,[1,3)左閉右開
lst = [1,2,3,4]lst.reverse() #Reverse list elementsprint(lst)lst.sort(reverse=True) #列表進行排序,reverse=True反序print(lst)
#元組#Tuples are immutabletup1 = () #創建空的元組tup2 = (50,) #對於元組而言,If there is only one element, a comma needs to be added to distinguish it from the bracket expressionprint(type(tup2)) #The type changes after the element is includedprint(type(tup1)) #The default empty tuple is the tuple typetup3 = ("a", 1 ,2, "b", "a") #可以有重復元素print(tup3)
#增tup1 = (1,2,3)tup2 = ("a", "b", "c")tup = tup1 + tup2 #Elements of tuples are connected in a connected manner,返回一個新的元組print(tup)
#字典#The key requirement must use a data type that cannot be changed,The key requirement is unique,值可以是重復的info = {"name":"qaq", "ages":12} #字典的定義print(info["name"]) #The values of the dictionary are accessed by keysprint(info["ages"])print(info["age"]) #直接訪問不存在的鍵,會拋出錯誤print(info.get("genter")) #使用get方法,If there is no key to query,就返回noneprint(info.get("genter"), "111") #When accessing an indeterminate key again,可以使用get方法
#增info = {"name":"qaq", "age":18}id = 233info["id"] = id #Use the new key directly,並且復制,完成字典的添加print(info)
#刪info = {"name":1, "age":2}del info["name"] #刪除一個鍵值對del info #刪除整個字典info.clear() #清空字典的所有內容,The dictionary still exists
#改info = {"name":"qaq", "age":18}id = 233info["age"] = id #Access directly by key,復賦值,修改print(info)
#查info = {"name":"qaq", "age":18}print(info.keys()) #Returns all key valuesprint(info.values()) #返回全部的值print(info.items()) #Returns all key values對
open
打開文件,有多種模式可供選擇w寫,r讀
read
讀取字符(可傳參)
radline
讀取一行數據
readlines
讀取全部數據
try: f = open("123.txt") print(1) #If an error was thrown before,下面的語句不再執行except (IOError, NameError) as message: print(message) #except捕獲異常,Possible exceptions can be enclosed in parentheses,ExceptionContains all exceptions pass
python 的 異常捕獲語法
try: f = open("123.txt") #可能出現異常的語句except Exception as msg: print(msg) #What statement should be executed after an exception occurselse: print("ok") #如果沒有出現異常,執行什麼語句finally: f.close() #Regardless of whether there is an error or not,都會執行的語句