Everything in Python is an object. The various data types are listed in Table 1.
| No. | Data Type | Description |
| 1 | Numbers | The numbers can be integers, float, fractions or complex. |
| 2 | Boolean | Can be True or False |
| 3 | String | Sequence of unicode characters |
| 4 | List | Sequence of ordered homogenous values |
| 5 | Tuples | Sequence of heterogenous values |
| 6 | Bytes, Bytearray | These are binary buffers that represent bytes (numbers 0 through 255), often helpful in image processing. |
| 7 | Sets | Set is an unordered collection with no duplicate elements. |
The numbers can be integers, long integers, float or complex. The entries int(10), float(10), long(10) and complex(10,3) return 10 , 10.0 , 10L and 10+3j respectively.
Boolean is a True or False.
Strings contain homogenous objects, put within single or double quotes.
x='hellow'
type x*3 at a Python console and see what happens.
y='friend'
The strings x and y are concatenated by x+y. Try out.
Type x[1:3] and observe the response.
Lists look like comma separated values, included within square backets. They
are useful in iterations. The operations that are done on strings are possi-
ble on lists as well.
Unlike many langauages, Python support tuples. They
are nonhomogenous values, separated by commas, included within brackets.
Apart from these changes in the syntax, there are a couple of differences.
Lists contain homogenous objects while tuples contain heterogenous ones.
The elements in the list are mutable, meaning that they can be changed af-
ter the assignment. The tuple elements are immutable.
In the code below,
the variable x is a list, while y is a tuple.
x=[ 1 , 2 , 3 , 4 , 5 ]
y=( 'May 2014' , hari' )
Bytes and Bytearrays are used for storing binary buffers that hold values between 0 and 255. These objects are often useful when working with digital images.
members = [0, 200, 100, 25, 10, 255]
value = bytearray(members)
print value
returns the bytearray
If the last element of members is increased to 256, it raises an exception ValueError: byte must be in range(0, 256). The returned array value is a mutable array on which slicing etc. is posssible.
Sets are lists without duplicate entries. Set objects support mathematical operations like union, intersection, difference, and symmetric difference. Observe the outputs of the code.
x=['tomato','apple','tomato','papaya','brinjal', 'cucumber','papaya']
y=set(x)
print y
z=set(['tomato','apple','papaya'])
print y.union(z)
print y.intersection(z)
print y.difference(z)
hari 2017-08-09
Comments
Post a Comment