Interacting with Python - PYTHON MADE SIMPLE (2015)

PYTHON MADE SIMPLE (2015)

Chapter 2: Interacting with Python

To be able to interact with python: It is important that you know how, when, why and what can be done. In chapter one, I introduced to you how to invoke python.

To prompt the python program, type the text print “Hello, python!”; and press enter.

The output will be shown as:

Hello, Python! (Indicated in blue in the screen shots)

This will be seen below:

This is the first program suggesting that the program is active and working.

Python Identifiers

An identifier is a name that can be used in the identification of a variable, class, function, class, module or object. In most cases, in

Naming rules in Python

1. When using a class name, always start with an upper case letter.

2. All other identifiers must always use a lower case letters

3. When an identifier starts with a leading underscore, it is a suggestion that the identifier is private

4. Two leading identifiers suggests that the identifier is strongly private

5. Ending an identifier with two underscores suggests that the identifier is language defined with a special name.

Reserved words in Python

This are words which cannot be used as constants, variable or key words as an identifier in Python. The words are always in lower case letters only. They include:

yield

global

with

from

while

for

try

finally

return

exec

raise

except

print

else

pass

elif

or

def

not

continue

lambda

class

is

break

in

assert

import

and

if

Lines and indentation in Python

Python as a program does not know how to identify blocks of algorithms as a block of codes for class and sometimes functions. Hence, the only way Python is able to read and understand your program is by the use of blocks of codes created by indentations.

The number of spaces in the indents is not fixed. Moreover, all statements within the codes need to be indented in the same length.

For instance, see the following code created in Python 2.7

Mixing the indentations in most cases bring an error in the python shell when the program is run by pressing on F5 key or by selecting run followed by run module, either way, it is the same.

Multiple lines in python can also be used

Most of the statements in python end up with a new line. However, line continuation can be used by the use of characters (\). This shows that the line is a continuation from the previous statement.

For example

However, when the statements are contained within [], (), or {} types of brackets. They do not need the use of the line continuation characters.

For examples

Using Quotation in python

One can use single quotes (‘’), or double quotes (“”) and sometimes triple quotes (‘‘‘ or ”””) as a way of representing string literals. The rule with the use of quotes is that they should be the same when being used at the start and at the end of the string.

The triple quotes are often used when spanning a string over several lines.

Check out the use of the quotes below:

Using comments in Python

To be able to understand your program, one may need to make comments in between the codes or even after the codes. The comments can be protected from being recognized as codes in the program in a variety of ways.

A hash sign (#) is used at the start of a comment. The program would automatically assume everything starting with a hash sign. However, when it is used. The rule is that the all the comments must not be used as string literals, i.e. they should not be placed inside quotes.

Also, all the characters that are preceded by the hash sign to the physical end of the line are recognized as part of the comment. The interpreter ignores them automatically.

Note also, that a comment can be placed in the following ways:

1. It may come after a line of code

2. You can also comment after as in the shown below.

3. Comments can also be in multiple lines, but every line needs to start with a hash (#) sign.

Blank Lines

Blank lines in python are ignored automatically. They could be a blank line with only white space. They can also be blank line with a comment using a hash (#) sign. They are all ignored by python.

However, when developing an interactive interpreter. A blank line can be used to terminate a multiline statement.

Using multiple statements on a single line

To create multiple statements on a single line, a semicolon is used. However, this is on condition that the new statement does not represent a new block of code that can initiate a different process.

The semi colons can be used as follows:

Multiple Statement Groups as Suites:

When there are groups of a single code expressed as single statements in Python, they are referred to as suites.

Compound statements involving the use of words such as if, def, while, class; require a header line and a suite.

Moreover, also important are the header lines. They start a statement with a key word and they terminate with a colon (:). They require a few other lines so as they can make up a suite.

Here is an example of the use of if, elif and else to make a suite.

Note: the indentation as used is important.

In summary

Interacting with python requires that you should be able to comprehend the basic rules for using Python. A full functional code will require that the right indentations, blank lines, colons, comments and other interacting approaches are correctly applied.