deutsch     english    français     Print

 

1.2 FIRST STEPS

 

 

INTRODUCTION

 

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

 

You can vary the following proposals as you wish, how ever you might find them to be more interesting or fun. Start TigerJython and select the Console button.

Start by typing the examples below to get to know the four basic arithmetic operations:

 
>>> 4 + 13
    17
>>> 2.5 - 5.7
    -3.2
>>> 1356 * 22345
    30299820
>>> 1 / 7
    0.14285714285714285

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
    45.0

>>> 66 - 12 * 5 / 6
    56.0

The integer division and the remainder (modulo operation) are also important:

>>> 5 // 3
    1

>>> 5 % 3
    2

Python can easily manage long numbers without a problem, for example with the use of the power operator:

>>> 45**123
    22138041353571795138171990088959838587798501812515796
    35495262099494113535880540560608088894435720496058262
    03407737866682728901508127084151522949268748976128137
    6128136645054322872994134741020388901233673095703125L

There are a number of built-in functions, for example:

>>> abs(-9)
    9

>>> max(1, 5, 2, 4)
    5

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.

>>> import math
>>> math.pi
    3.141592653589793
>>> math.cos(pi)
    -1
>>> from math import *
>>> pi
    3.141592653589793
>>> sin(pi)
    1.2246467991473533e-16

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
>>> b = 3
>>> sum = a + b
>>> print "The sum of", a, "and", b, "is", sum

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]
>>> print li

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().

>>> li.append("egg")
>>> print li

In the output window: [1, 'chicken', 3.14, 'egg']

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):
>>>     return a + b
>>> sum(2, 3)

     5

 

 

GIVING THE TURTLE COMMANDS

 

The console is very useful for quickly trying out a few commands or functions. For example, if you want to familiarize yourself with turtle graphics, first import the module gturtle and then create a window with a turtle in it using the command makeTurtle().

>>> from gturtle import *
>>> makeTurtle()


Afterwards, you have all the commands of turtle graphics at your disposal. For example:

 


forward(100) short: fd(100) Move 100 steps (pixels) forward
back(50) short: bk(50) Move 50 steps backwards
left(90) short lt(90) Rotate 90° to the left
right(90) short: rt(90) Rotate 90° to the right
clearScreen() short: cs() Delete all traces and place the turtle in the middle


Example:

>>> fd(100)
>>> dot(20)
>>> rt(90)
>>> fd(100)
>>> dot(20)
>>> home()

 

 

With the keyword repeat you can execute one or more statements repeatedly. If you want to repeat a series of commands as a command block, you have to indent them by the same amount.

>>> repeat 4:
       fd(100)
       rt(90)

 

 

As shown above, you can combine several statements under their own name by defining your own function. The main advantage of functions is that you can call them by their name as often as you would like, instead of writing down their code in its entirety each time.

>>> def drawSquare():
      repeat 4:
         fd(100)
         rt(90)

>>> drawSquare()
>>> rt(180)
>>> drawSquare()

 

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.