python 3.10更新的一些新特性

@目录

IDLE

首先最容易看到的变化就是python自带的IDLE:
在这里插入图片描述
在这里插入图片描述
把指示用的指针移动。

match-case

我们先来看一个Java代码示例(无作用,仅语法):
在这里插入图片描述
如果是对c或者java等语言有了解的话,应该不难理解,但是,python以前是没有这种语法的,但是在3.10版本之后,python更新了属于自家的match-case语句。
来看官方文档怎么说:
A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. This is superficially similar to a switch statement in C, Java or JavaScript (and many other languages), but it can also extract components (sequence elements or object attributes) from the value into variables.
match 语句采用表达式,并将其值与作为一个或多个事例块给出的连续模式进行比较。这在表面上类似于C,Java或JavaScript(以及许多其他语言)中的switch语句,但它也可以从值中提取组件(序列元素或对象属性)到变量中。
match-case语法结构如下:
在这里插入图片描述
接下来我们一起来看match-case的作用:

匹配数据

首先,就是匹配数据,例如

a=2
match a:
    case 1:
        print("太小了")
    case 3:
        print("太大了")
    case _:
        print("刚刚好")

其中“_”是通配符,代表一定会匹配成功,相当于其它语言迭代default。
接下来我们来看一个简单题:
在这里插入图片描述

力扣连接.
首先我们用ifelse语句来写出第一版:

class Solution:
    def calPoints(self, ops)->int:
        ans=[]
        for i in ops:
            if i=="C":
                ans.pop()
            elif i=='+':
                ans.append(ans[-1]+ans[-2])
            elif i=='D':
                ans.append(ans[-1]*2)
            else:
                ans.append(int(i))
        return sum(ans)

现在不用if语句,直接使用我们的新特性:

class Solution:
    def calPoints(self, ops)->int:
        ans=[]
        for i in ops:
            match i:
                case "C":
                    ans.pop()
                case '+':
                    ans.append(ans[-1]+ans[-2])
                case 'D':
                    ans.append(ans[-1]*2)
                case _:
                    ans.append(int(i))
        return sum(ans)

接下来提交:
在这里插入图片描述
可以看出,两种语法并不是差别很大,值得一提的是,在match中可以使用“|”,也就是逻辑或。

绑定变量

match可以用来绑定变量,也就是说匹配一部分(自我理解)

def matchh(a):
    match a:
        case [0,222]:
            print("0位置是0")
        case [111,0]:
            print("1位置是0")
        case _:
            print(a)

matchh([111,0])
matchh([0,222])
matchh([111,222])

结果如下:
在这里插入图片描述
当然,match里面还能嵌套if语句:

def matchh(a):
    match a:
        case [111,0] | [0,222]:
            print("其中包含了零")
        case [x,y] if a[0]==a[-1]:
            print("两个元素相等")
        case _:
            print(a)

matchh([111,0])
matchh([0,222])
matchh([111,222])
matchh([111,111])

在这里插入图片描述

bit_count()

先来看一道题:
在这里插入图片描述

你会怎么做?转字符串记数?还是位运算?(此处如果对位运算有疑问,可移步至此:位运算.python3.10更新了一个简单的方法(bit_count()),类似于Java的Integer.bitCount(),其能统计一个数中二进制位一的个数:
我们先来看位运算解题:

class Solution:
    def hammingWeight(self, n: int) -> int:
        count=0
        while n:
            count+=n&1
            n=n>>1
        return count

顺便发一下Java版:

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count=0;
        for (int i = 0;i <32 ;i++){
            count+=n&1;
            n=n>>1;
        }
        return count;
    }
}

现在我们来看新更新的bit_count():

class Solution:
    def hammingWeight(self, n: int) -> int:
        return n.bit_count()

后记

以上就是对新更新的match-case语句的一些浅显的理解,如有错误,还请帮忙斧正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值