Python syntax defines a set of rules that are used to create a Python Program. Python syntax also comprises a set of rules and elements that collectively make up the Python Syntax. In this article, we will explore what are different components in syntax.
Table of contents:
Python Line Structure
Python code is composed of both physical and logical lines.
-
- Physical line: It is a sequence of characters that ends with the line itself. Unlike C and Java, Python does not require a semicolon to indicate the conclusion of a statement.
- Logical Line: A complete statement that may include one or more physical lines. Python can combine many physical lines to make a single logical line.
Example
Variable = 6
NOTE: A logical line that only contains spaces, comments, or tabs are called blank lines and they are ignored by the interpreter.
Data Analysis Mastery with Python and Excel
Gain Expertise in Data Wrangling, Statistical Analysis, and Reporting
Python Multiline Statements
In Python, a new line represents the beginning of a new statement. However, Python allows you to break a statement over many lines or combine numerous statements into a single logical line.
1. Explicit Line Joining
A backslash (\) is used to split a statement across multiple lines.
Example:
We can also use explicit line joining to join two or more physical lines into a logical line, using backslash.
2. Implicit Line Joining
Statements inside parentheses (), brackets [], or curly braces {} can be broken into multiple lines without using a backslash.
Example:
3. Multiple Statements on a Single Line
Python allows multiple statements on a single line using a semicolon (;), but this is not recommended as it reduces code readability.
Example:
Whitespaces and Indentation
Unlike most programming languages, Python enforces indentation to define blocks of code. According to PEP 8 (Python’s official style guide), the recommended indentation size is four spaces per level.
Incorrect indentation leads to errors:
Learn Python the Right Way
Structured Learning Path to Python Programming for Beginners and Experts
Identifiers
Identifiers in Python are nothing but user-defined names to represent programmable entities like (Python Variables, Python Functions, Python Class, Python Modules), or any other objects. But, there are a few rules that we need to follow while defining an identifier. They are:
-
- We can use a sequence of letters [lowercase (a to z) or uppercase (A to Z)], and we can also mix up digits (0 to 9) or an underscore (_) while defining an identifier.
- We can’t use digits to begin an identifier’s name.
- We should not use Reserved Keywords to define an identifier.
- Other than underscore (_), we are not allowed to use any other special characters.
- Even though Python doc says that we can name an identifier with unlimited length, it is not entirely true. Using a large name (more than 79 chars) would lead to the violation of a rule set by the PEP8 standard.
Reserved Words
Python has a set of reserved words (keywords) that cannot be used as identifiers. These words have predefined meanings and are case-sensitive.
False |
class |
finally |
is |
return |
None |
continue |
for |
lambda |
try |
True |
def |
from |
non-local |
while |
and |
del |
global |
not |
with |
as |
el |
if |
or |
yield |
assert |
else |
import |
pass |
|
break |
except |
in |
raise |
|
Python Quotations
Python supports both single (‘) and double (“) quotes for defining strings. The type of quote used at the beginning must match the one used at the end.
Example:
Conclusion
In this guide, we explored the fundamental elements of Python syntax, including line structure, indentation, reserved words, and quotation rules. Understanding these basics will help you write clean, efficient, and error-free Python code. If you want to learn more about the same, please check out our Comprehensive Python Course.