size = 5
greeting = "Hello, Python!"
print(greeting)
print("Size is:", size)
print("Double the size:", size * 2)Variables
When you write a Python program, you need a way to keep track of the things you create. A variable is a name you attach to a value — a label that lets you find it and work with it later.
In this lesson you’ll start with simple values, then use variables to build and control a 3D scene.
Your First Variables
A variable can hold any value: a number, a piece of text, or something more complex. You create one using =
Put the name on the left and the value on the right.
Run the cell below to create two variables and see what’s inside them.
Two variables were created:
| Variable | Value | Type |
|---|---|---|
size |
5 |
A number |
greeting |
"Hello, Python!" |
Text (called a string) |
Notice that size * 2 printed 10 — you can use a variable just like the value it holds.
Using Variables in 3D Scenes
We can use the same approach to create variables in 3D scenes. Instead of holding simple values like numbers and text, we can store a complex object like a 3D sphere:
sphere = scene3d.Shapes.Sphere(5)The right side creates a sphere. The = attaches the label sphere to it. From that point on, whenever you write sphere in your code, Python knows exactly which sphere you mean.
A Scene and a Sphere
Run the cell below. A sphere appears in a 3D scene!
Hint: Hold the left mouse button to rotate the scene. Use the scroll wheel to zoom.
Two variables make this happen — scene and sphere. Look for them in the code before you run it.
import scene3d
scene = scene3d.Scene()
sphere = scene3d.Shapes.Sphere(5)
scene.add(sphere)How the Variables Work
Two lines in that code create variables:
scene = scene3d.Scene()
sphere = scene3d.Shapes.Sphere(5)| Variable | What it holds |
|---|---|
scene |
The 3D scene — the world where everything lives |
sphere |
The sphere shape — an object inside that world |
Once a variable exists, you use its name to work with what it holds:
| Line | What happens |
|---|---|
scene.add(sphere) |
Uses both variables — places the sphere into the scene |
One Variable, Many Uses
Variables really shine when the same value appears in multiple places.
The code below uses a single SIZE variable to control both how big the sphere is and how high it sits — so it always rests on the ground no matter what size you pick.
Try changing the SIZE slider and running the cell again. Notice that one change updates everything.
import scene3d
SIZE = 3 #@param {type:"slider", min:1, max:6, step:0.5}
scene = scene3d.Scene()
scene.set_ground(length=14, width=14)
sphere = scene3d.Shapes.Sphere(diameter=SIZE, segments=24)
sphere.set_position(0, SIZE / 2, 0)
scene.add(sphere)Think of a real-life example of a label or placeholder. It could be a contact name in your phone, a locker number, a nickname. These all work like variables.
Now think about code: if you were building a 3D city scene, what variables would you create?
Write three variable names and describe what each one would hold.
{ “question_type”: “multiple_choice”, “question”: “What is a variable in Python?”, “options”: [ { “key”: “a”, “text”: “A type of 3D shape” }, { “key”: “b”, “text”: “A name that stores a value so you can use it later” }, { “key”: “c”, “text”: “A color code” }, { “key”: “d”, “text”: “The = sign” } ], “answer”: “b”, “submitted_answer”: “” }
{ “question_type”: “true_false”, “question”: “In Python, the = sign checks whether two values are equal.”, “answer”: “False”, “submitted_answer”: “” }
{ “question_type”: “multiple_choice”, “question”: “In the SIZE example, why is it better to write SIZE = 3 and use SIZE in your code than to write the number 3 directly each time?”, “options”: [ { “key”: “a”, “text”: “It makes Python run faster” }, { “key”: “b”, “text”: “Changing one line updates every place SIZE is used” }, { “key”: “c”, “text”: “It stops you from using the number 3 by accident” }, { “key”: “d”, “text”: “It makes the variable name longer” } ], “answer”: “b”, “submitted_answer”: “” }
{ “question_type”: “true_false”, “question”: “Once you create a variable called sphere, you can use the name sphere anywhere later in your code to work with that sphere.”, “answer”: “True”, “submitted_answer”: “” }