Writing programs in Python

Writing statements with IPython is fine for test purposes. Writing more complex programs command by command is tedious. To program efficiently, we will have to save statements as programs, so that we can execute them later.

A program consists of more than one line which are executed in one go.

Here, you will learn to create a first program. Exercises 1 - 9 are meant to be run in PyCharm, Spyder or any other interactive Python environment.

Exercise 10 can be submitted.

Exercise 1

Open an editor (so no IPython console) and create an empty file. Add the following:

import turtle

turtle.forward(100)
turtle.left(50)
turtle.forward(100)

Save the file as graph.py.

Exercise 2

Now, you can execute the program.

Exercise 3

Explain what the following program does:

import turtle

strecke = 100
turtle.forward(strecke)
turtle.left(90)
strecke = strecke - 50
print(strecke)
turtle.forward(strecke)

Exercise 4

Draw a square:

Exercise 5

Draw a square:

Aufgabe 6

Draw the home of Nikolaus. The square root can be calculated with the module math:

import math

vier = math.sqrt(16)

Using the keyboard

Now, we will add the keyboard as input to the program.

Exercise 7

What does this short program?

name = input("What is your name? ")
print(name)

Exercise 8

Which input commands are correct?

Exercise 9

Which print commands are correct?

Exercise 10

Write a program where the user can determine the size of the square. Use the following snippet:

number = int(input("Enter a number: "))
print(number)