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.
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
.
Now, you can execute the program.
F5
.Using command line, change to the directory where graph.py
resides and write:
python3 graph.py
Explain what the following program does:
import turtle
strecke = 100
turtle.forward(strecke)
turtle.left(90)
strecke = strecke - 50
print(strecke)
turtle.forward(strecke)
Draw a square:
Draw a square:
Draw the home of Nikolaus. The square root can be calculated with the module math
:
import math
vier = math.sqrt(16)
Now, we will add the keyboard as input to the program.
What does this short program?
name = input("What is your name? ")
print(name)
Which input
commands are correct?
a = input()
a = input("enter a number.")
a = input(enter your name)
a = input(3)
Which print
commands are correct?
print("9" + "9")
print "nine"
print(str(9) + "nine")
print(9 + 9)
print(neun)
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)