0% found this document useful (0 votes)
87 views3 pages

Python

The document discusses Python string and list operations. It shows how to access characters in a string, split strings, join lists with a delimiter, concatenate and multiply lists, create tuples, and access dictionary values. Errors are demonstrated for invalid syntax and type mismatches.

Uploaded by

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

Python

The document discusses Python string and list operations. It shows how to access characters in a string, split strings, join lists with a delimiter, concatenate and multiply lists, create tuples, and access dictionary values. Errors are demonstrated for invalid syntax and type mismatches.

Uploaded by

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

>>> >>> 'o' >>> ' ' >>>

string='hello world' string[4] string[5] string()

Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> string() TypeError: 'str' object is not callable >>> >>> string('') Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> string('') TypeError: 'str' object is not callable >>> string.split('') Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> string.split('') ValueError: empty separator >>> string 'hello world' >>> string.split(' ') ['hello', 'world'] >>> string.split('r') ['hello wo', 'ld'] >>> words=['this' 'is' 'a' 'strings' 'of' 'words'] >>> words ['thisisastringsofwords'] >>> words=['this','is','a','strings','of','words'] >>> words ['this', 'is', 'a', 'strings', 'of', 'words'] >>> join(words) Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> join(words) NameError: name 'join' is not defined >>> ' '.join(words) 'this is a strings of words' >>> 's'.join(words) 'thississasstringssofswords' >>> words=[2,3,4,5,6] >>> words [2, 3, 4, 5, 6] >>> type(words) <type 'list'> >>> 1.join(words) SyntaxError: invalid syntax >>> ''join(words) SyntaxError: invalid syntax >>> ' '.join(words) Traceback (most recent call last): File "<pyshell#22>", line 1, in <module>

' '.join(words) TypeError: sequence item 0: expected string, int found >>> this string='hello' SyntaxError: invalid syntax >>> this_string='hello' >>> print "hi", this_string hi hello >>> emptyTuple= SyntaxError: invalid syntax >>> emptyTuple=() >>> type(emptyTuple) <type 'tuple'> >>> a=12,3,4,5 >>> type(a) <type 'tuple'> >>> a=[1,2,3,4,5\ KeyboardInterrupt >>> a=[1,2,3,4,5] >>> type(a) <type 'list'> >>> a+a [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] >>> b=(1,2,3,4,5) >>> a+b Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> a+b TypeError: can only concatenate list (not "tuple") to list >>> b+b (1, 2, 3, 4, 5, 1, 2, 3, 4, 5) >>> type(a) <type 'list'> >>> type(b) <type 'tuple'> >>> 3*a [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] >>> 3*b (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5) >>> single("aa") Traceback (most recent call last): File "<pyshell#42>", line 1, in <module> single("aa") NameError: name 'single' is not defined >>> single("aa",) Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> single("aa",) NameError: name 'single' is not defined >>> single=('aaaa') >>> aingle Traceback (most recent call last): File "<pyshell#45>", line 1, in <module> aingle NameError: name 'aingle' is not defined >>> single

'aaaa' >>> type(single) <type 'str'> >>> type('single',) <type 'str'> >>> single=('singlee',) >>> type(single) <type 'tuple'> >>> thisdic={'a':1,'b':2,'c':3} >>> a [1, 2, 3, 4, 5] >>> thisdic[a] Traceback (most recent call File "<pyshell#53>", line thisdic[a] TypeError: unhashable type: >>> thisdic['a'] 1 >>> type(thisdic) <type 'dict'> >>> x=[1,2,3,4,5] >>> x*5 [1, 2, 3, 4, 5, 1, 2, 3, 4, >>> last): 1, in <module> 'list'

5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

You might also like