Tute

Explain python syntax and comments?

Explain_python_syntax_and_comments

Python Syntax

Execute Python Syntax

 Python syntax can be executed by writing directly in the Command Line:


>>> print("Hello, World!")
 
Hello, World!

Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

C:UsersYour Name>python myfile.py
 

First Python Program

python is vary easy to learn , the basic syntax of python by using which, we will run a simple program to print hello world on the console.

Python provides us the two ways to run a program:

  • Using Interactive interpreter prompt
  • Using a script file
 

Interactive interpreter prompt

Python provides us the feature to execute the python statement one by one at the interactive prompt. in this mode we will get output in every statement .It is preferable in the case where we are concerned about the output of each line of our python program.

project developmemnt is not possible in this mode

To open the interactive mode, open the terminal (or command prompt) and type python (python3 in case if you have python2 and python3 both installed on your system).

It will open the following prompt where we can execute the python statement and check their impact on the console.


Using a script file

Interpreter prompt is good to run the individual statements of the code. However, we can not write the code every-time on the terminal.

We need to write our code into a file which can be executed later. For this purpose, open an editor like notepad, create a file named first.py (python used .py extension) and write the following code in it.

Print ("hello world"); #here, we have used print() function to print the message on the console.   

To run this file named as first.py, we need to run the following command on the terminal.

$ rlchats.py

Hence, we get our output as the message Hello World ! is printed on the console.


Comments

comments are statements that is ignored by the translator , when run program. it increase the readbility of the program. 

Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

Python supports two types of comments:

1) Single Line Comment:

In case user wants to specify a single line comment, then comment must start with ?#?

Eg:


>>> # This is single line comment.  

>>>print "Hello Python by rlchats"  

 

Output:

Hello Python by rlchats

2) Multi Line Comment:

Multi lined comment can be given inside triple quotes.

eg:


   #single line comment  
    print "Hello Python"  
    """This is
    multiline comment"""

Output:

 this is multiltne comment

Related Topics