Skip to main content
Welcome back. Right now your dashboard prints the same static information every time. To change values like your mood without editing many lines of code each time, you use variables. In this lesson you will learn to:
  • create and use variables to store data
  • distinguish between strings, integers, floats, and booleans
  • use input() to make a program interactive
  • perform calculations and build messages with variables
What is a variable? A variable is like a labeled box on a shelf: you give it a name and store a value in it. Later you refer to the variable name to access or change the value. Example — the original hard-coded dashboard
Create and use a simple variable
When you run this, Python prints the value stored in name. Using variables in multiple places
Output:
You typed "Ada" once, but reused it multiple times via the variable. Order matters Python executes code from top to bottom. If you try to use a variable before assigning it, you’ll get an error:
Do not reference variables before they are assigned. This causes a NameError because Python hasn’t stored a value yet.
Example that raises an error:
Running this results in:
Always assign name before you use it. Tip: If your terminal output is cluttered, use the clear command (or cls on Windows) to wipe the slate. Comments to temporarily disable code If parts of the dashboard distract you while you test other sections, comment them out so they won’t execute. In many editors (for example, VS Code) press Command+/ on macOS or Control+/ on Windows to toggle # at the start of selected lines. Basic data types Python infers a variable’s type from the value you assign. The most common primitive types are in the table below. Strings (text)
  • Use single or double quotes (be consistent).
Integers (whole numbers)
Floats (numbers with a decimal point)
Booleans (True or False)
Embedding variables into output You can pass text and variables as separate arguments to print() to show them on one line:
For clearer formatting, consider Python f-strings (available in Python 3.6+):
Building the dashboard with variables Create variables at the top and use them in the dashboard print statements:
Numeric variables and calculations To do math with a variable, store it as a number (integer or float) rather than text. For example, use an integer for energy so you can add or subtract points:
If you nap and restore 10 points of energy:
Organize code with comments Use comments to group related variables and make your script easier to read:
String concatenation Joining strings together is called concatenation. Example:
Concatenation lets you store the combined string in a variable and reuse it later, which is better than recreating the same output repeatedly inside print(). User input Use input() to get information from the user. Note: input() always returns a string.
input() returns text. If you need numeric input for calculations, convert the result with int() or float(). Always validate or handle invalid input in real programs.
Example showing raw input:
Converting input for arithmetic:
Make the dashboard interactive Add inputs for mood, energy, and goals so the dashboard reflects the user’s responses:
If you plan to calculate with energy, wrap the input() call in int() as shown earlier. Variable naming rules and conventions
  • Use underscores for multi-word names: user_age, not user age.
  • Start names with a letter or underscore (not a number).
  • Use only letters, numbers, and underscores (no hyphens).
  • Pick clear, descriptive names (avoid n; use name or user_count instead).
A person wearing a KodeKloud t-shirt stands beside a screen showing a code editor with Python variable-name examples (e.g., user_age vs user-age) and a SyntaxError output in the terminal. The file variables.py is open in the project explorer.
There should be a variables.py script in your workspace — try experimenting with naming conventions and see which ones cause syntax errors. Summary
  • Variables store information so your program can remember and reuse values.
  • Python detects data types automatically: strings use quotes, integers and floats are numeric, booleans are True/False.
  • input() collects text from the user (cast it with int() or float() if you need numbers).
  • Variables let you combine text (concatenation) and perform arithmetic with numbers.
  • Good naming and organization (comments, grouping) make code easier to read and maintain.
Next: you will use variables to make decisions — for example, show one message if a user is happy and a different message if they are sad.

Watch Video

Practice Lab