DEV Community

Sarah Bartley-Dye
Sarah Bartley-Dye

Posted on

Introduction to Python Module Two Part Two Best Practices

This week, the SoloLearn Introduction to Python course is moving right along with the next part of module two. Part two of this lesson is about some of the best practices and standards in Python. SoloLearn focuses on comments and some of the best practices you need to know for creating variables in Python.

Every programming language has best practices and standards that all developers are encouraged to follow. Best practices and standards are there to make a developer’s job easier. It helps developers collaborate with others on a project and make sure everyone is on the same page.

Comments

Comments are a universal best practice in every programming language, so this doesn’t just apply to Python. Comments are how developers can communicate with each other in a code file. Comments help when developers debug code because they can disable code without deleting it.

If the fix doesn’t work, they can add the code easily by removing the comment. There are a couple of ways to write a comment in Python.  If you only want to put a single comment, you can use the hashtag symbol (#).

Just put the hashtag at the beginning of the line you want to comment out, then write your message after it. When you run your code, the computer will see the comment as a specific instruction, so it will ignore it, not appearing in your web app.

# This is how a single comment is written in Python. It will not appear as you run your code.
print("This is code. It will appear in the console.")
Enter fullscreen mode Exit fullscreen mode

If you need to comment multiple lines of code, you will need to use single quotes (‘). To comment multiple lines of code, you will need to start with three single quotes before your comment. Once you have finished, end the comment with three single quotes.

‘’’
This is a way to comment multiple lines of code. 

print("Hello World")

Now let’s end the comment.
‘’’
def greeting():
    print(“Hello”)
greeting()
Enter fullscreen mode Exit fullscreen mode

Variable Best Practices

You have learned a little bit about Python so you can start learning some best practices. Let’s talk about some best practices you need to know about variables. First, be careful with what letters you capitalize and which ones are lowercase.

Python is case-sensitive. This means that letters in the wrong case mean different things. So your variable names are lowercase.

score = 10
num = 4
Enter fullscreen mode Exit fullscreen mode

Next, use snake case. Snake case uses underscores to create spaces in a variable name.

player_one_score = 0
player_two_score = 4
Enter fullscreen mode Exit fullscreen mode

Finally, don’t start a variable with a number. You can use numbers in variable names, but avoid starting the name of the variable with a number.

Top comments (0)