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:

  1. x = 7
  2. 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.

  1. # SpiralMyName.py - prints a colorful spiral of the user's name
  2. import turtle # Set up turtle graphics
  3. t = turtle.Pen()
  4. turtle.bgcolor("black")
  5. colors = ["red", "yellow", "blue", "green"]
  6. # Ask the user's name using turtle's textinput pop-up window
  7. your_name = turtle.textinput("Enter your name", "What is your name?")
  8. # Draw a spiral of the name on the screen, written 100 times
  9. for x in range(100):
  10. t.pencolor(colors[x%4]) # Rotate through the four colors
  11. t.penup() # Don't draw the regular spiral lines
  12. t.forward(x*4) # Just move the turtle on the screen
  13. t.pendown() # Write the user's name, bigger each time
  14. t.write(your_name, font = ("Arial", int( (x + 4) / 4), "bold"))
  15. t.left(92) # Turn left, just as in our other spirals

image.png

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.

Challenges