+

Search Tips   |   Advanced Search


Jython


Variables

To define a variable in the Python language, name it using an identifier. Identifiers can consist of any ordering of letters, numbers, or underscores. An identifier must always begin with a non-numeric character value.

Python does not give any type for the value, which allows any variable to hold any type of data. A variable originally assigned with an integer, can later contain a String. For example, we can assign an integer value to a variable, and later change it to a float.

Reserved words which cannot be used as variables:


Indentation

Python uses indentation rather than punctuation to define the structure of code. As long as the first line of a code block is out-dented by at least one space, the rest of the block can maintain a consistent indentation. Four characters is standard


The def statement

Functions are named portions of code that perform one or more tasks and return a value. To define a function we use the def statement.

def precedes the function name and parameter list when defining a function.

Functions w/parameters:

Functions can be passed as parameters to other functions if needed. For example:


Classes

Classes are defined using the class keyword and can contain functions, methods, and variables. Like functions, methods are created using the def keyword. Methods take a parameter known as self that refers to the object to which the method belongs.

Classes contain what is known as an initializer method, and it is called automatically when a class is instantiated.

In this example, we define a class named my_object which accepts parameters, x and y.

A class initializer method named __init__() initializes values used in the class. An initializer also defines what values can be passed to a class in order to create an object. Each method and function within the class accepts the self argument. This is how the class shares variables.


Statements

Statement Keywords:


if-elif-else Statement

Evaluate an expression.


print Statement

Display program output onto the screen

Because the type of the first operand, is a string, the parser treates the (+) operator as a concatenation operator, not as an addition operator. If you try to append a numeric value to a String you will end up with an error.

Python formatting options.

To include the contents of a variable or the result of an expression...

Each of the formatting operators, which are included in the string of text, will be replaced with the corresponding values from those variables at the end of the print statement. The % symbol between the line of text and the list of variables tells Python that it should expect the variables to follow, and that the value of these variables should be placed within the string of text in their corresponding positions.

Using expressions as opposed to variables within our statement.

Different formatting operators.


try-except-finally

The try-except-finally method performs error handling within a Python application.

Place cleanup code within the finally clause of the block. All code within the finally clause is always invoked before the exception is raised.


raise Statement

Place a raise statement anywhere that you wish to raise a specified exception. There are a number of defined exceptions within the language which can be raised. For instance, NameError is raised when a specific piece of code is undefined or has no name.

To specify your own message raise a generic Exception...


import Statement

The import statement brings external modules or code into a program.

If a class is stored in an external module that is named the same as the class itself, the import statement can be used to explicitly bring that class into an application. To import only a specific identifier from another module into the current module, specific code can be named within using syntax...

For example:

To import a module named the same as another identifier in the current program, use the as syntax.


Iteration

A list contains objects or values that can be indexed.

for loop...

while loop...

The len() function returns the number of elements contained in the list.

Python for loop:

The range function provides a range from one particular value to another. In the example, we pass the value 10 into the range which gives us all values between 0 and 10, inclusive of the zero at the front and exclusive at the end. We see this in the resulting print out after the expression.


Basic Keyboard Input