Copyright (c) Hyperion Entertainment and contributors.
Difference between revisions of "AmigaOS Manual: Python Getting Started"
(Added example Age.py) |
(Added Calc.py example and edited the Age.py introduction.) |
||
Line 26: | Line 26: | ||
== Age.py == |
== Age.py == |
||
− | This program displays a prompt for input and then reads the entered information. |
+ | This program displays a prompt for input and then reads the entered information. It also introduces you to the [[AmigaOS_Manual:_Python_Functions#str.28.29|str()]] function which converts an object to a string and a string concatenation operator (+) which joins two strings. |
=== Program 2. Age.py === |
=== Program 2. Age.py === |
||
Line 39: | Line 39: | ||
Python PYTHON:Scripts/Age.py |
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 2. Calc.py === |
||
+ | |||
+ | <syntaxhighlight lang="python"> |
||
+ | # 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.' ) |
||
+ | </syntaxhighlight> |
||
+ | |||
+ | Save this program as '''PYTHON:Scripts/Calc.py''' and run it with the command: |
||
+ | |||
+ | Python PYTHON:Scripts/Calc.py |
Revision as of 02:05, 9 January 2019
Contents
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 2. 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