basicprogramming.org

BASIC programmming blog › Basic vs. python by: menn
Skip to content

Basic vs. python by: menn

This article was originally released at basicprogramming.org forum

talk of basic vs. python is a lot like talk about yesterday vs. today. i’m not saying basic is necessarily “yesterday” anymore than python is… python is based on some very old ideas, the c language python’s interpreter is usually implemented in is probably even older. what i mean is some of the different pros and cons are probably familiar to people who have found themselves in debates about the “good old days” of coding. everyone wants to use their favorite tool for development, so here are my criteria:


free software

my first priority in choosing a tool is that it be something that can be managed without the original developers, without permission, and that it can be shared with other people so it can help them learn to use the software and the language. the biggest problem with my favorite tool from 20 years ago is you cannot do much of anything to improve on it at this point, it can’t be expanded, upgraded, shared (legally) or brought up to date. i don’t want to repeat that, so i choose free(libre) software.

i have tried a lot of basic programming tools, and examined them carefully for how they implement (or don’t implement) various features, for how they are used by the communities that enjoy them, and for what’s to be gained by using them instead. i don’t really want to use 10 different kinds of basic, so i’ve always looked for the best one. a few years ago i found yabasic and it’s the only free tool for basic that has remained useful enough to me for me to keep using it- though it’s rare i do, it’s all i use.

basic loses a lot in this regard, because i think it’s safe to say that throughout its history most implementations (including in the present) and the most popular and powerful implementations have been proprietary. my favorite implementation of basic was proprietary, and finding an alternative that suited my needs was far from painless- to me it’s nothing like switching from internet explorer to firefox, more like a migration from windows to gnu/linux.

furthermore in the world of basic programming, you don’t (by comparison) find a healthy attitude toward sharing in the modern sense of the word. there are lots of old attitudes and confusion about “freeware” and the public domain, there’s rarely desire on anyone’s part to open their code, but they “publish” their source freely everywhere people will post it, and technically you still need permission to use it (because for decades now, american copyright and the berne convention establish copyright whether it’s registered or marked or not) and there’s tons of ridiculous talk about people “stealing” code and even ideas, when very few of the ideas are original.

not that i think you ought to use things without permission… whatever people insist you respect, you might as well do as they wish, only the basic community has perhaps more to instantly gain from a more open attitude (and using explicitly open code) than any other group, they seem to understand the benefits less than any other group. exceptions exist and they are most welcome- note i’m talking about the overall experience for someone looking through any or all basic dialects, not just one or two dialects or one or two (sub)communities.

ease of use

this is a complicated thing to rate, what’s easy for one person isn’t always for someone else, an advanced user may consider some very complicated things “easier” in a big picture, it’s-only-easier-if you understand the major changes to the process kind of way, and that’s legitimate for an advanced user. i was looking for something easier for say, a person who was mostly unfamiliar with computers. i learned basic and computing at the same time, and look for tools that could be used to introduce computing to students.

the relationship between typing and thinking is almost always relevant. whether using the command line, a gui, or writing code, there are times when an abbreviation is a wonderful thing, and times when abstraction saves tons of tedious work. other times people learn more comfortably typing out countless if statements until they understand loops that mean they never have to do something so mundane again. i like a balance of options in this regard, but it doesn’t have to be a 50/50 balance, just a good balance.

basic makes anything that’s easy into something extremely easy, but it depends on the dialect. some dialects (and i insist that i’m referring to several different examples here, not just one or two) being with ambitions of expanding basic into something easy but “more powerful” and end up much more complicated, because the developers simply don’t know (or forget) how to make certain things as simple as basic makes them. ease of use is complicated to rate, but it seems also complicated to implement.

basic sometimes makes moderately complicated things even more complicated. this is where i think python wins. in a perfect world, basic would be the ideal first language and python the ideal second language, because its strengths are in making the intermediate very easy. traditionally easier tasks are easy in python too, but not always exactly as easy as they would be in basic. still by the time you count all the would be’s, python starts to look like a very good compromise for a first language if basic is not sufficient.

fun

if it can’t be a used as a source of entertainment, i much prefer to find something that can. most serious high-tech tools used by everyday people have features that exist just for fun, or may have such features added easily. having fun is the best way to learn a programming language, especially if you’re going to learn it before the age of 30. if it’s either too tedious or too complicated to understand, it’s likely another language will rise up to take its place.

basic can be very, very, very fun, and i think as a language ideally designed to offer instant gratification, basic is traditionally more fun, even better suited to having fun. then again what one generation considers entertaining may not be so much to a newer generation (this accounts for any changes in the tone or scope of content in tutorials you might notice over time) and getting basic to do some traditionally intermediate things that are easy enough in python might not be any more fun in basic than in python.

i always thought graphics were easier to learn and implement in qbasic than any other language. people who like turtle graphics may disagree, although qbasic even had a sort of turtle macro command. people who want to process images from or for the web should choose another dialect, though many languages that provide native handling of image files are a little more tedious when using primitives like circles and lines. in basic, what’s both fun and easy comes to be very specific, until game languages emerge.

but basic (even by its name) was always intended to be an all-purpose language, and keeping it extensible has proven a challenge for any developer. libraries are often tedious and schizophrenic to use, in python they are often painless, straightforward and second nature. i would prefer to shield new users from libraries and i suppose, from any “fun” that comes of using them, though python is the second language which i’ve used libraries with, and the only one that introduced them without me thinking about libraries.

free documentation and project maturity

the more mature a project is, the easier it is to find free and friendly (and complete) documentation, which substantially increase a user’s ability to learn a language. when i was learning basic, i did not just use the help system. as far as i know, the interpreter i used did not include online help, but i had a rom cartridge that loaded basic into the pc which seemed to include a very friendly printed manual that acted as a tutorial. from that book i learned basic, and i obtained another book later on, and many years later i set out to write a free book that would serve the same kind of user for teaching qbasic.

i think python wins in project maturity and free documentation because the implementations that are the most popular are mature, and there is not one but at least two excellent free(libre) books that can teach you how to have fun learning python. “think python,” available in printed or html form from green tea press or in wiki form at wikibooks.org, is in every way the book i would have written for python if i knew it as well as i did qbasic, and if i wrote as well as i wanted to write a second edition of my qbasic book.

obviously basic itself is very mature, but any implementation you’re using is probably not. for a basic as good as yabasic you could not hope for a manual much better than the html included with the download and available from the website, but “think python” it’s not. other dialects are simply impossible to document, they are still changing too much too often for it to be worth outlining how the language works- and this is true for perhaps most of the dialects people get excited about.

there’s nothing wrong with diversity or with doing something new, but a combination of great diversity and rare project maturity does not make for the best documentation, at least it doesn’t make great (or accurate) documentation likely to be found. you can work around inaccurate documentation, (indeed some of “think python” is in danger of becoming inaccurate) but these things are all relative, and i’m rating things best to less, not perfect or perfectly useless.

availability from one machine / os / distro to another

my favorite basic is yabasic, if i want to install it in windows i just download an .exe and run it it’s installed… but the old installer is a bit odd. in the past i’ve had to do some bizarre things to keep it from automatically uninstalling the moment it’s installed. i could redistribute a zip file and make it easier on people that know how to use them, or i could create another installer if i wanted, such is the nature of free software, however none of these options are ideal.

from any number of popular gnu/linux distros i can run yabasic in wine, much the way i’d run qbasic in dosbox. i can install yabasic natively with the command “sudo apt-get install yabasic” or from synaptic in debian based distros, otherwise i can probably run the native binaries in many but not all of my favorite distros.

python is readily available in virtually all the distros i prefer, and trivial to install in the distros i use most. it is easy to install in windows and even pre-installed on os/x. to say that python gives a reasonably consistent experience is an understatement, i haven’t had such a consistent experience since i relied on qbasic. however, some libraries, such as pygame, make installation and preinstallation a little less trivial. this probably narrows the number of distros i would use with pygame, and thus my use of the library.

my choice

i haven’t given up on basic or on yabasic, which is still being developed. in the past year, yabasic is the only basic that’s been useful to me based on my particular requirements, i’ve used it on occasion and i’ve looked for a language i want to use every day. it was a coincidence that i found python, and i was not expecting to learn it so quickly or be looking forward to using it every day.

i didn’t think i’d be able to port bungy to python without even testing the code, i ported the entire program (it’s a page or so, but it’s involved) from basic, found a couple syntax issues when i put them through the interpreter, and fixed them, found one or two more bugs, and it was ported. from one basic dialect to another, i’ve never had as easy a time doing this as i did porting to python- that’s one reason i never switched over entirely from one dialect to another i’ve still got qb-only programs, mostly graphical.

until the perfect basic comes along, or yabasic becomes perfect, i like having something i can code in and do things easily. for my purposes, python makes those tasks trivial. it’s my favorite language because it best serves the purposes i relied on qbasic for… like prototyping (all i really do is prototype, but to me, the prototypes are useful) small utilities and write quick once-used or maybe more-than-once-used scripts for simple tasks. i like to do this without stopping to think, with code that fits the way i talk to the computer.

obviously when i still use basic, i still use yabasic, and if you still use basic, i still recommend it. there are no perfect basics out there, to me only qbasic comes pretty close. yabasic comes close to qbasic, and generally does what you expect it to, and it’s compact and easy to learn, it’s fun to use, and it will give you some modern features like access to the mouse without authoring really complicated routines or using interrupts. my needs are not everyone’s needs, if you want to write games yabasic may be a good first language, but you may prefer a basic designed for writing games, etc.

what i want is easy-to-learn, easy-to-remember, easy-to-use, and easy-to-teach programming. from the early 80’s to the present, that’s meant basic every time. these days- for me- i am pretty certain it means python. i thought it was my idea, but the more i look around, the more i see the same idea: python is the new basic. as someone who always rejected the very concept of “is the new basic,” i think it is, but i admire the authors of yabasic and i hope it continues to be the best basic- at least the best basic by name.

examples

and still the way i decide these things is in trying to implement certain familiar tasks, and seeing how much i have to learn, how much it feels like what i used to do, and how it felt to learn basic. “think python” is such a friendly book, even if it has a few too many “big words” to throw to a first year student, i almost felt like i was getting my childhood back. (obviously i had some nerdy interests for a first year student.)

(graphics)

i take it on faith that graphics are trivial in python, as what introduced me to the language originally was a series of brief program examples which probably used the pygame library. at the time i didn’t understand what i was looking at, but now i know i was looking at some library and bits of code which looked to me remarkably like basic programs. i then looked for a “text only” example to play with, started toying with python then and there, and within days i’d read all of “think python,” and had written my own fibonacci program (unlike the version in the book it did not use recursion, so it seemed to run many times faster.) i’ve used graphics in yabasic but i can’t get them to run most of the time, because of the environments i choose to use yabasic in. still i’d rather do graphics in yabasic (which i use, rarely) or qbasic (which i do not use) or python (which i have only looked at the examples) than in any other language or dialect.

hello world


print "hello, world!" 'qbasic
print "hello, world!" //yabasic
print "hello, world!" #python
nbsp;
print "same-"; : print "line" 'qbasic
print "same-"; : print "line" //yabasic
 
print "same-", ; print "\x08"+"line" #python

you don’t need the backspace (”\x08″) to print on the same line, only to avoid the space python puts between items printed separately.

color text

qbasic: you need to know 8 hues numbered 0,7 first for the foreground, then for the background. adding 8 (8-15) give you lighter versions of the hues: “light black” is dark gray, “light brown” is yellow. the color command assigns the hues by number to the any text you print after the assignment.

yabasic: you seem to need the graphics mode- or, you need to print ansi escape sequences for hues 0 to 7, but ansi orders them differently, and brown (or really, dark yellow) usually looks more like curry than chocolate- really it depends what term you use. by using the ansi attribute for bold you can get bright colors basically the same as in qbasic.

python: you can use ansi codes like in yabasic, but they will not work in idle (if you don’t use idle, it doesn’t matter that much.) you can also use the curses library, which is a little more to learn in the beginning, but will probably save you a lot of trouble in general.

curses is usually but not always the better solution if you want interactive text-mode programs in python: note that while ansi codes affect the color of the actual text in the term (graphical or vt) and the print command, curses uses a virtual (invisible) “window” in the (graphical or vt) term, which disappears on exit.

variables


print variable 'prints 0 in qbasic if not assigned
print variable //prints 0 in yabasic if not assigned
print variable #raises an error in python if referenced without a prior assignment

assigning a variable does not require declaring the variable first:


q$="hello" : b=5.5 'qbasic
q$="hello" : b=5.5 //yabasic
q="hello" ; b=5.5 #python

concatenation is the same:


q$=q$+"... hello again!" 'qbasic
q$=q$+"... hello again!" //yabasic
q=q+"... hello again!" #python... still raises an error if q has never been assigned

numeric / string

qbasic, yabasic, and python appear to handle math in a similar overall fashion, though in qbasic and yabasic, 5/2 will give you 2.5. in python 5/2 is a pair of integers, so you get integer output: 5/2 is 2… 5.0/2 is 2.5… float(5)/2 and float(”5″)/2 are also 2.5, note the float command being used like val() in qbasic or yabasic, it can convert strings or integers to floats.


print 8 mod 7 'prints 1 in qbasic
print mod(8,7) //prints 1 in yabasic
print 8 % 7 #prints 1 in python
 
print int(2.5) '2 in qbasic
print int(2.5) //2 in yabasic
print int(2.5) #2 in python. can also be used to convert strings to int
 
print "*"+str$(5)+"*" '* 5* in qbasic?
print "*"+str$(5)+"*" //*5* in yabasic
print "*"+str(5)+"*" #*5* in python
 
print asc("zoo") '122, ascii for z in qbasic
print asc("zoo") //122, ascii for z in qbasic
print ord("zoo") #python produces an error if string more than 1 character, use ord("z") or ord("zoo"[0])
 

print chr$(122) 'qbasic
print chr$(122) //yabasic
print chr(122) #python

it’s very easy to get 1 letter from anywhere in a string in python, (see the ord example) but for left$ and right$ (which use a similar method) you’ll want to learn how to do that in a python tutorial. it’s not too complicated but it can be confusing if you’re not familiar. it’s a little confusing for me because i’m familiar with mid$()

if you import math, random you can access functions in python like math.cos() and random.randint(min,max) similar to the functions in qbasic and yabasic. if you: from math import * you won’t have the namespace “math.” to contend with, but presumably importing every library function into the same namespace is a terrible idea.

keyboard input


print "enter your name: ": line input q$ 'qbasic
line input "enter your name: " q$ //yabasic
 
q=raw_input("enter your name: ") #python

qbasic has the best keyboard polling function: inkey$ … there is also an inkey$ function in yabasic, but it seems to be more like input$(1) in qbasic. (it may be more flexible than input$(1) in qbasic.)

in python, the closest thing to input$(1) that you want to use is getch() but you can only use getch() in curses. (if you’re using pygame you’ll probably want to use a different method.) there is no simple function for waiting for a trapping a single keypress in python, but it is possible to create a complex function that uses native libs to poll input directly from the term (more like inkey$ in qbasic,) without curses or pygame- it’s possible. but you’d likely prefer the curses library- this is why it’s better for interactive programs.

arrays and data statements

i’m so glad to be done with arrays and data statements in other languages, i don’t even want to get into them again. note in qbasic and yabasic (and in general) i’m more accustomed to using long strings either separated by fixed length fields (more like savedgame files or pascal string handling) or some kind of unique string (more like csv files or c string handling.) i’d rather do that than do much with arrays in basic, but better arrays than data statements, which i never liked.

however in javascript and python i really like arrays, and (maybe most of the time) i’d rather use them. also, switching back and forth between the way i did things before and using arrays is easier in up to date scripting languages. python has lists, dictionaries, and “tuples,” with slightly different features and advantages, all made very friendly and for free by the book “think python.”

lists are possibly the easiest to explain and probably the closest to arrays in qbasic:


dim list$(0 to 25) : list$(0)="a" : list$(25) = "z" 'one field for each letter of the alphabet in qbasic
list=[0]*26 ; list[0]="a" ; list[25]="z" #one field for each letter in python

lists can be used the way data statements are used in qbasic:


linelabel: data "hello", "there", "world"
restore linelabel
read a,b,c
print a : print b : print c 'qbasic
 
labelname=["hello","there","world"]
 
print labelname[0] ; print labelname[1] ; print labelname[2] #python

lists can be concatenated, “sliced” (like using mid$ on a string) edited, doubled, tripled, multiplied (variable concatenation) and they keep their contents when you add to them or multiply their size:


list=["hello",2.5,100] ; print list #prints ['hello', 2.5, 100]
list=list*3
list[7]="there"
 
print list #now prints ['hello', 2.5, 100, 'hello', 2.5, 100, 'hello', 'there', 100]
print list[3] #prints: hello

whitespace

this is a non issue except in control structures like loops, branches and functions, where it’s trivial compared to the rules most people make for themselves just to make code easier to read. i thought i would never be able to tolerate a language that had rules about whitespace, but it does make my code much easier to read, i don’t spend the time or attention i figured i would on indentation, it’s a feature of the language i (almost) don’t notice.

as a bonus it does not require me to define any functions in this{senseofthephrase} but the rules about whitespace naturally encourage me to create functions at reasonable times (though i don’t have to think about when that is) because it’s as easy to create (and call) a function as in any other language:


def hellothere():
    print "hello, there!" #that's the whole thing

now i use the function like any other (void) function:


hellothere() #print "hello, there!" to the screen

return e/x/p/r/e/s/s/i/o/n (in a function) does the same thing in python that functionname = e/x/p/r/e/s/s/i/o/n does in a function in qbasic. arguments in function calls are coded into the function definition the same way as qbasic. when creating functions is that easy, you start creating them to avoid more complicated indentation.

the whitespace convention will turn a few people off of using python completely, it almost stopped me from ever learning python, but i’ve decided i actually like it- and i’m a person who just about never indents code. in my opinion, a language ought to be so easy to read that indentation isn’t necessary. basic is not such a language however, and a lot my code is really difficult to look at. now that i use python, i almost feel like my code looks twice as professional, and it is much easier to look over, so i like this about python.

as i understand it, the closest language to python that doesn’t have this business with whitespace (back to end ifs and end functions, end loop, and that kind of thing) is ruby. it’s very simple to end a function or other structure the old way, but part of me hates to imagine it. i admit, my assumptions about whitespace in python did not match my experience, but it’s nice to know if i ever got tired of it i could just start using ruby instead. whitespace or no, i think python is probably closer to what i wanted in a lot of ways.

menn
free content – license: cc by 3.0

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*