03 Basic Syntax
03 Basic Syntax
However, there
are some definite differences between the languages.
FirstPython Program
Let us execute the programs in different modes of programming.
$ python
Python 3.3.2 (default, Dec 10 2013, 11:35:01)
[GCC 4.6.3] on Linux
Type "help", "copyright", "credits", or "license" for more information.
>>>
On Windows:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.
>>>
Type the following text at the Python prompt and press Enter-
If you are running the older version of Python (Python 2.x), use of parenthesis as
in print function is optional. This produces the following result-
Hello, Python!
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-
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
On Windows
C:\Python34>Python test.py
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-
Hello, Python!
Reserved Words
The following list shows the Python keywords. These are reserved words and you
cannot use them as constants or variables or any other identifier names. All the
Python keywords contain lowercase letters only.
as finally or
continue if return
elif is with
except
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-
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.
#!/usr/bin/python3
# First comment
print ("Hello, Python!") # second comment
Hello, Python!
You can type a comment on the same line after a statement or expression-
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.
#!/usr/bin/python3
input("\n\nPress the enter key to exit.")
Here, "\n\n" is used to create two new lines before displaying the actual line.
Once the user presses the key, the program ends. This is a nice trick to keep a
console window open until the user is done with an application.