End interactive session 1B
Code
temperature = 21
site_name = "Santa Barbara"🐍 Python Essentials: Variables, Strings & f-strings

A cartoon depicting the idea of a variable. MidJourney 5
Now that you’re comfortable moving around Positron, let’s meet the handful of Python building blocks you’ll use in every single notebook this week: variables, strings, the print() and type() functions, and the star of today, f-strings, for building readable output. We’re keeping the vocabulary small on purpose so you can use it fluently.
Set up your notebook using the same ritual from this morning:
Create a new notebook:
Ctrl + Shift + P (Cmd + Shift + P on macOS) and run Create: New Jupyter Notebook.Save your notebook (Ctrl + S, or Cmd + S on macOS) as: Session_1B_Python_Essentials.ipynb
Add a title cell (Markdown), updating the date to today:
# Day 1: Session 1B - Python Essentials
[Session Webpage](https://eds-217-essential-python.github.io/course-materials/interactive-sessions/1b_python_essentials.html)
Date: 08/31/2026Save your work frequently with Ctrl+S (Cmd+S on macOS).
A variable is a name that refers to a value. You create one with the assignment operator, =. The name goes on the left, the value on the right.
Nothing prints when you assign a variable. Python just remembers it. Ask for the name again and you get the value back:
Values come in different types. The two numeric types you’ll see are integers (whole numbers, int) and floats (decimal numbers, float):
You can do basic arithmetic with numbers, just like a calculator, and store the result in a new variable:
We’ll cover Python’s full set of operators on Day 3. Today, basic +, -, *, and / are all you need.
🐍 Choose variable names that say what the value is (site_name, not s). Python style favors lowercase names with underscores. Names can’t start with a number, and a few words are reserved by Python itself, so your editor will warn you if you hit one.
Create two variables, morning_temp and afternoon_temp, give them values, and compute their difference into a new variable called daily_swing. Display daily_swing.
A string is a piece of text. You write one by wrapping characters in single or double quotes, and the two are interchangeable:
The only practical difference is which quote is easier to include inside the text. Use double quotes when your text contains an apostrophe:
You can also write a multi-line string using triple quotes, which is handy for longer blocks of text:
🐍 A string is just data. The quotes tell Python to treat these characters as text. We’ll learn ways to transform text (cleaning, splitting, reformatting) later in the course. For now we just need to create strings and print them.
Create a string variable favorite_place holding the name of somewhere you like. Then create a second string that includes an apostrophe (like "I'm from Ventura.") and make sure it doesn’t cause an error.
type()When you’re not sure what kind of value a variable holds, ask Python with the type() function:
Those three, int, float, and str, are the types you’ll work with today. Knowing a value’s type tells you what you can do with it.
Predict the type of each of these before you run it, then check with type(): 19, 19.0, and "19". Were any surprising?
print()Asking for a variable’s value shows it only when it’s the last line of a cell. To display values whenever and wherever you want, use the print() function:
print() can take several values at once, separating them with spaces:
In a single cell, print two lines: one showing your favorite_place, and one showing the daily_swing you computed earlier. Use a separate print() for each.
Stitching text and variables together with commas works, but it gets awkward fast. The modern, readable way to build a message from your data is the f-string (formatted string literal). This is the one construct we want you to be able to write from scratch by the end of the day, so we’ll spend the most time here.
An f-string is a string with the letter f right before the opening quote. Inside it, anything you put in curly braces {} is replaced by the value of that variable:
That’s the whole idea: f"text {variable} more text". The f turns it into an f-string, and each {variable} becomes its value.
The pattern to memorize is f"text {variable}". The f must come before the quote, and the variable name goes inside the braces. Forget the f and you’ll just print the braces literally.
You can drop in as many variables as you like:
We recorded 42 readings at Goleta.
f-strings work anywhere a string does. You can build one and store it in a variable, then print it later:
Coming from R? An f-string is like sprintf() or glue(), but the variables sit right inside the text where they’ll appear, with no separate list of arguments to line up.
Let’s build up some fluency. Given these variables:
Write f-strings that produce sentences:
The station is CNSI Roof Top.
It logged 30 readings today.
The warmest reading was 27 degrees.
Change warmest to a new value and re-run the last f-string. The sentence should update automatically. Then write a new f-string that mentions both station and readings in the same sentence.
You can also put a computed value in a variable and drop it into an f-string. Create coolest = 14, compute spread = warmest - coolest, and print: f"The temperature spread was {spread} degrees."
Here’s the kind of thing you’ll do constantly: compute a few values, then describe them in plain language with f-strings.
Rainfall is a good quantity to summarize because it accumulates. Three days of rain add up to a three-day total, and dividing by three gives an average daily rate. Start from this small rainfall log and compute a couple of values using basic arithmetic:
Using the variables above (and the values you computed), write two or three f-string print() statements that summarize the rainfall log in complete sentences. For example, your output might read something like:
Write your own versions in a code cell. Each sentence should pull at least one variable in with { }.
name = value. Nothing prints on assignment.int (whole numbers), float (decimals), and str (text). Check any value with type().print() displays values anywhere in a cell and accepts several at once.f"text {variable}", are the readable way to combine text and values, and the one thing you should be able to write on your own after today.print() covers printing and f-strings.f"text {variable}".End interactive session 1B