Python Variables
Python Variables
In programming, a variable is a name given to a memory location. It's a way to label and store data so you can use and modify it later in your program.
Creating and Assigning Variables
You create a variable the moment you first assign a value to it using the assignment operator (=
). Python is dynamically-typed, which means you don't have to declare the variable's type; it's inferred automatically from the assigned value.
# The variable 'score' stores an integer (int)
score = 100
# 'player_name' stores a string of text (str)
player_name = "Alex"
# 'is_game_over' stores a boolean value (bool)
is_game_over = False
Displaying Variables
The most common way to see a variable's value is with the print()
function. You can use f-strings (formatted strings) to easily embed variables inside text.
level = 5
print(f"Welcome, {player_name}! You are on level {level}.")
# Output: Welcome, Alex! You are on level 5.
Changing a Variable's Value
You can update a variable by assigning it a new value.
points = 50
print(points) # Output: 50
# Reassign a completely new value
points = 75
print(points) # Output: 75
# Update the value based on the current one
points = points + 10 # Now 85
# Use an augmented assignment operator (shorthand)
points += 5 # Same as points = points + 5. Now 90
print(points) # Output: 90
Common Data Types and Literals
A "literal" is a fixed value written directly in the source code, like 100
or "Hello"
. The type of literal you use determines the variable's data type.
-
Numbers (
int
,float
): Integers (int
) are whole numbers. Floats (float
) are numbers with a decimal point.player_age = 28 # An integer item_price = 19.99 # A float
-
Text (
str
): A sequence of characters, known as a string. You create them with single ('
) or double ("
) quotes.message = "Game Over" button_text = 'Start'
-
Booleans (
bool
): Represents one of two values:True
orFalse
. They are crucial for controlling program flow (e.g., inif
statements).is_active = True has_key = False
-
None Type (
None
): A special type representing the absence of a value. It's often used as a placeholder.current_weapon = None # The player has not selected a weapon yet
Rules and Conventions for Naming Variables
Rules (Required):
- A variable name must start with a letter (a-z, A-Z) or an underscore (
_
). - The rest of the name can only contain letters, numbers (0-9), and underscores.
- Variable names are case-sensitive (
score
andScore
are different variables). - You cannot use Python keywords (like
if
,for
,class
) as variable names.
Conventions (Best Practices):
- Use
snake_case
for variable names: all lowercase letters with words separated by underscores (e.g.,player_score
,high_score_limit
). - Choose descriptive names that clearly indicate the variable's purpose (e.g.,
user_name
is better thanun
). - For constants (values that are not meant to change), use
ALL_CAPS
(e.g.,MAX_LIVES = 3
,PI = 3.14159
).
Multiple Assignment
You can assign values to multiple variables on a single line, which can make your code more compact.
# Assign values to three different variables
x, y, z = 10, 20, 30
# Assign the same value to multiple variables
gold = silver = bronze = 0
# A common trick to swap the values of two variables
a = 5
b = 10
a, b = b, a # Now a is 10 and b is 5