Python Variables and Data Types Explained for Beginners
What is a Variable in Python?
Variable consider as a Container or Location where we can store data.
A variable is a name that refers to a value stored in memory.
Example :
name = "Tejaswini"
age = 23
Here:
namestores a stringagestores a number
Python automatically understands the data type.
Rules for Naming Variables
When creating variables in Python, follow these rules:
Variable name must start with a letter or underscore
Variable name cannot start with a number
Variable names are case-sensitive
No spaces allowed
No Special characters allowed except underscore(_).
Valid variable names:
my_name = "Python"
_age = 20
totalMarks = 90
Invalid variable names:
2name = "Wrong"
my name = "Wrong"
Constants in Python
A constant is a variable whose value should not be changed once assigned.
⚠️ Python does not provide built-in constants like some other languages.
Instead, Python uses a naming convention to represent constants.
How to Declare a Constant
In Python, constants are written in UPPERCASE letters.
Example:
PI = 3.14
MAX_USERS = 100
APP_NAME = "MyPythonApp"
By convention:
These values should not be changed
Other developers understand that these are constants
Why Use Constants?
Using constants helps:
Improve code readability
Avoid magic numbers
Maintain consistency across the program
Example:
TAX_RATE = 0.18
total_tax = price * TAX_RATE
This is clearer than writing 0.18 everywhere.
Python Data Types
Data type defines what kind of data or values a variable holds.
Python has several built-in data types. Let’s understand the most commonly used ones.
Integer (int)
Integers are whole numbers without decimal points.
Example:
count = 10
score = -5
Float (float)
Floats are numbers with decimal points.
Example:
price = 99.99
percentage = 85.5
String (str)
Strings are used to store text and are written inside quotes**(‘ ‘, “ “, ’’’ ‘‘‘).**
Example:
message = "Hello Python"
language = 'Python'
Boolean (bool)
Boolean data type stores only True or False values.
Example:
is_active = True
is_logged_in = False
Type Checking in Python
You can check the data type of a variable using the type() function.
Example:
x = 10
print(type(x))
Output :
<class 'int'>
Dynamic Typing in Python
Python is a dynamically typed language, which means you can change the data type of a variable.
Example:
x = 10
x = "Python"
This is allowed in Python.
Practice Examples
Try writing these programs:
Store your name and age in variables
Add two numbers and print the result
Check the data type of different variables
👍Practice will make these concepts clear.
Conclusion
Variables help store data, and data types define what kind of data is stored.
Once you understand variables and data types, learning Python becomes much easier.
