Python Forum
New to python, question regarding PEP8 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: New to python, question regarding PEP8 (/thread-36936.html)



New to python, question regarding PEP8 - AthertonH - Apr-13-2022

definitions = {
	"For loops": "For loops allow you to run a block of code repeatedly, just like while loops. However, for loops run "
	             "a block of code a set number of times.",
	"Functions": "A function is a block of code repeatedly, just like while loops. However, for loops run a block of"
	             "code a set number of times",
	"If statements": "An if statement runs a block of code based on whether or not a condition is true.",
	"Loops": "Loops check a condition and then run a code block. The loop will continue to check and run until a "
	         "specified condition is reached.",
	"Python": "Python is a programming language that's currently becoming more and more powerful with every new library"
	          " added to its collection."
}
Hey everyone, I'm going through this book by Eric Mathes, and I'm trying to develop good habits to use in the future. Currently, I'm working on dictionaries. I feel like I've formatted this simple code correctly, however, I receive an error from PyCharm stating, "PEP 8: E101 indentation contains mixed spaces and tabs".

Is this formatting okay? Or is something wrong with it so I can catch this habit and fix it? Thanks so much for your help!


RE: New to python, question regarding PEP8 - snippsat - Apr-14-2022

Formatting is okay.
It give warning because your dictionary values is a little long(uncommon) with split up text on new line.
Black is used much and have become more like standard way of formatting code in Python.
So eg Black will format it automatically like in code i show under.
definitions = {
    "For loops": "For loops allow you to run a block of code repeatedly, just like while loops. However, for loops run"
    "a block of code a set number of times.",
    "Functions": "A function is a block of code repeatedly, just like while loops. However, for loops run a block of"
    "code a set number of times",
    "If statements": "An if statement runs a block of code based on whether or not a condition is true.",
    "Loops": "Loops check a condition and then run a code block. The loop will continue to check and run until a "
    "specified condition is reached.",
    "Python": "Python is a programming language that's currently becoming more and more powerful with every new library"
    " added to its collection.",
}   
So a more common dict it will be format like this.
# Orginal
d = {"apple": "fruit", "ball": "object", "cricket": "sports", 'car': 'vehicles'}

# After Black
d = {
    "apple": "fruit",
    "ball": "object",
    "cricket": "sports",
    "car": "vehicles",
}
It's normal to have a formatter integrated in Editor,i have used Black for many years in VS Code.
For PyCharm look at Editor integration
Can also test online here Black Playground.


RE: New to python, question regarding PEP8 - AthertonH - Apr-14-2022

Awesome, thanks for your help!


RE: New to python, question regarding PEP8 - buran - Apr-14-2022

I am not 100% sure the warning is about the format/indentation of the dict literal. On other hand if it was about other part of the code, you should get IndentationError when trying to run, not PyCharm/linter warning

In any case, never mix tabs and spaces for indentation. The PEP8 recommends using 4 spaces per level. Most IDEs have a setting to automatically convert tab to defined number of spaces.