Python Syntax

Before composing the first Python program, you must gain proficiency with the fundamentals. We will walk you through Python fundamentals with the basic python syntax before going to the comments and variable definition, that will help as a structure block for your Python language.

Python Syntax

Writing First Python Program for Python Syntax

There are two manners by which you can compose and execute an essential Python program:

In Interactive mode – where you compose a program and execute it

Utilizing Script mode – where you execute and right now saved Python program(.py document)

We should see those in real life.

Writing “Hello, World!” on Interpreter

To enter the intelligent Python mode enter the accompanying order on your Terminal.

Also, presently you ought to be in the intelligent mode.

Be that as it may, in case you are utilizing an IDE you don’t have to type the order above to get into intuitive programming mode.

At the point when you compose this program and press enter, your IDE should show “Hello, Word!”

print("Hello World")

Anything that you compose inside ” of print(” “) gets imprinted on your IDE.

For our situation, the yield that you should see on your screen would be

Running Your First Python Program in Scripting Mode

When you wrote your Hello World program as a Python Syntax earlier, let’s assume that you saved it in a Python file. When you save your first program, look for a .py extension file.

Assuming that you saved it as Hello_Python.py, here’s how you would execute this code in a scripting mode.

First of all, the file you saved has to be made executable. We ideally do that by the command:

$ chmod +x test.py

Now that your file is executable, let’s run the program in scripting mode.

$ python Hello_Python.py

Once you execute this command, you will see “Hello, World!” was printed on your Terminal.

"Hello, World”

And, there you have learned your first Python Syntax.

Lines and Indentation

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −

if True: 
          Print ("True" )
else: 
          Print ("False")

However, the following block generates an error −

if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"

Thus, in Python all the continuous lines indented with same number of spaces would form a block. The following example has various statement blocks −

Python Multi-Line Statements

Python allows us to break some statements into multiple lines by using the backslash operator ( \ ).

We write a space and a backslash where we want the statement to cut and continue the statement on a new line.

# single line
result = 1 + 2

# multi line
result = 1 \
         + \
         2

Multiple statements on one line

If we want to write multiple statements on one line we have to terminate each statement with the semicolon operator ( ; ).

 message = "Hello World!"; print(1 + 2); shopping_list = ["Bread", "Milk"]

Using Blank Lines

A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.

In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement

Command Line Arguments

Many programs can be run to provide you with some basic information about how they should be run. Python enables you to do this with -h −

$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser (also PYTHONDEBUG=x)
-E     : ignore environment variables (such as PYTHONPATH)
-h     : print this help message and exit

[ etc. ]

Use Our Online Compiler

You can use our online compiler to compile the codes. The codes can be edited as well.

image 11