• Python
Python Fundamentals
  • Python Variables
  • Python Operators
  • Python Input-Output
  • Python Type Conversion
Python Data Types
  • Python Strings
  • Python List
  • Python Tuple
  • Python Dictionnaries
  • Python Sets
Python Flow Control
  • Python Conditions
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
Python Functions
  • Python Functions
  • Python Arguments
  • Python Functions Scope
  • Python Recursion
Python Classes
  • Python Classes
  • Python Classes and Static Methods
  • Python Properties
  • Python Decorators
  • Python Error Handling

Create an Account

FREE

Join our community to access more courses.

Create Account
  • Pricing
  • Blog

On this page

Python VariablesCreating and Assigning VariablesDisplaying VariablesChanging a Variable's ValueCommon Data Types and LiteralsRules and Conventions for Naming VariablesMultiple Assignment

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 or False. They are crucial for controlling program flow (e.g., in if 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):

  1. A variable name must start with a letter (a-z, A-Z) or an underscore (_).
  2. The rest of the name can only contain letters, numbers (0-9), and underscores.
  3. Variable names are case-sensitive (score and Score are different variables).
  4. You cannot use Python keywords (like if, for, class) as variable names.

Conventions (Best Practices):

  1. Use snake_case for variable names: all lowercase letters with words separated by underscores (e.g., player_score, high_score_limit).
  2. Choose descriptive names that clearly indicate the variable's purpose (e.g., user_name is better than un).
  3. 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

Continue Learning

Removing Duplicates

Popular

Personalized Recommendations

Log in to get more relevant recommendations based on your reading history.