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 have Positron open and a notebook of your own, letโs meet the handful of Python building blocks youโll use in just about every notebook this week: variables, strings, and the print() and type() functions. Then weโll get to the star of today, f-strings, which build readable output out of the values you have computed. Weโre keeping the vocabulary small on purpose, so that you leave today using all of it fluently rather than half-remembering a longer list.
Set up your notebook using the same ritual from this morning:
Create the file. In the Explorer, hover over the EDS217 heading and click New Fileโฆ, then type the name in full, extension included: Session_1B_Python_Essentials.ipynb
Check the kernel. The Kernel Selector in the notebookโs action bar should read Python 3.11.15 (Conda: eds217). If it reads anything else, click it, choose Change Kernel, and pick the eds217 entry.
Add a title cell. Click + Markdown in the action bar, and give it this content, with todayโs date:
# 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/2026Ctrl + S (Cmd + S on macOS), and keep saving as you go.Add a code cell below the title cell to start writing Python.
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, because Python stores the value away instead of displaying it. Ask for the name again and you get the value back:
Values come in different types. The two numeric types we use today 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 the comparison operators on Day 3. Today we only need basic +, -, * and /.
๐ 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 Python keeps 35 words for itself, so class = 5 raises a SyntaxError when you run the cell.
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 quotes, and single quotes and double quotes do the same job:
The only practical difference is which quote character you can then use inside the text without ending the string early. 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 the characters between them 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:
int, float, and str are the three types we use today. Knowing a valueโs type tells you what you can do with it: you can subtract one number from another, but Python raises an error if you try to subtract one string from another.
Predict the type of each of these before you run it, then check by running print(type(19)), and the same for 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). 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 the variable you name there:
The recipe is 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, with no error message to tell you what went wrong.
You can drop in as many variables as you like:
We recorded 42 readings at Goleta.
Assigning to a name that already exists replaces what was there, so site_name holds Goleta from here on rather than Santa Barbara.
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:
Each f-string below turns one of the variables above into a sentence:
The station is CNSI Roof Top.
It logged 30 readings today.
The warmest reading was 27 degrees.
Change warmest in the cell that defines it, run that cell, then run the cell of f-strings again. The last sentence should update on its own, without you touching the f-string itself! 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 is the pattern you will use all week: 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 include at least one variable inside { }.
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 values 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