Understanding Python Syntax: A Comprehensive Guide for  Python Programming

Understanding Python Syntax: A Comprehensive Guide for Python Programming

A Comprehensive Guide on Python Programming Syntax

In this article, we'll be looking at Python Programming Syntax.

In the previous article titled: How to install and set up Python Programming Environment we covered extensive details for ease of access if you're joining the Python series for the first time.

For an in-depth understanding of programming and other related concepts this book: Tech is Easy to Learn: Simplifying tech to help you get started in your area of interest will be very handy.

What is Syntax?

Syntax is a rule or principle that guides the way a programmer should use a programming language to write a program. A violation of a programming language syntax is called a syntax error.

What is Semantics?

Semantic is the meaning or definition of the rule or data type that is used in the program. A violation of using a function or reserved word as intended by the programming language definition is called a semantic error

Syntax in Python

In Python basically, there are several rules to adhere to while writing codes.

The first rule that is commonly ignored but important for beginners is the file extension for any file with Python codes.

Every file containing Python code has to be saved with a file extension. The file extension is what informs and defines how the OS should handle the file.

Python Indentation

Indentation is the blank space at the beginning of any line of code. The indentation technique in Python follows a strict order to enable the compilation and execution of the program as opposed to some programming languages where indenting is done for readability purposes at the developer's discretion.

For example:

def code_syntax():
    print("this is a code syntax for function in Python")
code_syntax()

Notice the indentation (tab space) on the second line. Once it is removed it results in a violation of the Python syntax which is called a syntax error.

Depending on the choice of code editor there are several extensions out there for displaying errors and hints when writing Python codes.

This same approach applies to conditional statements, classes etc in Python.

var = 100 == 101
if var:
    print(var)
else:
    print(var)

If any modification is made to the indentation for conditional statement it throws a syntax error.

Python Variables

A variable is a temporary storage location for a value. There are no data types and semi-colon additions at the end of Python variable declarations/initialization. This is one of the reasons why Python is an easy-to-learn language.

name = "Python Syntax"

In some programming languages, the datatype and statement closure symbol must be specified.

Writing codes other than what is expected according to Python coding rules will result in syntax errors.

Python Identifiers

Identifiers are symbols or characters that can be used to identify a variable, class name, function name etc... Any identifier can start with an alphabet (A to Z or a to z), an underscore (_) or numbers | digits ( 0 to 9).

Other symbols such as @, $, &, single usage of ), (, [, ], and % etc.. are not allowed as identifiers.

An identifier with "name" is different from "NAME" because Python is a case-sensitive language.

Comments

Adding comments when writing codes for clarity purposes or ease of inheritance of any codebase is very important. However, there's a defined way of doing such in Python. This also informs the compiler to ignore such lines while executing the codes.

As simple as it may look the # symbol at the beginning of the code makes it a valid comment according to Python's Syntax.

An example of omitting the # symbol will result in the error as displayed below.

Python Reserved Keywords

There are keywords exclusively reserved by Python for development purposes that cannot be used as a variable name or an identifier.

List of Reserved Keywords

That's a lot!!! How would I know which reserved keyword not to use? Do not fret! A syntax error will notify you and over time you'll get to know as many reserved keywords as possible.

NB: Use the command below to view all the reserved keywords for the installed version of Python on a machine

import keyword
print(keyword.kwlist)

Python Multi-Line Statements

Python statements can be written in multiple lines using the backward slash symbol \ to indicate that the code continues on the next line.

john_age = 10
doe_age = 20
total_age = john_age + \
            doe_age

print(total_age)

Python Multiple Statements in One Line

The semi-colon ; symbol can be used to create multiple statements in one line.

import keyword;print(keyword.kwlist);print("one line with multiple statements")

Python Docstrings

The Python docstring is used to add documentation to Python codes to ease the display of explanation of any block of code. It can be used to inspect several aspects of the code at runtime.

def article_on_syntax(description):
   '''This function returns the description of the article on syntax do demonstrate Python docstring.'''
   return description
print ("Docstring of Article on Syntax function:", article_on_syntax.__doc__)

Python Quotations

Triple quotations """ can be used to create multiple line string comments.

description = """
In the beginning of the article.
Python is a programming language.
Easy to learn and use.
"""

print(description)

Conclusion

Understanding Python's Syntax is very important and helpful to stay within the defined rules and ease debugging through error definition. The usage of any of the syntax is by the Python project needs. There's no better way to master Python syntax than reading the documentation, coding and debugging consistently.

Watch out for the next article to continue your Python developer journey.

If you're looking forward to developing applications in Python using the OOP (Object-oriented programming), the major concepts are simplified on my blog here.

Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!