Lecture 1: Variables and Data
This is a sample lesson with runnable code blocks. Click Run on any block to execute it in your browser. For the authoring guide to Python imports and runtime behavior, see Code.
If you haven’t already, check out the getting started tutorial to learn how readrun works.
Variables and types
Python figures out types for you. Run the code below to see it in action:
x = 42
name = "Alice"
pi = 3.14159
is_active = True
for var_name, var_val in [("x", x), ("name", name), ("pi", pi), ("is_active", is_active)]:
print(f"{var_name} = {var_val} ({type(var_val).__name__})")
Lists
Lists are ordered collections that you can loop through, slice, and modify:
fruits = ["apple", "banana", "cherry", "date"]
print("All fruits:", fruits)
print("First two:", fruits[:2])
print("Last one:", fruits[-1])
fruits.append("elderberry")
print("After adding one:", fruits)Dictionaries
Dictionaries store key-value pairs. They’re useful for structured data:
student = {
"name": "Bob",
"grade": 85,
"subjects": ["maths", "physics"]
}
for key, value in student.items():
print(f" {key}: {value}")The same data rendered as a card:
function StudentCard() {
const student = { name: "Bob", grade: 85, subjects: ["Maths", "Physics"] };
const gradeColor =
student.grade >= 90 ? "text-green-600" :
student.grade >= 70 ? "text-blue-600" : "text-red-600";
return (
<div className="border border-gray-200 rounded-xl p-5 max-w-xs shadow-sm">
<p className="text-xs uppercase tracking-widest text-gray-400 mb-1">student</p>
<h3 className="text-xl font-bold text-gray-800">{student.name}</h3>
<p className={`text-4xl font-mono font-bold mt-2 ${gradeColor}`}>
{student.grade}<span className="text-lg text-gray-400">%</span>
</p>
<div className="flex gap-2 mt-3">
{student.subjects.map(s => (
<span key={s} className="bg-blue-50 text-blue-700 text-xs font-medium px-2.5 py-1 rounded-full border border-blue-100">
{s}
</span>
))}
</div>
</div>
);
}
render(<StudentCard />);
Regular code blocks
Not every code block needs to be runnable. Standard markdown fences display code without a Run button — good for showing commands, pseudocode, or examples you don’t want executed:
# This is a regular code block — display only
# Use triple backticks for these
print("You can't run this one")
Next
Continue to Functions to learn about defining and using functions.