Python Variables Continued: Assignments , Globals & Naming Styles

Welcome back! If you’ve read our first post Crack Python Basics: Learn Variables the Easy Way you’ve already taken the first step toward mastering Python. Now, with Python variables continued, we’re building on that foundation with slightly more advanced (but still beginner-friendly) concepts that will make your code cleaner, smarter, and easier to manage.

In this continuation, we’ll explore:

  • ✅ Assigning multiple values in one line
  • 🌍 How global variables work—and when to use them
  • 🧩 Naming multi-word variables with PascalCase, snake_case, and camelCase
  • 📜 Official rules for naming variables in Python
  • 📦 Unpacking lists into multiple variables
  • 🔒 Using constants to protect important values

These techniques will help you write more flexible, readable code. Let’s dive in and keep cracking those Python basics!

Assigning Multiple Values at Once

Python lets you assign values to multiple variables in a single line:

x, y, z = 1, 2, 3

Each variable gets its corresponding value. You can also assign the same value to multiple variables:

a = b = c = 0

This is handy when initializing several variables with the same starting value.

Global Variables in Python

Imagine you’re writing a story, and you want the main character’s name to appear in every chapter. Instead of repeating it, you define it once at the top—and every part of your story can use it. That’s how a global variable works.

A global variable is:

  • Defined outside of any function
  • Accessible from anywhere in your code—including inside functions

counter = 0  # global variable

def increment():
    global counter
    counter += 1

Without the global keyword, Python treats counter as a new local variable inside the function.

Pros and Cons of Global Variables

Pros:

  • Accessible from anywhere
  • Great for shared settings (e.g., flags, counters)
  • Simplifies small scripts
  • Quick to implement

Cons:

  • Harder to debug
  • Risk of accidental changes
  • Reduces modularity
  • Increases chance of name conflicts
  • Can lead to messy code

Best Practice: Use global variables sparingly. They’re fine for small scripts or shared constants, but for larger projects, steer towards local variables and function parameters to keep your code clean and predictable.

Multi-Word Variable Names: Naming Styles

Python doesn’t enforce a specific style for multi-word variable names, but consistency matters. Here are three common conventions:

StyleExampleUse Case
PascalCaseUserProfileClass names
snake_caseuser_profileVariables and functions in Python
camelCaseuserProfilePopular in other languages (e.g., JavaScript)

Tip: Stick with snake_case for variables and functions—it’s the Python community standard.

Python Variable Naming Rules

Here’s a quick checklist to keep your variable names valid:

  • ✅ Must start with a letter or underscore (_)
  • ❌ Cannot start with a number
  • ✅ Can include letters, numbers, and underscores
  • 🔍 Case-sensitive: age, Age, and AGE are different
  • 🚫 Cannot be a Python keyword (like for, if, class, etc.)

Want to see all reserved keywords? Try this:

import keyword
print(keyword.kwlist)

Unpacking Lists with Multiple Variable Assignment

Python makes it easy to unpack lists or tuples into variables:

colors = ["red", "green", "blue"]
r, g, b = colors

Each variable grabs one item from the list. You can also use the asterisk * to capture remaining items:

first, *middle, last = [1, 2, 3, 4, 5]
# first = 1, middle = [2, 3, 4], last = 5

This improves readability and avoids manual indexing.

Python Constants: Fixed Values That Speak Loudly

A constant is a variable that’s meant to stay the same throughout your program. Python doesn’t enforce constants, but developers follow a naming convention to signal intent:

Constants are written in ALL CAPS with underscores:

MAX_RETRIES = 5
API_URL = "https://example.com"
PI = 3.14159

This tells anyone reading your code: “This value is important—and it shouldn’t change.”

Why Use Constants?

  • Clarity: Descriptive names like MAX_RETRIES are easier to understand than raw numbers
  • Consistency: Define once, use everywhere
  • Safety: Reduces accidental changes
  • Team-friendly: ALL CAPS signals intent to other developers

Important Note: Python won’t stop you from changing a constant—it’s just a convention. So while this is allowed:

MAX_RETRIES = 5
MAX_RETRIES = 10  # Python allows this

…it’s considered bad practice. Treat constants with care and respect.

Best Practices for Constants

Don’t overuse them—keep your code clean and focused

Use ALL CAPS for constants

Define them at the top of your script or in a config file

Only use them for values that truly shouldn’t change

Python Variables Continued

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top