当前位置:   article > 正文

Python中简单的编程_python简单程序

python简单程序

1、Python:字符串的翻转

# 使用字符串切片
str1 = 'Python'
print(str1[::-1])
# 使用 reversed()
str2 = 'Python'
print(''.join(reversed(str2)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

运行结果:
nohtyP
nohtyP
2、Python:在三个数中找最大值。

n1 = int(input('请输入第一个数字:'))
n2 = int(input('请输入第二个数字:'))
n3 = int(input('请输入第三个数字:'))
max_num = 0
if n1 > n2:
 max_num = n1
 if n1 > n3:
  max_num = n1
 else:
  max_num = n3
else:
  max_num = n2
  if n2 > n3:
   max_num = n2
  else:
   max_num = n3
print('最大值是:%d' % max_num)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行结果:
请输入第一个数字:6
请输入第二个数字:8
请输入第三个数字:9
最大值是:9
3、Python:编程实现有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

numberList = [1,2,3,4]
complexList = []
def permutationNum():
    for i in numberList:
        for j in numberList:
                for k in numberList:
                    if i!=j and k != j and i!=k:
                        complexList.append(str(i)+str(j)+str(k))
    print("共有{}种组合,分别为{}".format(len(complexList),complexList))

permutationNum()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

运行结果:
共有24种组合,分别为[‘123’, ‘124’, ‘132’, ‘134’, ‘142’, ‘143’, ‘213’, ‘214’, ‘231’, ‘234’, ‘241’, ‘243’, ‘312’, ‘314’, ‘321’, ‘324’, ‘341’, ‘342’, ‘412’, ‘413’, ‘421’, ‘423’, ‘431’, ‘432’]
4、Python:List= [‘a’,‘b’,‘c’,1,2,3]输出前三个元素,输出第2个和第5个元素,输出除第一个外所有元素。

List = ['a','b','c',1,2,3]
i = 0
while i<3:
 print(List[i],end=" ")
 i+=1
print(" ")
print(List[1],List[4])
j=1
while j<len(List):
    print(List[j],end=" ")
    j+=1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

运行结果:
a b c
b 2
b c 1 2 3
5、Python从100个数中随机生成5个数(不一样的5个数)

import random
x=set()
while(len(x)<5):
    x.add(random.randint(5,100))
print(x)
  • 1
  • 2
  • 3
  • 4
  • 5

运行结果:
{9, 79, 83, 51, 28}

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号