Python follows a set of rules, much like any other computer language. These guidelines specify how a program in that language should be written. It also illustrates how the interpreter decodes the code. These rules are set on the runtime system, and the person developing the code must respect them. Let's look at some of the Python basic syntax.
In Python, what does "syntax" mean?
The syntax is the set of rules that define the structure of the Python language. The meaning of the code is difficult to comprehend without this. These include symbols, punctuation, and words and how to use them while coding in the language.
Python syntax example
Output
Python Identifiers
An Identifier is a name you give to a function, module, class, variable, or other objects.
There are certain guidelines to follow while establishing these identifiers:
- The identifier can begin with any letter of the alphabet: A-Z or a-z.
- The underscore character can be used as a starting point.
- It must not start with a number or a special character.
- Only alpha-numeric letters and underscores are permitted in the identification, which must not begin with a number.
- Also, because the variables are case sensitive, Apple and apple are two distinct variables.
Python also includes a set of conventions for describing variables.
- A class variable's name must begin with an uppercase letter and end with all lowercase letters.
- The variable concludes with two underscores when naming a language-defined special name.
- When naming a private variable, use a single underscore as the first character.
- When naming a strongly private variable, use two underscores as the first character.
Python reserved keywords
These are unique keywords reserved by Python for specialised functions. They offer a set of pre-defined features. Constants, variables, or any other identifier can be used. The pre-defined function, however, does not work when it is set as a constant or variable.
As a result, these keywords should not be used as constants, variables, or any other identifier name. Here's a list of some of the keywords that have been set aside:
Python Lines and Indentation
Brackets are used to display code blocks in programming languages including C, Java, and C.
To say blocks of code in Python, you don't need to use braces or semicolons. Indentation is used to express this.
The thumb guideline to follow when using indentation in Python is:
The indentation begins the code block and ends with the first unwanted line. This indentation must be the same through the block.
Indentation in Python with an example:
This displays a Python example with proper indentation.
There is a colon after each conditional statement, and the following line has an indentation. The basic guideline is that there must be at least one space; however, you as a programmer can have more.
Output
When you run the code, the output reveals what happens. The code runs perfectly when the indentations are accurate and the IDLE shell appears
End of Statements in Python
In Python, you don't need to specify the end of a statement with a special character.
To begin a new statement, hit enter here.
EOL Error as an Example
As illustrated in the figure below, running the code will result in an "invalid syntax" error notice because each new line is a new line of code.
After you've fixed this code, it should appear like this:
Code with proper EOL syntax as an example:
Output
Python Statements with Multiple Lines
Now that we've covered how to end a line, what should you do if you have code or print statements that are too long to fit on a single line?
To improve readability, you should keep each line of code to a maximum of 79 characters.
So, let's look at how to deal with code lines that are longer than 79 characters.
A backslash is used to separate a line of code and indicate that the following line is not a new line.
Code with multiple lines as an example:
Output
The preceding example demonstrates how to use a backslash to write a line of code across multiple lines. When you use a backslash in this case, Python understands it as a continuation of the preceding line rather than a new one.
Python print statements with many lines as an example:
Output
The backslash is demonstrated in the example above for a print statement. You can use the backslash like previously if you need to print something with a lot of characters.