Python Programming 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Exp11: Concatenation , Multiple the string

and checking for event in a sequence


>>> [1,2,3]+[4,5,6]
Result:
[1, 2, 3, 4, 5, 6]
>>> "TJ "+"Idea for innovation"
Result:
'TJ Idea for innovation'
Note:Two different data types cant be
Concatenated
>>> [1,2,3]+"TJ"
Result:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
[1,2,3]+"TJ"
TypeError: can only concatenate list (not "str") to
list
Multiplying string

>>> "TJ "*10


Result:
'TJ TJ TJ TJ TJ TJ TJ TJ TJ TJ '
>>> [12]*10
Result:
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12]

Sorting a char
>>> Company="TJ Idea for Innovation"
>>> 'z' in Company
Result:
False
>>> 't' in Company
Result:
True
Note: Its Case sensitive
>>> 'j' in Company
Result:
False
>>> 'TJ' in Company
Result:
True

Sorting a string over a sequence


>>> animals=['dog','cat','lion','tiger']
>>> 'dog' in animals
Result:
True
>>> 'snake' in animals
Result:
False
Note: Its Case sensitive
>>> 'LION' in animals
Result:
False
Exp12: More about Sequences and list
>>> numbers=[1,20,30,59,70]
To know Length of the index with key function
>>> len(numbers)
5
Max value of integer over the array
>>> max(numbers)
70
Min value of integer over the array
>>> min(numbers)
1
String to array of characters
>>> list('TJ Idea for Innovation')
['T', 'J', ' ', 'I', 'd', 'e', 'a', ' ', 'f', 'o', 'r', ' ', 'I', 'n',
'n', 'o', 'v', 'a', 't', 'i', 'o', 'n']
>>> numbers
[1, 20, 30, 59, 70]

Replace the integer with index value


>>> numbers[3]=40
>>> numbers
[1, 20, 30, 40, 70]
To del the integer from the array select index
and delete
>>> del numbers[3]
>>> numbers
[1, 20, 30, 70]
Exp13: Slicing with list
String to array of characters
>>> example=list('TJ')
Result:
>>> example
['T', 'J']
Adding string to list
>>> example[2:]=list('Idea')
Result:
>>> example
['T', 'J', 'I', 'd', 'e', 'a']
Adding string to list
>>> example[6:]=list('for innovation')
Result:
>>> example
['T', 'J', 'I', 'd', 'e', 'a', 'f', 'o', 'r', ' ', 'i', 'n', 'n', 'o',
'v', 'a', 't', 'i', 'o', 'n']
Adding variables between list variables
>>> example=[1,4,5]
Result:
>>> example
[1, 4, 5]
Adding variables from list
>>> example[1:1]=[2,3]
Result:
>>> example
[1, 2, 3, 4, 5]
Removing variables from list
>>> example[1:4]=[]
Result:
>>> example
[1, 5]
Exp14: Introduction to Methods
>>> mynumbers=[23,26,24]
>>> mynumbers
Result:
[23, 26, 24]
To add a one variable to array of numbers
>>> mynumbers.append(27)
>>> mynumbers
Result:
[23, 26, 24, 27]
>>>cart=['brush','apples','soap','soap','sugar','b
atteries','batteries']
>>> cart
Result:
['brush', 'apples', 'soap', 'soap', 'sugar',
'batteries', 'batteries']
To count same strings or numbers in a array
>>> cart.count('soap')
Result:
2
>>> cart.count('apples')
Result:
1
>>> cart.count('batteries')
Result:
2
>>> numbers1=[1,2,3]
>>> numbers1
Result:
[1, 2, 3]
>>> numbers2=[4,5,6]
To add a one or more variable to array of
numbers
>>> numbers1.extend(numbers2)
>>> numbers1
Result:
[1, 2, 3, 4, 5, 6]
Exp15:Index and Methods
>>> cart
Result:
['brush', 'apples', 'soap', 'soap', 'sugar',
'batteries', 'batteries']
To know the index of the value in a array
>>> cart.index('apples')
Result:
1
Insert new value to array using index
>>> cart.insert(2,'chocolets')
>>> cart
Result:
['brush', 'apples', 'chocolets', 'soap', 'soap',
'sugar', 'batteries', 'batteries']
To delete the value from array
>>> cart.pop(5)
Result:
'sugar'
>>> cart
Result:
['brush', 'apples', 'chocolets', 'soap', 'soap',
'batteries', 'batteries']

To delete the value from array


>>> cart.remove('apples')
>>> cart
Result:
['brush', 'chocolets', 'soap', 'soap', 'batteries',
'batteries']
Note: both “pop” and “remove” methods
are same ,but only the difference is “pop”
returns the value after its action .where
”remove” doesn’t returns any value.

>>> cart
['brush', 'chocolets', 'soap', 'soap', 'batteries',
'batteries']
To reverse the array variables index
>>> cart.reverse()
>>> cart
['batteries', 'batteries', 'soap', 'soap',
'chocolets', 'brush']
Exp16:Sort and Tuples
>>> mynumbers=[21,20,19,12,1,2,3]
>>> mynumbers
Result:
[21, 20, 19, 12, 1, 2, 3]
To sort the Integer values
>>> mynumbers.sort()
>>> mynumbers
Result:
[1, 2, 3, 12, 19, 20, 21]
To sort the string values
>>> sorted('TJ idea for innovation')
Result:
[' ', ' ', ' ', 'J', 'T', 'a', 'a', 'd', 'e', 'f', 'i', 'i', 'i', 'n', 'n',
'n', 'o', 'o', 'o', 'r', 't', 'v']
Note:Tuples are slightly similar to list which
exist with “( )” ,where tuples variable can’t be
inserted with new values or deleted.
>>> 22,3,6,9
Result:
(22, 3, 6, 9)

>>> TJ=(1,5,6,4,8)
>>> TJ
Result:
(1, 5, 6, 4, 8)
>>> TJ[3]
Result:
4

You might also like