Python For Kids (Level1-Level 2) - 2nd Week
Python For Kids (Level1-Level 2) - 2nd Week
Python For Kids (Level1-Level 2) - 2nd Week
Chapter 3.
Numbers and Variables: Python Does the Math
Variables: Where We Keep Our Stuff
In Chapter 1 and Chapter 2, we used a few variables (you might remember name from our
first program in Chapter 1 or x and sides from Chapter 2). Now let’s look at what variables really
are and how they work.
A variable is something you want the computer to remember while your program is
running.
my_name = "Bryson"
Let’s try a program using some variables. Type the following code in a new IDLE window
and save it as ThankYou.py.
ThankYou.py
my_name = "Bryson"
my_age = 43
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")
print("Thank you for buying my book,", your_name, "!")
Figure 3-1. A program with four variables and the output it creates
Python Numbers
The two primary types of numbers in Python are called integers (whole numbers, including
negatives, like 7, -9, or 0) and floating-point numbers (numbers with decimals, like 1.0, 2.5,
0.999, or 3.14159265).
Python Operators
The math symbols like + (plus) and - (minus) are called operators because they operate, or
perform calculations, on the numbers in our equation. When we say “4 + 2” aloud or enter it on our
calculator, we want to perform addition on the numbers 4 and 2 to get their sum, 6.
Python uses most of the same operators that you would use in a math class, including +, -,
and parentheses, (), as shown in Table 3-1. However, some operators are different from what you
may have used in school, like the multiplication operator (the asterisk, *, instead of ×) and the
division operator (the forward slash, /, instead of ÷). We’ll get to know these operators better in
this section.
Try typing some of the examples listed in Table 3-1 and see what Python says; Figure 3-2
shows some sample output. Feel free to try your own math problems as well.
Figure 3-2. Type the example math problems (expressions) from Table 3-1, and Python
gives the answers!
Take a look at Figure 3-3 to see some examples of syntax errors, followed by the
expressions stated in a way that Python can understand.
Figure 3-4. Python remembers our variable’s value for as long as we want.
Figure 3-6. Python prints a screen full of my name when I run SayMyName.py.
SpiralMyName.py
# SpiralMyName.py - prints a colorful spiral of the user's name import turtle
# Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]
# Ask the user's name using turtle's textinput pop-up window
your_name = turtle.textinput("Enter your name", "What is your name?")
# Draw a spiral of the name on the screen, written 100 times
for x in range(100):
t.pencolor(colors[x%4]) # Rotate through the four colors
t.penup() # Don't draw the regular spiral lines
t.forward(x*4) # Just move the turtle on the screen
t.pendown()
# Write the user's name, bigger each time
t.write(your_name, font = ("Arial", int( (x + 4) / 4), "bold") )
t.left(92) # Turn left, just as in our other spirals
The final result is a lovely spiral; my son Max ran the one shown in Figure 3-8.
ColorSpiralInput.py
import turtle # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black") # Set up a list of any 8 valid Python color names
colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"]
# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int (turtle.numinput("Number of sides", "How many sides do you want (1-8)?", 4,
1, 8))
# the turtle.textinput() function asked the user for a string
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
t.pencolor(colors[x % sides]) # Only use the right number of colors
t.forward(x * 3 / sides + x) # Change the size to match number of sides
t.left(360 / sides + 1) # Turn 360 degrees / number of sides, plus 1
t.width(x * sides / 200) # Make the pen larger as it goes outward
Figure 3-9 shows the drawings that result from entering eight sides and three sides.
Figure 3-9. The picture from ColorSpiralInput.py with eight sides (left) and three sides (right)
MathHomework.py
print("MathHomework.py") # Ask the user to enter a math problem
problem = input("Enter a math problem, or 'q' to quit: ")
# Keep going until the user enters 'q' to quit
while (problem != "q"):
# Show the problem, and the answer using eval()
print("The answer to ", problem, "is:", eval(problem) )
# Ask for another math problem
problem = input("Enter another math problem, or 'q' to quit: ")
# This while loop will keep going until you enter 'q' to quit
PROGRAMMING CHALLENGES
To practice what you’ve learned in this chapter, try these challenges.