Variables

A variable is a name used to store data in memory. The value of a variable can be changed during program execution.

Definition

A variable is an identifier that stores a value, such as a number, string, or object, which can be used and modified in a program.

Rules for Naming Variables

  • Variable names must start with a letter (A–Z or a–z) or an underscore (_).
  • They cannot start with a number.
  • They can contain letters, numbers, and underscores.
  • Variable names are case-sensitive (age and Age are different).
  • Python keywords (e.g., if, for, class) cannot be used as variable names.

Examples

name = "Alice"
age = 20
height = 5.6

print(name)
print(age)
print(height)

Output

Alice
20
5.6

Data Types

A data type specifies the type of value that a variable can store. Python automatically determines the data type based on the assigned value (dynamic typing).

Common Built-in Data Types

Data TypeDescriptionExample
intInteger numbers10, -25
floatDecimal numbers3.14, -7.5
complexComplex numbers2 + 3j
strText (string)"Hello"
boolBoolean valuesTrue, False
listOrdered, mutable collection[1, 2, 3]
tupleOrdered, immutable collection(1, 2, 3)
setUnordered collection of unique values{1, 2, 3}
dictKey-value pairs{"name": "Alice", "age": 20}

Examples of Different Data Types

integer = 100
decimal = 99.5
message = "Welcome"
status = True

print(integer)
print(decimal)
print(message)
print(status)

Output

100
99.5
Welcome
True

Checking the Data Type

Use the type() function to determine the type of a variable.

x = 10
y = 3.14
z = "Python"

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

Output

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

Multiple Variable Assignment

Python allows assigning values to multiple variables in one statement.

a, b, c = 10, 20, 30

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

Output

10
20
30

Assigning the Same Value to Multiple Variables

x = y = z = 100

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

Output

100
100
100

Dynamic Typing

Python variables can change their data type during execution.

value = 100
print(type(value))

value = "Python"
print(type(value))

Output

<class 'int'>
<class 'str'>

Advantages of Python Variables

Reduces development time

Easy to create (no type declaration required)

Supports dynamic typing

Improves code readability