Python 快速入门笔记(4):表达式

本系列随笔是本人的学习笔记,初学阶段难免会有理解不当之处,错误之处恳请指正。转载请注明出处:https://www.cnblogs.com/itwhite/p/12297709.html

简介

像加减乘除、取余、赋值等这类运算,python 与其它语言没有太大的不同,本文仅介绍一些 python 特有或与其它语言不一致的内容。

比较运算:

表达式描述
 x == y  x 等于 y
 x != y  x 不等于 y 
 x > y   x 大于 y 
 x < y   x 小于 y 
 x >= y   x 大于或等于 y 
 x <= y   x 小于或等于 y 
 y < x < z  x 大于 y 且小于 z ,这种方式叫:链式比较
 x is y

 x 和 y 是同一个对象,例如:
 x = y = [1, 2, 3]
 z = [1, 2, 3]
 x == y == z   # True
 x is y  # True
 x is z  # False

 x is not y   x 和 y 不是同一个对象 
 x in y   x 是容器(如序列) y 的成员 
 x not in y  x 不是容器(如序列) y 的成员

 

Python 中的“与”、“或”、“非”

大多数编程语言都使用“&&”、“||”和“!”符号来作为逻辑运算符,python中不支持:

  • “与”操作使用 and ,python 不支持 &&
  • “或”操作使用 or ,python 不支持 ||
  • “非”操作使用 not, python 不支持 !

 

Python 中没有自增(++)和自减(--)操作符

Python 中没有自增(++)和自减(--)操作符,前置、后置都没有!!!

如果你看到 ++i 或者 --i  (i 的值都不会变),它只是表示两个正负号而已,别搞错了!

 

Python 中的三目运算

Python 中不支持 ?: 这种三目运算符,要用 if-else 这种倒装形式,例如:

foo = 123 if bar else 456

 

切片操作

Python 中 字符串列表元组 三种数据类型都属于 序列 ,序列都支持切片操作,常用切片操作的形式如下:

a[start:stop]       # items start through stop-1,左开右闭
a[start:]           # items start through the rest of the array
a[:stop]            # items from the beginning through stop-1
a[:]                # a copy of the whole array

# 另外一种形式:
a[start:stop:step]  # start through not past stop, by step
a[::-1]             # all items in the array, reversed

参考:https://stackoverflow.com/questions/509211/understanding-slice-notation

 

正则表达式

Python 中的正则式并不是一种单独的数据类型,它就是普通的字符串而已,因此它无法跟“不区分大小写”这种修饰符写到一起,修饰符都是通过正则式模块提供的函数参数传递的。

注:我们通常见到别人写正则式以“r”为前缀(例如:r'^https?$'),这里的前缀“r”表示原始字符串(raw string,例如:r"\n"等价于"\\n",而不是换行符)而已。

Python 通过 re 模块提供的函数来处理正则匹配,示例:

import re
path = "https://www.baidu.com"
if re.match("https?://", path, re.I):   # 相当于 re.search("^https?://", path, re.I)
    print(path + " is an URL")
    if re.search("baidu\.com", path, re.I):
        print("The URL refers to Baidu site")

其中 match() 和 search() 的参数和返回值相似,它们的区别在于,match() 从字符串起始位置开始匹配,若不匹配直接返回 None,而 search() 会一直搜索到匹配的子串为止,若没有才返回None。

分组

import re
url = "https://www.example.com/index.html"
m = re.search("(https?)://www\.([^/]+)(/.*)$", url, re.I)
print("URL: %s" % (m.group(0)))      # URL: https://www.example.com/index.html
print("Protocol: %s" % (m.group(1))) # Protocol: https
print("Domain: %s" % (m.group(2)))   # Domain: example.com
print("Path: %s" % (m.group(3)))     # Path: /index.html
print(m.groups())                    # ('https', 'example.com', '/index.html')

 反向引用分组

import re
foo = '''The abbreviation "RTFM" means 'read the fucking code'. 
         It appeared in print in 1979 on the table of contents page of the LINPACK User's Guide.'''
print(foo)
bar = re.sub(r"(['\"])(.+?)\1", r"{\2}", foo) # 将引号(无论是单引号还是双引号)引用内容的引号替换为{}
#                          ^^   ^  ^^ 注意:反向引用这里的字符串是 raw 字符串
print(bar)

输出结果:
The abbreviation "RTFM" means 'read the fucking code'. 
         It appeared in print in 1979 on the table of contents page of the LINPACK User's Guide.
The abbreviation {RTFM} means {read the fucking code}.
         It appeared in print in 1979 on the table of contents page of the LINPACK User's Guide.

 

lambda 表达式 

lambda 表达式相当于定义了个匿名函数,它本身的值是个可调用的,例如:

foo = lambda x,y: x+y
print foo(2, 3) # 输出 5
# 上面的代码相当于定义了一个函数 foo
def foo(x, y):
    return x + y

lambda 表达式常用来当做某个函数的参数,例如:

foo = [1, 3, 5, 7, 9]
bar = map(lambda x:x*2-x/2, foo) # 相当于 perl 中 @bar = map { $_*2-$_/2 } @foo
print(bar)  # [2, 5, 8, 11, 14]

 

完。

posted @ 2020-02-12 08:29  itwhite  阅读(505)  评论(0编辑  收藏  举报