Rules for formatting Python code
In fact, Python is a very interesting programming language today. For many specialists, this language will never become obsolete. Writing code in this language can at the same time please with its simplicity and interest with its capabilities.
Python courses for kids
It may seem that a programming language like Python will be quite difficult for a child, but this is not the case. Python is one of the most understandable programming languages. It will be very interesting for children to learn this language and feel like little programmers.
How to format code in Python
In fact, Python is the right programming language, the knowledge of which will be useful to anyone, even a novice programmer. One of the interesting topics in learning Python is proper code formatting.
In fact, like many programming languages, Python has its own well-defined rules for coding. This affects many aspects of this language.
Let’s take a closer look at the rules for formatting code in Python.
- Indentation
When working with Python, it is recommended to use 4 spaces per indentation level. Some versions of Python do not allow mixing tabs and spaces in indentation. The task of the programmer is to indent only with spaces.
- Using semicolons
If you’re working with Python, remember not to separate lines, or code commands that are on the same line, using semicolons.
An example of the correct separation of lines of code
a = ‘String’
b = 15
c=7.2
- Parentheses
As for brackets, use them only where necessary. They should not be used in return statements or in relation to a conditional construct. In the latter case, this may only be appropriate if you need to break a line of code. Parentheses are also appropriate when you need to create a tuple.
- Spaces in expressions and statements
When working with Python, you cannot put spaces in the middle of parentheses.
An example of a correctly placed space
pineapple(pine[1], {apple: 2})
Also, do not put spaces before a parenthesis that will be opened if that parenthesis is used to start an argument list, index, or slice.
An example of a correctly placed space
get_number_of_guests(2)
In Python, spaces cannot be placed before characters such as periods, commas, or semicolons. You can put them after these characters if they are exactly at the end of the line.
- Line length
The length of the lines also deserves special attention. Don’t make strings long, they should be 79 characters maximum. Also, don’t use a backslash to break a new line.
Python has the ability to concatenate strings using parentheses and curly braces. If your code requires it, you can add extra parentheses around your expression.
- Names
As for defining names in Python, it should be said right away that do not use names that consist of only one character, unless it is a counter and iterators. Also, you cannot use letters such as “l”, “O”, “I” as identifiers that consist of a single letter. This rule was created because, depending on the font, these letters can be confused with the numbers 0 and 1. If you really need to put the letter “l” in your code, choose its capital version in the form of “L”.
- Function names
Function names must be in lowercase letters, while words must be separated by an underscore character.
Correct naming convention
my_variable = ‘Variable’.
- Comments
Get used to the fact that your comments should be complete sentences and have a clearly articulated idea. The first word in your comment must start with a capital letter. After completing the writing of the proposal for the comment, do not forget to put a space twice. Remember that comments must be written in English only.
Thus, we have considered with you the basic rules for formatting code in Python. Despite the complexity of some of its sections, Python programming is a really interesting and exciting activity for children in our time. This fact is due, as we said, to the simplicity of the rules of this language. Starting to learn programming with Python is the best thing a teenager who is interested in coding can do.
Educational courses for children
Our IT school Go-Mother offers training in modern programming languages and working with interesting programs and platforms. With our help, your child will be able to learn the field of IT technologies without gaps in the acquired knowledge, as well as practice applying the acquired knowledge, up to creating their own games, websites and applications. Our teachers will help you at all stages of learning.
Our courses are held online or at locations: Akademgorodok metro station, Zhitomirskaya metro station. As a platform for online classes, we offer discord or hangouts. We work seven days a week and provide our students with the choice of a convenient time for classes.