Variables - Where we keep our stuff.
A variable is something you want the computer to remember while your program is running. When Python “remembers” something, it’s storing that information in the computer’s memory.
we assign a value to a variable with the equal sign (=
). An assignment like x = 7
tells the computer to remember the number 7 and give it back to us anytime we call out x
. We also use the equal sign to assign a string of keyboard characters to a variable; we just have to remember to put quotation marks (“) around the string, like this:
x = 7
my_name = "Bryson" #The quotation marks around "Bryson" tell us that it is a string.
Numbers
The computer is great at remembering values. We can use the same variable hundreds or thousands of times in the same pro- gram, and the computer will always give us the right value as long as we’ve programmed it correctly.
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).
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.
Basic Math Operators in Python
Math symbol | Python operator | Operation | Example | Result |
---|---|---|---|---|
+ | + | Addition | 4+ 2 | 6 |
– | - | Subtraction | 4- 2 | 2 |
× | * | Multiplication | 4* 2 | 8 |
÷ | / | Division | 4/ 2 | 2.0 |
4 | ** | Exponent or power | 4 ** 2 | 16 |
() | () | Parentheses (grouping) | (4 + 2) * 3 | 18 |
Math
You can type a math problem (called an expression in programming) like 4 + 2 directly at the command prompt (the >>>
symbol with the flashing cursor)in the Python shell, and when you press enter, you’ll see the result of the expression, or the answer to the math problem.
Strings
Strings are what we call text, or keyboard characters, in a programming language; they are groups (or “strings”) of letters, numbers, and symbols.
# 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
Lists
A list is a group of values, separated by commas, between square brackets, []. We can store any value type in lists, including numbers and strings; we can even have lists of lists.
Summary
At this point, you should be able to do the following:
- Create your own variables to store numbers, strings, and lists.
- Discuss the differences between number types in Python.
- Use basic math operators in Python to perform calculations.
- Explain the difference between strings, numbers, and lists.
- Write out short programs as steps in English and then write those steps as comments to help you build your code.
- Ask for user input in a variety of situations and use that input in your programs.