What are Variables?
In Python, a variable is a container used to store data. You can think of a variable as a box that holds a value. Each variable has a name, and you can use that name to access the value stored inside.
Creating a Variable
To create a variable, you simply choose a name for it and assign it a value. In Python, we use the =
sign to assign a value to a variable.
Example:
age = 25
In this example:
age
is the name of the variable.25
is the value stored in the variable.
Printing the Value of a Variable
To see the value stored in a variable, you can use the print()
function.
Example:
age = 25
print(age)
Sample Output:
25
Types of Values
Python variables can store different types of values. Here are the most common types:
- Numbers:
- Integers: Whole numbers.
- Floats: Decimal numbers.
Example of both:
age = 25 # Integer
height = 5.7 # Float
- Text (Strings): This is used to store words or sentences. A string is enclosed in quotes.
Example:
name = "John"
- Booleans: These represent two values,
True
orFalse
.
Example:
is_student = True
- Lists: A list is an ordered collection of items, which can be numbers, strings, or other variables.
Example:
fruits = ["apple", "banana", "cherry"]
- Dictionaries: A dictionary is a collection of key-value pairs.
Example:
person = {"name": "John", "age": 25}
Changing the Value of a Variable
Once you’ve created a variable, you can change its value by simply assigning a new value to it.
Example:
age = 25
print(age)
age = 30
print(age)
Sample Output:
25
30
Variable Naming Rules
There are some rules for naming variables in Python:
- Variable names can only contain letters, numbers, and underscores (
_
). - A variable name must start with a letter or an underscore, not a number.
- Variable names are case-sensitive, meaning
age
andAge
are two different variables. - You cannot use Python reserved words like
True
,False
,if
,else
,class
, etc.
Dynamic Typing
Python is dynamically typed, which means you don’t need to specify the type of a variable when you create it. Python automatically determines the type based on the value assigned to the variable.
Example:
my_var = 10 # Python sees this as an integer
my_var = "Hello" # Now, Python sees this as a string
Multiple Variables in One Line
You can assign values to multiple variables in one line by separating them with commas.
Example:
name, age, height = "John", 25, 5.7
print(name)
print(age)
print(height)
Sample Output:
John
25
5.7
Constants
In Python, variables are usually not constant, meaning you can change their value. However, you can create a variable that should not be changed by convention. In Python, this is done by writing the variable name in all capital letters.
Example:
PI = 3.14159
Example of Using Variables Together
You can use variables together in expressions, like math calculations or combining text.
Example:
first_name = "John"
last_name = "Doe"
age = 25
full_name = first_name + " " + last_name
print(full_name)
years_to_100 = 100 - age
print("Years to 100:", years_to_100)
Sample Output:
John Doe
Years to 100: 75