Python Notes: Simple Calendar (BASIC vs. Python3)

  1. The information presented here is intended for educational use.
  2. The information presented here is provided free of charge, as-is, with no warranty of any kind.
Edit: 2019-11-28

BASIC-to-Python conversion example

Anyone who started playing with BASIC on personal computers in the 1970s and 1980s will recognize the importance of that language for noodling around. The creators of BASIC intended it to be used to teach computer programming concepts to FORTRAN students, but noodling around made BASIC ideal for teaching other concepts in science, engineering, and math. The biggest problem with BASIC was lack of a standard (er, BASIC was standardized but almost no one adopted it). Since Python is published by one group of people sponsored by big companies like Google. it is replacing BASIC for many purposes including noodle around

Generate a simple calendar (this demo does not deal with leap years)

Instructions:

  • generate a list of month-day pairs for a whole year (for some unspecified purpose; perhaps for a primary key for a database)
  • start with an outer loop stepping between 1 and 12
  • add an inner loop stepping between 1 and 30
  • optionally, add a subroutine to change the month size (number of days per month); since a year is not specified, do not adjust for LEAP YEAR
  • ensure that the days and months are presented with leading zeroes so the total output is always 4 characters

Original PC-BASIC source code

1 REM ====================================
2 REM title   : MMDD01.bas
3 REM author  : Neil Rieck
5 REM language: GW-BASIC/QBASIC/QuickBASIC
6 REM ====================================
10 PRINT "*** MMDD01.bas ***"
20 FOR M = 1 TO 12
30   GOSUB 900
40   FOR D = 1 TO L
41       X$ = LTRIM$(STR$(M))
43       IF LEN(X$) = 1 THEN X$ = "0" + X$
44       Y$ = LTRIM$(STR$(D))
46       IF LEN(Y$) = 1 THEN Y$ = "0" + Y$
50       PRINT X$ + Y$
55   NEXT D
60 NEXT M
70 GOTO 999
900 REM *** compute days-per-month ***
901 IF M = 2 THEN L = 28: RETURN
902 IF M = 9 THEN L = 30: RETURN
903 IF M = 4 THEN L = 30: RETURN
904 IF M = 6 THEN L = 30: RETURN
905 IF M = 11 THEN L = 30: RETURN
906 L = 31
907 RETURN
999 END





Python3

#!/usr/bin/python3
# ===================
# title  : MMDD01.py
# author : Neil Rieck
# created: 2019-11-28
# ===================
print("*** MMDD01.py ***")
#
#   subroutine
#
def days_per_month(MM):
    if (MM == 2):
        return(28)
    if MM in [9,4,6,11]:
        return(30)
    return(31)
#
#   main
#
for MM in range(1,13):
    LIMIT = days_per_month(MM)
    # print("test case: ",MM," ",LIMIT)
    for DD in range(1,LIMIT+1):
        TEST = str(MM).zfill(2) + str(DD).zfill(2)
        print(TEST,end =" ")
        row = mysql3.get_entry(TEST)
        print(row[2])




C/C++

// ====================
// title   : mmdd01.c
// author  : Neil Rieck
// created : 2019-11-28
// language: C/C++
// ====================
#include <stdio>
//
long msize(long mm){
    switch(mm){
    case  9:
    case  4:
    case  6:
    case 11:
        return(30);
    case  2:
        return(28);
    default:
        return(31);
    }
}
//
void main(){
    long mm,dd,limit;
    for (mm=1;mm<=12;mm++){
        limit = msize(mm);
        for (dd=1;dd<=limit;dd++){
            printf("%02ld%02ld\n",mm,dd);
        }
    }
}

Comments:
BASICPython3
remarks begin with "REM"remarks begin with "#" (the very first line is a SHEBANG which is only relevant on UNIX or Linux systems)
PRINT statements output anything after the word PRINT"print" statements in Python3 are implemented as a function so require soft braces (not so with Python2)
outer loop: all code between "FOR M" and "NEXT M"
(indenting is optional)
outer loop: begins with "for MM"
notice the use of a colon (":") and 4-space indenting to indicate what is included in the loop
range is 1 to 13 because the Python ranges are up-to but not-including the last number
inner loop: all code between "FOR D" and "NEXT D"
(indenting is optional)
inner loop: begins with "for DD"
notice the use of a colon (":") and 4-space indenting to indicate what is included in the loop
need LIMIT+1 because the Python ranges are up-to but not-including the last number
zero padding: "PRINT USING" is the usual way to issue leading zeros but that feature was never implemented with this BASIC so we need to resort to other means; function STR$() in many BASICs will only return minimum output but this BASIC always includes a leading space which is why I needed to call LTRIM$() to remove it; zero padding: function "str()" converts a number into a string; the zfill method associated with the string class (most of Python3 is object oriented) ensures that every string will be at least two characters wide.

External Links


 Back to Home
 Neil Rieck
 Waterloo, Ontario, Canada.