栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【科学与工程计算】 实验3 Python类和对象

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【科学与工程计算】 实验3 Python类和对象

【科学与工程计算】

实验3 Python类和对象

题目1

编写程序,打印九九乘法表。

for i in range(1,10):
    for j in range(1,i+1):
        print('{1}*{0}={2}'.format(i,j,i*j).ljust(6),end=' ')
    print()

题目2

编写程序,判断今天是今年的第几天

from datetime import datetime
def is_leap_year(year):
    if  (year % 100 != 0 and year % 4 == 0) or (year % 400 == 0):
        return True
    return False
def main():
    year_month_day = input('请输入年月日(yyyy/mm/dd):')
    date = datetime.strptime(year_month_day, '%Y/%m/%d')
    print(date)
    year = date.year  
    month = date.month  
    day = date.day  
    days = 0
    days += day
    _30_month = {4, 6, 9, 11}
    _31_month = {1, 3, 5, 7, 8, 10, 12}    
    for i in range(1, month):
        if i in _30_month :
            days += 30
        elif i in _31_month :
            days += 31
        else:
            days += 28
    if is_leap_year(year) and month > 2:
        days += 1
    print('这是{}年的第{}天'.format(year,days))

if __name__ == '__main__':
    main()

题目3

编写代码,输出由星号*组成的菱形图案,并且可以灵活控制图案的大小。

n = int(input('num:'))
for i in range(n):
    print((' * '*i).center(n*3))
for i in range(n,0,-1):
    print((' * '*i).center(n*3))

题目4

自定义队列结构,实现入队、出队操作。

class Queue(object):
	#初始化一个队列
    def __init__(self):
        self.__list = []
    #入队
    def pushqueue(self,item):
        #尾部进队,头部出队
        self.__list.append(item)
        #头部进队,尾部出队
        # self.__list.insert(0,item)
    #出队
    def putqueue(self):
        return self.__list.pop(0)
        # return self.__list.pop()
    #判断是否为空
    def is_empty(self):
        return self.__list == []
    #判断长度
    def size(self):
        return len(self.__list)
if __name__ == '__main__':
    s = Queue()
    s.pushqueue(1)
    s.pushqueue(3)
    s.pushqueue(2)
    print('进队之后的长度', s.size())
    print(s.putqueue(),end=' ')
    print(s.putqueue(),end=' ')
    print(s.putqueue())
    print('出队之后的长度',s.size())
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号