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
Python for Beginners
2015.10.02
Variables¶Introduction¶Whew. Experimenting with the angles requires you to change three different places in the code each time. Imagine you’d want to experiment with all of the square sizes, let alone with rectangles! We can do better than that. 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 line of text.
Strings are indicated with a starting and a leading You can even use a variable to give the turtle 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 have a variable called Solution¶angle = 20 turtle.left(angle) 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(angle) ... 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.
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. |