HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Variables

Variables

What a variable is, how to create one, naming rules, multiple assignment and printing a value — from start to finish.

Simple enough for a 5-year-old to understand

Variable: a box that stores information

A variable is a name you give to a value. Think of it as a box where you keep the value, with a label stuck on it.

In Python there is no separate command to create a variable — a variable is created the moment you assign a value.

Your first variables
1
x = 5
We put the number 5 into the box "x" (no quotes = number).
VariableWe put the number 5 into the box "x" (no quotes = number).
2
name = "Alex"
We put the text "Alex" in the "name" box (quoted = text).
VariableWe put the text "Alex" in the "name" box (quoted = text).
3
print(x)
Writes the contents of x to the screen: 5
Output / InputWrites the contents of x to the screen: 5
4
print(name)
prints the contents of name: Alex
Output / Inputprints the contents of name: Alex
VariableOutput / Input
💡 Hint: Equals (=) does not mean "is it equal?"; it means "put the right-hand side into the left-hand box".

The value can change

You can later put a new value into the same box; the old one is erased.

Example code
1
score = 10
score is now 10.
Variablescore is now 10.
2
score = 20
Now score is 20; the old 10 is gone.
VariableNow score is 20; the old 10 is gone.
3
print(score)
Writes 20.
Output / InputWrites 20.
VariableOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.What does the line x = 5 do?
2.Create a variable named city, put "Madrid" in it and print it on the screen.

🎯 Expected output: Madrid

Loading editor…

About this topic

A variable is a name you give to a piece of information. There is no separate command to create a variable in Python: the moment you assign a value to a name with an equals sign, the variable is born. You can think of it like a box with a label stuck on it — the label is the name, and what's inside is the value.

The difference between Python and other languages is that you don't have to declare the type of the variable. If you assign a number, it becomes a number; if you assign text, it becomes text, and you can even change its type later. This is called dynamic typing; it makes writing code easier, but leaves the responsibility of keeping track of what a variable currently holds up to you.

What you will learn

  • Creating a variable with the equals sign
  • Variable naming rules and common writing conventions
  • Assigning a new value to the same variable
  • Defining multiple variables on a single line
  • Learning the type of a variable with type

When to use?

You use variables in any situation where you will use the same information in multiple places. Instead of manually typing a price, a file path, or a username into the code several times, putting it in a variable once means editing just a single line when you need to change it.

It also makes the code readable: someone seeing the number 0.18 won't know what it is, but seeing a variable named vat_rate makes it instantly understandable.

Common mistakes

Wrong

2nd_price = 50

A variable name cannot start with a digit; Python will give a "SyntaxError: invalid decimal literal". It must start with a letter or an underscore.

Correct

second_price = 50

Wrong

print(total)
total = 10

A variable must be defined BEFORE it is used. Python runs code from top to bottom, which is why you will get a "NameError: name 'total' is not defined" error.

Correct

total = 10
print(total)

Wrong

name = "Ada"
Name = "Lovelace"
print(name)

Python is case-sensitive: name and Name are two separate variables. Printing an old value thinking you updated it because of a typo is a very common mistake.

Correct

name = "Ada"
name = "Lovelace"
print(name)

Frequently asked questions

How should a variable name be?

Lowercase, underscores between words: user_name, total_price. This is called snake_case and Python's official style guide PEP 8 recommends it. Turkish characters technically work but don't use them: they cause problems when running your code in other environments.

Can I change the type of a variable later?

Yes, Python allows this: you can assign a number first and then text to the same name. However, this is rarely a good idea; having the same variable hold different types in different parts of the code makes debugging harder.

How do you define a constant?

There are no true constants in Python. By convention, values that shouldn't change are written in ALL CAPS — for example MAX_RETRY — so other developers understand that it shouldn't be changed. The language doesn't enforce this or give warnings.

Summary

A variable is a name given to a value, and is created in Python using the equals sign; there is no need to declare its type. Following the lowercase and underscore convention for naming makes the code both readable and portable.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code