Python编写2-1000的回文素数

news/2024/7/6 5:36:01

问题描述:找出2-1000的回文素数。

import math
for i in range(2, 1001):
    for j in range(2, int(math.sqrt(i))):
        if i % j == 0:
            break
    else:
        m = str(i)
        if m[0] == m[-1]:
            print("%d为回文素数" % i)

Python中for循环和else是可以连用的,当for循环执行完就会执行else里的代码,如果被break就不会执行else中的代码。

官方的解释为:当迭代的对象迭代完并为空时,位于else的语句将会执行,而如果在for循环里有break时,则会直接终止循环,并不会执行else里的代码。


http://www.niftyadmin.cn/n/3649768.html

相关文章

go 创建自定义包_在Go中创建自定义错误

go 创建自定义包介绍 (Introduction) Go provides two methods to create errors in the standard library, errors.New and fmt.Errorf. When communicating more complicated error information to your users, or to your future self when debugging, sometimes these two …

VM打开Linux蓝屏重启问题

在VM中配置好虚拟机,打开时报错。 报错信息:在该系统上全局禁用了虚拟打印功能,不会为该虚拟机启动此功能。虚拟设备“seriaIO”将开始断开连接。 解决办法:把虚拟机配置中的打印设备移除就可以了。

[MSN谈话] 关于技术和人生道路的问题

[MSN谈话] 关于技术和人生道路的问题网友 说:想向你请教下关于技术和人生道路的问题。话入正题吧,现在就是对做技术开发有点茫然。不知道是不是真的有前途或者有自己的奋斗目标。我本身来讲并不是对技术有狂热爱好的人,但是却进入了这个行业。精于心,简于…

spring react_使用React Spring在React中介绍动画

spring reactIn this article we’ll be exploring one of the best animation frameworks for React: React Spring. You’ll learn some of the basics behind changing you component styling into smooth, physics-based transitions. 在本文中,我们将探索React…

中国大学的集体堕落---麒麟vs汉芯

贴文不说话:“今年年初的时候,我发现FreeBSD的官方网站以及全球最大的开源网站SourceForge曾经一度被封,并且对于FreeBSD使用的是关键字过滤的极端方式封锁。联系这一系列事件,我不得不对年初的FreeBSD被封事件产生了一个可怕的怀…

[收藏]该怎么做,才能取得转型的成功呢?[人力资本]

看了《人力资本》这期的转型专题,给出了几个成功人士的故事,虽然有点老生常谈,不过作为辅助教材,还是有很大指导意义的。刘述尧作为技术人员转型的标杆,谈的感想都蛮现实的:“我上学时候的理想就是将来创办…

命令行 上下箭头符号_命令行基础知识:符号链接

命令行 上下箭头符号Symbolic links allow you to links files and directories to other files and directories. They go by many names including symlinks, shell links, soft links, shortcuts, and aliases. At a glance, a symbolic link looks like just a file or dir…

golang 延迟_了解Go中的延迟

golang 延迟介绍 (Introduction) Go has many of the common control-flow keywords found in other programming languages such as if, switch, for, etc. One keyword that isn’t found in most other programming languages is defer, and though it’s less common you’…