Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
Yr 9 a computer science Nice to meet you variable challenge! 1.Variable assessment Looping in python 2.Looping assessment Drawing in python using turtle.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
For loops in programming Assumes you have seen assignment statements and print statements.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Sequences CMSC 120: Visualizing Information 2/26/08.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
See Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Graphics CIS 40 – Introduction to Programming in Python
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Repetition: Definite Loops
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CS190/295 Programming in Python for Life Sciences: Lecture 6
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Module 2 Lesson 3 Over and Over Again
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Repetition: Definite Loops
Repetition: Definite Loops
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Assignment 2 sample solution is posted.
CISC101 Reminders Assignment 2 due today.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Assignment 2 due this Friday.
Introduction to Computer Science
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Module 2 Lesson 3 Over and Over Again
CMPE212 – Reminders Assignment 2 due today, 7pm.
Module 2 Lesson 3 Over and Over Again
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Presentation transcript:

Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1

The Other Loop… The other kind of looping syntax uses the for keyword. It can be more powerful, particularly with collections – producing very compact code. But you could almost always use a while loop instead – it would just take more code to carry out the same operation. We’ll make more use of the for loop later in the course. CISC101 - Prof. McLeod2Winter 2016

CISC101 - Prof. McLeod3 The for Loop Syntax: for variable_name in iterable : You make up variable_name iterable can be a collection, such as a string, list, tuple, set or dict. It can also be an object produced by certain BIFs such as reversed() or range(). It can even be a file! Winter 2016

CISC101 - Prof. McLeod4 The for Loop, Cont. It is still interchangeable with a while loop. These two “snippets” act in the same way: testString = "I like watching videos!" i = 0 while i < len(testString) : print(testString[i]) i = i + 1 for aChar in testString : print(aChar) Winter 2016

Some New Syntax Two new things snuck their way into the code on the previous slide: len( ) BIF is used with collections and gives you the number of elements in the collection. For a string, it gives you the length of the string. The square brackets: [ ] is called the “slice operator” and returns a single element (or character) or even a range of elements or characters from a collection. CISC101 - Prof. McLeod5Winter 2016

The range() BIF This BIF generates an iterable and is often used with a for loop. Remember our while loop code to display 1 to 20?: i = 1 while i < 21 : print(i) i = i + 1 See RangeForLoopsDemo.py Winter 2016CISC101 - Prof. McLeod6

Aside - Slice Operator It is placed after a collection (a list, tuple, string, etc.) or a variable that is a collection type. Used as in: [ # ] or [ # : # ] The first way gives you the element at the location specified. The other way gives you all elements between the first location and one less than the last location. We will look at this operator again later. Winter 2016CISC101 - Prof. McLeod7

Turtle Graphics The “Turtle” is an easy way of drawing to the screen. One use is to illustrate how loops work. See section 24.1 in the Python Standard Library. To start you’ll need to import the turtle module: from turtle import * CISC101 - Prof. McLeod8Winter 2016

A Few turtle Commands To make the turtle look like a “turtle”: shape(“turtle”) To set the line thickness and line colour: pensize(5) pencolor(“green”) The turtle starts out at the “home” position, (0, 0), facing right or “east”. To draw, just issue commands to move the turtle. CISC101 - Prof. McLeod9Winter 2016

A Few More turtle Commands Turning: right(num_degrees)# clockwise left(num_degrees)# counter-clockwise Moving in the direction he is pointing; forward(length) Changing speed: speed(5)# 0 is no animation, 1 slow 10 fastest See TurtleDemo1.py CISC101 - Prof. McLeod10Winter 2016

Filling Commands Use begin_fill(), then draw, followed by end_fill() to fill a closed, drawn area. See TurtleDemo2.py See TurtleDemo3.py to see how to draw a filled circle. CISC101 - Prof. McLeod11Winter 2016

Regular Polygons Modify how circle() is invoked to draw regular polygons instead of circles. See TurtleDemo4.py Draw text, too! See TurtleDemo5.py Fun! CISC101 - Prof. McLeod12Winter 2016

Turtle Loop Demos The turtle can use loops to draw many similar figures one after the other without a lot of extra code. These demos will show loops, nested loops and the use of conditionals inside loops. CISC101 - Prof. McLeod13Winter 2016

while Loop Demo Draw 10 squares with turtle, one inside the other. See TurtleLoopDemo1.py In essence: size = 40 while size <= 400 : # drawing stuff size = size + 40 CISC101 - Prof. McLeod14Winter 2016

Nesting Loops, Example A loop is useful in preventing repetitious coding practice. Can you see any part of the previous example that is repetitive? How would you fix it? See TurtleLoopDemo2.py –Every time the outer loop iterates once, the inner loop iterates 3 times. CISC101 - Prof. McLeod15Winter 2016

Nesting Loops, Example Cont. See TurtleLoopDemo2Alt.py Uses goto(x, y) to move to a corner of the square. Uses setheading(angle) to make the turtle point South. Inner loop iterates four times now. More compact code. Winter 2016CISC101 - Prof. McLeod16

Turtle Squares Again Alternate red and blue squares. See TurtleLoopDemo3.py –Shows an if statement inside the loop. –Every time the outer loop iterates once, the if statement executes once. How can you change the code to have just the top side of the red square drawn in green? Draw the bottom side of the blue square in green? CISC101 - Prof. McLeod17Winter 2016