Download our e-book of Introduction To Python
Pallavi Dhotre
2 years ago
# Sequence types in python - lists
demo_list=[4,5,6] #creating list
print(demo_list[0]) #accessing elements
print(demo_list[1])
4
5
# Sequence types in python - tuple
demo_tuple=(4,5,6) #creating tuple
print(demo_tuple[0]) #accessing elements
print(demo_tuple[1])
4
5
# Sequence types in python - string
demo_string="Welcome" #creating string
print("String is ",demo_string)
print(demo_string[0]) #accessing characters of string by their location
print(demo_string[1])
String is Welcome
W
e
# Sequence types in python - byte sequence
demo_size= 5
demo =bytes(demo_size) #creating byte sequence
print(demo)
print(bytes([4,3,5,2,1])) #converting iterables into bytes
b'\x00\x00\x00\x00\x00'
b'\x04\x03\x05\x02\x01'
# sequence in python example - a byte array
print(bytearray(4))
print(bytearray([4,3,5,2,1])) #converting iterables into bytes
bytearray(b'\x00\x00\x00\x00')
bytearray(b'\x04\x03\x05\x02\x01')
# sequence in python example - range object
for i in range(1,5):
print(i)
1
2
3
4
# Sequence types in python - concatenation
a='India'
b=' is best'
print(a+b)
India is best
# Sequence types in python - Replication
a='hello'
b=' John'
print((a*2)+b)
hellohello John
# Sequence types in python - membership
demo_string="Welcome to this new blog"
print('this' in demo_string)
True
# Sequence types in python - slice
demo_string="Welcome to this new blog"
print(demo_string[3:7])
come
# Sequence types in python - functions
demo_string="Welcome to this new blog"
print(len(demo_string))
24
# Sequence types in python - functions
demo_string="Welcome"
print(min(demo_string))
print(max(demo_string))
W
o
# Collections module in python - sets
demo_set={2,3,4}
print(demo_set)
{2, 3, 4}
# Collections module in python - dictionaries
demo_dict={2:'hello',4:'hi',6:'welcome'}
print(demo_dict)
print(demo_dict[2]) #accessing element having key 2
{2: 'hello', 4: 'hi', 6: 'welcome'}
hello