0
点赞
收藏
分享

微信扫一扫

海象运算符

林肯公园_97cc 2022-02-13 阅读 38
python
# pyhton3.8提供了一个海象运算符
course_list = ["django", "scrapy", "tornado"]
# 减少调用的次数
course_size = len(course_list)
if course_size >= 3:
    print("课程较多,课程的数量:{}".format(course_size))

# 使用海象运算符号进行优化
if (course_size := len(course_list)) >= 3:
    print("课程较多,课程数量为{}".format(course_size))


power = [course_size := len(course_list), course_size**2, course_size**3]

import  re
desc = "hello:18"
m = re.match("hello:(.*)", desc)
if m:
    age = m.group(1)
    print(age)


if m := re.match("hello:(.*)", desc):
    age = m.group(1)
    print(age)
  • Go

	// 海象运算符
	var data int
	var err error
	if data, err = strconv.Atoi("12"); err != nil {
		fmt.Println("转换出错")
	}
	fmt.Println(data)
举报

相关推荐

0 条评论