0% found this document useful (0 votes)
2 views36 pages

Python Turtle Workshop - Maputo

The Python Workshop at the Technical University of Dresden is designed for programming beginners, focusing on using the Python Turtle framework to create graphics and animations. Participants will learn fundamental programming concepts, basic commands, and how to use loops and functions to optimize their code. The workshop includes hands-on tasks to reinforce learning, with resources provided for further exploration.

Uploaded by

maxa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views36 pages

Python Turtle Workshop - Maputo

The Python Workshop at the Technical University of Dresden is designed for programming beginners, focusing on using the Python Turtle framework to create graphics and animations. Participants will learn fundamental programming concepts, basic commands, and how to use loops and functions to optimize their code. The workshop includes hands-on tasks to reinforce learning, with resources provided for further exploration.

Uploaded by

maxa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Amelie Wagner

Technical University of Dresden

Python Workshop:
Introduction to Programming
Generating Art with the Python Turtle-Framework
Content
of this workshop

For whom is this course?


- Programming Beginners
→ no prior programming knowledge needed

Learning Outcomes
- Understand fundamental concept of programming
- Ability to create own graphics / animations in python
- Not a security-workshop (will happen tomorrow)

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 2
General Idea:
Move turtle with python-Code

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 3
General Idea:
Repeat simple moves → Advanced shapes

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 4
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 5
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 6
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 7
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 8
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 9
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 10
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 11
Turtle
How it works

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 12
Necessary Software

- Lab Computer (Ubuntu or Windows): Python IDLE

- Your own Laptop:


Any Code Editor + python needs to be installed
Example Editors: PyCharm, sublime text, Visual Studio, Intellij…)

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 13
Basic Commands
Prepare the Canvas

- Printing text
print(‘hello’)

Starting with Turtle:

- Import turtle framework:


from turtle import *

- Initiate the turtle:


my_turtle = Turtle()

- Stop the canvas from closing immediately:


done()

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 14
Basic Commands
Practise

Draw a simple line:


my_turtle.forward(100)

Change line color:


my_turtle.color(‘blue’)

Change background color:


bgcolor(‘black’)

Change pen size:


my_turtle.width(10)

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 15
Basic Commands

Draw a simple line: Change the color:


my_turtle.forward(100) my_turtle.color(‘red’)

Turn around corner: Possible colors (for example):


my_turtle.right(90) red, purple, green, orange, blue,
yellow, pink, cyan, black …

Task 1: Create a square

Task 2: Create square with 4 different colors

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 16
Optimizing
using for() loop

● Repeating the same Code several times (increase efficiency):


for() loop

● for() loop executes the line of code inside loop repeatedly


● range(5) defines the number of iterations it is repeated (example here: 10 times)
● for number in range(): the counter of current iteration is saved in number

for _ in range(5):
print(‘Hello’)

for number in range(5):


print(‘This is the current iteration:’, number)

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 17
Optimizing Square
using for() loop

Task 3: Draw simple square by using for() loop

Hints:

my_turtle.forward(100)
my_turtle.right(90)

for _ in range(4):
# write your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 18
Task 4
Introducing Python lists (prerequisite for drawing colorful square)

Generate a Python list:


list = [‘mambo’, ‘poa’]

Print complete list:


print(list)

Print first element of list:


print(list[0])

Print second element of list:


print(list[1])

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 19
Task 4
Draw colorful square by using for() loop

Hints:
my_turtle.forward(100)
my_turtle.right(90)
my_turtle.color(‘blue’)

Python List:
list = [‘red’, ‘blue’, ‘green’, ‘yellow’]

for number in range(4):


print(list[number])
# write your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 20
More useful commands
for changing behaviour of turtle

Changing the drawing speed:


my_turtle.speed(...)

Change the size of triangle / turtle itself:


my_turtle.turtlesize(0.1, 0.1) # make triangle smaller
my_turtle.turtlesize(4, 4) # make triangle bigger

Clear the screen/canvas after drawing:


my_turtle.clear()

Add comments to your Code with #:


# this is a comment. It will not be executed

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 21
Task 5
Draw advanced square
Hints:

- Change pen size:


my_turtle.width()

- Forward turtle:
my_turtle.forward()

- Change direction:
my_turtle.right()

Start writing your Code like this:

for number in range(15):


# write your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 22
Task 6
Draw colorful advanced square

Modulo operator: % → remainder of a division

Modulo: x1 % x2 = y → Result: y < x2

for number in range(15)


print(‘current number/iteration:’, number)
print(‘result of number % 4:’, number % 4)

→ In this example:
If number is getting bigger than 4, number is getting “cut off” to a range in between [0-3]

→ use this information for coloring the square with > 4 iterations with only 4 colors

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 23
Task 6
Draw colorful advanced square

Hints:
Change color of turtle:
my_turtle.color()

Use same code as for black advanced square, add 4 colors using modulo % operator
Remember: The for loop has more than 4 iterations, but you have to use 4 colors repeatedly
→ use % operator

Start your Code like this:

colors = ['red', 'blue', 'green', 'orange']


for number in range(15):
# add new code for colors here
# reuse code for black advanced square here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 24
Introducing Functions
to structure your Code

Until now: Code is executed consecutively, all the Code is executed


→ not useful if you want to use only a part of your Code (draw only one turtle at a time)
This is why we use functions to structure the Code. This is an example how to write a function:
def my_function():
print("Hello from a function")

! It is important to indent the Code inside the function using tab


The Code in this function is not executed automatically, you have to call the function.
We do this by typing just the name of the function:
my_function()

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 25
Introducing Functions
Example:
# we define the function here
def my_function():
print("Hello from a function")
# we call the funcion -> the code inside the function will be executed:
my_function() # just type name of the function including brackets ()

Explanation:
my_function() = Name of function (you can choose any name)
def = keyword, so the interpreter knows this is a function (not a variable, etc.)
print(...) = example content of the function, put your content (for example turtle) here
my_function() = function call, the code in the function will be executed at this moment

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 26
Introducing Functions
to structure your Code

def function1():
print("Hello from function1!")

def function2():
print("Hello from function2!!")

# we always call the functions after defining them:

function1() # code of function1 will be executed


function2() # code of function2 will be executed
# exchange the function calls and see what happens:
function2()
function1()

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 27
Put your Turtle Code into functions:

from turtle import * # import framework always once start of your program
my_turtle = Turtle() # initialize my_turtle Object once

def draw_square():
for _ in range(4):
my_turtle.forward(100)
my_turtle.right(90)

def draw_line():
my_turtle.width(10)
my_turtle.forward(100)

# now you can call the function/turtle you want, for example:
draw_square()

done()

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 28
Task 7
Draw colorful lines
Functions necessary:
my_turtle.color(..)
my_turtle.width(..)
my_turtle.forward(..)
my_turtle.left(...)

Start your Code like this:


colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
for number in range(360):
# put your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 29
Task 8
Draw shell-art

Functions necessary:
# draw a simple circle
# with diameter 50:

turtle.circle(50)
turtle.left(..)

Start your Code like this:


for number in range(30):
# put your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 30
Thank you for attending the workshop!

If you have any questions, you can contact me here:

[email protected]

www.github.com/amyy

https://www.linkedin.com/in/amelie-w/

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 31
Sources
that I used as inspiration for this workshop:

Python Turtle documentation:


https://docs.python.org/3/library/turtle.html

Colorful lines + shell-art:


https://www.sharptutorial.com/python-home/turtle-in-python/

Moreover, I asked ChatGPT for some exercise ideas, it gave me the idea for the black advanced
square (I had to modify it though):
https://openai.com/chatgpt

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 32
Task …
Draw square with repeating colors by using
for() loop + modulo operator

Python List:
list = [‘red’, ‘green’]

for number in range(4)


print(list[number % 2])
# write your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 33
Task …
Draw square with repeating colors by using
for() loop + modulo operator

Hints:
my_turtle.forward(100)
my_turtle.right(90)
my_turtle.color(‘blue’)

Python List:
list = [‘red’, ‘green’]

for number in range(4)


print(list[number % 2])
# write your code here

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 34
Draw triangle

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 35
Task …

- Maybe also add this next time -

Programming Introduction Workshop - Python Turtle Framework


M.Sc. Amelie Wagner - [email protected] Slide 36

You might also like