python入门训练营——模块与datetime模块

1、怎么查出通过 from xx import xx 导⼊的可以直接调用的方法?

a. 去官网看文档。可以用【对象名称.__doc__】查看。

import scipy
print scipy.quiver.__doc__

b. 使用 Python 下的 help 指令。如:

import numpy   # import相应的包
print help(numpy.title) # help(包.函数)

 

2、了解Collection模块,编写程序以查询给定列表中最常见的元素。

题目说明:

输入:language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']

输出:Python

"""
Input file
language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
   
Output file
Python
"""
def most_element(language):
    """ Return a list of lines after inserting a word in a specific line. """
   
    from collections import Counter
    language = ['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python','PHP', 'Python']
    print(Counter(language))
    print(Counter(language).most_common(1))

3. 假设你获取了用户输入的日期和时间如 2020-1-21 9:01:30,以及一个时区信息如 UTC+5:00,均是 str,请编写一个函数将其转换为 timestamp:

"""
   
Input file
example1: dt_str='2020-6-1 08:10:30', tz_str='UTC+7:00'
example2: dt_str='2020-5-31 16:10:30', tz_str='UTC-09:00'
   
Output file
result1: 1590973830.0
result2: 1590973830.0
"""
   
   
def to_timestamp(dt_str, tz_str):
    # your code here
        pass

4. 编写Python程序以选择指定年份的所有星期日。

"""
   
Input file
   2020
   
Output file
   2020-01-05                         
   2020-01-12              
   2020-01-19                
   2020-01-26               
   2020-02-02     
   -----
   2020-12-06               
   2020-12-13                
   2020-12-20                
   2020-12-27 
"""

import datetime 

a = int(input())
   
def all_sundays(year):
   
    dt1=datetime.date(a,1,1)
    dt2=datetime.date(a,12,31)
    for i in range((dt2-dt1).days+1):
        day=dt1+datetime.timedelta(days=i)
        b=day.isoweekday()
        if b==7:
            print(day)
        else:
            continue       
all_sundays(a)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值