Python syntax refers to the set of rules that define how a Python program is written and interpreted. It dictates how you structure code using elements like variables, functions, loops, and conditionals. Here are some key aspects of Python syntax:
Python has a simple, statement-based syntax. However, there are a few properties you need to know about
• Statements execute one after another, until you say otherwise. Python normally runs statements in a file or nested block in order from first to last, but statements like if (and, as you’ll see, loops) cause the interpreter to jump around in your code. Because Python’s path through a program is called the control flow, statements such as if that affect it are often called control-flow statements
There are no braces or “begin/end” delimiters around blocks of code in Python; instead, Python uses the indentation of statements under a header to group the statements in a nested block. Similarly, Python statements are not normally terminated with semicolons; rather, the end of a line usually marks the end of the statement coded on that line
header + “:” + indented statements. All compound statements in Python follow the same pattern: a header line terminated with a colon, followed by one or more nested statements, usually indented under the header.
1. Case Sensitivity
Python is case-sensitive, meaning that Var and var are treated as different identifiers.
2. Indentation
Indentation is used to define the structure of code blocks like functions, loops, and conditionals.
Python does not use curly braces {} or keywords like begin/end; it relies on consistent indentation.
if x > 2:
print("x is greater than 2")
3. Comments
Single-line comments start with a # symbol.
# This is a single-line comment
Multiline comments can be done using triple quotes ‘ ‘ ‘or”””:
'''
This is a
multi-line comment
'''
4. Variables
Variables do not need to be declared with a specific type.
Python dynamically assigns types based on the value assigned.
x = 20 # Integer
y = 5.67 # Float
name = "John" # String