Getting started
Basic concepts: taking our first steps
Using an IDE
Objects and classes
Loops, ranges, and iterators
Working with files
Exception handling
Python data types
Course project: building a TODO application
And now what?

Variable naming

In my examples, I’ve picked the general name result, but you can choose any name you deem appropriate. As a general rule, always pick a variable name that best describes its contents. This practice makes your code more readable and easy to understand, not just for others, but also for future you. Trust me when I say that, if you read back your own code a few weeks or months later, you’ll be surprised at how much effort it requires you to grasp what’s going on.

If we were calculating the total price of a shopping cart here, for example, a good name would be shopping_cart_total.

Don’t skimp on the number of characters in your variable names. It’s better to have clean, readable names like shopping_cart_total instead of an abbreviation like sct. Even total is probably too short, because, when reading the code later on, you’ll need to spend time finding out what total is being calculated. As you’ll soon learn, a good code editor auto-completes things like variable names, so you don’t have to type them in completely if that’s what you’re worried about. In other words, you won’t save much time by using shorter variable names.

Valid Python variable names

Some characters are not allowed in a variable name; there are a few rules we need to abide by. Let’s start with the complete list of valid characters that can appear in a variable name:

  • Lowercase and uppercase letters: a-z and A-Z
  • Numbers: 0-9
  • Underscores: _

Additionally, there are these two rules:

  • Variable names must begin with a letter or the underscore character and can not start with a number.
  • Names are case-sensitive

Here are some valid variable names:

  • name_1
  • name_2
  • _database_connection

These are invalid names:

  • 1tomany (don’t start with a number)
  • my-number (- is not allowed)
  • my number (spaces are not allowed)

And these variables are not the same, due to case sensitivity:

  • cartTotal
  • carttotal

A note on camel-case

This is for those who come from another programming language, like C# or Java. Many programming languages use camel-case to name variables. With camel-case, we use uppercase letters to more clearly separate words. We can use camel-case in Python, but we prefer underscores for variable names. So instead of shoppingCartTotal, we Pythonistas use shopping_cart_total. However, camel-case is the norm for class names like ShoppingCart. If you don’t know what class names are, don’t worry; we’ll get to those later.