Python syntax refers to the set of rules that define how Python code is written and organized. It is simple, clear, and easy to understand that makes Python programming a great choice for both beginners and experienced developers.
In this chapter, you will learn about Python syntax, its basic rules, and how to write clean and correct Python code.
Python syntax is like the grammar of the Python language. It defines how statements are written so that the Python interpreter can understand and execute them correctly. Proper syntax helps in writing structured, readable, and error-free code.
Python provides two main ways to run programs: Interactive Mode and Script Mode. Let us see how to print "Hello, World!" using both modes.
In Interactive Mode, we use the Python interpreter directly from the command line by typing "python".
Syntax:
In the above syntax, >>> denotes a Python Command Prompt where we can type the commands. Let us now write a simple syntax to print "Hello, World!" in Python prompt and press Enter.
Output:
Hello, World!
Another method to use Python Interpreter is by using the Python Script mode. In script mode the code is written in a .py file and then executed from the command prompt. Let us now write a simple syntax to print "Hello, World!" using the script mode and save it as sample.py.
Output:
$ python sample.py Hello, World!
Variables in Python are used to store data values. Unlike other languages, Python variables do not require explicit declaration of type; they are dynamically typed. This means that the type of variable is determined at runtime based on the value assigned to it.
Let us see a simple example showing the way of defining variables in Python.
Output:
10 -> <class 'int'> TpointTech -> <class 'str'>
Explanation:
In the above example, we have defined two variables of different data types. As we can observe, we have not declared their data types explicitly.
To learn more about Variables and their data types in Python, visit - Python Variables and Python Data Types.
Python uses indentation to define blocks of code instead of using curly braces {} as in other languages like C or Java. Indentation enhances code readability and is a crucial part of Python syntax. Every block of code (such as loops, functions, and conditionals) must be indented at the same level.
Output:
9 is greater than 5 This is part of the if block This is outside the if block
Explanation:
In the above example, we have used the if statement to check if 9 is greater than 5. If true, it will print the indented line of codes.
In case of improper indentation, it will raise an IndentationError. Proper indentation ensures the logical structure of the code is maintained.
Identifiers are names used for variables, functions, classes, and other entities in Python. They must follow specific rules to be valid:
Keywords are reserved words in Python that cannot be used as variable names. Examples include if, else, while, for, def, class, import, etc.
| False | None | True | and |
| assert | as | await | async |
| break | class | continue | def |
| del | elif | else | except |
| finally | for | from | global |
| if | import | not | in |
| return | yield | while | with |
| raise | or | pass | try |
| is | lambda | nonlocal | match |
| case |
To learn more about Keywords, visit - Python Keywords
Comments in Python are used to explain code and make it more readable. They help other developers understand the purpose of the code and serve as a reminder for the programmer. Comments are ignored by the Python interpreter, meaning they do not affect the execution of the program.
Here is an example showing different types of comments in Python.
Output:
Sum of a and b is 24
Explanation:
In the above example, the line followed by the # symbol is treated as comments. As a result, it is ignored by the interpreter. Moreover, the multiline comments declared using the triple quotes are often considered as docstrings.
To learn more about Comments, visit - Python Comments
Writing a long statement in a code is not good practice causing unreadability. To prevent this, we can break the long line of code into multiple lines. This can be done either explicitly using backslash (\) or implicitly by parentheses ().
A backslash (\) in Python is a special character used to indicate line continuation. It allows long lines of code to be split across multiple lines for better readability without affecting the code's execution.
Let us take a look at the following example showing how backslash (\) works in Python.
Output:
522
Explanation:
In the above example, we have used the backslash (\) to split the long line of code into multiple lines in order to increase the readability of the code.
To learn more about multiline statements, visit - Multi-Line Statements in Python
In Python, we use the input() function to take user input. The input is always received as a string, even if the user enters a number.
Let us see the following example showing how to take input from the users in Python.
Output:
Enter your first name: Tony Enter your last name: Prince Welcome, Tony Prince
Explanation:
In the above example, we have used the input() function to take input from the user.
We request you to subscribe our newsletter for upcoming updates.