python基礎語法合集 Python基礎語法介紹

2021-10-10 03:51:44 字數 2982 閱讀 4499

元組

基本概念、特性順序儲存相同/不同型別的元素

定義:使用()將元素括起來,元素之間用「,」括開

特性:不可變,不支援新增,修改,刪除等操作

查詢:通過下標查詢元組指定位置的元素

其他空元組定義:non_tuple = ()

只包含乙個元素的元組:one_tuple = ("one",)

順序儲存相同/不同型別的元素user_info = ("wukong", 100, "male", "13834928470")

元組不同於列表,它不支援增,刪,改。#不支援增刪改操作,例如刪除乙個元組元素del user_info[1]輸出結果: del user_info[1]typeerror: 'tuple' object doesn't support item deletion

通過下標查詢元素#查詢下標1的元素age = user_info[1]print("age is %d"%age)

遍歷元組for item in user_info: print (item, end = "")輸出結果:wukong100male13834928470

字典基本概念、特性儲存key-value鍵值對型別的資料

定義:;字典裡不能出現相同的鍵名

特性:具有增刪改操作

查詢:根據key查詢value

內建方法:get,keys,values,items,clear

迴圈遍歷字典

內建方法keys的用法user_info_dict = for key in user_info_dict.keys(): #key值組成的列表 print(user_info_dict[key])輸出結果:zhangsan3013523464219

內建方法items的用法#用法(1)for item in user_info_dict.items(): print(type(item)) print(item[0]) #key print(item[1]) #value輸出結果:namezhangsanage30tel13523464219#用法(二)for key, value in user_info_dict.items(): print(key) print(value)輸出結果:zhangsanage30tel13523464219

集合基本概念、特性無序儲存不同資料型別不重複元素的序列

定義:特性:無序儲存,可以對元素列表去重

方法:add,update(序列),remove,pop

集合操作:交集:intersection

並集:union

差集:difference

對稱差集:symmetric_difference

集合對列表去重id_list = ["id1", "id2", "id3", "id1", "id2"]distinct_set = set(id_list) #去重print(distinct_set)輸出結果:

集合對字元去重string_set = set("hello")print(string_set) #字串看成是帶下標的列表,字串會拆開然後列表去重輸出結果: note:set是無序的。所以你再執行一次,列表的元素順序是變化的。

空集合none_dict = {} #建立乙個空字典none_set = set() #建立乙個空集合print(none_set)輸出結果:set()

in 和not inuser_id = "id1"if user_id in distinct_set: print(user_id)else: print("not find")輸出結果:id1

add:新增元素到集合name_set = name_set.add("wangwu")print(name_set)輸出結果:

update(序列)name_set.update(["wukong", "lisi", "bajie"]) #列表中的每個元素去重後新增到set裡print(name_set)輸出結果:

函式函式定義def funname (parameter_list) function block return value

例子一:有引數有返回def sum(x, y): sum = x + y return sum#函式呼叫sum = sum(1, 2)print(sum)輸出結果:3

例子二:有多個返回def x_y_comp_tuple(x, y): res1 = x + y res2 = x * y return res1, res2a, b = x_y_comp_tuple(2, 3)print("a = {}, b = {}".format(a, b))輸出結果:a = 5, b = 6

例子三:列表作為返回值稍後填充

字串 :常用內建方法

find(str[, start, end])line = "hello world hello python"print(line.find("world"))print(line.find("hello"))print(line.find("hello", 6)) #查詢範圍從索引」6「開始輸出結果:6012

count(str[, start, end])print(line.count("hello")) #查詢文字的某個字段或者某個字串中某個單詞輸出結果:2

replace(old, new[, count])new_line = line.replace("hello", "hi", 1) #count不指定就全部替換print(line)print(new_line)輸出結果:hello world hello pythonhi world hello python

split(sep[, maxsplit])line.split(" ") #以空格作為分隔符,以列表方式返回輸出結果:['hello', 'world', 'hello', 'python']#指定分隔的個數line.split(" ", 1)輸出結果:['hello', 'world hello python']

startswith(prefix[, start, end])

endswith(suffix[, start, end])

upper:字串所有字元大寫

lower:字串所有字元小寫

Python合集之Python語法特點(三)

在上一節的合集中我們了解了python語法特點中的 縮排規則,本節將繼續講解語法特點中的編碼規範。編碼規範,在職業生涯及不同的公司中,都是重點強調的乙個問題,為什麼如此重要呢?因為遵循一定的 編寫規則和命名規範,可以使 更加的規範化,對 的理解與維護都會起到至關重要的作用。python中目前主要採用...

Python基礎 Python語法基礎

關鍵字是python語言的關鍵組成部分,不可隨便作為其他物件的識別符號 andas assert break class continue defdel elif else except exec finally forfrom global ifimport inis lambda notor p...

UltraEdit設定語法高亮支援python

ultraedit預設不支援python的語法高亮,通過以下設定可實現語法高亮 1.在 下有各種語言的語法高亮支援,找到對應的python版本,現在已有的為python2.5和2.6,開啟鏈結,將頁面內容儲存為.uew格式,我使用的是python2.6,儲存為python2.6.uew 3.需要注意...