Python Variables, Identifiers, and Data Types – A Complete Beginner's Guide

 

Python Variables, Identifiers, and Data Types – A Complete Beginner's Guide

Published on Code with Py  |  Category: Python Basics  |  Reading time: ~14 min

When you write your first Python program, three concepts are absolutely essential — variables, identifiers, and data types. These are the foundation of every program you will ever write. In this guide, you will learn what variables are, how to name them correctly using identifier rules, and how Python classifies different kinds of data — all explained with clear examples, outputs, and real-life scenarios.

Table of Contents

  1. What is a Variable in Python?
  2. How to Create a Variable
  3. Dynamic Typing in Python
  4. Multiple Assignment and Variable Swapping
  5. What is an Identifier?
  6. Rules for Naming Identifiers
  7. Good Naming Practices
  8. What are Data Types in Python?
  9. SVDT – Single Value Data Types
  10. MVDT – Multi Value Data Types
  11. How to Check a Data Type
  12. Quick Comparison – All Data Types
  13. FAQ

1. What is a Variable in Python?

variable is a name given to a memory location where data is stored. Think of it like a labeled box — you put a value inside the box, give the box a name, and use that name whenever you need the value later.

In real life, you keep sugar in a jar and water in a bottle. In Python, you keep data in variables.

# Real-life data stored in variables
name = "Rohan"
age = 21
city = "Mandya"

print(name)
print(age)
print(city)
Rohan
21
Mandya
In Python, a variable is created the moment you assign a value to it using =. There is no need to declare it first or mention its type — Python handles all of that automatically.

2. How to Create a Variable

Creating a variable in Python is straightforward. Use the assignment operator = to store a value.

# Syntax: variable_name = value
x = 10
price = 99.5
city = "Bengaluru"
is_active = True
Without variable vs with variable

Using variables makes your code readable, reusable, and easy to change.

# Without variable – hardcoded, difficult to update
print(500 * 12)
print(500 + 100)

# With variable – clear and reusable
salary = 500
bonus = 100
print(salary * 12)
print(salary + bonus)
6000
600
6000
600
If the salary changes to 600, you only update one line with a variable. Without a variable, you would need to update every single place where 500 appears.

3. Dynamic Typing in Python

Python is a dynamically typed language. This means you do not need to declare a variable's data type — Python figures it out automatically based on the value you assign. You can also reassign a variable to a completely different type without any error.

x = 10
print(x, type(x))   # int

x = 3.14
print(x, type(x))   # float

x = "Hello"
print(x, type(x))   # str

x = True
print(x, type(x))   # bool
10 <class 'int'>
3.14 <class 'float'>
Hello <class 'str'>
True <class 'bool'>
This is one of Python's most beginner-friendly features. Unlike C or Java where you must declare int x = 10, Python simply writes x = 10 and handles the rest.

4. Multiple Assignment and Variable Swapping

Python allows you to assign values to multiple variables in a single line, which makes your code cleaner.

# Assign same value to multiple variables
a = b = c = 0
print(a, b, c)

# Assign different values in one line
x, y, z = 10, 20, 30
print(x, y, z)

# Swap two variables without a temp variable
a = 5
b = 10
a, b = b, a
print(f"a = {a}, b = {b}")
0 0 0
10 20 30
a = 10, b = 5
Swapping two variables using a, b = b, a is a Python shortcut. In other languages, you need a temporary variable to do the same thing — Python does it in one clean line.

5. What is an Identifier?

An identifier is the name you give to a variable, function, class, or any other element in Python. It helps Python know what you are referring to in your code. Without identifiers, Python would have no way to distinguish one piece of data from another.

age = 21           # age is the identifier
total_marks = 450   # total_marks is the identifier

def calculate_sum():  # calculate_sum is a function identifier
    pass

class StudentData:    # StudentData is a class identifier
    pass
Think of an identifier like a name tag. In a classroom, teachers use your name to call you. In Python, identifiers are used to access the data stored in variables and the logic inside functions.

6. Rules for Naming Identifiers

Python has specific rules for naming identifiers. Breaking these rules causes a SyntaxError or NameError. Learn these rules once and they will stay with you forever.

Must start with a letter or underscore

Invalid
1name    2value
Valid
name1    _total    Age

No special characters except _

Invalid
user-name    total@marks
Valid
user_name    total_marks

No spaces in the name

Invalid
total marks    student name
Valid
total_marks    student_name

Keywords cannot be identifiers

Invalid
class = 10    if = 5
Valid
class_name = 10    if_value = 5

Python is case-sensitive

This is one of the most important rules — ageAge, and AGE are three completely different identifiers in Python.

age = 20
Age = 30
AGE = 40

print(age)   # 20
print(Age)   # 30
print(AGE)   # 40
20
30
40
Beginners often get confused when Python says a variable is undefined — the most common cause is a typo in case. If you defined Name but are using name, Python treats them as two different variables.

Side-by-side — valid vs invalid identifiers

Invalid identifiers
1student = "Rohan"
user-name = "test"
total marks = 100
class = "Python"
my@value = 5
Valid identifiers
student1 = "Rohan"
user_name = "test"
total_marks = 100
class_name = "Python"
my_value = 5

7. Good Naming Practices

Beyond the rules, good programmers also follow conventions that make code easy to read and understand by others.

# Use descriptive names — not single letters for real variables
s = 450               # Bad – what is s?
student_marks = 450   # Good – clear meaning

# Use snake_case for variables and functions
total_price = 1200
calculate_gst = True

# Use PascalCase for class names
class StudentRecord:
    pass

# Use UPPER_CASE for constants
MAX_SPEED = 120
PI = 3.14159
Python convention (PEP 8): use snake_case for variables and functions, PascalCase for class names, and UPPER_CASE for constants. Following these makes your code professional and readable.

8. What are Data Types in Python?

data type tells Python what kind of value a variable is holding and what operations are allowed on it. You cannot add a number to a string directly — Python needs to know the type of data to handle it correctly.

age = 21           # int – whole number
price = 99.5       # float – decimal number
name = "Rohan"    # str – text
is_active = True  # bool – True or False

Python classifies all data types into two broad categories:

SVDT – Single Value Data Types
Store only one value at a time
int, float, complex, bool
MVDT – Multi Value Data Types
Store multiple values in one variable
str, list, tuple, set, dict

9. SVDT – Single Value Data Types

Single value data types store exactly one value at a time. Python has four SVDT types.

int

Integer

Whole numbers, positive or negative, no decimal

float

Float

Numbers with a decimal point

complex

Complex

Numbers with real and imaginary parts

bool

Boolean

Only two possible values — True or False

# int – whole numbers
age = 21
marks = -5
print(age, type(age))

# float – decimal numbers
price = 99.5
temperature = -2.3
print(price, type(price))

# complex – real + imaginary
c = 2 + 3j
print(c, type(c))

# bool – True or False
is_logged_in = True
is_paid = False
print(is_logged_in, type(is_logged_in))
21 <class 'int'>
99.5 <class 'float'>
(2+3j) <class 'complex'>
True <class 'bool'>
In Python, bool is actually a subclass of int. That means True == 1 and False == 0 in Python. You can use them in arithmetic: True + True = 2.

10. MVDT – Multi Value Data Types

Multi value data types can store multiple values inside a single variable. Each type has different properties — some are ordered, some allow duplicates, and some are mutable (changeable) while others are not.

str

String

Text — sequence of characters, immutable

list

List

Ordered, mutable, allows duplicates

tuple

Tuple

Ordered, immutable, allows duplicates

set

Set

Unordered, mutable, no duplicates

dict

Dictionary

Key-value pairs, ordered (Python 3.7+)

# str – text
name = "Rohan"
print(name, type(name))

# list – ordered, changeable
marks = [80, 85, 90, 80]
print(marks, type(marks))

# tuple – ordered, cannot be changed
coordinates = (10.5, 20.3)
print(coordinates, type(coordinates))

# set – unique values only
numbers = {1, 2, 2, 3, 3}
print(numbers, type(numbers))

# dict – key-value pairs
student = {"name": "Rohan", "age": 21, "marks": 90}
print(student, type(student))
Rohan <class 'str'>
[80, 85, 90, 80] <class 'list'>
(10.5, 20.3) <class 'tuple'>
{1, 2, 3} <class 'set'>
{'name': 'Rohan', 'age': 21, 'marks': 90} <class 'dict'>
Notice that the set {1, 2, 2, 3, 3} automatically removed the duplicates and stored only {1, 2, 3}. Sets never allow repeated values — this makes them useful for removing duplicates from a list.

11. How to Check a Data Type

Use the built-in type() function to find out what data type a variable holds. This is especially useful when debugging or when you receive data from user input or external sources.

values = [10, 3.14, "Python", True, [1,2], (3,4), {5,6}, {"a":1}]

for v in values:
    print(f"{str(v):15} → {type(v).__name__}")
10              → int
3.14            → float
Python          → str
True            → bool
[1, 2]          → list
(3, 4)          → tuple
{5, 6}          → set
{'a': 1}        → dict

12. Quick Comparison – All Data Types

CategoryTypeExampleMutable?Allows Duplicates?
SVDTint21No
SVDTfloat99.5No
SVDTcomplex2+3jNo
SVDTboolTrueNo
MVDTstr"Python"NoYes
MVDTlist[1, 2, 3]YesYes
MVDTtuple(1, 2, 3)NoYes
MVDTset{1, 2, 3}YesNo
MVDTdict{"a": 1}YesKeys: No

Quick Summary – What You Learned
  • A variable is a named memory location that stores a value
  • Python is dynamically typed — no need to declare the type manually
  • Multiple assignment allows x, y, z = 1, 2, 3 in one line
  • An identifier is the name given to a variable, function, or class
  • Identifiers cannot start with a digit, contain spaces, or use keywords
  • Python is case-sensitive — ageAge, and AGE are three different variables
  • SVDT holds one value — int, float, complex, bool
  • MVDT holds multiple values — str, list, tuple, set, dict
  • Use type() to check what data type any variable holds

Frequently Asked Questions (FAQ)

Q1. Do I need to declare a variable before using it in Python?

No. In Python, a variable is created the moment you assign a value to it. There is no separate declaration step. Just write age = 21 and the variable is ready to use.

Q2. What is the difference between a list and a tuple in Python?

Both store multiple ordered values. The key difference is mutability — a list is mutable (you can add, remove, or change items). A tuple is immutable (once created, its values cannot be changed). Use tuples for data that should not change, like coordinates.

Q3. Why does Python not require you to declare the data type?

Python uses dynamic typing — it infers the data type from the value you assign. When you write x = 10, Python automatically knows x is an integer. This makes Python faster to write and easier for beginners to learn.

Q4. What is the difference between a set and a list in Python?

A list is ordered and allows duplicate values. A set is unordered and automatically removes duplicates. Sets are faster for checking membership (using in), while lists are better when order and duplicates matter.

Q5. Can a variable name start with an underscore in Python?

Yes. A variable can start with an underscore, like _total or __name. Single underscore variables are often used for temporary or private values. Double underscore names have special meaning in Python classes.

Q6. What is the difference between a dictionary and a set in Python?

Both use curly braces {}, but they are different. A set stores individual unique values like {1, 2, 3}. A dictionary stores key-value pairs like {"name": "Rohan"}. An empty {} creates a dict, not a set — use set() for an empty set.


Found this post helpful? Share it with a friend who is just starting Python. Drop your questions in the comments — happy to help!

"Every Python expert once started exactly where you are. Keep learning, keep building."

Comments

Post a Comment

Popular posts from this blog

Python Strings – A Complete Guide for Beginners (with Examples)

Python Control Flow Statements – Complete Guide with Real-Life Examples

Python Loops Explained – for Loop and while Loop with Examples and Output