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
  • 阅读 ( 246 )
  • 分类:编程

你可能感兴趣的文章

5个实用的谷歌备忘单,更好地使用谷歌应用程序

...用户。但谁有时间?Psst,而不是速成班,仔细阅读这些备忘单,以提高水平。 ...

  • 发布于 2021-03-11 21:58
  • 阅读 ( 219 )

适用于mac的尤利西斯键盘快捷键备忘单

... 这就是为什么我们提出了这个备忘单,你可以保存到你的桌面快速访问。它包含所有的键盘快捷键,你需要管理各种视图,面板,和尤利西斯模式,并在他们之间轻松切换。您还可以找到一些文本编辑...

  • 发布于 2021-03-18 17:19
  • 阅读 ( 198 )

如何创建自己的私有自托管ReadItLater应用程序

... ArchiveBox是用python3编写的。它还使用Wget、headlesschrome、youtubedl和其他Unix工具等依赖项来保存网页。你不需要一个持续运行的后端服务器。每次您想导入新链接并更新静态输出时,只需...

  • 发布于 2021-03-19 09:29
  • 阅读 ( 285 )

免费下载这个超级生产力备忘单包!

...,改善你的工作流程在Microsoft Word中,等等,这个免费的备忘单捆绑是为你! ...

  • 发布于 2021-03-20 00:50
  • 阅读 ( 238 )

linux命令参考备忘单

...到内存中。这就是为什么我们准备了这个方便的Linux命令备忘单。如果您想运行其中的几个,请查看如何在带屏幕的Linux终端上执行多任务。 ...

  • 发布于 2021-03-22 04:06
  • 阅读 ( 231 )

学习python编程的5个最佳网站

...,因为它不仅教您如何使用Python编程语言,而且还可以像程序员一样思考。如果这是您第一次接触到代码,那么这个站点将是您宝贵的资源。 ...

  • 发布于 2021-03-22 19:13
  • 阅读 ( 204 )

为什么gpio零比rpi.gpio文件用于树莓pi项目

...为初学者的首选系统。它的通用输入/输出(GPIO)管脚使初出茅庐的**商很容易尝试DIY电子项目。 ...

  • 发布于 2021-03-23 12:59
  • 阅读 ( 306 )

关于python编程最常见的问题

...量教程、它支持快速开发的事实以及编程就业市场对Python程序员的巨大需求。 ...

  • 发布于 2021-03-25 18:32
  • 阅读 ( 436 )

如何使用python if语句

... 相关内容:Python RegEx初出茅庐程序员备忘单 ...

  • 发布于 2021-03-27 00:49
  • 阅读 ( 274 )

如何使用“helloworld”脚本开始使用python

...提到了“你好,世界”。多年来,随着他的传奇之书成为初出茅庐的计算机科学家的圣经,“你好,世界”程序逐渐成为开始一个人的编码之旅的同义词。 ...

  • 发布于 2021-03-28 01:18
  • 阅读 ( 304 )
萍萍喔喔
萍萍喔喔

0 篇文章

相关推荐