python 面试题库

python面试题库,内附答案

本人在网上搜集的python面试题,但是没有答案,自己搜集或回答了一部分。现分享给大家,有些题目回答的并不准确,望各路大佬提出批评纠正,完善!!!

一,python 基础

  1. 代码中要修改不可变数据会出现什么问题?抛出什么异常?
代码不会正常运行,抛出TyprError异常

2.模块和包是什么?

在python中,模块是搭建程序的一种方式。每一个python代码文件都是一个模块,并可以引用其他的模块,比如对象和属性。
一个包,含许多python代码的文件夹是一个包,一个包可以包含模块和子文件夹。

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

def threenum():
    count = 0  # 计数
    nums = []  # 初始化
    for i in range(1, 5):  # 百位循环
        for j in range(1, 5):  # 十位循环
            for K in range(1, 5):  # 个位循环
                if (i != j) and (i != K) and (j != K):  # 判断不重复的三个数
                    num = 100*i+10*j+K
                    count += 1  # 累加
                    if num not in nums:
                        nums.append(num)  # 添加到数组
    return count, nums
if __name__ == '__main__':
    print(threenum())
-------------------------------------------------------------------------
(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.输入某年某月某日,判断这一天是这一年的第几天?

方法一:

import datetime
year = int(input("请输入4位数的年份:
"))
month = int(input("请输入月份:
"))
day = int(input("请输入当月哪一天:
"))
targetDay = datetime.date(year, month, day)
dayCount = targetDay - datetime.date(targetDay.year - 1, 12, 31)
print("%s是%s年的第%s天." % (targetDay, targetDay.year, dayCount))

方法二:


# 特殊情况,闰年且输入月份大于2时需考虑多加一天
# 用来输入年、月、日的,并将它们转换成数字,赋值给不同的变量
year = int(input('请输入年:
'))
month = int(input('请输入月:
'))
day = int(input('请输入日:
'))
# 除了二月外,其余的上个月月底是今天的第几天
months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
# 用月来判断,如果大于0,小于等于12,得到上个月月底是今年的第几天
if 0 < month <= 12:
    sum = months[month - 1]
else:
    print('填写的日期错误')
# 再加上这个月的天数,就得到了是今年的第几天
sum += day
leap = 0
# 通过判断是否是瑞年,考虑二月那一天是否加1:
# 如果是闰年,并且输入的月份大于2,就需要将天数加1,因为闰年的2月是29天
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
    leap = 1
if (leap == 1) and (month > 2):
    sum += 1  
print("这是今年的第%d天" %sum)

方法三:

year = int(input("Please enter the years:"))
month = int(input("Please enter the months(1-12):"))
day = int(input("Please enter the days(1-31):"))
days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
if year % 4 == 0 and year % 100 != 0:
    if month > 2:
        totaldays = days[month - 1] + day + 1
    else:
        totaldays = days[month - 1] + day
else:
    totaldays = days[month - 1] + day
print("你输入的日期是:", year, "年", month, "月", day, '日', ",是今年的第", totaldays, "天!")
python
原文地址:https://www.cnblogs.com/bky20061005/p/14715737.html