English 中文(简体)
Jython - Variables and Data Types
  • 时间:2024-12-22

Jython - Variables and Data Types


Previous Page Next Page  

Variables are named locations in computer’s memory. Each variable can hold one piece of data in it. Unpke Java, Python is a dynamically typed language. Hence while using Jython also; prior declaration of data type of variable is not done. Rather than the type of variable deciding which data can be stored in it, the data decides the type of variable.

In the following example, a variable is assigned an integer value. Using the type() built-in function, we can verify that the type of variable is an integer. But, if the same variable is assigned a string, the type() function will string as the type of same variable.

> x = 10
>>> type(x)
<class  int >

>>> x = "hello"
>>> type(x)
<class  str >

This explains why Python is called a dynamically typed language.

The following Python built-in data types can also be used in Jython −

    Number

    String

    List

    Tuple

    Dictionary

Python recognizes numeric data as a number, which may be an integer, a real number with floating point or a complex number. String, List and Tuple data types are called sequences.

Jython Numbers

In Python, any signed integer is said to be of type ‘int’. To express a long integer, letter ‘L’ is attached to it. A number with a decimal point separating the integer part from a fractional component is called ‘float’. The fractional part may contain an exponent expressed in the scientific notation using ‘E’ or ‘e’.

A Complex number is also defined as numeric data type in Python. A complex number contains a real part (a floating-point number) and an imaginary part having ‘j’ attached to it.

In order to express a number in the Octal or the Hexadecimal representation, 0O or 0X is prefixed to it. The following code block gives examples of different representations of numbers in Python.

int     -> 10, 100, -786, 80
long    -> 51924361L, -0112L, 47329487234L
float   -> 15.2, -21.9, 32.3+e18, -3.25E+101
complex -> 3.14j, 45.j, 3e+26J, 9.322e-36j

Jython Strings

A string is any sequence of characters enclosed in single (e.g. ‘hello’), double (e.g. “hello”) or triple (e.g. ‘“hello’” o “““hello”””) quotation marks. Triple quotes are especially useful if content of the string spans over multiple pnes.

The Escape sequence characters can be included verbatim in triple quoted string. The following examples show different ways to declare a string in Python.

str = ’hello how are you?’
str = ”Hello how are you?”
str = """this is a long string that is made up of several pnes and non-printable
characters such as TAB ( 	 ) and they will show up that way when displayed. NEWLINEs
within the string, whether exppcitly given pke this within the brackets [ 
 ], or just
a NEWLINE within the variable assignment will also show up.
"""

The third string when printed, will give the following output.

this is a long string that is made up of
several pnes and non-printable characters such as
TAB ( 	 ) and they will show up that way when displayed.
NEWLINEs within the string, whether exppcitly given pke
this within the brackets [
], or just a NEWLINE within
the variable assignment will also show up.

Jython Lists

A List is a sequence data type. It is a collection of comma-separated items, not necessarily of the same type, stored in square brackets. Inspanidual item from the List can be accessed using the zero based index.

The following code block summarizes the usage of a List in Python.

pst1 = [ physics ,  chemistry , 1997, 2000];
pst2 = [1, 2, 3, 4, 5, 6, 7 ];
print "pst1[0]: ", pst1[0]
print "pst2[1:5]: ", pst2[1:5]

The following table describes some of the most common Jython Expressions related to Jython Lists.

Jython Expression Description
len(List) Length
List[2]=10 Updation
Del List[1] Deletion
List.append(20) Append
List.insert(1,15) Insertion
List.sort() Sorting

Jython Tuples

A tuple is an immutable collection of comma-separated data items stored in parentheses. It is not possible to delete or modify an element in tuple, nor is it possible to add an element to the tuple collection. The following code block shows Tuple operations.

tup1 = ( physics , chemistry‘,1997,2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

Jython Dictionary

The Jython Dictionary is similar to Map class in Java Collection framework. It is a collection of key-value pairs. Pairs separated by comma are enclosed in curly brackets. A Dictionary object does not follow zero based index to retrieve element inside it as they are stored by hashing technique.

The same key cannot appear more than once in a dictionary object. However, more than one key can have same associated values. Different functions available with Dictionary object are explained below −

dict = { 011 : New Delhi , 022 : Mumbai , 033 : Kolkata }
print "dict[‘011’]: ",dict[ 011 ]
print "dict[ Age ]: ", dict[ Age ]

The following table describes some of the most common Jython Expressions related to Dictionary.

Jython Expression Description
dict.get(‘011’) Search
len(dict) Length
dict[‘044’] = ‘Chennai’ Append
del dict[‘022’] Delete
dict.keys() pst of keys
dict.values() List of values
dict.clear() Removes all elements
Advertisements