73
More Coding Basics with Some Turtle

More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

More Coding Basics

with Some Turtle

Page 2: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Announcement

� Homework 2 will be posted this Thursday. Due dates will be adjusted. � Missing quiz policy: no quiz submissions will be accepted late, except

in the case of a documented medical emergency.� Since we drop a quiz grade, there is NO make-up quiz unless there is

a medical emergency.� If you have situations where you suspect you will miss the quiz

regularly (e.g. military service), please discuss with the instructor via writing, supporting documents are required.

� If you do miss a quiz, you will have other opportunities to earn points throughout the class

Page 3: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Learn to program = Learn to solve problems

Page 4: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Python

More Coding Basics

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

Page 5: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

What is a function?

Page 6: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Def FUNCTIONNAME(parameters):____STATEMENTS

Syntax

Indentation (4 empty spaces)

header

body

Page 7: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

User-defined Functions

Page 8: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def new_line(): print print "The weirdest movie at Sundance this year is:"new_line()print "Swiss Army Man."

Page 9: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

The weirdest movie at Sundance this year is:

Swiss Army Man.

Page 10: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def new_line(): print print "The weirdest movie at Sundance this year is:"new_line()new_line()print "Swiss Army Man."

Page 11: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

The weirdest movie at Sundance this year is:

Swiss Army Man.

Page 12: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

print ' * 'print ' *** 'print ' ***** 'print '*******'for h in range(0,5): print '| |'print '-------'

Page 13: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted
Page 14: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def print_house(height): print ' * ' print ' *** ' print ' ***** ' print '*******' for h in range(0,height): print '| |' print '-------'print_house(2)

Page 16: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

A Function can be called repeatedly.Function can call another function.

Page 17: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def new_line(): print def print_house(height): print ' * ' print ' *** ' print ' ***** ' print '*******' for h in range(0,height): print '| |' new_line() print '-------'print_house(2)

Page 18: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted
Page 19: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Advantages of using a function:� Group complex computations� Smaller code for repetitive ops

Page 20: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Flow of Executionhttp://www.pythontutor.com/index.htmlForward bottom will show step by step execution of the program

Page 21: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Build-in Functions

Page 22: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

https://docs.python.org/2/library/functions.html

Page 23: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

print abs(5)print abs(-5)print pow(2,3)print max(2,3,8,1)

Page 24: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

5588

Page 25: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Functions can be composed.

Page 26: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

print abs(max(-1,-2,-5))print max(abs(-1),abs(-2),abs(-5))

Page 27: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

15

Page 28: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Local variables are only defined inside the function.

Page 29: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def print_house(height): print ' * ' print ' *** ' print ' ***** ' print '*******' for h in range(0,height): print '| |' print '-------' print type(height)print_house(2)

Page 30: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def print_house(height): print ' * ' print ' *** ' print ' ***** ' print '*******' for h in range(0,height): print '| |' print '-------'print_house(2)print type(height)

Page 31: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Modulus operator: %

Page 32: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

remainder = 11%2print remainderright_most_digit = 123%10print right_most_digit

Page 33: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

13

Page 34: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Conditionals

Page 35: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Boolean Values and ExpressionsTrue, False

==, !=, >=, <=, >, <

Page 36: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

print type(True)print 5==5print 4==5

Page 37: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

<type 'bool'>TrueFalse

Page 38: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x=4y=5print x!=yprint x>yprint x<yprint x>=yprint x<=y

Page 39: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

TrueFalseTrueFalseTrue

Page 40: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Boolean Values and Expressions

Page 41: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x=4y=5print (x!=y) and (x>y)print (x!=y) or (x>=y)print not(x==y)

Page 42: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

FalseTrueTrue

Page 43: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

If Statement

Page 44: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

IF BOOLEAN EXPRESSION:____STATEMENTS

Syntax

Indentation (4 empty spaces)

header

body

Page 45: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x = 4if x>0: print "x is positive."

Page 46: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

TrumpIsPresident = Trueif TrumpIsPresident print "What would you do?"

Page 47: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

if BOOLEAN EXPRESSION:____STATEMENTSelse:____STATEMENTS

Syntax

Page 48: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x = -4if x>0: print "x is positive." else: print "x is not positive."

Page 49: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x = 4if x%2==0: print x, "is even." else: print x, "is odd."

Page 50: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

if TrumpBecomesPresident print "The republicans win." else print “The democrats might win.”

Page 51: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

if BOOLEAN EXPRESSION:____STATEMENTSelif BOOLEAN EXPRESSION:____STATEMENTSelse:____STATEMENTS

Syntax: Chained Conditionals

Page 52: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x = 0if x>0: print "x is positive." elif x<0: print "x is negative."else: print "x is zero."

Page 53: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Nested Conditionals

Page 54: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

x = 2y = 3if x==y: print x," equals ", yelse: if x<y: print x, " less than ", y else: print x, " is bigger than ", y

Page 55: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

TrumpBecomsePresident=FalseHillaryBecomesPresident=Falseif TrumpBecomsePresident: print "The republicans win." else: if HillaryBecomesPresident: print "The Democrats win." else: print "Some independent wins."

Page 56: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Return Statement

Page 57: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Terminates the execution of a function before it reaches the end

Page 58: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def print_square_root(x): if x <= 0: print "Invalid input." return

result = x**0.5 print "The square root of", x, "is", resultprint_square_root(-8)

Page 59: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Type Conversion

Page 60: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

print int("123")print int(1.25)print float(2.5)print float("2.6")print str("12345")

Page 61: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Function that Returns

Page 62: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

def absolute_value(x): if x < 0: return -x else: return xprint absolute_value(-5)

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

Page 63: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

For Loop

Page 64: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

for iterating_var in sequence:____STATEMENTS

Syntax

Page 65: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Python Turtle

Page 66: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

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

https://trinket.io/python

Page 67: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

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

Page 68: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

import turtledef run_in_square(x,y): johnny = turtle.Turtle() johnny.hideturtle() johnny.setpos(x, y) print johnny.position() for i in range(0,4): johnny.forward(10) johnny.right(90)run_in_square(10,10)run_in_square(20,20)for i in range(1,5): run_in_square(i*10,i*10)

Page 69: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

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 70: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

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 71: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Play with Python

labs on your own!

Page 72: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

thanks!

Any questions?

You can find me [email protected]

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

Page 73: More Coding Basics with Some Turtle · Announcement Homework 2 will be posted this Thursday. Due dates will be adjusted. Missing quiz policy: no quiz submissions will be accepted

Credits

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