How to Define a Variable in Python

What you’ll build or solve

You’ll define variables in Python, update them safely, and avoid common naming and type mistakes.

When this approach works best

This approach works best when you:

  • Want to reuse a value instead of repeating it, like a file path, tax rate, or user name.
  • Need to build up a result step by step, like a running total or a message string.
  • Are debugging and want to print or log intermediate values with clear names.

Avoid this approach when:

  • You need truly constant values enforced by the language. Python does not have real constants, so you rely on naming conventions instead.

Prerequisites

  • Python installed

Step-by-step instructions

1) Assign a value with =

In Python, you define a variable by assigning a value to a name using =.

age=25
name="Mina"
is_member=True

You can then use the variable later:

print(name)
print(age+1)

What to look for: = assigns. It does not mean “equals” like in math.


2) Use clear, valid variable names

Variable names must start with a letter or underscore, and they can include letters, numbers, and underscores.

Good names:

total_price=19.99
user_email="sam@example.com"
_tries=3

Names that will fail:

# 2nd_place = "silver"   # starts with a number
# user-email = "x@y.com" # hyphen is not allowed

What to look for: Python is case-sensitive. userName and username are different variables.


3) Update a variable and use augmented assignment

You can reassign a variable at any time.

count=0
count=count+1
print(count)

Augmented assignment is a shorter, common pattern:

count=0
count+=1
count+=5
print(count)

It also works for strings:

message="Hi"
message+=", Mina"
print(message)

4) Check the type when behavior surprises you

Variables can point to any type, and the type can change based on what you assign.

value=10
print(type(value))

value="10"
print(type(value))

If an operation fails, types are often the reason:

a="5"
b=2
# print(a + b)  # TypeError
print(int(a)+b)

What to look for: a numeric-looking string like "5" is still text until you convert it.


Examples you can copy

Example 1: Store a configuration value and reuse it

api_base_url="https://api.example.com"
endpoint="/v1/users"
url=api_base_url+endpoint

print(url)

Example 2: Build a running total

total=0
prices= [4.5,2.0,3.25]

forpriceinprices:
total+=price

print(total)

Example 3: Assign multiple variables at once

x,y=10,20
print(x,y)

x,y=y,x
print(x,y)

Example 4: Store user input in a variable

raw_age=input("Enter your age: ")
age=int(raw_age)

message=f"Next year you will be{age+1}."
print(message)

Common mistakes and how to fix them

Mistake 1: Using a variable before defining it

You might write:

print(total)
total=10

Why it breaks: Python reads top to bottom, so total does not exist yet.

Correct approach:

total=10
print(total)

Mistake 2: Mixing strings and numbers by accident

You might write:

age=input("Age: ")
print(age+1)

Why it breaks: input() returns a string, so Python cannot add 1 to it.

Correct approach:

age=int(input("Age: "))
print(age+1)

Troubleshooting

  • If you see NameError: name 'x' is not defined, check spelling and define the variable before using it.
  • If you see TypeError when adding or comparing, print type(variable) and convert with int(), float(), or str().
  • If you see SyntaxError on a variable name, remove hyphens or spaces and make sure the name does not start with a number.
  • If a variable changes unexpectedly, search for other assignments to the same name, especially inside loops and functions.
  • If you overwrote a built-in like list or str, rename your variable and restart your session if needed.

Quick recap

  • Define a variable by assigning with =.
  • Use clear names with letters, numbers, and underscores.
  • Update values with reassignment or +=.
  • Check types when operations fail or results look wrong.