csc 1010 lecture 6
play

CSC 1010 Lecture 6 What do we know so far? Class lecture, lab, - PDF document

CSC 1010 Lecture 6 What do we know so far? Class lecture, lab, Rephactor, Quick Checks, R&R, easter eggs Solve problems, computers useful, user vs. programmer CSC 1010 Programming for All Sequence of instructions,


  1. CSC 1010 Lecture 6 What do we know so far? • Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs • Solve problems, computers useful, user vs. programmer • CSC 1010 Programming for All Sequence of instructions, algorithm is step‐by‐step • Python is 3 rd 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 Lecture 6 • Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or • Input function, comparing strings, programming conventions Loops, Functions & Turtles • 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 1 2 Loops & Turtle Graphics Loops & Turtle Graphics Loops are ideal for repeating the same thing over and over. Repetition allows you to create more complex shapes easily: This program draws the 5 points of a star: i m por t t ur t l e i m por t t ur t l e t ur t l e. pencol or ( " gr een" ) f or count i n r ange( 5) : t ur t l e. f or war d( 100) f or i i n r ange( 41) : t ur t l e. r i ght ( 144) 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( ) t ur t l e. hi det ur t l e( ) To make the result clean, hide Experimentation is the best the turtle using hideturtle. way to discover new designs. 3 4 3 4 Other Design Ideas Other Design Ideas Spirals 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) The key piece is a nested loop… See the textbook for a variety of a for loop inside another for loop approaches and code examples 5 6 5 6 1

  2. CSC 1010 Lecture 6 Using Randomness Turtle Graphic Functions i m por t t ur t l e def dr aw_t wo_ci r cl es( r adi us) : i m por t r andom t ur t l e. hi det ur t l e( ) t ur t l e. ci r cl e( r adi us) f or i i n r ange( 20) : t ur t l e. penup( ) i f ( r andom . choi ce( [ Tr ue, Fal se] ) ) : t ur t l e. got o( r adi us * 2, 0) t ur t l e. r i ght ( 30) t ur t l e. pendown( ) el se: t ur t l e. ci r cl e( r adi us) t ur t l e. l ef t ( 30) t ur t l e. f or war d( 40) A function encapsulates a t ur t l e. st am p( ) reusable shape or design. Using random values can make computer generated graphics more interesting and unique. Explore The goto function moves methods in the random module. the turtle to a X, Y position 7 8 7 8 Honeycomb Example Honeycomb Example Step 1: Create reusable functions that draw an individual shape Step 2: Create a function that repeatedly calls the first function # Dr aws a honeycom b shape. def dr aw_hexagon( ) : def dr aw_honeycom b( ) : f or count i n r ange( 6) : f or dr aw_honeycom b i n r ange( 6) : t ur t l e. f or war d( 50) dr aw_hexagon( ) t ur t l e. l ef t ( 60) t ur t l e. f or war d( 50) t ur t l e. r i ght ( 60) 9 10 9 10 Honeycomb Example How to Approach Creating Designs • Look for examples to inspire new ideas. Step 3: Set color and pen size, and call draw_hexagon. • Invent ideas of your own. # M ai n pr ogr am t ur t l e. penup( ) • Reuse ideas that you see with your own wrinkles. t ur t l e. got o( - 35, 50) t ur t l e. pendown( ) • Think Functions! Break up code into smaller units. t ur t l e. pencol or ( ' gol d' ) • Think Loops! Use for or while loops to repeat things. t ur t l e. pensi ze( 7) dr aw_honeycom b( ) • Break up your idea into smaller pieces. t ur t l e. hi det ur t l e( ) • Create a function for each piece. 11 12 11 12 2

  3. CSC 1010 Lecture 6 Turtle Graphics Reference Random Numbers A random number generator is an object that produces a stream Check on the comprehensive Turtle Graphics Reference topic of pseudorandom numbers • Creating Turtles • Basic Movement They are based on a seed value that factors into a set of • Pen Control calculations • Dots and Circles To the user, they certainly appear random • Filling Shapes • Turtle Appearance The mechanism for generating random numbers in Python is the • Stamps random module • Getting the Turtle's State • Miscellaneous They are called pseudorandom because they are calculated using • Environment Control a complex formula… so they only appear to be random! 13 14 13 14 Random Numbers Random Numbers The random module has several methods for generating The arguments to randrange can also determine the full range random numbers of values, similar to the range function The randrange method accepts an argument N and returns an The first argument is the start of the range, while the second integer in the range 0 to N‐1 argument is 1 greater than the end of the range. i m por t r andom i m por t r andom num = r andom . r andr ange( 1, 51) num = r andom . r andr ange( 10) This call to randrange returns a value between 1 and 50 The variable num now contains a single integer between 0 and 9, inclusive There's an equal probability of getting any value in that range 15 16 15 16 Random Numbers Random Numbers When there is one argument Y, values go from 0 to Y – 1. With The seed value (an integer) can be set for a random object two arguments, X and Y, values go from X to Y – 1. using the seed method Expression Range i m por t r andom r andom . seed( 546721) r andom . r andr ange( 100) 0 t o 99 r andom . r andr ange( 256) 0 t o 255 The seed value determines exactly the stream of r andom . r andr ange( 1, 7) 1 t o 6 numbers that will be produced r andom . r andr ange( 100, 120) 100 t o 119 r andom . r andr ange( - 10, 40) - 10 t o 39 If you don't call seed, the current time (in milliseconds) r andom . r andr ange( - 50, - 40) - 50 t o - 41 is automatically used instead There are many other functions in the random module to generate random values and choices, and to shuffle lists. 17 18 17 18 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend