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

devbrainbox.local/output
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

devbrainbox.local/output
<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

devbrainbox.local/output
15
Hello Python

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

Digital AI Academy

DevBrainBox Portal is proudly developed and managed by

Digital AI Academy
About the academy