2014年8月8日金曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 6(A Modular Approach to Program Organization)、6.6(Exercises) 2-a, b, c, d, e, f, g.を解いてみる。

6.6(Exercises) 2-a, b, c, d, e, f, g.

コード(BBEdit)

入出力結果(Terminal, IPython)

$ ipython
Python 3.4.1 (default, May 21 2014, 01:39:38) 
Type "copyright", "credits" or "license" for more information.

IPython 2.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import calendar

In [2]: calendar.isleap?
Type:        function
String form: <function isleap at 0x10a437510>
File:        /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/calendar.py
Definition:  calendar.isleap(year)
Docstring:   Return True for leap years, False for non-leap years.

In [3]: help(calendar.isleap)
Help on function isleap in module calendar:

isleap(year)
    Return True for leap years, False for non-leap years.


In [4]: calendar.isleap(2014)
Out[4]: False

In [5]: for year in range(2014, 2051):
   ...:     if calendar.isleap(year):
   ...:         print(year)
   ...:         break
   ...: 
2016

In [6]: dir(calendar)
Out[6]: 
['Calendar',
 'EPOCH',
 'FRIDAY',
 'February',
 'HTMLCalendar',
 'IllegalMonthError',
 'IllegalWeekdayError',
 'January',
 'LocaleHTMLCalendar',
 'LocaleTextCalendar',
 'MONDAY',
 'SATURDAY',
 'SUNDAY',
 'THURSDAY',
 'TUESDAY',
 'TextCalendar',
 'WEDNESDAY',
 '_EPOCH_ORD',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_colwidth',
 '_locale',
 '_localized_day',
 '_localized_month',
 '_spacing',
 'c',
 'calendar',
 'datetime',
 'day_abbr',
 'day_name',
 'different_locale',
 'error',
 'firstweekday',
 'format',
 'formatstring',
 'isleap',
 'leapdays',
 'main',
 'mdays',
 'month',
 'month_abbr',
 'month_name',
 'monthcalendar',
 'monthrange',
 'prcal',
 'prmonth',
 'prweek',
 'setfirstweekday',
 'sys',
 'timegm',
 'week',
 'weekday',
 'weekheader']

In [7]: calendar.leapdays(2000, 2051)
Out[7]: 13

In [8]: calendar.weekday?
Type:        function
String form: <function weekday at 0x10a437bf8>
File:        /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/calendar.py
Definition:  calendar.weekday(year, month, day)
Docstring:
Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
day (1-31).

In [9]: calendar.weekday(2016, 7, 29)
Out[9]: 4

In [10]: calendar.day_name?
Type:        _localized_day
String form: <calendar._localized_day object at 0x10a440c88>
Length:      7
File:        /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/calendar.py
Docstring:   <no docstring>

In [11]: calendar.day_name[4]
Out[11]: 'Friday'

In [12]: calendar.day_name[calendar.weekday(2016, 7, 29)]
Out[12]: 'Friday'

In [13]: quit()
$

0 コメント:

コメントを投稿