Copyright (c) Hyperion Entertainment and contributors.

AmigaOS Manual: Python Getting Started

From AmigaOS Documentation Wiki
Jump to navigation Jump to search

Running Python programs

Using Python interactively

How to write Python programs

Amiga.py

This program shows how to use print() to display text on the screen. Print() is a function which can print the given object to the screen or to a file. In the following example text "Amiga. The Computer For the Creative Mind." will be printed on the screen. The text in the source code is surrounded by single quotes (') but you can also use double quotes (").

Program 1. Amiga.py

# A simple program
print( 'Amiga. The Computer For the Creative Mind.' )

Enter the above program using your favourite text editor, and save it as PYTHON:Scripts/Amiga.py. To run the program, open a Shell window and type:

Python PYTHON:Scripts/Amiga.py

You should see the following text in your Shell window:

Amiga. The Computer for the Creative Mind.

Age.py

This program displays a prompt for input and then reads the entered information. It also introduces you to the str() function which converts an object to a string and a string concatenation operator (+) which joins two strings.

Program 2. Age.py

# Calculate age in days
age = input( 'Please enter your age: ' )
print( 'You are about ' + str( age * 365 ) + ' days old.' )

Save this program as PYTHON:Scripts/Age.py and run it with the command:

Python PYTHON:Scripts/Age.py

Calc.py

This program introduces the for..in statement, which iterates over a sequence of objects, and the exponentiation operator (**).

Program 3. Calc.py

# Calculate some squares and cubes
for i in range( 1, 11 ): # Begin loop - 10 iterations
   print( str( i ) + ' ' + str( i ** 2 ) + ' ' + str( i ** 3 ) ) # Perform calculations
 
print( 'All done.' )

Save this program as PYTHON:Scripts/Calc.py and run it with the command:

Python PYTHON:Scripts/Calc.py