Art with Python Turtle

72
Art with Python Turtle

Transcript of Art with Python Turtle

Page 1: Art with Python Turtle

Art with Python

Turtle

Page 2: Art with Python Turtle

Announcement

� Homework 2 will be posted today after TA William’s tutorial.

Page 3: Art with Python Turtle

Learn

Iterations and recursions

Page 4: Art with Python Turtle

Python

Just a little bit more

Coding Basics

Credit: lecture notes modeled after http://www.openbookproject.net/thinkcs/python/english2e/index.html

Page 5: Art with Python Turtle

for Loop

http://www.pythontutor.com/index.html

Page 6: Art with Python Turtle

for iterating_var in sequence:____STATEMENTS

Syntax

Page 7: Art with Python Turtle

fruit = 'apple'for each_char in fruit: print each_char

Page 8: Art with Python Turtle

apple

Page 9: Art with Python Turtle

Range function

Page 10: Art with Python Turtle

A=range(10)B=range(2,7)C=range(0,10,2)D=range(-10, -30, -5)print Aprint Bprint Cprint D

Page 11: Art with Python Turtle

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][2, 3, 4, 5, 6][0, 2, 4, 6, 8][-10, -15, -20, -25]

Page 12: Art with Python Turtle

a = ['I', 'love', 'Python', 'programming']for i in range(len(a)): print i, a[i]

Page 13: Art with Python Turtle

0 I1 love2 Python3 programming

Page 14: Art with Python Turtle

while Statement

Page 15: Art with Python Turtle

while EXPRESSION:____STATEMENTS

Syntax

Page 16: Art with Python Turtle

def newyear_countdown(n): while n > 0: print n n = n-1 print "Happy New Year!"newyear_countdown(10)

Page 17: Art with Python Turtle

10987654321Happy New Year!

Page 18: Art with Python Turtle

def num_digits(n): count = 0 while n: count = count + 1 n = n / 10 return countprint num_digits(54320)print num_digits(int('012345'))print num_digits(int(23.45))print num_digits(23.45)

Page 19: Art with Python Turtle

552And an infinite loop

Page 20: Art with Python Turtle

def print_powers(n): i = 1 while i <= 6: print n ** i, '\t', i += 1 printprint_powers(2)

Page 21: Art with Python Turtle

2 4 8 16 32 64

Page 22: Art with Python Turtle

Lists

Page 23: Art with Python Turtle

List is an ordered set of values.It can contain mixed types.

Page 24: Art with Python Turtle

A = [1, 2, 3, 4]print AB = ["hello", "and", "good morning"]print BC = ["hello", 100, 'person', 2.5, [1, 2]]print C

Page 25: Art with Python Turtle

[1, 2, 3, 4]['hello', 'and', 'good morning']['hello', 100, 'person', 2.5, [1, 2]]

Page 26: Art with Python Turtle

empty = []print emptyprint 'this is[',empty,']'

Page 27: Art with Python Turtle

[]this is[ [] ]

Page 28: Art with Python Turtle

empty = []print emptyprint 'this is[',empty,']'print type(empty)

Page 29: Art with Python Turtle

[]this is[ [] ]<type 'list'>

Page 30: Art with Python Turtle

numbers = [1, 2, 3, 4, 5, 6]print numbers[0]print numbers[5]print numbers[-1]print numbers[-2]

Page 31: Art with Python Turtle

1665

Page 32: Art with Python Turtle

rainyday = ["today", "is", "a", "rainy","day"]

i = 0while i < len(rainyday): print rainyday[i] i += 1

#See the flow of the program

Page 33: Art with Python Turtle

todayisarainyday

Page 34: Art with Python Turtle

rainyday = ["today", "is", "a", "rainy","day"]

print "today" in rainydayprint 'Today' in rainydayprint 'is' in rainyday

Page 35: Art with Python Turtle

TrueFalseTrue

Page 36: Art with Python Turtle

a = [1, 2, 3]b = [10, 20, 30]c = a + bprint c

d = a[1]*bprint d

e = a*4print e

Page 37: Art with Python Turtle

[1, 2, 3, 10, 20, 30][10, 20, 30, 10, 20, 30][1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

Page 38: Art with Python Turtle

alphabet = ['a', 'b', 'c', 'd', 'e', 'f']print alphabet[1:3]print alphabet[:]print alphabet[0:1]print alphabet[0:8]

Page 39: Art with Python Turtle

['b', 'c']['a', 'b', 'c', 'd', 'e', 'f']['a']['a', 'b', 'c', 'd', 'e', 'f']

Page 40: Art with Python Turtle

Lists are mutable.

Page 41: Art with Python Turtle

pie = ["banana", "apple", "pear","strawberry"]pie[0] = "peach"pie[-1] = "chocolate"print pie

Page 42: Art with Python Turtle

['peach', 'apple', 'pear', 'chocolate']

Page 43: Art with Python Turtle

if []: print "empty"else: print "full"

# [] acts like 0 here

Page 44: Art with Python Turtle

full

Page 45: Art with Python Turtle

Lists are mutable, strings are not. Strings are immutable.

Page 46: Art with Python Turtle

Tuple is a sequence of items of any type.Tuple is immutable.

Page 47: Art with Python Turtle

my_tup = (1, 2, 3, 4, 5)print type(my_tup)print my_tup[0]my_tup[0] = 6 #Assignment is not supported

Page 48: Art with Python Turtle

<type 'tuple'>1TypeError: 'tuple' object does not support item assignment

Page 49: Art with Python Turtle

Recursion

Page 50: Art with Python Turtle

def recursive_sum(nested_num_list): sum = 0 for element in nested_num_list: if type(element) == type([]): sum = sum + recursive_sum(element) else: sum = sum + element return sum

print recursive_sum([1,2,3,4])

Page 51: Art with Python Turtle

Tail Recursion

Page 52: Art with Python Turtle

def newyear_countdown(n): if n == 0: print "Happy New Year!" else: print n newyear_countdown(n-1)newyear_countdown(5)

Page 53: Art with Python Turtle

54321Happy New Year!

#see the order of execution

Page 54: Art with Python Turtle

More Recursion Examples

Page 55: Art with Python Turtle

def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)print factorial(4)

Page 56: Art with Python Turtle

Fibonacci number

Credit: Wikipedia

Page 57: Art with Python Turtle

def fibonacci (n): if n == 0 or n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2)

print fibonacci(3)

Page 58: Art with Python Turtle

3

Page 59: Art with Python Turtle

Python Turtle Reviewhttps://trinket.io/python

Page 60: Art with Python Turtle

import turtlejohnny = turtle.Turtle()for i in range(0,4): johnny.forward(100) johnny.right(90)

https://trinket.io/python

Page 61: Art with Python Turtle

import turtledef draw_polygon(sides, length):

johnny = turtle.Turtle() for i in range(0,sides): johnny.forward(length) johnny.right(360/sides)

draw_polygon(4,20)

draw_polygon(6,20)

Credit: https://www.linuxvoice.com/issues/002/02drawing.pdf

Page 62: Art with Python Turtle

import turtledef draw_spiral(angle, length_start, length_increase, sides): for i in range(0,sides): johnny.forward(length_start+(i*length_increase)) johnny.right(angle)

johnny = turtle.Turtle() draw_spiral(30, 10, 2, 20)

Credit: https://www.linuxvoice.com/issues/002/02drawing.pdf

Page 63: Art with Python Turtle

import turtledef draw_petals(length, number): for i in range(0, number): johnny.forward(length) johnny.right(180-(360/number)) # number divisible by 360 johnny = turtle.Turtle() draw_petals(50,20)

Credit: https://www.linuxvoice.com/issues/002/02drawing.pdf

Page 64: Art with Python Turtle

Recursion with Python Turtlehttps://trinket.io/python

Page 65: Art with Python Turtle

import turtle

myTurtle = turtle.Turtle()myWin = turtle.Screen()

def drawSpiral(myTurtle, lineLen): if lineLen > 0: myTurtle.forward(lineLen) myTurtle.right(90) drawSpiral(myTurtle,lineLen-5)

drawSpiral(myTurtle,100) Credit: http://interactivepython.org/runestone/static/pythonds/index.html

Page 66: Art with Python Turtle

import turtle

def tree(branchLen,t): if branchLen > 5: t.forward(branchLen) t.right(20) tree(branchLen-15,t) t.left(40) tree(branchLen-15,t) t.right(20) t.backward(branchLen) Credit:

http://interactivepython.org/runestone/static/pythonds/index.html

Page 67: Art with Python Turtle

def main(): t = turtle.Turtle() myWin = turtle.Screen() t.left(90) t.up() t.backward(100) t.down() t.color("green") tree(75,t) myWin.exitonclick()

main() Credit: http://interactivepython.org/runestone/static/pythonds/index.html

Page 68: Art with Python Turtle

from turtle import *

def drawSnowFlake(length, depth): if depth > 0: for i in range(6): forward(length) drawSnowFlake(length // 3, depth - 1) backward(length) left(60)

drawSnowFlake(60,2)drawSnowFlake(60,3)

Page 69: Art with Python Turtle

William’s tutorial

on his Python Turtle

Page 70: Art with Python Turtle

Play with Python

labs on your own!

Page 71: Art with Python Turtle

thanks!

Any questions?

You can find me [email protected]

http://www.sci.utah.edu/~beiwang/teaching/cs1060.html

Page 72: Art with Python Turtle

Credits

Special thanks to all the people who made and released these awesome resources for free:� Presentation template by SlidesCarnival� Photographs by Unsplash