When writing code in Python, a parser reads the code that is executed. Python syntax in programming with Python is a set coding language with defined set of rules that defines how a program will be written in Python and is highly readable. In this Python tutorial, we will review the Python coding style, python line structure, python indentation, Python print statements, and Python explicit line joining.
What type of Coding Style is Python?
Python is a very simple but unique programming language that can be learned easily with consistent practice and completing exercises. Below are a few Python tips to help when learning how to program in Python:
- Python used spaces around statements and expressions.
- Python uses a single blank line to separate method definitions inside a class and separate top-level functions and class definitions.
- Python requires 4 spaces per indentation.
- Python has a maximum line length of 79 characters.
Python Line Structure
NEWLINE represents the end of a logical line in Python. Also, Python statements cannot cross a logical line boundaries unless NEWLINE is allowed by the syntax and the logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules required by Python.
In Python, a physical line end will end where the current platform’s convention is for terminating lines. For instance, if we were executing a Python script on a Linux operating system, this is the ASCII LF (linefeed) character. And if one were to execute a Python script on a Windows operating system, it would be there the ASCII sequence CR LF (return followed by linefeed).
Whitespaces are using in Python and are used to define program blocks to indicate blocks of code. In Python, the whitespaces in the indentation are not set, but all statements in the block must be indented. For example, the below is an if statements that will print if the variable var1 is greater than 10. The space before the variable named var1, is known as whitespace.
var1 = 10
if var1 >= 10:
Python Print Statement
Writing a Python print statement can be completed by writing the word print following parenthesis with quotations if needed.
print("Print a Python print statement")
Print a Python print statement
Python Explicit Line Joining
Python line joining can be executed by using backslash ( \ ) by joining two or more physical lines. However, in order to do so, a physical line must end with a backslash that is not part a comment or string literal. It must be joined with the following format.
var1 = 1
var10 = 10
var100 = 100
var1000 = 1000
if var1 == 1 and var10 == 10 \
and var100 == 100 and var1000 == 1000:
print("This is Python line joining!")
This is Python line joining!