Professional Documents
Culture Documents
Sequences
Sequences
• contains objects that are kept in a specific order
• can be enclosed in single ('), double (") or triple quotes (''' or """) as follows:
• In a triple quoted string, line breaks in the literal are preserved as newline
characters. You cannot use a backslash in a triple-quoted string
Strings (immutable or static )
How characters are stored in a string
• Example s = “Mi”
names[-2:] #returns second from last to end of the list ['Caroline', 'Paula']
Lists (mutable)
Other list methods
• some of the methods
that can be applied to lists
Tuples (immutable)
Tuple objects
• unlike strings, which can contain only characters tuples can contain elements of any type
• are faster than lists, when an item has to be added on the list a new one is
created
• if tuples contain lists, then they can not be used as dictionary keys
• since the sets are unordered, they do not support slicing or other
sequence-like behaviour
• to tests membership
s1=set([3,5,6,10,11,100])
s2=set([1,3,5,6,11,15])
s1=set([3,5,6,10,11,100])
s2=set([1,3,5,6,11,15])
• In a difference operation, all the elements that are in the left set but not
in the right set will appear in the difference operation
s1=set([3,5,6,10,11,100])
s2=set([1,3,5,6,11,15])
• is the object that appears in one of the two sets provided but not in both
s1=set([3,5,6,10,11,100])
s2=set([1,3,5,6,11,15])
s1=set([3,5,6,10,11,100])
s2=set([1,3,5,6,11,15])
10 in s1 returns True
15 in s1 returns False
15 in s2 returns True
Sets (mutable)
Iteration through a set
s1=set([3,5,6,10,11,100])
for n in s:
if ((n % 2 ) == 0)
print (“%d is even” %n)
else:
print (“%d is odd” %n)
• Output: 3 is odd
5 is odd
6 is even
10 is even
11 is odd
100 is even
Sets (mutable)
Removing objects from a set
•there are four functions to remove objects from a set
•discard () removes a specified object from a set. No error if object does not exist