Python Variable & Data Type

Python Variables

Learn Python Variables with clear explanations, Python examples, and practical programming guidance.

What is a variable in Python?

A variable is a name that refers to an object in a Python program. Variables let you store values, use those values in expressions, and assign a different object later. Python determines the type at runtime, so you do not write a type declaration before the variable name.

Creating variables

Use the assignment operator (=) to bind a name to a value:

age = 17
name = "Daisy"

print(age)
print(name)
17
Daisy

In this example, age refers to an integer and name refers to a string. Python infers these types from the assigned values.

Variable naming rules

Variable names must follow Python's identifier rules. Good names also make a program easier to understand:

RuleValid exampleInvalid example
Use letters, digits, and underscoresstudent_2student-name
Do not start with a digitscore11score
Do not include spacesfirst_namefirst name
Names are case-sensitivetotal, TotalThey are not the same variable
Do not use reserved keywordsclass_nameclass

Use snake_case for variables and functions, choose descriptive names, and avoid names such as x when a clearer name such as total_price is possible.

Dynamic typing

Python is dynamically typed. A name can refer to objects of different types at different points in a program:

value = 21
print(value, type(value))

value = "Python"
print(value, type(value))
21 <class 'int'>
Python <class 'str'>

The name value was first bound to an integer and was later rebound to a string. The object has a type; the variable name itself does not have a permanently fixed type.

Assigning multiple values

Python supports assigning one value to several names and unpacking several values in one statement:

# The same object is assigned to three names
first = second = third = 182

# Each name receives the corresponding value
student, course, score = "Asha", "Python", 19.5

print(first, second, third)
print(student, course, score)

When unpacking, the number of names must normally match the number of values. This makes the relationship between the data and the variables explicit.

Changing a variable and type conversion

Some operations return a new type. Division with / returns a floating-point value, while functions such as int(), float(), and str() can perform explicit conversions:

number = 9
quotient = number / 4
whole_number = int(quotient)

print(quotient)
print(whole_number)
2.25
2

Conversion is not always lossless. For example, converting 2.25 to int removes the fractional part. Validate user input before converting it in a real application.

Checking a variable's type

The built-in type() function returns the type of an object. Use it while learning or debugging; in application logic, prefer clear behavior and appropriate validation:

count = 18
price = 82.6
language = "Python"
active = True
items = [4, 1, 8]

print(type(count))
print(type(price))
print(type(language))
print(type(active))
print(type(items))

Local and global scope

Scope describes where a name can be accessed. A name created inside a function is local to that function. A name created outside functions is global to the module, although using global state extensively can make programs harder to test:

tax_rate = 0.18  # global name

def calculate_tax(amount):
    tax = amount * tax_rate  # local name: available only in this function
    return tax

print(calculate_tax(100))
# print(tax)  # NameError: tax is local to calculate_tax()

Python resolves names through nested scopes. Prefer passing values into functions and returning results instead of changing global variables.

Variables are references to objects

A variable does not contain a separate copy of a value in the way beginners often imagine. It is a reference to an object. Assigning another name creates another reference to the same object:

first = ["Python", "Java"]
second = first

second.append("SQL")
print(first)
print(second)
print(first is second)
['Python', 'Java', 'SQL']
['Python', 'Java', 'SQL']
True

Both names refer to the same list, so changing the list through second is visible through first. Use first.copy() when you need a separate shallow copy.

Deleting a variable

The del statement removes a name from its namespace. It does not guarantee that an object is immediately destroyed; the object remains available while another reference points to it:

message = "Hello"
print(message)

del message
# print(message)  # NameError: message is no longer defined
Hello

Best practices for Python variables

  • Choose descriptive names such as total_price and student_count.
  • Use snake_case for variables and constants such as MAX_RETRIES.
  • Keep a variable's purpose and type consistent within a small section of code.
  • Avoid unnecessary global variables; pass data through function parameters.
  • Validate and convert external input before using it in calculations.

Further Reading

Continue learning with these related Python tutorials:

Continue learning