str,tupple,set
#str is a class
#str is type
#str is iterable
#str is a sequence
#str elements are indexed
#str is mutable
#creating string object
s1 = 'abc'
s1 = "abc"
s1 = '''abc'''
s1 = """abc"""
s1 = str( )
Similar to list it support both positive and negative indexing.
>>> s = ['Hari','Sapkota']
>>> ' '.join(s)
'Hari Sapkota'
>>>
Similar to list comprehension we have concept of set comprehension
Some basic operations in str.
All builtin methods apply to str() similar to list
len(), sorted()
Member function
s1. startswith(), s1.endswith(), s1.isdigit() etc.
List can be checked using help menu - help('str')
and many more ...
one of the important function is split function
s = "This is word".split(' ')
this will split the words with space and provide new list which can be iterated.
join ()
it returns single string after joining that with given symbol.
Example
>>> s = ['Hari','Sapkota']
>>> ' '.join(s)
'Hari Sapkota'
>>>
format()
>>> a,b,c = 10,20,30
>>> msg = "sum of {a} and {b} is {c}"
>>> msg = "sum of {} and {} is {}"
>>> msg
'sum of {} and {} is {}'
>>> msg.format(a,b,c)
'sum of 10 and 20 is 30'
>>>
tuple
#tuple is a class
#tuple is type
#tuple is an iterable
#tuple is a sequence
#tuple elements are indexed
#tuple is immutable
#tuple can contain values of different types like str
#creating tuple object
t = (1, 2, 3)
>>> type(t)
<class 'tuple'>
>>> t = tuple()
if we want store single element in tupple
t = (10) this would be treated as int
to store single element, it should be done as
t = (10,)
There is no need to keep parenthesis, by default comma separated elements are tuple.
t = 1,2,3,4
type (t) - tuple.
Since tuple is indexed - concept of indexing applies
It is iterable so elements can be looped.
Builtins method works similar to str and list such as sorted, max, min, sum,len,count,index etc.
class tuple(object)
| tuple(iterable=(), /)
|
| Built-in immutable sequence.
|
| If no argument is given, the constructor returns an empty tuple.
| If iterable is specified the tuple is initialized from iterable's items.
|
| If the argument is a tuple, the return value is the same object.
No append, insert, sort - since tuple is immutable.
We can add iterable element in to tupple and convert it as per below.
>>> t = tuple([1,2,3,4])
>>> t
(1, 2, 3, 4)
Set()
#set a class
#set is type
#set is an iterable
#set is NOT a sequence
#set has no concept of indexing
#set doesn't support slicing operator
#set is mutable
#set cannot have duplicate values
In set no order is guaranteed, it is internally created
based on the hierarchical data structure.
Example:
>>> s1
{1, 2, 3, 4}
>>> s = {2,1,5,3,9}
>>> s
{1, 2, 3, 5, 9}
- Here, order is interchanged.
- No, duplicates allowed.
- Set support heterogeneous elements.
- Predefined datatypes such as list and set not allowed.
Example - >>>
s = {2,1,5,3,9,5,6,5.6,[4,5,6]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>
we can use update method to change.
{4, 6, 5.6, 5, 9, 10}
>>> s.update([1,2,3])
>>> s
{2, 1, 4, 6, 5.6, 5, 9, 10, 3}
>>>
One more important point -
We can use add method to add single element in the set. In set we cannot use list
and other set, only allowable entry would be tuple. Since tuple acts as a single
element it can be added.
Example below.
>>> s.add([1,2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> s.add({1,2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> s.add((1,2))
>>> s
{2, 1, 4, 6, 5.6, 5, 9, 10, 3, (1, 2)}
Here, (1,2) is added and acts as a single entity. This is difference to be noted.
whereas update takes only iterables and place individual elements in set.
>>> s.update('this is a')
>>> s
{2, 1, 4, 6, 5.6, 5, 9, 10, 3, (1, 2), 'i', 's', ' ', 'a', 't', 'h'}
Even, though it is not indexed, we can access via for loop.
while loop isn't supported as there is no index.
{1, 2, 3, 'hari', 5, 6, 5.6, 9}
>>> for e in s:
... print(e)
...
1
2
3
hari
5
6
5.6
9
>>>
some member functions.
| add(...)
| Add an element to a set.
|
| This has no effect if the element is already present.
|
| clear(...)
| Remove all elements from this set.
|
| copy(...)
| Return a shallow copy of a set.
|
| difference(...)
| Return the difference of two or more sets as a new set.
|
| (i.e. all elements that are in this set but not the others.)
|
| difference_update(...)
| Remove all elements of another set from this set.
|
| discard(...)
| Remove an element from a set if it is a member.
|
| Unlike set.remove(), the discard() method does not raise
| an exception when an element is missing from the set.
|
| intersection(...)
| Return the intersection of two sets as a new set.
|
| (i.e. all elements that are in both sets.)
| pop(...)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
|
| remove(...)
| Remove an element from a set; it must be a member.
|
| If the element is not a member, raise a KeyError.
|
| symmetric_difference(...)
| Return the symmetric difference of two sets as a new set.
|
| (i.e. all elements that are in exactly one of the sets.)
|
| symmetric_difference_update(...)
| Update a set with the symmetric difference of itself and another.
|
| union(...)
| Return the union of sets as a new set.
|
| (i.e. all elements that are in either set.)
Similar to list comprehension we have concept of set comprehension
Example -
>>> s
{1, 2, 3, 4}
>>> { e for e in s}
{1, 2, 3, 4}
>>> l = [10,1,30,20,10,30,20,1,32,40]
>>> s = set(l)
>>> s
{32, 1, 40, 10, 20, 30}
Comments
Post a Comment