Welcome To Code School From Admin

CHAPTER 3 | Python Basic Syntax

Python Basic Syntax 

The Python language has many similarities to Perl, C and Java. However, there are some definite differences between the languages. This chapter is designed to quickly get you up to speed on the syntax that is expected in Python.

First Python Program: 

INTERACTIVE MODE PROGRAMMING: 

Invoking the interpreter without passing a script file as a parameter brings up the following prompt:

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>>

Type the following text to the right of the Python prompt and press the Enter key:

>>> print "Hello, Python!";

If you are running new version of Python, then you would need to use print statement with parenthesis like print ("Hello, Python!");. However at Python version 2.4.3, this will produce 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. All python files will have extension .py. So put the following source code in a test.py file.

print "Hello, Python!";

Here, I assumed that you have Python interpreter set in PATH variable. Now, try to run this program as follows:

$ python test.py

This will produce the following result:

Hello, Python! 

Let's try another way to execute a Python script. Below is the modified test.py file:

#!/usr/bin/python 
print "Hello, Python!"; 

Here, I assumed that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows:

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

This will produce the following 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 following identifier naming convention for Python:


  • Class names start with an uppercase letter and all other identifiers with a lowercase letter. 
  • Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private.
  •  Starting an identifier with two leading underscores indicates a strongly private identifier.
  •  If the identifier also ends with two trailing underscores, the identifier is a language-defined special name. 

Reserved Words: 

The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.











Share on Google Plus

About MANI KUMAR CHOWDARY

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment