Declare a Variable in Python
Python is a dynamically typed programming language, which means you do not have to define the data type of a variable before using it. In Python, a variable is created automatically when you assign a value to a name. This makes Python easy to learn and gives beginners more flexibility while writing programs.
Variables are used to store data that can be needed later in a program. The stored data can be a number, text, or any other supported Python data type. For example, you can create a variable named age and assign it the value 21. Python automatically recognizes 21 as an integer.
age = 21
print(age)
Output:
21
In this tutorial, you will learn how to declare a variable in Python, the rules for choosing valid variable names, how to store numbers and strings, and how to assign values to multiple variables.
Table of Contents

What Is a Variable in Python?
A variable is a name used to refer to a value stored in a Python program. Unlike some programming languages, Python does not require you to declare the type of a variable separately.
For example:
name = "Akshay"
age = 21
marks = 85.5
Python automatically determines the type of each value. In this example, name is a string, age is an integer, and marks is a floating-point number.
Rules for Naming Variables in Python
Python has some rules that should be followed when creating variable names. Following these rules prevents syntax errors and makes code easier to understand.
- A variable name must begin with a letter or an underscore
_. - A variable name cannot begin with a number.
- Variable names can contain letters, numbers, and underscores.
- Special characters such as
@,#,%,^,&, and*are not allowed. - Variable names are case-sensitive.
- Python reserved keywords cannot be used as variable names.
Valid Variable Names
name = "Rahul"
age = 21
student_name = "Devansh"
_marks = 90
Invalid Variable Names
2name = "Rahul"
student-name = "Devansh"
class = "BCA"
The first example starts with a number, the second contains a hyphen, and class is a reserved Python keyword.
Variable Names Are Case-Sensitive
Python treats uppercase and lowercase letters as different characters. Therefore, age, Age, and AGE are considered different variable names.
age = 20
Age = 25
AGE = 30
print(age)
print(Age)
print(AGE)
Output:
20
25
30
For this reason, it is important to use consistent naming styles in your programs.
Declaring Numbers in Python
Python supports several numeric data types. The three basic types commonly used with variables are integer, floating-point, and complex numbers.
1. Integer Variable
An integer represents a whole number without a decimal point.
num = 25
print("The type of num:", type(num))
print(num)
Output:
The type of num: <class 'int'>
25
2. Floating-Point Variable
A floating-point number contains a decimal value.
float_num = 12.50
print("The type of float_num:", type(float_num))
print(float_num)
Output:
The type of float_num: <class 'float'>
12.5
3. Complex Number Variable
Python also supports complex numbers. A complex number contains a real part and an imaginary part.
complex_num = 2 + 5j
print("The type of complex_num:", type(complex_num))
print("Is complex_num a complex number?",
isinstance(complex_num, complex))
Output:
The type of complex_num: <class 'complex'>
Is complex_num a complex number? True
Python allows integers to be very large, so you can work with numbers without needing to specify a maximum length for a normal integer variable.
Declaring Strings in Python
A string is a sequence of Unicode characters. Python allows strings to be written using single quotes, double quotes, or triple quotes.
Using Single Quotes
str_var = 'UpdateGadh'
print(str_var)
print(type(str_var))
Output:
UpdateGadh
<class 'str'>
Using Double Quotes
str_var1 = "Welcome to UpdateGadh"
print(str_var1)
print(type(str_var1))
Output:
Welcome to UpdateGadh
<class 'str'>
Using Triple Quotes
Triple quotes can be used when a string needs to cover multiple lines.
str_var2 = '''This is a string
using triple
quotes'''
print(str_var2)
print(type(str_var2))
Output:
This is a string
using triple
quotes
<class 'str'>
How to Check the Type of a Variable
Python provides the built-in type() function to check the data type of a variable.
name = "Akshay"
age = 21
marks = 90.5
print(type(name))
print(type(age))
print(type(marks))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
This is useful when learning Python and when you need to understand what type of data a variable currently contains.
Multiple Assignments in Python
Python allows you to assign values to multiple variables in a single statement. This can make simple assignments shorter and easier to read.
1. Assigning Multiple Values to Multiple Variables
You can assign different values to different variables on the same line.
x, y = 10, 20
print(x, y)
Output:
10 20
Here, 10 is assigned to x and 20 is assigned to y.
2. Assigning One Value to Multiple Variables
You can also assign the same value to several variables at once.
a = b = c = "UpdateGadh"
print(a)
print(b)
print(c)
Output:
UpdateGadh
UpdateGadh
UpdateGadh
All three variables contain the same string value.
Can You Declare an Empty Variable in Python?
Python does not require a separate declaration statement for variables. If you want to create a variable without storing a meaningful value yet, you can assign None to it.
result = None
print(result)
Output:
None
None represents the absence of a value and can be useful when a variable will receive its actual value later in the program.
Best Practices for Naming Python Variables
Although Python allows many valid variable names, choosing clear names makes your programs easier to understand.
- Use meaningful names such as
student_nameinstead of unclear names. - Use underscores to separate words when needed.
- Avoid using Python keywords as variable names.
- Keep capitalization consistent throughout your program.
- Use names that describe the value stored in the variable.
For example:
student_name = "Rahul"
student_age = 21
student_marks = 85
These names clearly describe the information stored in each variable.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
Conclusion
Declaring a variable in Python is simple because Python is dynamically typed. You do not need to specify the variable’s data type before assigning a value. A variable is created automatically when you assign a value to a name.
Python supports different types of values, including integers, floating-point numbers, complex numbers, and strings. You can check a variable’s type using the type() function. Python also allows multiple assignments, making it possible to assign several values or the same value to multiple variables in a single statement.
By following Python’s variable naming rules and using meaningful names, you can write cleaner and easier-to-understand programs.
Frequently Asked Questions (FAQ)
1. How do you declare a variable in Python?
You simply assign a value to a variable name:
name = "Akshay"
age = 21
Python automatically determines the data type.
2. Do I need to specify the variable type in Python?
No. Python is dynamically typed, so you do not need to specify the type before assigning a value.
3. What are the rules for naming variables in Python?
A variable name must begin with a letter or underscore, cannot contain prohibited special characters, is case-sensitive, and cannot be a Python reserved keyword.
4. Are Python variable names case-sensitive?
Yes. For example, age and AGE are treated as different variables.
5. How do I declare an empty variable in Python?
You can assign None to a variable when it does not have a meaningful value yet.
value = None
6. How do I assign multiple variables in Python?
You can assign multiple values in one statement:
x, y = 10, 20
7. Can multiple variables have the same value?
Yes. Python allows you to assign one value to multiple variables:
a = b = c = "Python"
8. How can I check a variable’s data type?
Use the type() function:
number = 25
print(type(number))
Related Keywords
Declare a Variable in Python, declare a variable in Python without value, what is variable in Python with example, Python declare variable type, types of variables in Python with example, rules for naming variables in Python, data types in Python, variables in Python, Python variable declaration, declare an empty variable in Python, declare a variable in Python example, Python variables tutorial, Python multiple assignment, Python variable naming rules, for loop Python, Python compiler.
Download New Real Time Projects: Click Here