In this tutorial, you'll explore the essential elements of Python's syntax. Understanding these fundamentals will enable you to start writing and reading Python code efficiently.
Whitespace and Indentation
Unlike programming languages such as Java, C#, or C++, Python does not use semicolons (;) to terminate statements. Instead, Python relies on whitespace and indentation to define the structure of the code.
Consider the following example:
# Define main function to print numbers from 1 to 9
def main():
i = 1
max = 10
while (i < max):
print(i)
i = i + 1
# Call main function
main()
Key Observations:
-
No semicolons are used to terminate lines.
-
Indentation is crucial and defines the scope of code blocks such as loops and functions.
Benefits of This Approach:
-
Clarity: Code blocks are visually distinguishable without additional syntax.
-
Consistency: Enforces a uniform coding style across projects.
-
Readability: Code appears cleaner and easier to understand.
Comments
Comments are used to explain why the code exists, making it easier to maintain and understand. They are ignored during execution.
-
Single-line comment:
# This is a single-line comment
Python does not support native multi-line comments like some other languages, but multi-line strings can be used in certain contexts (though they are not truly comments).
Continuation of Statements
Normally, each Python statement is written on a separate line. However, when a statement is too long, it can be continued using a backslash ().
Example:
if (a == True) and (b == False) and \
(c == True):
print("Continuation of statements")
This approach allows complex expressions to span multiple lines for improved readability.
Identifiers
Identifiers are names assigned to variables, functions, classes, and other objects.
Rules for Naming Identifiers:
-
Must begin with a letter (A–Z or a–z) or an underscore (_)
-
Can be followed by letters, digits (0–9), or underscores
-
Are case-sensitive (
counter
andCounter
are different) -
Cannot use reserved Python keywords
Keywords
Keywords are reserved words in Python that have special meanings and cannot be used as identifiers.
Here is a list of commonly used keywords in Python:
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
To programmatically list all current keywords, use:
import keyword
print(keyword.kwlist)
String Literals
Python supports several formats for defining string literals:
s = 'Single-quoted string'
print(s)
s = "Double-quoted string"
print(s)
s = '''Multi-line
string using
triple single quotes'''
print(s)
s = """Multi-line string
using triple double quotes"""
print(s)
Strings must begin and end with the same type of quotation mark.
Summary
-
Python uses indentation and whitespace instead of braces or semicolons to define code blocks.
-
Comments improve code readability and maintainability.
-
Statements can span multiple lines using the backslash ().
-
Identifiers name program elements and must follow specific naming rules.
-
Keywords are reserved and cannot be used as identifiers.
-
String literals can be defined with single, double, or triple quotes.
With these foundational elements, you're now prepared to begin writing clean and structured Python code.
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/python-tutorial/introduction-to-python-syntax/
License Agreement:Attribution-NonCommercial 4.0 International License