In this guide, you'll learn what Python variables are, how to create them, naming rules & examples.
What is a Variable in Python?
A variable is a container used to store data values. Instead of repeatedly typing the same value we can save it in a variable & use the variable name whenever needed.Example:
name = "kumar"
age = 25
print(name)
print(age)
Output:
kumar
25
In this example:
- name stores the text "kumar"
- age stores the number 25
Creating Variables in Python
The Python makes variable creation simple. You don't need to declare a variable type before using it.Syntax:
variable_name = value
Example:
city = "New York"
temperature = 30
The Python automatically determines the data type based on the assigned value.
Variable Assignment Examples
String Variable :message = "Hello World"
print(message)
Integer Variable :
number = 100
print(number)
Float Variable :
price = 49.99
print(price)
Boolean Variable :
is_active = True
print(is_active)
Python Variable Naming Rules
When creating variables follow these rules:Valid Variable Names :
name = "kumar"
user_age = 20
totalPrice = 100
_myVar = 50
Invalid Variable Names :
2name = "kumar"
user-age = 20
class = "Python"
Rules:
- The Variable names must start with a letter or underscore (_).
- They cannot start with a number.
- They can contain letters, numbers & underscores.
- Variable names are case-sensitive.
- Reserved keywords cannot be used as variable names.
Multiple Variable Assignment
The Python allows assigning values to multiple variables in one line.Example:
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Assign Same Value to Multiple Variables
a = b = c = 100print(a)
print(b)
print(c)
Changing Variable Values
The Variables can be updated anytime.score = 50
print(score)
score = 75
print(score)
Output:
50
75
Getting the Variable Data Type
Use the type() function to find the data type of a variable.name = "Python"
age = 10
price = 99.99
print(type(name))
print(type(age))
print(type(price))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
Global Variables
A global variable is created outside a function & can be accessed throughout the program.language = "Python"
def show_language():
print(language)
show_language()
Output:
Python
def show_language():
print(language)
show_language()
Output:
Python
Local Variables
A local variable is created inside a function & can only be used within that function.def display():
message = "Hello"
print(message)
display()
print(message)
display()
Constants in Python
The Python does not have true constants but programmers use uppercase names to indicate values that should not change.PI = 3.14159
MAX_USERS = 100
Example for Python Variable
1) Swap Two Variables :
a = 5
b = 10
a, b = b, a
print(a, b)
Output :
10 5
- The values of a and b are exchanged using the Python's tuple unpacking feature.
- After swapping, a becomes 10 & b becomes 5.
2) Find Average :
a = 10
b = 20
c = 30
average = (a + b + c) / 3
print(average)
Output :
20.0
Explanation:
- The program adds the three numbers & divides the total by 3.
- The calculated average of 10, 20 & 30 is 20.0.
Python Variables Interview Questions and Answers
1. What is a variable in Python?A variable is a named container used to store data values in memory. It allows programmers to save & reuse information throughout a program.
Example:
name = "kumar"
age = 25
2. How do you create a variable in Python?
The Variables are created by assigning a value using the assignment operator (=). The Python automatically determines the variable's data type.
Example:
city = "Mumbai"
temperature = 30
3. Is Python case-sensitive when dealing with variables?
Yes, Python is case-sensitive. The Variables with different letter cases are treated as separate variables.
Example:
name = "kumar"
Name = "David"
print(name) # kumar
print(Name) # David
4. What are the rules for naming variables in Python?
- Must start with a letter or underscore (_)
- Cannot start with a number
- Can contain letters, numbers & underscores
- Cannot use Python keywords
user_name = "Alex"
_age = 20