Code Conventions - IncrediCoders/Python1 GitHub Wiki

paul_ch1_avatar

Paul Python added this page on July 29, 2025


What are code conventions? We're glad you asked!

Code conventions are the code styling decisions we made in the book. Each convention and decision was made for various reasons, so let's get into it!

Table of Contents:

Case Styles

Typically, there are a few core casing styles (also called casing conventions or simply casing) that developers use: Snake Case VS Camel Case VS Pascal Case VS Kebab Case – What's the Difference Between Casings?

Dionysia Lemonaki did an amazing job writing that article that explains the different casing styles.

You name a lot of variables and functions when you code, so you want to be consistent with your naming conventions.

One thing to keep in mind is that the names of your variables and functions can't have spaces in them. So, these casing conventions don't include spaces, and they exist to make sure that the names you choose are consistent in how they appear.

Here is a very brief summary of the different casing styles:

  • camelCasing: Although the first word starts with a lowercase letter, each new word starts with a capital letter. It uses no spacing or dashes in between the words. This is also called medial casing (since you use uppercase in the middle), and it was started for the chemistry industry in 1813.
  • PascalCasing: A form of camel casing, this is also referred to as CamelCasing, where the first word has a capital "C." It was named after the Pascal programming language, which uses Pascal casing.
  • snake_casing: It uses an underscore between each word. It's also called underscore casing.
  • kebab-casing: It uses a dash between each word.
  • ALLCAPSCASING: Also known as the screaming case, it's all caps.
  • SCREAMING_SNAKE: All caps with snake casing, where you have an underscore between each word.

We landed on using three of these casings for the book:

  • Snake Casing: We use snake (or underscore) casing for functions, such as this_is_a_function(). A function is basically where you call a bunch of code to do a thing. More on that later.
  • Pascal/Camel Casing: We use the Pascal style of camel casing (with the first letter capped) for variables and lists.
  • All-Caps Casing: We use all-caps casing (also called scream casing) for environment variables, global variables, and ENUMS which are basically just variables that don't change (we want to keep the value throughout the program) or that are used across multiple functions, modules, and even files. For example, the variable SCREEN is in all caps, because it is an environment variable, which means that it sets up the environment. The environment includes things like the background, window, or screen.

Line Length

In the book, you will find some short lines of code and also some longer ones. A few of the long lines are shown across two printed lines so they can fit on the page. We do this on purpose to give you practice with both simple and more complex code, and to help you get comfortable reading, using, and fixing longer lines of code when you need to.

Initialization Files

An initialization file runs automatically when your program starts. It sets up important settings, prepares data, and ensures everything is ready before the main code runs. Think of it as your program’s “morning routine." Like "packing your bag" before going to school.

More resources about initialization files: Level 2: Initialization Files

Displaying Text

Because we’re displaying text in game windows, we draw the text to the screen. We don’t use the print() function, which prints to a text console.

Code Files

Throughout the book, we have a template file for readers to fill in the missing code. We also have the full solution files, which contain all the completed code.

The Repo Structure article provides a detailed walkthrough of all our files on this GitHub repo: Repo Structure

Advanced Coding

These advanced techniques include docstrings (which we use a little in this book; these are like comments when defining classes), sets (instead of lists), exception handling (which is especially useful for programs with data entry), advanced debugging (and the logging module), f-strings for organized string formatting, and list comprehensions (which we use a little).

  • Docstrings: These are string literals that you place inside the definition block of Python, to provide a description of what the class or function does. You place them between triple quotes, such as """" in the definition block.

    Example:

    def greet(name):
       """Greet the user by their name.""" 
       print(f"Hello, {name}!")
  • Sets: A Python set is a collection of unordered items. Lists (also called arrays) place the items in numbered slots but sets place them as unordered items. Typically, the items in a set are defined using the { } curly braces, but you can also use the set() function.

    Example:

    # Creating a set using curly braces
    fruits = {"apple", "banana", "cherry"}
    print(fruits) # Output: {'apple', 'banana', 'cherry'}
    # Creating a set using the set() function
    numbers = set([1, 2, 3, 4])
    print(numbers) # Output: {1, 2, 3, 4}
  • Exception Handling: An exception is an error that happen when you execute your code, and then your program crashes. Exception handling is how you manage and resolve those errors so that your program doesn't crash. Python uses some keywords to help you test your code so that you can find out why your program crashed. These keywords include try, except, else, and finally.

    Example:

    try:
       # Code that may raise an exception
       num = int(input("Enter a number: "))
       result = 10 / num
    except ValueError:
       print("Invalid input. Please enter a valid integer.")
    except ZeroDivisionError:
       print("Cannot divide by zero.")
    else:
       print(f"Result: {result}")
    finally:
       print("Execution completed.")
  • Advanced Debugging: There are many more methods you can use to debug your code to find out why your program crashes, why it doesn't perform as expected, or why a bug has appeared in your program.

    • Methods:
      • Use the built-in PBD debugger: Python provides PBD (Python De-Bugger) for you to pause the execution of your code, inspect the different variables, and move through each step of your code. PDB is imported like a module.

      • Use the breakpoint() function: In Python 3.7 and higher, you set the PYTHONBREAKPOINT environment variable.

      • Conditional breakpoints: You use PDB to stop the code when a condition is met.

        Example:

        import pdb
        
           for i in range(10):
              if i == 5:
                 pdb.set_trace()
              print(i)
      • Trace module: You use the trace module to show each line executed, which can help you understand the flow of your program.

      • Use logging: You can use logging instead of printing to get more details about the error.

    • For more information, see the Bonus Article: Bonus Article: Level 1: Troubleshooting & Error Handling
  • F-Strings: F-strings are formatted string literals that are in Python 3.6 and higher. They make string interpolation and formatting more concise, readable, and faster. You can create f-strings by prefixing a string with f and then embedding variables or expressions inside the { } curly braces.

  • List Comprehensions: Python list comprehensions is a faster and shorter way to create lists. You can assign a list of items to a variable, turning it into a list.

    Example:

    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    newlist = [x for x in fruits if "a" in x]
    print(newlist) # Output: ['apple', 'banana', 'mango']

Next Steps

We hope you took all that to heart, and you're ready to learn (or to continue learning)! Let's get back to the book! And you should probably also check out the rest of our resources, at IncrediCoders: Python Adventures - All Online Resources.

And you can take a gander at all the rest of our Big Book Buddies, which are pages we have on the Wiki that help out as buddies of the book!

⚠️ **GitHub.com Fallback** ⚠️