Tuesday, May 22, 2018

Common Sequence Operations

The operations in the following table are supported by most sequence types, both mutable and immutable.


OperationResultNotes
x in sTrue if an item of s is equal to x, else False(1)
x not in sFalse if an item of s is equal to x, else True(1)
s + tthe concatenation of s and t(6)(7)
s * n or n * sequivalent to adding s to itself n times(2)(7)
s[i]ith item of s, origin 0(3)
s[i:j]slice of s from i to j(3)(4)
s[i:j:k]slice of s from i to j with step k(3)(5)
len(s)length of s
min(s)smallest item of s
max(s)largest item of s
s.index(x[, i[, j]])index of the first occurrence of x in s (at or after index i and before index j)(8)
s.count(x)total number of occurrences of x in s
Below are the examples for the above tables.


x = 'Apple's = 'She eats Apple 't = 'in the morning'n = 2i = 10j = 20k = 21
print(x in s)
print(x not in s)
print(s + t)
print(x*n)
print(s[i])
print(s[i:j])
print(s[i:j:k])
print(len(s))
print(min('a','b'))
print(max('a','b'))
print(s.index(str(x)))
print(s.count('e'))


Output : 

True
False
She eats Apple in the morning
AppleApple
p
pple 
p
15
a
b
9
3



No comments:

Post a Comment

Program to check valid IP addresses