Python Variables
Variables are one of the most important concepts in Python. They are used to store data that can be used and modified throughout a program.
Think of a variable as a container that holds information. You can store values such as text, numbers, or other types of data inside a variable and use them whenever needed.
What is a Variable?
A variable is a name that refers to a value.
Example:
name = "John"
In this example:
-
nameis the variable name. -
"John"is the value stored in the variable.
You can display the value using the function:
print()
name = "John"
print(name)
Output:
John
Creating Variables
In Python, you do not need to declare the type of a variable before using it.
Simply assign a value using the equals sign (=):
age = 25
city = "Delhi"
price = 99.99
Python automatically determines the data type based on the assigned value.
Variable Types
Python variables can store different types of data.
String (Text)
name = "Alice"
Strings are used to store text.
Integer (Whole Numbers)
age = 20
Integers store whole numbers.
Float (Decimal Numbers)
price = 19.99
Floats store numbers with decimal points.
Boolean (True or False)
is_active = True
Booleans represent logical values.
Displaying Variables
Use the function to display variable values.
print()
name = "Alice"
print(name)
Output:
Alice
You can also print multiple variables:
name = "Alice"
age = 20
print(name, age)
Output:
Alice 20
Changing Variable Values
Variable values can be changed at any time.
name = "Alice"
name = "Bob"
print(name)
Output:
Bob
The old value is replaced with the new one.
Assigning Multiple Variables
You can assign values to multiple variables in a single line.
name, age, city = "Alice", 20, "Delhi"
print(name)
print(age)
print(city)
Output:
Alice
20
Delhi
Assigning One Value to Multiple Variables
Python allows multiple variables to share the same value.
x = y = z = 100
print(x)
print(y)
print(z)
Output:
100
100
100
Variable Naming Rules
When naming variables, follow these rules:
Valid Variable Names
name = "John"
user_age = 25
totalPrice = 100
Invalid Variable Names
2name = "John"
user-name = "John"
class = "Python"
These are invalid because:
- Variable names cannot start with a number.
-
Hyphens (
-) are not allowed. - Python keywords cannot be used as variable names.
Variable Naming Best Practices
Use meaningful names that describe the stored data.
Good:
student_name = "John"
total_price = 500
Bad:
a = "John"
x = 500
Descriptive names make your code easier to read and maintain.
Getting the Variable Type
Use the function to check the data type of a variable.
type()
name = "John"
print(type(name))
Output:
<class 'str'>
Examples:
age = 25
print(type(age))
Output:
<class 'int'>
price = 19.99
print(type(price))
Output:
<class 'float'>
Case-Sensitive Variables
Python treats uppercase and lowercase letters differently.
name = "John"
Name = "Alice"
print(name)
print(Name)
Output:
John Alice
Here, name and Name are two different variables.
Global Variables
A variable created outside of a function is called a global variable. Global variables can be accessed from anywhere in the program, including inside functions.
Example
name = "John"
def greet():
print("Hello,", name)
greet()
Output:
Hello, John
In this example, the variable name is defined outside the function, so it can be used inside the greet() function.
Creating a Global Variable Inside a Function
Normally, variables created inside a function are local and can only be used within that function. To create a global variable inside a function, use the global keyword.
def create_user():
global username
username = "Alice"
create_user()
print(username)
Output:
Alice
Modifying a Global Variable
To change the value of a global variable inside a function, you must use the global keyword.
count = 10
def update_count():
global count
count = 20
update_count()
print(count)
Output:
20
Constants in Python
Python does not have true constants, but developers usually use uppercase variable names to indicate that a value should not change.
PI = 3.14159
MAX_USERS = 100
This is a naming convention and not enforced by Python.
Real-World Example
student_name = "Rahul"
age = 18
course = "Python"
print("Student:", student_name)
print("Age:", age)
print("Course:", course)
Output:
Output:
Student: Rahul
Age: 18
Course: Python
Summary
- Variables are used to store data.
-
Create variables using the equals sign (
=). - Python automatically determines the data type.
- Variable values can be changed at any time.
- Use meaningful variable names.
- Variable names are case-sensitive.
-
Use
to check a variable's data type.type()
Variables are the foundation of Python programming, and understanding them is essential before moving on to data types, operators, and control structures.