How To Write Program In Python 3?

 

 

Basic Syntax Of Python3

 

First Python Program : 

Type the following text at the Python prompt and press Enter- 

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

If you are running the older version of Python (Python 2.x), use of parenthesis as inprint function is optional. This produces the following result- 

Hello, Python!


Script Mode Programming  

Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Let us write a simple Python program in a script. Python files have the extension.py. Type the following source code in a test.py file-  

print ("Hello, Python!") 

We assume that you have the Python interpreter set in PATH variable. Now, try to run this program as follows- 

On Linux :
 
$ python test.py  

This produces the following result- 

Hello, Python!

On Windows : 
 
C:\Python34>Python test.py 

This produces the following result- 

Hello, Python!

Let us try another way to execute a Python script in Linux. Here is the modified test.py file- 

#!/usr/bin/python3 

print ("Hello, Python!") 

We assume that you have Python interpreter available in the /usr/bin directory. Now, try to run this program as follows- 

$ chmod +x test.py     # This is to make file executable 

$./test.py 

This produces the follwing result- 

Hello, Python! 



Python Identifiers :

A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). 

Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python. 

Here are naming conventions for Python identifiers- 


  • Class names start with an uppercase letter. All other identifiers start with a lowercase letter.   

  • Starting an identifier with a single leading underscore indicates that the identifier is private.   

  • Starting an identifier with two leading underscores indicates a strong private identifier.

  • If the identifier also ends with two trailing underscores, the identifier is a language defined special name 

 

 

Multi-Line Statements :

Statements in Python typically end with a new line. Python, however, allows the use of the line continuation character (\) to denote that the line should continue. For example- 

total = item_one + \
            item_two + \
            item_three   

The statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example- 

days = ['Monday', 'Tuesday', 'Wednesday',
            'Thursday', 'Friday'] 

 

 

Quotation in Python  : 

Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string. 

The triple quotes are used to span the string across multiple lines. For example, all the following are legal- 

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences.""" 

 

 

Comments in Python :

A hash sign (#) that is not inside a string literal is the beginning of a comment. All characters after the #, up to the end of the physical line, are part of the comment and the Python interpreter ignores them.  

# First comment
print ("Hello, Python!") # second comment

 
This produces the following result- 

Hello, Python! 

You can type a comment on the same line after a statement or expression- 

name = "Madisetti" # This is again comment 

Python does not have multiple-line commenting feature. You have to comment each line individually as follows- 

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.