Basic Python Syntax | Python Structure Program

Python is free, open-source, high-level, object-oriented, and powerful programming language.

Every programming language in the real world follows some set of rules that are needed to write an error free program.

In order to write any Python program, we must know its syntax, programming structure, available keywords, data types, variables, constants, etc.

In this tutorial, we will know basic Python syntax and its structure program with various examples.

Python Syntax

In any programming language, syntax is the set of rules that define correct structure, combination, and sequence of words, symbols, indentation, spelling, capitalization, sentences, spaces, etc.

In other words, a syntax refers to a set of rules which defines how a program will be written.

The syntax of any programming language is like the grammar rules of a written language. Various programming languages, such as C, C++, Java, use different syntax to define the structure of any program.

Python provides a basic, simple and clearly defined syntax structure. Following are some examples of basic Python syntax and we will go more in further tutorial.

a) To declare a variable:

x = 'Hello'

b) To declare a list:

y = ['a', 'b', 'c', 'd', '1', '2', '3', '4']

c) To print:

print('Hello', y)

In order for the Python interpreter to read and execute the program properly, we must have to write our code in the appropriate format that it can understand.

An interpreter is a program that reads the high level language code line by line and executes it in a sequence. When the interpreter encounters an error, then it stops running, does not save the interpreted code, and displays the error.

Python is a great example of an interpreter, therefore, Python is known as an interpreted language.

Common Python Syntax Color in IDLE

Here is the list of some of the most common syntax colors and their meanings in Python IDLE. They are as:

  • Keywords : Orange
  • Strings : Green
  • Comments : Red
  • Definitions : Blue
  • Misc. words : Black

You do not need to memorize these colors. But, knowing the meaning of colors can help you see clearly where you typed something wrong. For example, if you type a string, and it is not green, you probably forgot the quotation marks.

Simple Structure of a Python Program

Python provides a very basic and simple structure for writing a program. It consists of different sections. Some sections are optional and we need some to be written in the program.

You can include or exclude the optional sections as per requirement or as per situation. A below figure shows the basic structure of Python program with brief explanations.

Basic Python syntax structure

A brief explanation of the above sections in the Python program structure is as follows:

a) Documentation section: The documentation section includes comments that specify the aim of the program. We write comments in a program to improve the readability of the program. Python allows us to write comments to improve readability and understanding of the code.

A comment is a non-executable statement which the Python interpreter ignores it while execution. It helps to read and understand the complex code easily.

Writing comments in a program is good programming practice. We can write comments anywhere in the Python program.

b) Import statements: This section includes various in-built or user-defined modules in different modules so that we can usage functionality already defined in the existing module. For example:

import math

In this example, we have imported math module that provides access to the mathematical function (sqrt) defined by C standard. The sqrt function returns the square root of a specified number.

c) Global declaration section: In this section, we define global variables for the programs. A global variable is a variable that we can access from anywhere in the program.

This means that we can use the value of the global variable inside the class, function, or method. These types of variables can share their values among different classes, functions, or methods.

d) Class section: This section tells the information about the user-defined classes present in the program. A class is a group of variables (called data members) and member function (called methods) that work on data members. It contains the class definition, data members, and methods definition. T

he definition of methods gives the information to the interpreter regarding the method name, the number of arguments to be passed to method and the set of statements written inside the method.

The statements written inside the method will execute when it is called from anywhere in the program.

e) Subprogram section: In this section, we define user-defined functions. A function is a set of statements that will execute when we call it from anywhere in the program.

The fundamental difference between a method and a function is that we define the body of the method within the class, but we do not define the body of the function in the class.

There is no way to define a function in the Python program. In Python, the functions are declared automatically when we define the function’s body.

f) Playground section: This is the main section where we call user-defined functions and class methods. Moreover, we create an object of the class in this section.

In Python, there is no main function (i.e. main method) that separates the other section from the playground (main) section. We can separate it by writing a comment line between the playground and other section for a better understanding.

A Simple Example Program in Python

Let’s take an example program in which we will find the sum of two numbers in Python.

Program code:

# Python program to calculate the sum of two numbers.
x = int(input("Enter your first number: "))
y = int(input("Enter your second number: "))
sum = x + y
print("Result = ", sum)
      Enter your first number: 20
      Enter your second number: 30
      Result =  50

In this example, the first line specifies the comment telling the purpose of the program. It is not an executable statement.

The second line creates a variable x and stores the value of the first number entered by the programmer.

The third line creates a variable y and stores the value of the second number entered by the programmer.

Fourth line calculates the sum of the first number and the second number and stores the outcome in the third variable sum.

Quotations in Python

Python allows us to use quotation marks for string literals. We can use single (‘), double (“), or triple (”’ or “””) quotes, provided that they must match at the beginning and at the end of the string. The following are examples of valid Python strings.

name = 'John'
myString = "Scientech Easy"
line = "Every person loves his own country."

The first example enclosed the string in a single quotation mark and the second and third in double quotation marks. We can also use triple quotation marks for strings that span more than one line. For example:

paragraph = '''This is a paragraph. 
               The paragraph contains over one sentence.'''
       This is a paragraph. 
               The paragraph contains over one sentence.

Python Statements

Statements are expressions we write within a program which is read and executed by the Python interpreter. Python supports the following statements that are as follows:

  • if statement
  • for statement
  • while statement
  • break statement
  • assignment statements

Python Multi-line statements

In Python, if we have statements that are too long to fit in a single line, then we can implicitly enclose it in several lines using braces {}, brackets [ ], or parentheses (). Consider the following examples below.

Example 1:

letters = ('a', 'b', 'c',
            'd', 'e', 'f',
            'g', 'h','i')

Exanple 2:

colors = ["blue", "red", "yellow",
           "green", "orange", "violet",
            "pink", "black", "white"]

We can also use a backslash character (\) to break a long code in two or more lines. In the example below, we are splitting the long line using a backslash (\) into three lines.

Example 3:

electricity_bill = 500
water_bill = 60
internet_bill = 1000
total_bill = electricity_bill + water_bill + internet_bill

# To break this line, we can write in this way.
total_bill = electricity_bill + \
             water_bill + \

Use of Colon in Python

In Python, a colon denotes the beginning of function, class, method, conditional statement, etc. An example will make it more clearly.

def sum(x, y): # Here, colon tells that this function block is going to begin from here.
    return( x + y )
print(sum(10, 20))

Use of Semicolon in Python

A semicolon in Python denotes the end of the statement. If we are writing more than one statement in a line, then we can use a semicolon at the end of each statement. For example:

def sum(x, y): # Here, colon tells that this function block is going to begin from here.
    z = 50; print(x + y + z) # Here, semicolon denotes the end of statement in a line.
sum(10, 30)

Python Case Sensitive

Python is a case sensitive programming language, which means when we type “World”, “world”, and “WORLD”, Python will understand all these different. For example:

num = 20
print(num) # Output: 20
print(Num) # It will throw NameError: name 'Num' is not defined.

In this tutorial, we have covered about Python basic syntax, a simple structure of Python program, and Python statements with different example programs. Hope that you will have understood the basic points of Python syntax and structure of program.
Thanks for reading!!!
Next ⇒ Indentation in Python⇐ Prev Next ⇒

Please share your love