Promoting Python

Peter Otten __peter__ at web.de
Tue Apr 5 08:46:33 EDT 2016


Gordon( Hotmail ) wrote:

Welcome!

> I am struggling to understand the basic principles of Python having spent
> many years as a pure Amateur tinkering with a variety of BASIC  as you can
> see on my site at http://www.sigord.co.uk/  I think you will agree all
> such versions of Basic I have used are far easier to understand than
> Python. 

I don't think it is Python versus Basic; older computers had simpler 
hardware and ran simpler software. Try to find your way around in the 
variant of Basic used by Microsoft's Word or Excel to see what I mean. You 
can show a dialog to fill in a form, but you may also be able to instrument 
a hostile takover of the machine of someone who opens your "document".

Python still allows you to write simple scripts that read a text file line 
by line, do something with the contents, and print the results in a terminal 
window or write them to another file. 

It also has libraries for almost everything you can imagine, too many to 
know all of them. The first thing you do when you try something new is to 
look for a library that may help. What are your options? Is there a de-facto 
standard? Is there something more advanced that promises to reduce the 
amount of code you'll have to write? Is there even a complete application 
ready to use?

> Though with my limited knowledge I accept Python may have many
> advantages over say Liberty Basic etc.
> 
> The problem I am finding is most of the sites claiming to help understand
> Python devote far too much space bragging about the wonders of Python
> instead of concentrating how to make sensible use of it. For example I
> struggle to find examples of such as below if you could please help me. 

> Liberty Basic
> for n = 32 to 255: print n;chr$(n) : next n

This is pretty much what it used to be:


for i in range(32, 256):
    print(i, chr(i))

The main gotcha is that Python uses half-open intervals (i. e. range(32, 
256) does include 32 but not 256).


> REM BBC Basic
> FOR c = 1 TO 15 : COLOUR c
>   PRINT "Color ";c
> NEXT c

You may find that easy to understand, but what is 

COLOUR 7

? Without the Basic manual you have no way of knowing.

Here's a simple way to print colored text in Python:

from termcolor import cprint
for color in ["grey", "red", "green", "yellow", "blue", "magenta", "cyan"]:
    cprint("Colour:", color)
cprint("Colour: white", "white", "on_red")

Here the hard part is finding the library that best fits your need.  
"termcolor" which I used is not part of Python's standard library, you have 
to download it from the Python Package Index (or install it from the 
repositories of your distribution if you are a Linux user).

> REM BBC Basic
> c = 0
> FOR x = 80 TO 2000 STEP 96
>   GCOL c: CIRCLE FILL x,500,50 : c = c + 1
> NEXT x

This is again harder. You don't get direct access to the screen; you have to 
pick a toolkit that is typically a library written in C or C++ with a thin 
Python wrapper. The C/C++ languages tend to offer interfaces that require a 
lot of bookkeeping, and some of this makes it into the Python interface. 

Another source of complexity is that these toolkits' main purpose is not 
drawing shapes on the screen.

In tkinter, for example, which is part of the standard library and wraps 
code written in TCL and C, you have to create a Window (called "Toplevel") 
and put a "Canvas" widget into it:

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=300)
canvas.pack()

def circle(x, y, radius, fillcolor=None):
    """Draw a circle of `radius` around the center (`x`, `y`)."""
    canvas.create_oval(x-radius, y-radius, x+radius, y+radius, 
fill=fillcolor)

RADIUS = 25
x = 80
for fillcolor in ["navy", None, "silver", "maroon", "white"]:
    circle(x, 150, RADIUS, fillcolor)
    x += 80 # draw next circle 80 pixels to the right
root.mainloop()

I iterated over the colors instead of the positions; I also wrote a helper 
function to allow you to specify the circles by their center and radius 
instead of the bounding box.

For beginners or children there is also the "turtle" module, a port of a 
library initially written in Logo.


> If
> I manage to duplicate some of my efforts on my site with Python I will be
> happy to upload the code to my site. I will also try to upload working
> exec files of such also, except my previous attempts at downloading the
> relevant software was blocked by AVG software as risky.

Before you really get frustrated you might consider using a cheap spare 
computer with a Linux installation for your experiments. If you don't hoard 
old machines in your basement the raspberry pi and the raspbian distribution 
nicely fit that bill ;)

> I am struggling to understand the basic principles of Python having spent

A good place to find help while making your first steps is the tutor mailing 
list.




More information about the Python-list mailing list