Python Variables & Types

Store information using Python variables and built-in data types.

When you write a Python program, you often need to store information for later use. You may want to store a person's name, age, salary, or whether they completed a task. Python stores this information using variables, and the kind of information stored is called a data type.

Understanding variables and data types is one of the most important steps in learning Python. Once you understand these concepts, writing programs becomes much easier.

What is a Variable?

A variable is a name that refers to a stored value. You can think of it as a labeled box containing information. Whenever you need the information, use the variable's name instead of writing the value again.

python
name = "John"
age = 25

print(name)
print(age)

Output

text
John
25
  • name stores the text John.
  • age stores the integer 25.

Python creates each variable when a value is assigned to its name.

Rules for Naming Variables

Valid Variable Names

python
student_name = "Alice"
age = 20
total_marks = 450
price2 = 199

Invalid Variable Names

python
2price = 100
student-name = "John"
class = "Python"
  • A variable name cannot start with a number.
  • Hyphens are not allowed because Python treats them as subtraction operators.
  • Python keywords such as class, if, and for cannot be used as variable names.
  • Names can contain letters, digits, and underscores, but they are case-sensitive.

Best Practices for Variable Names

Choose meaningful names that describe the stored data. Python code commonly uses lowercase snake_case names, with underscores separating words.

Clear Names

python
student_name = "Emma"
product_price = 799
is_logged_in = True

Unclear Names

python
x = 10
a = "John"

Short names can be appropriate in a small mathematical expression or brief loop, but descriptive names are usually easier to understand and maintain.

What is a Data Type?

A data type tells Python what kind of value is stored and which operations make sense for that value. Python determines a value's type at runtime based on the assigned object.

Common Data Types in Python

String (str)

A string stores text inside single or double quotation marks. Strings can contain letters, digits, spaces, and symbols.

python
name = "Alice"
city = "Delhi"

print(name)
print(city)

Integer (int)

An integer is a whole number without a decimal point. Integers are commonly used for counting and exact calculations.

python
age = 30
year = 2026

print(age)
print(year)

Float (float)

A float represents a floating-point number with a decimal component. Floats are useful for measurements, prices, and percentages, though many business applications use decimal types for exact monetary arithmetic.

python
price = 99.99
temperature = 32.5

print(price)
print(temperature)

Boolean (bool)

A Boolean has one of two values: True or False. Booleans are often used in conditions and state checks.

python
is_student = True
has_license = False

print(is_student)
print(has_license)

None Type

None represents the absence of a value. A variable may be assigned None when a result or value is not available yet.

python
result = None

print(result)

Checking the Data Type

The type() function returns the type of an object.

python
name = "John"
age = 22
price = 150.75

print(type(name))
print(type(age))
print(type(price))

Output

text
<class 'str'>
<class 'int'>
<class 'float'>

Multiple Variable Assignment

Python can unpack several values into several variables in one statement.

python
x, y, z = 10, 20, 30

print(x)
print(y)
print(z)

The same value can also be assigned to multiple variable names.

python
a = b = c = 100

print(a)
print(b)
print(c)

Type Conversion

Built-in conversion functions create a value of another type when the source value has a compatible format.

Integer to String

python
age = 25
text = str(age)

print(text)
print(type(text))

String to Integer

python
number = "50"
value = int(number)

print(value)
print(type(value))

Integer to Float

python
marks = 80
average = float(marks)

print(average)

Common conversion functions include int(), float(), str(), and bool(). Invalid conversions, such as int('hello'), raise an exception.

Why Variables and Data Types Matter

Variables store and reuse information throughout a program. Data types tell Python how to process that information. Adding numbers performs arithmetic, while adding strings concatenates text.

python
print(10 + 5)
print("Hello " + "Python")

Output

text
15
Hello Python

Because Python understands each value's type, it performs the appropriate operation.

Key Takeaways

  • A variable is a name that refers to stored data.
  • Python creates variables when values are assigned.
  • Meaningful variable names make code easier to read.
  • Common beginner data types include str, int, float, bool, and None.
  • Use type() to inspect an object's type.
  • Python supports assigning and unpacking multiple variables in one statement.
  • Functions such as int(), float(), and str() convert compatible values between types.
  • Understanding variables and data types is essential because almost every Python program uses them.
Let's learn with DevBrainBox AI