CSC 1010 Lecture 6 1
CSC 1010 Programming for All
Lecture 6 Loops, Functions & Turtles
What do we know so far?
- Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs
- Solve problems, computers useful, user vs. programmer
- Sequence of instructions, algorithm is step‐by‐step
- Python is 3rd most popular language, core principles
- Syntax, runtime, & logic errors, testing & debugging
- Hardware vs. software
- control flow – step‐by‐step, function call, conditional, loop
- IDLE shell, editor, install Python, Hello World
- Intrepreter, compiler, Python Standard Library
- Variables, assignment, numeric expr., precedence
- Print function, Strings, concatenation, indexes, in, *
- Interactive programs, if, if‐else, if‐elif‐else, int, float
- Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or
- Input function, comparing strings, programming conventions
- variable & function names lowercase, CONSTANTS, indent
- while, for, range, augmented assignments, palindromes
- Turtle Graphics, forward, left, right, pensize, pencolor, dot, circle
- goto, penup, pendown, fillcolor, begin_fill, end_fill, speed
- calling & defining functions, import, parameters vs. arguments, return
- positional args, default args, variable args, keyword args, local variables
- string methods, replace, method vs. function, built‐in & external functions
2
Loops & Turtle Graphics
Loops are ideal for repeating the same thing over and over. This program draws the 5 points of a star:
3
i m por t t ur t l e f or count i n r ange( 5) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 144) t ur t l e. hi det ur t l e( )
To make the result clean, hide the turtle using hideturtle.
Loops & Turtle Graphics
Repetition allows you to create more complex shapes easily:
4
i m por t t ur t l e t ur t l e. pencol or ( " gr een" ) f or i i n r ange( 41) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 123) t ur t l e. hi det ur t l e( )
Experimentation is the best way to discover new designs.
Other Design Ideas
5
Spirals
See the textbook for a variety of approaches and code examples
Other Design Ideas
6
Flowers
f or count 1 i n r ange( 10) : f or count 2 i n r ange( 2) : t ur t l e. f or war d( 100) t ur t l e. r i ght ( 60) t ur t l e. f or war d( 100) t ur t l e. r i ght ( 120) t ur t l e. r i ght ( 36)