A computer program typically consists of several statements. With Python, you can immediately execute single statements. This approach is a particularly good way to try out Python for the first time or test something out. To get started you have to click on the console icon, which opens the console window. On the command line that starts with >>>, you can type the instruction and then end it with the ENTER key (carriage return). As in any other ordinary editor, you can easily use the cursor keys to move back and forth on the command line and delete or insert single characters. As soon as you press ENTER the command line is executed, unless it is a multi-line command. In this case, the command is only executed after you press ENTER repeatedly.
You can mark already processed statements with the mouse, and copy it to the clipboard using Ctrl+C. You can paste the contents of the clipboard when you are on the command line using Ctrl+V.The underline symbol is a placeholder for the result of a previous calculation, using Cursor-Up you can get the last command and using Cursor-Left/Cursor-Right edit.
GETTING TO KNOW PYTHON
As you see, you can use whole numbers or decimals. The whole numbers are called integer (int) and the decimals are called float.
You can write several operations on a single line. Pay attention to the order of operations that applies, where * and / bind more strongly than + and -, and with operations of the same rank, the expression is evaluated from left to right. You can put the operations that belong together in parentheses. (Square and curly brackets have different meanings):
>>> (66 - 12) * 5 / 6
>>> 66 - 12 * 5 / 6
The integer division and the remainder (modulo operation) are also important:
>>> 5 // 3
>>> 5 % 3
Python can easily manage long numbers without a problem, for example with the use of the power operator:
There are a number of built-in functions, for example:
>>> max(1, 5, 2, 4)
Many other functions are available only after you import the respective modules. You can import in two ways. In the first way, you must precede a function with its module name followed by a dot. In the second way, you can call the function directly.
Here you can see that a computer program never calculates exactly, since sin(pi) would have to be exactly 0.
A succession of letters and punctuation marks is called string and you can define it by using single or double quotes. With the print command, you can write strings and other values to an output window. The comma is used as a separator.
>>> print "The result is", 2
Produces in the output window: The result is 2
As in mathematics, you can assign values to variable names. To do this, use an identifier of one or more letters. Some characters are not allowed such as blank spaces, umlauts, accents and most other special characters. One benefit of using variables is to achieve a previously calculated result faster. Quite conveniently, the already known variables are listed in the right section of the console window.
>>> a = 2
Produces in the output window: : The Sum von 2 and 3 is 5
A one-dimensional collection of arbitrary data is called a list. Lists are a highly convenient and flexible data type in all programming languages. In Python you simply write list items in square brackets and you can also display them in the output window by calling print.
>>> li = [1, "chicken", 3.14]
In the output window: [1, "chicken", 3.14]
Lists and many other objects have associated functions which are called methods. For example, you can add a new element to the end of the list with the method append().
You can also define your own functions. For this purpose, you will use the keyword def. You can return values using return. After you define it, you can call your function as you would with any other built-in function:
>>> def sum(a, b):
GIVING THE TURTLE COMMANDS
It might be fun to try some more turtle commands on your own. You can find an overview of the commands in the chapter Turtle Graphics under dokumentation. In that chapter you will also systematically learn how to write entire programs.