The scalar types objects are the one without accessible internal structure like int and float. The non-scalar objects are called structured types as one can use indexing to extract individual characters from it an also slice it into sub string. There are basically 4 structured type objects :
string
tuples
list
dict
The literals of type string and tuples are classified as non-mutable type objects as the objects once created cannot be modified while literals of type list and dict are called mutable types. Here we will discuss how to define these immutable type objects and some basic functions related to them.
Strings
Objects of type str are used to represent string of characters using single or double quotes. e.g. 'xyz' or "xyz". When defined within inverted commans liters of numeric type also become string. So '1234' is a string.
Operations on string :
Concatenation: adding two strings
>>>a1= '1234'
>>>a2='123'
>>>print a1+a2
will give output:
>>>1234123
Indedxing : accessing particular element of string
>>>a1[1]
will give output:
>>>2
Slicing : breaking strings in parts
>>>a1[1:3]
will give output:
>>>234
Tuples
Like strings, tuples are ordered sequences of elements except that the elements need not be characters. The individual elements can be of any type and need not be of the same type as each other. Tuples are defined with comma-seperated values within parentheses.
For e.g
t1 = (1,4)
t2 = (1,'')
While writing a singleton tuple also the comma is denoted after value like this (1,).
Operations on Tuples :
Concatenation : adding two tuples
>>>t1 = (1, 4)
>>>t2 = (1, 'two', 3)
>>> print (t1+t2)
will give the output:
>>>(1, 4, 1, 'two', 3)
Slicing : slicing a tuple in two parts
>>> print (t1+t2)[0:2]
will give the output:
>>>(1, 4, )
Indexing : reaching any particular element
>>>print (t1+t2)[3]
will give the output:
>>>('two')
string
tuples
list
dict
The literals of type string and tuples are classified as non-mutable type objects as the objects once created cannot be modified while literals of type list and dict are called mutable types. Here we will discuss how to define these immutable type objects and some basic functions related to them.
Strings
Objects of type str are used to represent string of characters using single or double quotes. e.g. 'xyz' or "xyz". When defined within inverted commans liters of numeric type also become string. So '1234' is a string.
Operations on string :
Concatenation: adding two strings
>>>a1= '1234'
>>>a2='123'
>>>print a1+a2
will give output:
>>>1234123
Indedxing : accessing particular element of string
>>>a1[1]
will give output:
>>>2
Slicing : breaking strings in parts
>>>a1[1:3]
will give output:
>>>234
Tuples
Like strings, tuples are ordered sequences of elements except that the elements need not be characters. The individual elements can be of any type and need not be of the same type as each other. Tuples are defined with comma-seperated values within parentheses.
For e.g
t1 = (1,4)
t2 = (1,'')
While writing a singleton tuple also the comma is denoted after value like this (1,).
Operations on Tuples :
Concatenation : adding two tuples
>>>t1 = (1, 4)
>>>t2 = (1, 'two', 3)
>>> print (t1+t2)
will give the output:
>>>(1, 4, 1, 'two', 3)
Slicing : slicing a tuple in two parts
>>> print (t1+t2)[0:2]
will give the output:
>>>(1, 4, )
Indexing : reaching any particular element
>>>print (t1+t2)[3]
will give the output:
>>>('two')
No comments:
Post a Comment