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
- 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.
Original exercise · 3e:2-cc1
Museum name badge
Book: Ch. 2, 'Changing Case in a String with Methods', p. 20
A museum desk stores a sign-in name in all lowercase. Print a badge line for the printer and a loud line for the lobby speaker.
Use variables and string methods. Leave the stored sign-in text lowercase.
What to do
- Create a variable named visitor and set it to the string lina park.
- Create badge: the visitor's name in title case, then the characters space-bar-space and the word Guest. The badge text is Lina Park | Guest.
- Create shout: the visitor's name in all capitals.
- Print exactly two lines: Badge: Lina Park | Guest and then PA announcement: LINA PARK.
Loading Python in your browser. The first time can take a few seconds.
The editor is ready while Python loads.
Original quiz · 3e:quiz:2
Check what you remember
Book: Ch. 2, 'Variables and Simple Data Types', pp. 15–32
Three questions: a choice, a prediction, and a short program. Your best score is this chapter’s proficiency, saved in this browser.
Loading Python in your browser. The first time can take a few seconds.
1You stored a trail label as label = "quiet study". Which expression returns the new string Quiet Study and leaves label unchanged?
2What does this program print? Type both lines, in order.
tool = "whisk" print(tool.upper()) print(tool)3Create a variable named trail set to the string fern. Print how many characters that string has by calling len. The program should print only 4.