Python中简单的编程

1、Python:字符串的翻转

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

运行结果:
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)

运行结果:
请输入第一个数字: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()

运行结果:
共有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

运行结果:
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)

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Azure++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值