0
0
PythonHow-ToBeginner · 3 min read

How to Declare a Variable in Python: Simple Guide

In Python, you declare a variable by simply writing its name followed by an equals sign and the value, like variable_name = value. Python automatically figures out the type of the variable based on the value you assign.
📐

Syntax

To declare a variable in Python, use the format variable_name = value. Here:

  • variable_name is the name you choose for your variable.
  • = is the assignment operator that sets the value.
  • value is the data you want to store, like a number, text, or more.

Python does not require you to specify the type of the variable; it figures it out automatically.

python
x = 10
name = "Alice"
price = 19.99
is_active = True
💻

Example

This example shows how to declare different types of variables and print their values.

python
age = 25
username = "bob123"
height = 1.75
is_member = False

print(age)
print(username)
print(height)
print(is_member)
Output
25 bob123 1.75 False
⚠️

Common Pitfalls

Some common mistakes when declaring variables in Python include:

  • Using spaces in variable names (e.g., my variable = 5 is wrong).
  • Starting variable names with numbers (e.g., 1st_place = "Alice" is invalid).
  • Using reserved words like for or if as variable names.
  • Forgetting that Python is case-sensitive (Age and age are different).
python
# 1st_place = "Alice"  # ❌ Invalid: starts with a number
first_place = "Alice"  # ✅ Valid

# my variable = 10       # ❌ Invalid: contains space
my_variable = 10       # ✅ Valid
📊

Quick Reference

Remember these tips when declaring variables in Python:

  • Use letters, numbers, and underscores only.
  • Start with a letter or underscore, never a number.
  • Variable names are case-sensitive.
  • Choose meaningful names to make your code clear.

Key Takeaways

Declare variables by writing name = value without specifying type.
Variable names must start with a letter or underscore and contain no spaces.
Python automatically detects the variable type from the assigned value.
Avoid using Python reserved words as variable names.
Variable names are case-sensitive; be consistent with naming.