GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing opentechschool.github.io — Variables — Introduction to Programming with Python Variables¶Introduction¶Whew. Experimenting with the angles requires you to change three different places (numbers) in the code each time. Imagine you’d want to experiment with all of the squares’ sizes, or with with rectangles! Fortunately there are easier ways to do so than changing lots of numbers every time. This is where variables come into play: You can tell Python that from now on, whenever you refer to a variable, you actually mean something else. That concept might be familiar from symbolic maths, where you would write: Let x be 5. Then x * 2 will obviously be 10. In Python syntax, that very statement translates to: x = 5 After that statement, if you do turtle.forward(x) Variables can store all sorts of things, not just numbers. A typical
other thing you want to have stored often is a string - a piece of text.
Strings are indicated with a starting and ending You can even use a variable to refer to the turtle by a name: timmy = turtle Now every time you type timmy.forward(50) timmy.left(90) turtle.forward(50) A variable called angle¶Exercise¶If we make a variable called Solution¶tilt = 20 turtle.left(tilt) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.left(tilt) ... and so on! Bonus¶Can you apply that principle to the size of the squares, too? The house of santa claus¶Exercise¶Draw a house.
Hint You can calculate the length of the diagonal line with the Pythagorean
theorem. That value is a good candidate to store in a variable.
To calculate the square root of a number in Python, you’ll need to import
the math module and use the import math c = math.sqrt(a**2 + b**2) |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |