In this post, I will show how to play with the Sequences, by playing i meant how to modify a sequence.
As usual open up the IDLE, and lets create 2 sequence and assign it to variables
>>> a = [6,4,5,7,8]
>>> b = [3,5,6,7]
>>> a+b
[6, 4, 5, 7, 8, 3, 5, 6, 7]
>>>
>>> [4,8,5,9,11,32]+[22,67,32,67,12]
[4, 8, 5, 9, 11, 32, 22, 67, 32, 67, 12]
>>>
So this was how we concatenate two sequence with "+" operator
Now lets try to concatenate to string type sequence.
>>> x = 'Python'
>>> y = 'Journey'
>>> x+y
'PythonJourney'
>>>
>>> 'Love '+'Python'
'Love Python'
>>>
>>> fruits = ['apple ','mango ','banana ']
>>> k = ['I ', 'just ','hate ','fruits']
>>> fruits+k
['apple ', 'mango ', 'banana ', 'I ', 'just ', 'hate ', 'fruits']
Worked well, I guess we know by now that strings are written in qoutes.
Ok till now we have seen how to concatenate between to same data types.
Now lets try to add to different data types and see what does python say:
>>> ex1 = 'Whiskey'
>>> ex2 = [4,2,6,8,1,0]
>>> ex1+ex2
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
ex1+ex2
TypeError: cannot concatenate 'str' and 'list' objects
>>>
Yeah, It cannot Python is dumb, not intelligent like us, doesn't understand if you ask him to concatenate two different data types sequence.
Seems Good till now, now lets learn something else..
How can we display a sequence multiple times
>>> 'Python '*10
'Python Python Python Python Python Python Python Python Python Python '
>>>
Well multiplying the sequence with given number repeated the string that many times and displayed.
Will it be the same for Integer type Sequence??
yes even for Integer type data it will provided you have it in Quotes.
>>> 12*10
120
>>>
>>> '12 '*10
'12 12 12 12 12 12 12 12 12 12 '
>>>
Here is one Last Thing to share..
Its known as Membership.
We will check if something, is in a sequence, m sorry i could make it more simple for defining. Check the example here:
Lets make a Sequence
>>> name = 'whiskey'
>>> 'b' in name
False
>>> 'W' in name
False
>>> 'w' in name
True
>>>
Here "in" is a built in keyword to check a condition. With this statement, python thinks it like
Is 'b' present in the variable name?? replies to himself.. No
And displays False
Same is the case, if a sequence has more than one element.
>>> family=['mom','dad','bro','sis']
>>> 'sis' in family
True
>>> 'dog' in family
False
>>>
So I hope this was interesting, practice this, and dont forget to check my next post.
Thank You.
As usual open up the IDLE, and lets create 2 sequence and assign it to variables
>>> a = [6,4,5,7,8]
>>> b = [3,5,6,7]
>>> a+b
[6, 4, 5, 7, 8, 3, 5, 6, 7]
>>>
>>> [4,8,5,9,11,32]+[22,67,32,67,12]
[4, 8, 5, 9, 11, 32, 22, 67, 32, 67, 12]
>>>
So this was how we concatenate two sequence with "+" operator
Now lets try to concatenate to string type sequence.
>>> x = 'Python'
>>> y = 'Journey'
>>> x+y
'PythonJourney'
>>>
>>> 'Love '+'Python'
'Love Python'
>>>
>>> fruits = ['apple ','mango ','banana ']
>>> k = ['I ', 'just ','hate ','fruits']
>>> fruits+k
['apple ', 'mango ', 'banana ', 'I ', 'just ', 'hate ', 'fruits']
Worked well, I guess we know by now that strings are written in qoutes.
Ok till now we have seen how to concatenate between to same data types.
Now lets try to add to different data types and see what does python say:
>>> ex1 = 'Whiskey'
>>> ex2 = [4,2,6,8,1,0]
>>> ex1+ex2
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
ex1+ex2
TypeError: cannot concatenate 'str' and 'list' objects
>>>
Yeah, It cannot Python is dumb, not intelligent like us, doesn't understand if you ask him to concatenate two different data types sequence.
Seems Good till now, now lets learn something else..
How can we display a sequence multiple times
>>> 'Python '*10
'Python Python Python Python Python Python Python Python Python Python '
>>>
Well multiplying the sequence with given number repeated the string that many times and displayed.
Will it be the same for Integer type Sequence??
yes even for Integer type data it will provided you have it in Quotes.
>>> 12*10
120
>>>
>>> '12 '*10
'12 12 12 12 12 12 12 12 12 12 '
>>>
Here is one Last Thing to share..
Its known as Membership.
We will check if something, is in a sequence, m sorry i could make it more simple for defining. Check the example here:
Lets make a Sequence
>>> name = 'whiskey'
>>> 'b' in name
False
>>> 'W' in name
False
>>> 'w' in name
True
>>>
Here "in" is a built in keyword to check a condition. With this statement, python thinks it like
Is 'b' present in the variable name?? replies to himself.. No
And displays False
Same is the case, if a sequence has more than one element.
>>> family=['mom','dad','bro','sis']
>>> 'sis' in family
True
>>> 'dog' in family
False
>>>
So I hope this was interesting, practice this, and dont forget to check my next post.
Thank You.
that was Coool
ReplyDeletethank you :)
Welcome :)
Delete