Python Naming Conventions Cheat Sheet



  1. Python Coding Standards Cheat Sheet
  2. Python Class Naming Convention
  3. Python Naming Conventions Cheat Sheet Answers
  4. Python Naming Conventions Cheat Sheet Pdf
  5. Naming Convention Python Project
  6. Python Naming Conventions Cheatsheet

In my Python workshops and online courses I see that one of the trickiest things for newcomers is the syntax itself. It’s very strict and many things might seem inconsistent at first. In this article I’ve collected the Python syntax essentials you should keep in mind as a data professional — and I added some formatting best practices as well, to help you keep your code nice and clean.

  1. Variables in Python Machine Learning in Python Summary: Variables are quite simply a placeholder for something. That something could be numbers, strings, booleans or any other object type. We will explore what a variable can be, how it should Read More Python Variables.
  2. We will go over the programming conventions for Python described by the PEP-8 document and you’ll emerge as a better programmer on the other side! Are you completely new to Python programming? Then I’d suggest first taking the free Python course before understanding this style guide.

When naming elements in Python the naming convention used are underscores between each word with no Camel/Pascal Case involved e.g. Testfile.py = testfile.py When naming elements, the defined element name cannot exist in the reserved builtin element names. To find the reserved names built into Python 3, run the below commands.

These are the basics. If you want to go deep down the rabbit hole, I’ll link to some advanced Python syntax and formatting tutorials at the end of this article!

This article is the part of my Python for Data Science article series. If you haven’t done so yet, please start with these articles first:

The 3 major things to keep in mind about Python syntax

#1 Line Breaks Matter

Unlike in SQL, in Python, line breaks matter. Which means that in 99% of cases, if you put a line break where you shouldn’t put one, you will get an error message. Is it weird? Hey, at least you don’t have to add semicolons at the end of every line.

So here’s Python syntax rule #1: one statement per line.

There are some exceptions, though. Expressions

  • in parentheses (eg. functions and methods),
  • in bracket frames (eg. lists),
  • and in curly braces (eg. directories)

can actually be split into more lines. This is called implicit line joining and it is a great help when working with bigger data structures.

Additionally, you can also break any expression into more than one line if you use a backslash () at the end of the line. And you can do the opposite, too: inserting more than one statement into one line using semicolons (;) between the statements. However, these two methods are not too common, and I’d recommend using them only when necessary. (E.g. with really long, 80+ character long statements.)

one line – more statements

Python Coding Standards Cheat Sheet

a dummy example: one statement in more lines — print(‘Hello’)

#2 Indentations Matter

Do you hate indentations? Well, you are not alone. Many people who are just starting off with Python dislike the concept. For non-programmers it is unusual and on the top of that it causes the most errors in their scripts at the beginning. As for me, I love indentations and I promise that you will get used to them, too. Well, if you’ve worked your way through my Python articles so far, I’m pretty sure that you already have.

Why do we need indentations? Easy: somehow you have to indicate which code blocks belong together — e.g. what is the beginning and the end of an if statement or a for loop. In other languages, where you don’t have to use indentations, you have to use something else for that: e.g. in JavaScript you have to use extra brackets to frame your code blocks; in bash you have to use extra keywords. In Python, you have to use indentations – which in my opinion is the most elegant way to solve this issue.

So we have Python syntax rule #2: make sure that you use indentations correctly and consistently.
Note: I talked about the exact syntax rules governing for loops and if statements in the relevant articles.

One more thing: if you watch the Silicon Valley TV show, you might have heard about the debate of “tabs vs spaces.” Here’s the hilarious scene:

So tabs or spaces? Here’s what the original Style Guide for Python Code says:

Pretty straight forward!
ps. To be honest, in Jupyter Notebook, I use tabs.

#3 Case Sensitivity

Python is case sensitive. It makes a difference whether you type and (correct) or AND (won’t work). As a rule of thumb, learn that most of the Python keywords have to be written with lowercase letters. The most commonly used exceptions I have to mention here (because I see many beginners have trouble with it) are the Boolean values. These are correctly spelled as: True and False. (Not TRUE, nor true.)

There’s Python syntax rule #3: Python is case sensitive.

Other Python Best Practices for Nicer Formatting

Let me just list a few (non-mandatory but highly recommended) Python best practices that will make your code much nicer, more readable and more reusable.

Python Best Practice #1: Use Comments

Python Class Naming Convention

You can add comments to your Python code. Simply use the # character. Everything that comes after the # won’t be executed.

Python Best Practice #2: Variable Names

Conventionally, variable names should be written with lower letters, and the words in them separated by _ characters. Also, generally I do not recommend using one letter variable names in your code. Using meaningful and easy-to-distinguish variable names helps other programmers a lot when they want to understand your code.

my_meaningful_variable = 100

Python Best Practice #3: Use blank lines

If you want to separate code blocks visually (e.g. when you have a 100 line Python script in which you have 10-12 blocks that belong together) you can use blank lines. Even multiple blank lines. It won’t affect the result of your script.

same script – with blank lines

Python Best Practice #4: Use white spaces around operators and assignments

For cleaner code it’s worth using spaces around your = signs and your mathematical and comparison operators (>, <, +, -, etc.). If you don’t use white spaces, your code will run anyway, but again: the cleaner the code, the easier to read it, the easier to reuse it.

Python Best Practice #5: Max line length should be 79 characters

If you reach 79 characters in a line, it’s recommended to break your code into more lines. Use the above-mentioned character. Using the at the end of the line, Python will ignore the line break and will read your code as if it were one line.
(Or in some cases you can take advantage of implicit line joining.)

Python Best Practice #6: Stay consistent

And one of the most important rules: always stay consistent! Even if you follow the above rules, in specific situations you’ll have to create your own. Either way: make sure you are using these rules consistently. Ideally, you have to create Python scripts that you can open 6 months later without any trouble understanding them. If you randomly change your formatting rules and naming conventions, you’ll create an unnecessary headache for your future self. So stay consistent!

The Zen of Python – a nice easter egg

What else could come at the end of this article but a nice Python easter egg.
If you type import this to your Jupyter Notebook you will get the 19 design “commandments” of Python:

Python Naming Conventions Cheat Sheet

Use these advices wisely! 😉

Conclusion

Well this is it. Follow this advice, and if you want to learn more about Python syntax essentials and best practices, I recommend these articles:

  • Google’s Python Style Guide,
  • PEP8,
  • BOBP guide.

Now go ahead and check out the last article of the series: how to import Python libraries!

  • If you want to learn more about how to become a data scientist, take my 50-minute video course: How to Become a Data Scientist. (It’s free!)
  • Also check out my 6-week online course: The Junior Data Scientist’s First Month video course.

Cheers,
Tomi Mester

In my Python workshops and online courses I see that one of the trickiest things for newcomers is the syntax itself. It’s very strict and many things might seem inconsistent at first. In this article I’ve collected the Python syntax essentials you should keep in mind as a data professional — and I added some formatting best practices as well, to help you keep your code nice and clean.

These are the basics. If you want to go deep down the rabbit hole, I’ll link to some advanced Python syntax and formatting tutorials at the end of this article!

This article is the part of my Python for Data Science article series. If you haven’t done so yet, please start with these articles first:

The 3 major things to keep in mind about Python syntax

#1 Line Breaks Matter

Unlike in SQL, in Python, line breaks matter. Which means that in 99% of cases, if you put a line break where you shouldn’t put one, you will get an error message. Is it weird? Hey, at least you don’t have to add semicolons at the end of every line.

So here’s Python syntax rule #1: one statement per line.

There are some exceptions, though. Expressions

  • in parentheses (eg. functions and methods),
  • in bracket frames (eg. lists),
  • and in curly braces (eg. directories)

can actually be split into more lines. This is called implicit line joining and it is a great help when working with bigger data structures.

Additionally, you can also break any expression into more than one line if you use a backslash () at the end of the line. And you can do the opposite, too: inserting more than one statement into one line using semicolons (;) between the statements. However, these two methods are not too common, and I’d recommend using them only when necessary. (E.g. with really long, 80+ character long statements.)

one line – more statements

a dummy example: one statement in more lines — print(‘Hello’)

#2 Indentations Matter

Do you hate indentations? Well, you are not alone. Many people who are just starting off with Python dislike the concept. For non-programmers it is unusual and on the top of that it causes the most errors in their scripts at the beginning. As for me, I love indentations and I promise that you will get used to them, too. Well, if you’ve worked your way through my Python articles so far, I’m pretty sure that you already have.

Why do we need indentations? Easy: somehow you have to indicate which code blocks belong together — e.g. what is the beginning and the end of an if statement or a for loop. In other languages, where you don’t have to use indentations, you have to use something else for that: e.g. in JavaScript you have to use extra brackets to frame your code blocks; in bash you have to use extra keywords. In Python, you have to use indentations – which in my opinion is the most elegant way to solve this issue.

So we have Python syntax rule #2: make sure that you use indentations correctly and consistently.
Note: I talked about the exact syntax rules governing for loops and if statements in the relevant articles.

One more thing: if you watch the Silicon Valley TV show, you might have heard about the debate of “tabs vs spaces.” Here’s the hilarious scene:

So tabs or spaces? Here’s what the original Style Guide for Python Code says:

Pretty straight forward!
ps. To be honest, in Jupyter Notebook, I use tabs.

Sheet

#3 Case Sensitivity

Python is case sensitive. It makes a difference whether you type and (correct) or AND (won’t work). As a rule of thumb, learn that most of the Python keywords have to be written with lowercase letters. The most commonly used exceptions I have to mention here (because I see many beginners have trouble with it) are the Boolean values. These are correctly spelled as: True and False. (Not TRUE, nor true.)

There’s Python syntax rule #3: Python is case sensitive.

Other Python Best Practices for Nicer Formatting

Let me just list a few (non-mandatory but highly recommended) Python best practices that will make your code much nicer, more readable and more reusable.

Python Best Practice #1: Use Comments

Cheat

You can add comments to your Python code. Simply use the # character. Everything that comes after the # won’t be executed.

Python Best Practice #2: Variable Names

Conventionally, variable names should be written with lower letters, and the words in them separated by _ characters. Also, generally I do not recommend using one letter variable names in your code. Using meaningful and easy-to-distinguish variable names helps other programmers a lot when they want to understand your code.

my_meaningful_variable = 100

Python Best Practice #3: Use blank lines

If you want to separate code blocks visually (e.g. when you have a 100 line Python script in which you have 10-12 blocks that belong together) you can use blank lines. Even multiple blank lines. It won’t affect the result of your script.

same script – with blank lines

Python Best Practice #4: Use white spaces around operators and assignments

For cleaner code it’s worth using spaces around your = signs and your mathematical and comparison operators (>, <, +, -, etc.). If you don’t use white spaces, your code will run anyway, but again: the cleaner the code, the easier to read it, the easier to reuse it.

Python Best Practice #5: Max line length should be 79 characters

Python Naming Conventions Cheat Sheet Answers

If you reach 79 characters in a line, it’s recommended to break your code into more lines. Use the above-mentioned character. Using the at the end of the line, Python will ignore the line break and will read your code as if it were one line.
(Or in some cases you can take advantage of implicit line joining.)

Python Best Practice #6: Stay consistent

And one of the most important rules: always stay consistent! Even if you follow the above rules, in specific situations you’ll have to create your own. Either way: make sure you are using these rules consistently. Ideally, you have to create Python scripts that you can open 6 months later without any trouble understanding them. If you randomly change your formatting rules and naming conventions, you’ll create an unnecessary headache for your future self. So stay consistent!

The Zen of Python – a nice easter egg

What else could come at the end of this article but a nice Python easter egg.
If you type import this to your Jupyter Notebook you will get the 19 design “commandments” of Python:

Use these advices wisely! 😉

Python Naming Conventions Cheat Sheet Pdf

Conclusion

Python naming conventions pdf

Well this is it. Follow this advice, and if you want to learn more about Python syntax essentials and best practices, I recommend these articles:

  • Google’s Python Style Guide,
  • PEP8,
  • BOBP guide.

Now go ahead and check out the last article of the series: how to import Python libraries!

Naming Convention Python Project

  • If you want to learn more about how to become a data scientist, take my 50-minute video course: How to Become a Data Scientist. (It’s free!)
  • Also check out my 6-week online course: The Junior Data Scientist’s First Month video course.

Python Naming Conventions Cheatsheet

Cheers,
Tomi Mester