python regex初出茅廬的程式設計師備忘單

使用這個Python正則表示式列表可以更好地使用這種通用程式語言。...

使用Python解決各種技術問題及其簡單的學習曲線使它成為最流行的現代程式語言之一。儘管學習起來很快,但它的正則表示式可能很棘手,特別是對於新手。

The Python Regex Cheat Sheet for Budding Programmers-Featured

儘管Python有很多庫,但是您應該瞭解它的常規語法。即使您是這方面的專家,也可能仍然需要偶爾查詢一些Python命令來重新整理記憶體。

因此,我們準備了這個Python正則表示式備忘單,以幫助您更好地掌握語法。

免費下載:此備忘單可從我們的發行合作伙伴TradePub下載PDF格式。您必須填寫一個簡短的表格才能第一次使用它。下載Python RegEx備忘單,面向初出茅廬的程式設計師。

python regex初出茅廬的程式設計師備忘單

Expression Action Examples
列印() 顯示命令的結果 x=“Hello world”列印(x)輸出:Hello world
輸入() 收集使用者的輸入 列印(輸入(“你叫什麼名字?”))輸出:你叫什麼名字?
型別() 查詢變數的型別 x="Regular expressi***" type(x) output:
長度() 查詢變數中的項數 len([1,2,3])輸出:3
\ 轉義改變一行程式碼意圖的字元 列印(“I want you to add\”\“”)輸出:I want you to add“”
\不 打斷字串以從下一行開始 列印(“這是一行\n這是第二行”)輸出:這是一條線這是第二條線
def function\u name(引數):命令 使用可選引數初始化函式 def yourName(x):列印(x+1)
λ 呼叫匿名函式 加法3到=λy:y+3列印(加法3到(4))輸出:7
返回 從函式返回結果 def yourName(x):返回x+1
建立Python物件 班級myClass:定義myFunc(x) 地址:
定義初始化__ 初始化類的屬性 類myClass:def uu init(self,attributes…)
“\uuu初始化\uuuuuuuuuuuy.py” 儲存包含模組的檔案,以便在另一個Python檔案中成功讀取該模組 將包含模組的檔案重新命名為:“\uu init\uuu.py”
int() 將變數轉換為整數 int(1.234)輸出:1
str() 將變數轉換為字串 str(1.234)輸出:“1.234”
浮動() 將變數轉換為浮點 浮子(23)輸出:23.0
dict(Counter()) 使用Python內建計數器排序後,將列表或元組轉換為字典 從集合匯入Counterdict(Counter([1,1,2,1,2,3,3,4]))輸出:{1:3,2:2,3:2,4:1}
圓形() 將運算的輸出四捨五入到最接近的整數 圓形(23.445)輸出:23
四捨五入(運算或數字,小數位) 將操作的輸出四捨五入到特定的小數位數 圓形(23.4568,2)輸出:23.46
如果: 啟動條件語句 如果2<3:列印(“兩個較小”)
elif公司: 如果陳述是錯誤的,就進行反陳述 如果2<3:print(“兩個較小”)elif 2==3:print(“繼續”)
其他: 如果其他條件不成立,則進行最後的反陳述 如果2<3:print(“兩個較小”)elif 2==3:print(“繼續”)其他:列印(“三更大”)
持續 忽略一個條件並執行迴圈的其餘部分 a=[1,4,-10,6,8]對於a中的b:如果b&lt;=0:繼續列印(b)輸出:1468
打破 在給定條件下終止迴圈流 a=[1,4,-10,6,8]表示a中的b:如果b&gt;=6:中斷列印(b)輸出:14-10
透過 忽略一組先前的指令 對於a中的b:透過
嘗試,除了 嘗試一段程式碼,否則,引發已定義的異常 try:print(a)except:print(“發生錯誤!”)輸出:發生錯誤!
最後 當try和except塊失敗時執行最後一個程式碼 try:print(a)except:print(d)finally:print(“您不能列印未定義的變數”)輸出:您不能列印未定義的變數
引發異常() 引發異常,在無法執行命令時停止該命令 a=7+2如果a&lt;10:引發異常(“噢!你沒有得到10分
匯入x 匯入整個模組或庫 匯入數學
從x匯入y 從檔案或類y匯入庫x 從scipy.統計匯入模式
作為 根據您的首選名稱自定義表示式 作為pd匯入
在裡面 檢查變數中是否存在值 x=[1,4,6,7]如果x中有5:print(“有一個5”),否則:print(“沒有5”)輸出:沒有5
檢查兩個變數是否引用一個元素 x=[1,4,6,7]x=b列印(x是b)真
目標 宣告空值 x=目標
< 檢查一個值是否小於另一個值 5<10輸出:真
> 檢查一個值是否大於另一個值 5> 10輸出:假
<= 檢查一個值是否小於或等於另一個值 2*2<=3輸出:假
>= 檢查一個值是否大於或等於另一個值 2*2>=3輸出:真
"== 檢查一個值是否正好等於另一個值 3==4輸出:假
!= 確定一個值不等於另一個值 三!=4輸出:真
進口re 匯入Python的內建正則表示式 匯入雷爾·芬德爾(“字串”,變數)
a | b級 檢查字串中是否存在兩個元素中的任何一個 import resomeText=“Hello正則表示式”a=關於芬德爾(“regular | Hello”,someText)列印(a)輸出:[“Hello”,“regular”]
字串$ 檢查變數是否以一組字串結尾 import resomeText=“Hello正則表示式”a=關於芬德爾(“expression$”,someText)輸出:['expression']
^字串 檢查變數是否以一組字串開頭 import resomeText=“Hello正則表示式”a=關於芬德爾(“^Hello”,someText)列印(a)輸出:[“Hello”]
字串索引() 檢查字串的索引位置 a=“Hello World”a.index('H')輸出:0
字串。大寫() 將一組字串中的第一個字元大寫 a= "Hello World" a.capitalize() output: 'Hello world'
字串.swapcase() 每個單詞的第一個字母用小寫字母列印,其他字母用大寫字母列印 a=“Hello World”a.swapcase()輸出:'Hello World'
string.lower() Convert all the strings to a lowercase a= "Hello World" a.lower() output: 'hello world'
string.upper() Convert all strings to uppercase a= "Hello World" a.upper() output: 'HELLO WORLD'
string.startswith() Check if a string starts with a particular character a= "Hello World" a.startswith('a') output: False
string.endswith() Check if a string ends with a particular character a= "Hello World" a.endswith('d') output: True
string.split() Separate each word into a list a= "Hello World" a.split() output: ['Hello', 'world']
strings {}'.format() Display an output as string a=3+4 print("The answer is {}".format(a)) output: The answer is 7
is not None Check if the value of a variable is not empty def checknull(a): if a is not None: return "its full!" else: return "its empty!"
x%y Find the remainder (modulus) of a division 9%4 output: 1
x//y Find the quotient of a division 9//4 output: 2
"= Assign a value to a variable a={1:5, 3:4}
"+ Add elements together ["a two"] + ["a one"] output: ['a two', 'a one'] 1+3 output=4
"- Find the difference between a set of numbers 3-4 output=-1
"* Find the product of a set of numbers 3*4 output:12
a+=x Add x to variable a without assigning its value to a new variable a=2 a+=3 output: 5
a-=x Subsract x from variable a without assigning it to a new variable a=3 a-=2 output: 1
a*=x Find the product of variable a and x without assigning the resullt to a new variable a=[1, 3, 4] a*=2 output: [1, 3, 4, 1, 3, 4]
x**y Raise base x to power y 2**3 output: 8
pow(x, y) Raise x to the power of y pow(2, 3) output: 8
abs(x) Convert a negative integer to its absolute value abs(-5) output: 5
x**(1/nth) Find the nth root of a number 8**(1/3) output: 2
a=b=c=d=x Assign the same value to multiple variables a=b=c=d="Hello world"
x, y = y, x Swap variables x = [1, 2] y = 3 x, y = y, x print(x, y) output: 3 [1, 2]
for Loop through the elements in a variable a=[1, 3, 5] for b in a: print(b, "x", "2", "=", b*2) output: 1 x 2 = 2 3 x 2 = 6 5 x 2 = 10
while Keep looping through a variable, as far as a particular condition remains True a=4 b=2 while b<=a: print(b, "is lesser than", a) b+=1 output: 2 is lesser than 4 3 is lesser than 4 4 is lesser than 4
range() Create a range of positive integers between x and y x=range(4) print(x) range(0, 4) for b in x: print(b) output: 0 1 2 3
sum() Iterate through the elements in a list print(sum([1, 2, 3])) output:6
sum(list, start) Return the sum of a list with an added element print(sum([1, 2, 3], 3)) output: 9
[] Make a list of elements x=['a', 3, 5, 'h', [1, 3, 3], {'d':3}]
() Create a tupple---tupples are immutable x=(1, 2, 'g', 5)
{} Create a dictionary a={'x':6, 'y':8}
x[a:b] Slice through a list x=[1, 3, 5, 6] x[0:2] output: [1, 3]
x[key] Get the value of a key in dictionary x a={'x':6, 'y':8} print(a['x']) output: 6
x.append() Add a list of values to an empty list x=[1] x.append([1,2,3]) print(x) output: [1, [1,2,3]]
x.extend() Add a list of values to continue an existing list without necessarily creating a nested list x=[1,2] x.extend([3,4,6,2]) print(x) output: [1, 2, 3, 4, 6, 2]
del(x[a:b]) Delete an item completely from a list at a specific index x=[1,2,3,5] del(x[0:2]) print(x) output: [2,3,5]
del(x[key]) Delete a key and a value completely from a dictionary at a specific index y={1:3, 2:5, 4:6, 8:2} del(y[1], y[8]) print(y) output= {2:5, 4:6}
dict.pop() Pop out the value of a key and remove it from a dictionary at a specific index a={1:3, 2:4, 5:6} a.pop(1) output: 3
dict.popitem() Pop out the last item from a dictionary and delete it a={1:2, 4:8, 3:5} a.popitem() output: (3, 5) print(a) output: {1:2, 4:8}
list.pop() Pop out a given index from a list and remove it from a list a=[1, 3, 2, 4, 1, 6, 6, 4] a.pop(-2) output: 6 print(a) output: [1, 3, 2, 4, 1, 6, 4]
clear() Empty the elements of a list or a dictionary x=[1, 3, 5] x.clear() print(x) output: []
remove() Remove an item from a list x=[1, 5, 6, 7] x.remove(1) output: [5, 6, 7]
insert() Insert elements into a llist x=[3, 5, 6] x.insert(1, 4) print(x) output: [1, 4, 3, 5, 6]
sort(reverse=condition) Reverse the direction of the elements in a list x=[1, 3, 5, 6] x.sort(reverse=True) print(x) output: [6, 5, 3, 1]
update() Update a dictionary by changing its first element and adding any other item to its end x={1:3, 5:6} x.update({1:4, 8:7, 4:4}) print(x) output: {1: 4, 5: 6, 8: 7, 4: 4}
keys() Show all the keys in a dictionary a={1:2, 4:8} a.keys() output: dict_keys([1, 4])
values() Show all the values in a dictionary a={1:2, 4:8} a.values() output: dict_values([2, 8])
items() Display the keys and the values in a dictionary a={1:2, 4:8} a.items() output: dict_items([(1, 2), (4, 8)])
get(key) Get the value of an item in a dictionary by its key a={1:2, 4:8, 3:5} a.get(1) output: 2
setdefault(key) Return the original value of an element to a dictionary a.setdefault(2)
f={**a, **b} Merge two dictionaries a={'x':6, 'y':8} b={'c':5, 'd':3} f={**a, **y} print(f) output:{'x': 6, 'y': 8, 'c': 5, 'd': 3}
remove() Remove the first matching value of an element from a list without minding its index a=[1, 3, 2, 4, 4, 1, 6, 6, 4] a.remove(4) print(a) output: [1, 3, 2, 4, 1, 6, 6, 4]
memoryview(x) Access the internal buffers of an object a=memoryview(object)
bytes() Convert a memory buffer protocol into bytes bytes(a[0:2])
bytearray() Return an array of bytes bytearray(object)
# Write a single line of comment or prevent a line of code from being executed # Python regex cheat sheet
""" """ Write a multi-line comment """The Python regex cheat sheet is good for beginners It's equally a great refresher for experts"""
Command Line
pip install package Install an online library pip install pandas
virtualenv name Use virtaulenv to create a virtual environment virtualenv myproject
mkvirtualenv name Use virtual environment wrapper to create virtual environment mkvirtualenv myproject
python file.py Run the commands in a Python file "python my_file.py
pip freeze List out all the installed packages in a virtual environment pip freeze
pip freeze > somefiles Copy all installed libraries in a single file pip freeze > requirements.txt
where Find the installation path of Python where python
--version Check the version of a package python --version
.exe Run a Python shell python.exe
with open(file, 'w') Write to an existing file and overwrite its existing content with open('regex.txt', 'w') as wf: wf.write("Hello World!")
with open(file, 'r') Open a file as read-only with open('regex.txt', 'r') as rf: print(rf.read()
with open(file, 'a') Write to a file without overwriting its existing content with open('regex.txt', 'a') as af: af.write("\nHello Yes!")
file.close Close a file if it's not in use af=open('regex.txt') af.close
exit Exit the Python shell exit()

使用python時要有創意

學習Python的正則表示式是成為一個更好的Python程式設計師的一大步,但這只是你需要做的少數事情之一。

然而,玩弄它的語法並對它們進行創造性的使用可以提高你的編碼技能。因此,除了學習語法之外,在實際專案中使用它們,您將成為一名更好的Python程式設計師。

  • 發表於 2021-03-29 18:37
  • 閱讀 ( 48 )
  • 分類:程式設計

你可能感興趣的文章

5節課,成為一名真正優秀的自學程式設計師

...所以他知道你來自哪裡。從基礎知識開始,然後深入瞭解Python。然後,揭開硬體概念,最後用大資料和人工智慧來完善它。 ...

  • 發佈於 2021-03-12 09:53
  • 閲讀 ( 51 )

初學者程式碼訓練營:你應該參加嗎?

... 編碼訓練營通常提供JavaScript、Ruby、Python、Rails、ExpressJS、Django和MySQL課程。你希望用你的程式設計技巧來決定你將選擇哪一個音軌。例如,您可能只專注於Rails。或者,您可以選擇一個全面的web開發訓練營來...

  • 發佈於 2021-03-13 09:38
  • 閲讀 ( 55 )

微軟邀請您製作自己的xbox one遊戲

...推出了Xbox Live創造者計劃。現在,它向所有人開放。對於初出茅廬的開發者來說,這意味著一個在Xbox商店(和Windows10)上釋出遊戲的機會。對我們其他人來說,這意味著一系列新遊戲,其中許多是免費的。 ...

  • 發佈於 2021-03-13 11:29
  • 閲讀 ( 42 )

如何在adobe photoshop中新增和編輯文字

...的。新增、修改和編輯您的文字再簡單不過了,它可以為初出茅廬的平面設計師開啟一個偉大設計的世界。 ...

  • 發佈於 2021-03-13 23:16
  • 閲讀 ( 58 )

這個免費的應用程式每天為你設定一個新的漂亮的牆紙

...配,因此某些牆紙可能無法與所有牆紙匹配。但對於一個初出茅廬的攝影師來說,桌布的視覺效果總是一種靈感。 ...

  • 發佈於 2021-03-14 08:57
  • 閲讀 ( 35 )

兒童linux軟體:8個有趣的教育性linux軟體

... 還有一個龐大的創意和專案社群,供初出茅廬的程式設計師尋找靈感。 ...

  • 發佈於 2021-03-21 18:28
  • 閲讀 ( 84 )

學習python程式設計的5個最佳網站

在過去的十年中,Python程式語言在所有型別的編碼中都得到了迅速的普及。從網路開發者到影片遊戲設計師,從資料科學家到內部工具創造者,許多人都愛上了Python。為什麼?因為Python易於學習,易於使用,而且非常強大。 ...

  • 發佈於 2021-03-22 19:13
  • 閲讀 ( 54 )

windows上音樂製作的6個最佳garageband備選方案

...功能。透過快速構建的環路錄製聲帶的能力使其非常適合初出茅廬的MC和歌手。 ...

  • 發佈於 2021-03-23 08:18
  • 閲讀 ( 53 )

為什麼gpio零比rpi.gpio檔案用於樹莓pi專案

...皮是學習的完美電腦。基於Linux的Raspbian作業系統內建了Python,這使它成為初學者的首選系統。它的通用輸入/輸出(GPIO)管腳使初出茅廬的**商很容易嘗試DIY電子專案。 ...

  • 發佈於 2021-03-23 12:59
  • 閲讀 ( 68 )

如何使用python if語句

if語句是邏輯程式設計的驅動力。因此,更好地掌握Python的if是對Python程式設計技能的重要補充。 ...

  • 發佈於 2021-03-27 00:49
  • 閲讀 ( 51 )