0% found this document useful (0 votes)
21 views

5th Dec Python Basic

The document demonstrates various string and list operations in Python like: 1) Accessing characters in a string using indexes and slicing operations like s[0], s[-1], s[0:3] etc. 2) Common string methods like upper(), lower(), find(), count() etc. 3) Splitting a string into a list of substrings using split() method. 4) Basic list operations - accessing/changing elements, slicing, appending, inserting, popping etc. So in summary, the document shows the usage of important string and list methods and indexing/slicing operations in Python through examples.

Uploaded by

bhuvimsit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

5th Dec Python Basic

The document demonstrates various string and list operations in Python like: 1) Accessing characters in a string using indexes and slicing operations like s[0], s[-1], s[0:3] etc. 2) Common string methods like upper(), lower(), find(), count() etc. 3) Splitting a string into a list of substrings using split() method. 4) Basic list operations - accessing/changing elements, slicing, appending, inserting, popping etc. So in summary, the document shows the usage of important string and list methods and indexing/slicing operations in Python through examples.

Uploaded by

bhuvimsit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: s = "sudh"

In [2]: type(s)

str
Out[2]:

In [3]: s[0]

's'
Out[3]:

In [4]: s[1]

'u'
Out[4]:

In [5]: s[2]

'd'
Out[5]:

In [6]: s[3]

'h'
Out[6]:

In [7]: s[-1]

'h'
Out[7]:

In [8]: s[-2]

'd'
Out[8]:

In [9]: a = "my name is sudh"

In [10]: a[-1]

'h'
Out[10]:

In [11]: a[0]

'm'
Out[11]:

In [12]: a[100]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-12-cd7724b6c85f> in <module>
----> 1 a[100]

IndexError: string index out of range

In [14]: a

'my name is sudh'


Out[14]:

In [15]: a[0:10]

'my name is'


Out[15]:

In [16]: b = "ineuron"

In [17]: b[0:3]

'ine'
Out[17]:

In [18]: b[0:300]

'ineuron'
Out[18]:

In [19]: b[300]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-19-6ff6ddb2b1a6> in <module>
----> 1 b[300]

IndexError: string index out of range

In [21]: b[-100]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-21-b20bba380709> in <module>
----> 1 b[-100]

IndexError: string index out of range

In [22]: b[-1:-4]

''
Out[22]:

In [23]: b[0:4]

'ineu'
Out[23]:

In [24]: a = "kumar"

In [27]: a[0:300:1]

'kumar'
Out[27]:

In [29]: a[0:300:3]

'ka'
Out[29]:

In [30]: a[0:100:-1]

''
Out[30]:

In [32]: a[-1:-4:-1]

'ram'
Out[32]:

In [34]: a[0:-10:-1]

'k'
Out[34]:

In [35]: a

'kumar'
Out[35]:

In [36]: a[::]

'kumar'
Out[36]:

In [37]: a[:8]

'kumar'
Out[37]:

In [39]: a[-2:-1]

'a'
Out[39]:

In [40]: a

'kumar'
Out[40]:

In [41]: a[::-1]

'ramuk'
Out[41]:

In [42]: a[-1::-1]

'ramuk'
Out[42]:

In [43]: a = "i am working with ineuron"

In [44]: a[::-1]

'norueni htiw gnikrow ma i'


Out[44]:

In [46]: a[-5:5:-1]

'eni htiw gnikro'


Out[46]:

In [47]: a[-2:-10:-1]

'orueni h'
Out[47]:

In [48]: a[0:100:3]

'imoi tiun'
Out[48]:

In [49]: "sudh"*3

'sudhsudhsudh'
Out[49]:

In [50]: "sudh" + "kumar"

'sudhkumar'
Out[50]:

In [51]: a

'i am working with ineuron'


Out[51]:

In [52]: len(a)

25
Out[52]:

In [54]: "sudh"*4

'sudhsudhsudhsudh'
Out[54]:

In [55]: a

'i am working with ineuron'


Out[55]:

In [56]: a.find('a')

2
Out[56]:

In [58]: a.find('ia')

-1
Out[58]:

In [59]: a.find('in')

9
Out[59]:

In [60]: a.count('i')

4
Out[60]:

In [61]: a.count('x')

0
Out[61]:

In [62]: a

'i am working with ineuron'


Out[62]:

In [66]: l = a.split()

In [67]: l

['i', 'am', 'working', 'with', 'ineuron']


Out[67]:

In [68]: l[0]

'i'
Out[68]:

In [69]: l[1]

'am'
Out[69]:

In [70]: l[2]

'working'
Out[70]:

In [71]: l[0:3]

['i', 'am', 'working']


Out[71]:

In [75]: a.split('wo')

['i am ', 'rking with ineuron']


Out[75]:

In [73]: a

'i am working with ineuron'


Out[73]:

In [76]: a

'i am working with ineuron'


Out[76]:

In [77]: a.upper()

'I AM WORKING WITH INEURON'


Out[77]:

In [78]: s = "sUdh"

In [79]: s.swapcase()

'SuDH'
Out[79]:

In [80]: s.title()

'Sudh'
Out[80]:

In [81]: s.capitalize()

'Sudh'
Out[81]:

In [82]: b = "sudh"
c = "ineuron"

In [84]: b.join(c)

'isudhnsudhesudhusudhrsudhosudhn'
Out[84]:

In [85]: " ".join("sudh")

's u d h'
Out[85]:

In [87]: for i in reversed("sudh"):


print(i)

h
d
u
s

In [88]: s= "sudh"
s[::-1]

'hdus'
Out[88]:

In [90]: s = " sudh "

In [91]: s.rstrip()

' sudh'
Out[91]:

In [92]: s.lstrip()

'sudh '
Out[92]:

In [93]: s.strip()

'sudh'
Out[93]:

In [94]: s = "sudh"

In [96]: s.replace("t" , "xyz")

'sudh'
Out[96]:

In [97]: "sudh\tkumar".expandtabs()

'sudh kumar'
Out[97]:

In [98]: s= "sudh"

In [101… s.center(40 , '#')

'##################sudh##################'
Out[101]:

In [102… b=" indians are the best in the world"


b.strip("are")

' indians are the best in the world'


Out[102]:

In [103… b=" indians are the best in the world"


b.strip()

'indians are the best in the world'


Out[103]:

In [108… s = "SUDH"

In [109… s.isupper()

True
Out[109]:

In [110… s.islower()

False
Out[110]:

In [ ]:

In [114… s= " "

In [115… s.isspace()

True
Out[115]:

In [120… s = "43543"

In [121… s.isdigit()

True
Out[121]:

In [122… s = "sudh"

In [124… s.endswith('x')

False
Out[124]:

In [126… s.startswith('s')

True
Out[126]:

In [127… s.istitle()

False
Out[127]:

In [130… s.encode()

b'sudh'
Out[130]:

In [131… s = "sdfsaf dsaf safasdf asdfsadfdas fadf dfa fadfa dsaf adfsa"

In [132… l = ["sudh" , "kumar" , 3543 , 4+6j , True , 345.56]

In [133… type(l)

list
Out[133]:

In [134… l[0]

'sudh'
Out[134]:

In [135… l[-1]

345.56
Out[135]:

In [136… l[-5]

'kumar'
Out[136]:

In [137… l

['sudh', 'kumar', 3543, (4+6j), True, 345.56]


Out[137]:

In [138… l[0:4]

['sudh', 'kumar', 3543, (4+6j)]


Out[138]:

In [139… l[::-1]

[345.56, True, (4+6j), 3543, 'kumar', 'sudh']


Out[139]:

In [140… l[-1:6]

[345.56]
Out[140]:

In [142… type(l[0])

str
Out[142]:

In [146… type(l[4])

bool
Out[146]:

In [148… l[0][1]

'u'
Out[148]:

In [151… l[3].imag

6.0
Out[151]:

In [152… l1 = ["sudh" , "kumar" , 4356]


l2 = ["xyz" , "pqr" , 546.567]

In [153… l1+l2

['sudh', 'kumar', 4356, 'xyz', 'pqr', 546.567]


Out[153]:

In [154… l1 + ["sudh"]

['sudh', 'kumar', 4356, 'sudh']


Out[154]:

In [156… l1*2

['sudh', 'kumar', 4356, 'sudh', 'kumar', 4356]


Out[156]:

In [157… l1

['sudh', 'kumar', 4356]


Out[157]:

In [164… l1[0] =4356455634564

In [165… l1

[4356455634564, 'kumar', 4356]


Out[165]:

In [166… l1[1]

'kumar'
Out[166]:

In [167… s = "sudh"

In [169… s[0] = "k"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-169-00de5ec5ca3c> in <module>
----> 1 s[0] = "k"

TypeError: 'str' object does not support item assignment

In [170… l1

[4356455634564, 'kumar', 4356]


Out[170]:

In [171… l[1] = "sudh"

In [172… s.replace('s','v')

'vudh'
Out[172]:

In [173… s

'sudh'
Out[173]:

In [174… s[0] = 'v'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-174-3868546884ef> in <module>
----> 1 s[0] = 'v'

TypeError: 'str' object does not support item assignment

In [175… l1

[4356455634564, 'kumar', 4356]


Out[175]:

In [176… len(l1)

3
Out[176]:

In [178… 345 in l1

False
Out[178]:

In [180… l = ['okay','kumar', 78,45,789,5.5,True, 5+8j]


l[-1:6:-1]

[(5+8j)]
Out[180]:

In [181… l2

['xyz', 'pqr', 546.567]


Out[181]:

In [182… l2.append("sudh")

In [183… l2

['xyz', 'pqr', 546.567, 'sudh']


Out[183]:

In [184… l2.pop()

'sudh'
Out[184]:

In [185… l2.pop(3)

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-185-e4fec1d81b7d> in <module>
----> 1 l2.pop(3)

IndexError: pop index out of range

In [186… l2.pop(2)

546.567
Out[186]:

In [187… l2

['xyz', 'pqr']
Out[187]:

In [188… l2

['xyz', 'pqr']
Out[188]:

In [189… l2.append(345435)

In [190… l2

['xyz', 'pqr', 345435]


Out[190]:

In [191… l2.insert(1,"dsgadfgda")

In [192… l2

['xyz', 'dsgadfgda', 'pqr', 345435]


Out[192]:

In [193… l2.insert(3,[34,45,56,576,67])

In [194… l2

['xyz', 'dsgadfgda', 'pqr', [34, 45, 56, 576, 67], 345435]


Out[194]:

In [195… l2

['xyz', 'dsgadfgda', 'pqr', [34, 45, 56, 576, 67], 345435]


Out[195]:

In [196… l2[::-1]

[345435, [34, 45, 56, 576, 67], 'pqr', 'dsgadfgda', 'xyz']


Out[196]:

In [197… l2.reverse()

In [198… l2

[345435, [34, 45, 56, 576, 67], 'pqr', 'dsgadfgda', 'xyz']


Out[198]:

In [199… l1

[4356455634564, 'kumar', 4356]


Out[199]:

In [200… l2

[345435, [34, 45, 56, 576, 67], 'pqr', 'dsgadfgda', 'xyz']


Out[200]:

In [202… l2[1][2]

56
Out[202]:

In [204… l2[1][2]

56
Out[204]:

In [205… l2

[345435, [34, 45, 56, 576, 67], 'pqr', 'dsgadfgda', 'xyz']


Out[205]:

In [207… l2.count('xfddsfsfyz')

0
Out[207]:

In [208… l2.append("dssafasf")

In [209… l2

[345435, [34, 45, 56, 576, 67], 'pqr', 'dsgadfgda', 'xyz', 'dssafasf']
Out[209]:

In [210… l2.append([3,4,54,6])

In [211… l2

[345435,
Out[211]:
[34, 45, 56, 576, 67],
'pqr',
'dsgadfgda',
'xyz',
'dssafasf',
[3, 4, 54, 6]]

In [212… l1

[4356455634564, 'kumar', 4356]


Out[212]:

In [213… l1.extend([2,54,656, "sdfs"])

In [214… l1

[4356455634564, 'kumar', 4356, 2, 54, 656, 'sdfs']


Out[214]:

In [215… print(l2.reverse())

None

In [216… l1=[43,4343]
l1+list('sudh')
l1+['sudh']

[43, 4343, 'sudh']


Out[216]:

In [217… list("sudh")

['s', 'u', 'd', 'h']


Out[217]:

In [220… s.endswith("")

False
Out[220]:

In [221… s

'sudh'
Out[221]:

In [222… l = ["sudh " , 345,4354353,4353]

In [223… l.append("kumar")

In [224… l

['sudh ', 345, 4354353, 4353, 'kumar']


Out[224]:

In [225… l.append([2,3,4,5,6])

In [226… l

['sudh ', 345, 4354353, 4353, 'kumar', [2, 3, 4, 5, 6]]


Out[226]:

In [230… l.extend([1,2,3,4])

In [231… l

['sudh ',
Out[231]:
345,
4354353,
4353,
'kumar',
[2, 3, 4, 5, 6],
's',
'u',
's',
'd',
'f',
's',
'f',
1,
2,
3,
4]

In [232… [2,3,4,4,5,76] + [4,5,6,77]

[2, 3, 4, 4, 5, 76, 4, 5, 6, 77]


Out[232]:

In [236… "fdsaffsafsafd afsa saf asdfa sdsaf afsad ".split('&')

['fdsaffsafsafd afsa saf asdfa sdsaf afsad ']


Out[236]:

In [237… 3 +4j

(3+4j)
Out[237]:

In [239… L=['sudh', 'kumar', 3543, 4+6j, True, 345.6]


L[-1:6]

[345.6]
Out[239]:

In [ ]:

In [240… "dfdasfasfda\tsafasf\t\tfadsfsafdsaf\t".expandtabs()

'dfdasfasfda safasf fadsfsafdsaf '


Out[240]:

In [241… a = "I am working in ineuron"

a[0:-10:-1] gives blank but a[0:-100:-1] gives ‘I’


Can you please explain this?

File "<ipython-input-241-fef3cd6af93b>", line 3


a[0:-10:-1] gives blank but a[0:-100:-1] gives ‘I’
^
SyntaxError: invalid syntax

In [242… a = "I am working in ineuron"

In [243… a[0:-10:-1]

''
Out[243]:

In [245… a[0:-100:-1]

'I'
Out[245]:

In [246… a[0::-1]

'I'
Out[246]:

In [ ]: l2=['prweu', 5345]
l3=l2
l3[0]='newatl3'
print(l3)
print(l2)
o/p ['newatl3', 5345]
o/p ['newatl3', 5345]
why it has changed l2?

s1='sudh'
s2=s1
s2='newdata'
print(s1) in this case why it has not changed s1?
print(s2)
'sudh'
'newdata'

In [247… l2=['prweu', 5345]


l3=l2

In [259… l2

['sudh', 5345]
Out[259]:

In [260… l3

['dsfdsafsa', 5, 6, 7, 7]
Out[260]:

In [256… l3 = [4,5,6,7,7]

In [258… l3[0] = "dsfdsafsa"

In [249… l3[0] = "sudh"

In [250… l3

['sudh', 5345]
Out[250]:

In [251… l2

['sudh', 5345]
Out[251]:

In [253… s1='sudh'
s2=s1
s2='newdata'

In [254… s2

'newdata'
Out[254]:

In [255… s1

'sudh'
Out[255]:

In [270… l1= [569, "Sudh", "ravi"]

In [ ]:

In [273… g = l1[2].replace('v',"s")

In [267… l1.pop()

'ravi'
Out[267]:

In [274… l1.append(g)

In [276… l1.pop(2)

'ravi'
Out[276]:

In [277… l1

[569, 'Sudh', 'rasi']


Out[277]:

In [279… l=['ved', 12,True]


l[0]=str(l[0].replace('e', 'E') )

In [280… l

['vEd', 12, True]


Out[280]:

In [285… s = 'USA fdf'

In [286… s.isupper()

False
Out[286]:

In [289… l = ["sunny" , "gadhiya" , "9999" , 9+9j , True , 99.9]

In [291… l[3].imag

9.0
Out[291]:

In [292… l[3].real

9.0
Out[292]:

In [295… s = ['abc','pqr','xyz']
s.pop(1)

'pqr'
Out[295]:

In [297… " ".join("sudh") + " "

's u d h '
Out[297]:

In [298… s

['abc', 'xyz']
Out[298]:

In [299… s = "sudh"

In [300… s.index('d')

2
Out[300]:

In [302… l = [3,4,5,6]

In [303… l.index(5)

2
Out[303]:

In [ ]: s = "sudh"

In [306… list = [43,4343]


l1+list('sudh')
l1+['sudh']

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-306-1715e50e8d9b> in <module>
1 l1= [43,4343]
----> 2 l1+list('sudh')
3 l1+['sudh']

TypeError: 'list' object is not callable

In [3]: l1= [43,4343]

In [4]: l1+list('sudh')

[43, 4343, 's', 'u', 'd', 'h']


Out[4]:

In [5]: list("sudh")

['s', 'u', 'd', 'h']


Out[5]:

In [6]: l1+['sudh']

[43, 4343, 'sudh']


Out[6]:

In [1]: list("sudh")

['s', 'u', 'd', 'h']


Out[1]:

In [2]: l = [4,5,6,6]

In [7]: l.insert(-1,45)

In [8]: l

[4, 5, 6, 'sudh', 45, 45, 6]


Out[8]:

In [5]: l.insert(3,"sudh")

In [6]: l

[4, 5, 6, 'sudh', 45, 6]


Out[6]:

In [10]: type(l[3])

str
Out[10]:

In [ ]: s = "sudh"

In [ ]: l2 =[1,2]
l2[2] = 3 Why can't assign l2[2] = 3

In [11]: a = 'i am working with ineuron'


a[-5:5:-1]

'eni htiw gnikro'


Out[11]:

In [ ]:

In [ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy