Skip to main content
5 Style Guide in Python Programming Language

5 Style Guide in Python Programming Language

Hello there, fellow developers! Before learning more about Python, it’s a good idea to familiarize yourself with the Style Guide When Learning the Python Programming Language so that you can understand the code properly and correctly when coding. Remember that Python prioritizes code readability. Did you know that programmers have access to a plethora of style guides?

Python is a programming language that is simple to learn for inexperienced programmers. The Python programming language can be used to create almost anything, including web applications, games, and apps.

5 Style Guide in Python

So, before learning the Python style guide, you should prepare the material first so you can practice it at the same time, because theory is just wishful thinking. The thing that must be ready is, of course, Python. Python can be downloaded from the official website here. Install Python, which we have successfully downloaded to our computer.

1. Python Syntax

The Python code we write can be run directly from the command line.

>>> print("Hello World")
Hello World

Line 2 is output.

You can also use the command line to run Python files created on the server with the .py extension.

python filename.py

Oh, and did you know that when we write a statement in Python, we don’t have to use a semicolon? Isn’t that cool? Use a semicolon to separate each statement if you write more than one line. However, writing more than one line in a single Python statement is not recommended, as this is considered the non-pythonic way.

2. Python Indent

When writing multilevel code, the Python programming language uses indented lines of code. In Python, indents are used to indicate code levels.

Example of code with proper indentation:

a = True
if a:
    print("variable a is true")
else:
    print("variable a is false")

An example of code with incorrect indentation:

a = True
if a:
print("variable a is true")
else:
print("variable a is false")

Line indents can be added with spaces or tabs. In a single Python file, use the same indentation consistently.

3. Variable Naming in Python

In Python, variable names can be written in a variety of styles, including:

  • lowercase_with_an_underscore_word_separator
  • UPPERCASE_WITH_AN_UNDERSCORE_WORD_SEPARATOR
  • uppercaseAndLowercase
  • Capital_Letters_With_Underscore_Separator

Oh, and avoid using the characters I (lowercase L), O (uppercase O), or I (uppercase I) as 1 character variable names because they are difficult to differentiate from the numbers 1 and 0. When using I (lowercase L), replace it with L (uppercase L).

4. Comment Code

Comments are lines of code that are not executed, but they provide information about the code we write. In Python, there are several ways to write comments, including:

  • Using Hash Sign (#)
# this is a comment
  • Using Triple Quotation Marks (“””. . .”””)
""" this is a comment """

These triple quotation marks are typically used when documenting programs or making multiline comments in Python.

5. Conditional (IF)

Python can also be used to express logical conditions derived from mathematics. Such as IF statements and loops. The following is an example of IF code in Python:

a = 6
b = 3
if a > b:
   print ("a is greater than b")

Conclusion

This is an article about 5 Python programming style guides. In fact, there are still many style guides in Python, which you can read on this page. If you have practiced, you can ask in the comments column if there is an error. Thank you very much.

Other interesting articles to read:

Share this

Shafy Gunawan

I’m a web developer. I spend my whole day, practically every day, experimenting with HTML, CSS, JavaScript, and PHP; dabbling with Python programming language; and share the knowledge that I learned.

Leave a Reply

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