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.
# 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.
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
- Variable names can contain alphabets, digits, and underscores (_).
- The first character of a variable must be an alphabet or an underscore (_).
- Special symbols (except underscore) are not allowed in variable names.
- Keywords cannot be used as variable names.
- Use meaningful and descriptive variable names.
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.
- 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:- int
- float
- bool
- complex
- str
- bytes
- bytearray
- range
- list
- tuple
- set
- frozenset
- dict
- None
Exercise
- 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.