Python was specifically designed to be a highly readable language The Python Syntax is made for ease of use and has made it one of the most used languages among freshers as well as experts. Python syntax is English which makes it much easier to write, read, and understand a Python script as compared to a similar script written in other languages such as C or Java.
The syntax of any language refers to the structure of that language which includes a certain set of rules, subjective to that specific language. The Python syntax also comprises a set of rules and elements that collectively make up the Python Syntax.
Table of contents:
Python Line Structure
Python coding style comprises physical lines as well as logical lines or statements. A physical line in a Python program is a sequence of characters, and the end of the line terminates the line sequence as opposed to some other languages, such as C and C++ where a semi-colon is used to mark the end of the statement. A logical line, on the other hand, is composed of one or more physical lines. The use of a semi-colon is not prohibited in Python, although it’s not mandatory. The NEWLINE token denotes the end of the logical line.
Python Code 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
As we saw in Python, a new line simply means that a new statement has started. However, Python does provide a way to split a statement into a multiline statement or to join multiple statements into one logical line. This can be helpful to increase the readability of the statement. Following are the two ways to split a line into two or more lines:
Explicit Line Joining
In explicit line joining, we use a backward slash to split a statement into a multiline statement.
Example:
We can also use explicit line joining to join two or more physical lines into a logical line, using backslash.
Implicit Line Joining
Statements that reside inside [], {}, or () parentheses can be broken down into two or more physical lines without using a backslash.
Example:
Multiple Statements on a Single Line
In Python, it is possible to club multiple statements in the same line using a semi-colon; however, most programmers do not consider this to be a good practice as it reduces the readability of the code.
Example:
Whitespaces and Indentation
Unlike most of the programming languages, Python uses indentation to mark a block of code. According to the Python coding style guideline or PEP8, we should keep an indent size of four.
![Whitespaces and Indentation](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20179%20179'%3E%3C/svg%3E)
Most programming languages provide indentation for better code formatting and don’t enforce it. But in Python it is mandatory. This is why indentation is so crucial in Python.
For example, if we run the following code, we will get an error of ‘expected indentation’:
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
Reserved words are nothing but a set of special words, which are reserved by Python and have specific meanings. Remember that we are not allowed to use keywords as Variables in Python.
Reserved words in Python are case-sensitive. The following table shows all these reserved words used in Python.
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
In Python, single quotes and double quotes, both are supported by strings. If we have started a string with a single quote, it is mandatory to end it with a single quote only. The same goes for double quotes.
Example:
This brings us to the end of this module to Learn Python. We learned about the Python Syntax along with the Python basics Syntax and Python Coding. The next module highlights Python variables. See you there!
Our Python Courses Duration and Fees
Cohort starts on 11th Feb 2025
₹20,007