Data Types in Python

What Is an Identifier in Python?

An identifier is the name used for variables, functions, classes, or objects to help recognize them in a program. It must begin with a letter (A-Z or a-z) or an underscore (_) and can include letters, numbers (0-9), or underscores after that. However, you cannot use Python's built-in keywords as identifiers.

Python Identifiers Examples
# Variable identifier
student_name = "John Doe" # Stores student name

# Function identifier
def calculate_sum(a, b): # Adds two numbers
    return a + b

# Class identifier
class CustomerOrder: # Stores customer orders
    pass

What Are Variables in Python?

A variable is a name that stores data in Python. It's value can change during program execution. Variables can hold different types of data like numbers, text, or lists.

Python Variables Examples
age = 25 # Stores the number 25 in the variable 'age'  
message = "Hello!" # Assigns text to 'message'  
age = 30 # Updates 'age' to 30  

Rules for Using Variables in Python

  1. Variable names can contain alphabets, digits, and underscores (_).
  2. The first character of a variable must be an alphabet or an underscore (_).
  3. Special symbols (except underscore) are not allowed in variable names.
  4. Keywords cannot be used as variable names.
  5. Use meaningful and descriptive variable names.
Python Variable Naming Examples
employee_name = "John Doe"
# 1employee_name = "John Doe"  # Invalid: Variable names cannot start with a digit.

monthly_salary = 5000.0
# monthly-salary = 5000.0  # Invalid: Hyphens are not allowed in variable names.

_employee_id = 12345
# _employee@id = 12345  # Invalid: '@' is not allowed in variable names.

employee_age_30 = 30
# 30_employee_age = 30  # Invalid: Variable names cannot start with a digit.

emp_sal = 4500.0
# for = 4500.0  # Invalid: 'for' is a Python keyword and cannot be used as a variable name.

What Are Data Types in Python?

In Python, data types specify the kind of value a variable can store, such as numbers, text, or True/False. They ensure efficient memory allocation and help the computer process data correctly.

Analogy:

Think of data types as containers. Just like different containers store specific items, data types in Python hold different kinds of values. For example:

  • A bottle stores liquids.
  • A measuring cup stores precise amounts of liquid.
  • A bookshelf stores books.
Similarly, in Python:
  • An integer stores whole numbers.
  • A float stores decimal numbers.
  • A string stores text.
  • A boolean stores True or False.

Each container (data type) is designed to store specific types of data, keeping everything organized and efficient.

Classification of Data Types in Python

In Python Programming, we have 14 data types and they are classified into 6 categories. They are:

1. Fundamental Data Types:
  1. int
  2. float
  3. bool
  4. complex
2. Sequential Data Types:
  1. str
  2. bytes
  3. bytearray
  4. range
3. List Data Types (Collection Data Types):
  1. list
  2. tuple
4. Set Data Types (Collection Data Types):
  1. set
  2. frozenset
5. Dict Data Types (Collection Data Types):
  1. dict
6. NoneType Data Types:
  1. None

Exercise

  1. Fill in the Blanks:
    • - An _______ is the name used for variables, functions, classes, or objects in Python.
    • - A variable in Python must begin with a _______ or an _______.
    • - Special symbols (except _______) are not allowed in variable names.
    • - The four fundamental data types in Python are _______, _______, _______, and _______.
    • - The _________ data type represents the absence of a value.