0% found this document useful (0 votes)
3 views

03 Basic Syntax

The document provides an overview of Python programming, highlighting its similarities and differences with Perl, C, and Java. It explains how to execute Python programs in both interactive and script modes, discusses reserved words, multi-line statements, string quotations, comments, and user input handling. Additionally, it mentions the use of semicolons for multiple statements on a single line.

Uploaded by

yiwav67616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

03 Basic Syntax

The document provides an overview of Python programming, highlighting its similarities and differences with Perl, C, and Java. It explains how to execute Python programs in both interactive and script modes, discusses reserved words, multi-line statements, string quotations, comments, and user input handling. Additionally, it mentions the use of semicolons for multiple statements on a single line.

Uploaded by

yiwav67616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The Python language has many similarities to Perl, C, and Java.

However, there
are some definite differences between the languages.

FirstPython Program
Let us execute the programs in different modes of programming.

Interactive Mode Programming


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

$ 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-

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

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!

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 following result-

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.

and exec Not

as finally or

assert for pass

break from print

class global raise

continue if return

def import try


del in while

elif is with

else lambda yield

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-

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.

#!/usr/bin/python3

# 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.

Waiting for the User


The following line of the program displays the prompt and the statement saying
“Press the enter key to exit”, and then waits for the user to take action −

#!/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.

Multiple Statements on a Single Line


The semicolon ( ; ) allows multiple statements on a single line given that no
statement starts a new code block. Here is a sample snip using the semicolon-

import sys; x = 'foo'; sys.stdout.write(x + '\n')

You might also like