Python必考的5道面试题集合

作者:小旭2021 时间:2021-07-11 19:38:41 

1、使用while循环实现输出2 - 3 + 4 - 5 + 6 ... + 100的和

#方法一
#从2开始计算
i = 2
#定义一个变量用于保存结果
sum=0
while i <= 100:
   if i % 2 == 0:
       sum = sum + i
   else:
       sum = sum - i
   i += 1
print("2-3+4-5+6...+100=",sum)
#方法二
n=3
sum=2
while n<=100 :
   #n对2取余
   if n % 2 != 0:
       sum = sum - n
       n = n + 1
   else:
       sum = sum + n
       n = n + 1
print("2-3+4-5+6...+100=",sum)

运行结果:

Python必考的5道面试题集合

2、从键盘获取一个数字,然后计算它的阶乘,例如输入的是3,那么即计算3!的结果,并输出。

提示:

1!等于1

2!等于1*2

3!等于1*2*3

n!等于1*2*3*...*n

n = int(input("请输入一个非负的数字:")) # 负数不算阶乘
def factorial(n):
   if n == 0:
       return 1  # 0的阶乘是1
   else:
       return n * factorial(n - 1)

if __name__ == '__main__':
   result=factorial(n)
   print("{}的阶乘为:{}".format(n,result))

Python必考的5道面试题集合

3、用户输入考试成绩,当分数高于90(包含90)时打印A;否则如果分数高于80(包含80)时打印B;否则如果当分数高于70(包含)时打印C;否则如果当分数高于60(包含60)时打印D;其他情况就打印E。

try:
   score=float(input('请输入考试成绩:'))
   if score>=90:
       print('A')
   elif 80<=score<90:
       print('B')
   elif 70<=score<80:
       print('C')
   elif 60<=score<70:
       print('D')
   else:
       print('E')
except Exception as e:
   print('您输入有误!')

Python必考的5道面试题集合

4、假设一年的定期利率为3.52%,需要几年才能让定期存款连本带息的翻一番(例如:需要多少年10000才能变成20000)?

save_money = float(input("请输入你要存入银行的钱:"))
print("你存了{}元到银行".format(save_money))
total_money = save_money * 2 # 定义变量用于保存总钱数
year = 1 # 定义变量用于记录年份
while save_money < total_money:
   save_money *= (1 + 0.0352)
   year += 1
print("定期利率为3.52%,需要{}年本金和利息才能翻一番!".format(year))

Python必考的5道面试题集合

5、将列表a =["I","T","e","s","t","e","r"]拼接成字符串,请用多种方法实现。

# 方法一  字符串函数调用
a = ["I","T","e","s","t","e","r"]
print("".join(a))

#方法二 for循环
a = ["I","T","e","s","t","e","r"]
s = ""
for item in a:
   s += item
print(s)

来源:https://www.cnblogs.com/chenyablog/p/15172882.html

标签:Python,必考,面试题
0
投稿

猜你喜欢

  • response.setHeader()方法设置http文件头的值

    2010-03-11 22:43:00
  • 如何使用表格来储存数据库的记录?

    2010-05-16 15:14:00
  • SQLServer WITH 的用法

    2009-07-09 18:54:00
  • Python 高级专用类方法的实例详解

    2023-10-11 14:13:52
  • 关于textarea的直观换行

    2010-03-18 15:59:00
  • python如何编写win程序

    2022-12-09 11:48:38
  • python:目标检测模型预测准确度计算方式(基于IoU)

    2023-04-17 08:51:19
  • Centos 安装 PHP7.4 和 Nginx的操作方法

    2023-10-14 01:11:55
  • python复制文件的方法实例详解

    2021-12-22 11:43:45
  • 前后端分离和跨域问题的详细解决方案(CORS的原理)

    2023-05-30 01:19:26
  • win10系统下Anaconda3安装配置方法图文教程

    2022-08-06 23:01:49
  • PHP实现获取两个以逗号分割的字符串的并集

    2023-06-01 03:24:53
  • 详解Python读取配置文件模块ConfigParser

    2022-02-25 09:05:23
  • python 多进程队列数据处理详解

    2022-04-10 23:49:44
  • 安装SQL Server 2005时出现计数器错误

    2008-11-28 14:19:00
  • 完美的js验证网址url(正则表达式)

    2008-06-07 09:36:00
  • server.mappath方法详解

    2023-07-05 08:07:48
  • ASP.NET(C#)读取Excel的文件内容

    2023-07-10 22:38:35
  • 页面重构中的组件制作要点

    2009-10-25 13:06:00
  • SQL Server 2000里的数据类型

    2011-06-11 14:07:00
  • asp之家 网络编程 m.aspxhome.com