当前位置:首页 > 实验代码 > 正文内容

python数字组合

千帆1年前 (2024-12-24)实验代码4334

有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

遍历全部可能,把有重复的替换掉。


num=0

for a in range(1,5):

    for b in range(1,5):

        for c in range(1,5):

            if((a!=b)and(a!=c)and(b!=c)):

              print(a,b,c)

              num+=1

print (num)

扫描二维码推送至手机访问。

版权声明:本文由千帆生活网发布,如需转载请注明出处。

本文链接:http://ntshw.com/?id=185

分享给朋友:

“python数字组合” 的相关文章

Python随机数生成

# 生成 0 ~ 9 之间的随机数''' 此处为多行注释:导入 random(随机数) 模块,使用了 random 模块的 randint() 函数来生成随机数,你每次执行后都返回不同的数字(0 到 9)该函数的语法为:random.randint(a,b)''...

Python计算每个月天数

import calendar'''输出的是一个元组,第一个元素是所查月份的第一天对应的是星期几(0-6),第二个元素是这个月的天数。以上实例输出的意思为 2016 年 9 月份的第一天是星期四,该月总共有 30 天。'''monthRange = c...

Python代码打包成exe文件

要将Python代码打包成exe文件,可以使用PyInstaller工具。以下是使用PyInstaller打包Python脚本为exe文件的步骤:安装PyInstaller:pip install pyinstaller使用PyInstaller打包Python脚本:pyinstaller --on...

python文本处理:统计文本中单词出现的频率

text = "This is a sample text for word frequency analysis."words = text.split()word_count = {}for word in words:    if word in wor...

python文件操作:复制文件

import shutilsrc_file = 'source.txt'dest_file = 'destination.txt'shutil.copyfile(src_file, dest_file)...