Chapter 2 · Part I: Basics

Variables and Simple Data Types

Book: Ch. 2, 'Variables and Simple Data Types', pp. 15–32

Give values names, and use string methods that hand back a new string.

Key ideas

  • A variable is a name attached to a value
  • Assigning again points that name at a new value
  • title() and upper() return a new string and leave the original alone
  • An f-string drops values into a sentence
  • capitalize() changes only the first character

Pages this chapter covers

Section titles and printed pages from the 3rd edition. Notes below use the full citation.

  • What Really Happens When You Run hello_world.pyp. 15
  • Variablesp. 16
    • Naming and Using Variablesp. 17
    • Avoiding Name Errors When Using Variablesp. 17
    • Variables Are Labelsp. 18
  • Stringsp. 19
    • Changing Case in a String with Methodsp. 20
    • Using Variables in Stringsp. 20
    • Adding Whitespace to Strings with Tabs or Newlinesp. 21
    • Stripping Whitespacep. 22
    • Removing Prefixesp. 23
    • Avoiding Syntax Errors with Stringsp. 24
  • Numbersp. 26
    • Integersp. 26
    • Floatsp. 26
    • Integers and Floatsp. 27
    • Underscores in Numbersp. 28
    • Multiple Assignmentp. 28
    • Constantsp. 28
  • Commentsp. 29
    • How Do You Write Comments?p. 29
    • What Kinds of Comments Should You Write?p. 29
  • The Zen of Pythonp. 30
  • Summaryp. 32

Notes

A name for a value

Book: Ch. 2, 'Variables', p. 16

A variable is a name you attach to a value so you can use it again. The single = attaches the name. It does not ask whether two sides are equal.

seat = 14
print(seat)
seat = 15
print(seat)

The first print shows 14. After the second line, the name seat refers to 15, so the next print shows 15. The number 14 was not edited. The name was pointed at a different value.

Use names you could say out loud, in lowercase, with underscores between words: seat, bird_count. A name has to be assigned before you use it. Printing a name that was never assigned stops the program with a NameError.

Methods hand you a new string

Book: Ch. 2, 'Changing Case in a String with Methods', p. 20

A method is a function that belongs to a value. Call it with a dot. title() and upper() do not edit the original text. They return a new string, which you can print or store under another name.

raw = "blue heron"
print(raw.title())
print(raw.upper())
print(raw)

That prints:

Blue Heron
BLUE HERON
blue heron

title() capitalizes the first letter of each word. upper() capitalizes every letter. The last line is still lowercase because raw itself never changed. Keep the new text by assigning it:

badge = raw.title()

capitalize() is a different method. It changes only the first character of the whole string, so "blue heron".capitalize() is "Blue heron", not "Blue Heron".

Drop a value into a sentence

Book: Ch. 2, 'Using Variables in Strings', p. 20

An f-string is a string with an f before the opening quote. Curly braces mark a spot where a value should be inserted.

bird = "heron"
count = 3
print(f"The marsh had {count} {bird}s.")

That prints The marsh had 3 herons. Python turns the number into text for you.

You can also join pieces with +. That only works when both pieces are already text, so an f-string is the calmer choice while the sentence mixes words and numbers.